use of SessionServiceAttachment instead if SignalSA

This commit is contained in:
Brice 2020-12-17 14:45:57 +11:00
parent c286efae9d
commit 0467147cfe
6 changed files with 45 additions and 36 deletions

View File

@ -2,27 +2,35 @@ package org.thoughtcrime.securesms.attachments
import android.content.Context import android.content.Context
import com.google.protobuf.ByteString import com.google.protobuf.ByteString
import org.session.libsession.database.dto.DatabaseAttachmentDTO
import org.session.libsession.database.MessageDataProvider import org.session.libsession.database.MessageDataProvider
import org.session.libsession.database.dto.AttachmentState import org.session.libsession.database.dto.AttachmentState
import org.session.libsignal.service.internal.push.SignalServiceProtos import org.session.libsession.messaging.sending_receiving.attachments.SessionServiceAttachmentPointer
import org.session.libsession.messaging.sending_receiving.attachments.SessionServiceAttachmentStream
import org.session.libsignal.libsignal.util.guava.Optional
import org.thoughtcrime.securesms.database.Database import org.thoughtcrime.securesms.database.Database
import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.database.SmsDatabase
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
import org.thoughtcrime.securesms.jobs.AttachmentUploadJob import org.thoughtcrime.securesms.jobs.AttachmentUploadJob
import org.thoughtcrime.securesms.mms.PartAuthority
import org.thoughtcrime.securesms.util.MediaUtil import org.thoughtcrime.securesms.util.MediaUtil
class DatabaseAttachmentProvider(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), MessageDataProvider { class DatabaseAttachmentProvider(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), MessageDataProvider {
override fun getAttachment(attachmentId: Long): DatabaseAttachmentDTO? {
override fun getAttachmentStream(attachmentId: Long): SessionServiceAttachmentStream? {
val attachmentDatabase = DatabaseFactory.getAttachmentDatabase(context) val attachmentDatabase = DatabaseFactory.getAttachmentDatabase(context)
val databaseAttachment = attachmentDatabase.getAttachment(AttachmentId(attachmentId, 0)) ?: return null val databaseAttachment = attachmentDatabase.getAttachment(AttachmentId(attachmentId, 0)) ?: return null
return databaseAttachment.toDTO() return databaseAttachment.toAttachmentStream(context)
} }
override fun setAttachmentState(attachmentState: AttachmentState, attachment: DatabaseAttachmentDTO, messageID: Long) { override fun getAttachmentPointer(attachmentId: Long): SessionServiceAttachmentPointer? {
val attachmentDatabase = DatabaseFactory.getAttachmentDatabase(context) val attachmentDatabase = DatabaseFactory.getAttachmentDatabase(context)
attachmentDatabase.setTransferState(messageID, AttachmentId(attachment.attachmentId, 0), attachmentState.value) val databaseAttachment = attachmentDatabase.getAttachment(AttachmentId(attachmentId, 0)) ?: return null
return databaseAttachment.toAttachmentPointer()
}
override fun setAttachmentState(attachmentState: AttachmentState, attachmentId: Long, messageID: Long) {
val attachmentDatabase = DatabaseFactory.getAttachmentDatabase(context)
attachmentDatabase.setTransferState(messageID, AttachmentId(attachmentId, 0), attachmentState.value)
} }
@Throws(Exception::class) @Throws(Exception::class)
@ -38,29 +46,26 @@ class DatabaseAttachmentProvider(context: Context, helper: SQLCipherOpenHelper)
} }
// Extension to DatabaseAttachment class fun DatabaseAttachment.toAttachmentPointer(): SessionServiceAttachmentPointer {
return SessionServiceAttachmentPointer(attachmentId.rowId, contentType, key?.toByteArray(), Optional.fromNullable(size.toInt()), Optional.absent(), width, height, Optional.fromNullable(digest), Optional.fromNullable(fileName), isVoiceNote, Optional.fromNullable(caption), url)
}
fun DatabaseAttachment.toDTO(): DatabaseAttachmentDTO { fun DatabaseAttachment.toAttachmentStream(context: Context): SessionServiceAttachmentStream {
var databaseAttachmentDTO = DatabaseAttachmentDTO() val stream = PartAuthority.getAttachmentStream(context, this.dataUri!!)
databaseAttachmentDTO.attachmentId = this.attachmentId.rowId var attachmentStream = SessionServiceAttachmentStream(stream, this.contentType, this.size, Optional.fromNullable(this.fileName), this.isVoiceNote, Optional.absent(), this.width, this.height, Optional.fromNullable(this.caption), null)
databaseAttachmentDTO.contentType = this.contentType attachmentStream.attachmentId = this.attachmentId.rowId
databaseAttachmentDTO.fileName = this.fileName attachmentStream.isAudio = MediaUtil.isAudio(this)
databaseAttachmentDTO.caption = this.caption attachmentStream.isGif = MediaUtil.isGif(this)
attachmentStream.isVideo = MediaUtil.isVideo(this)
attachmentStream.isImage = MediaUtil.isImage(this)
databaseAttachmentDTO.size = this.size.toInt() attachmentStream.key = ByteString.copyFrom(this.key?.toByteArray())
databaseAttachmentDTO.key = ByteString.copyFrom(this.key?.toByteArray()) attachmentStream.digest = this.digest
databaseAttachmentDTO.digest = ByteString.copyFrom(this.digest) //attachmentStream.flags = if (this.isVoiceNote) SignalServiceProtos.AttachmentPointer.Flags.VOICE_MESSAGE.number else 0
databaseAttachmentDTO.flags = if (this.isVoiceNote) SignalServiceProtos.AttachmentPointer.Flags.VOICE_MESSAGE.number else 0
databaseAttachmentDTO.url = this.url attachmentStream.url = this.url
if (this.shouldHaveImageSize()) { return attachmentStream
databaseAttachmentDTO.shouldHaveImageSize = true
databaseAttachmentDTO.width = this.width
databaseAttachmentDTO.height = this.height
}
return databaseAttachmentDTO
} }
fun DatabaseAttachment.shouldHaveImageSize(): Boolean { fun DatabaseAttachment.shouldHaveImageSize(): Boolean {

View File

@ -1,14 +1,18 @@
package org.session.libsession.database package org.session.libsession.database
import org.session.libsession.database.dto.AttachmentState import org.session.libsession.messaging.sending_receiving.attachments.AttachmentState
import org.session.libsession.database.dto.DatabaseAttachmentDTO import org.session.libsession.messaging.sending_receiving.attachments.SessionServiceAttachmentPointer
import org.session.libsession.messaging.messages.visible.Attachment import org.session.libsession.messaging.sending_receiving.attachments.SessionServiceAttachmentStream
interface MessageDataProvider { interface MessageDataProvider {
fun getAttachment(attachmentId: Long): DatabaseAttachmentDTO? //fun getAttachment(attachmentId: Long): SignalServiceAttachmentStream?
fun setAttachmentState(attachmentState: AttachmentState, attachment: DatabaseAttachmentDTO, messageID: Long) fun getAttachmentStream(attachmentId: Long): SessionServiceAttachmentStream?
fun getAttachmentPointer(attachmentId: Long): SessionServiceAttachmentPointer?
fun setAttachmentState(attachmentState: AttachmentState, attachmentId: Long, messageID: Long)
fun isOutgoingMessage(timestamp: Long): Boolean fun isOutgoingMessage(timestamp: Long): Boolean

View File

@ -27,7 +27,7 @@ class MessageSendJob(val message: Message, val destination: Destination) : Job {
val message = message as? VisibleMessage val message = message as? VisibleMessage
message?.let { message?.let {
if(!messageDataProvider.isOutgoingMessage(message.sentTimestamp!!)) return // The message has been deleted if(!messageDataProvider.isOutgoingMessage(message.sentTimestamp!!)) return // The message has been deleted
val attachments = message.attachmentIDs.map { messageDataProvider.getAttachment(it) }.filterNotNull() val attachments = message.attachmentIDs.map { messageDataProvider.getAttachmentStream(it) }.filterNotNull()
val attachmentsToUpload = attachments.filter { !it.isUploaded } val attachmentsToUpload = attachments.filter { !it.isUploaded }
attachmentsToUpload.forEach { attachmentsToUpload.forEach {
if(MessagingConfiguration.shared.storage.getAttachmentUploadJob(it.attachmentId) != null) { if(MessagingConfiguration.shared.storage.getAttachmentUploadJob(it.attachmentId) != null) {

View File

@ -44,7 +44,7 @@ class LinkPreview() {
title?.let { linkPreviewProto.title = title } title?.let { linkPreviewProto.title = title }
val attachmentID = attachmentID val attachmentID = attachmentID
attachmentID?.let { attachmentID?.let {
val attachmentProto = MessagingConfiguration.shared.messageDataProvider.getAttachment(attachmentID) val attachmentProto = MessagingConfiguration.shared.messageDataProvider.getAttachmentStream(attachmentID)
attachmentProto?.let { linkPreviewProto.image = attachmentProto.toProto() } attachmentProto?.let { linkPreviewProto.image = attachmentProto.toProto() }
} }
// Build // Build

View File

@ -60,7 +60,7 @@ class Quote() {
private fun addAttachmentsIfNeeded(quoteProto: SignalServiceProtos.DataMessage.Quote.Builder, messageDataProvider: MessageDataProvider) { private fun addAttachmentsIfNeeded(quoteProto: SignalServiceProtos.DataMessage.Quote.Builder, messageDataProvider: MessageDataProvider) {
val attachmentID = attachmentID ?: return val attachmentID = attachmentID ?: return
val attachmentProto = messageDataProvider.getAttachment(attachmentID) val attachmentProto = messageDataProvider.getAttachmentStream(attachmentID)
if (attachmentProto == null) { if (attachmentProto == null) {
Log.w(TAG, "Ignoring invalid attachment for quoted message.") Log.w(TAG, "Ignoring invalid attachment for quoted message.")
return return
@ -74,7 +74,7 @@ class Quote() {
} }
val quotedAttachmentProto = SignalServiceProtos.DataMessage.Quote.QuotedAttachment.newBuilder() val quotedAttachmentProto = SignalServiceProtos.DataMessage.Quote.QuotedAttachment.newBuilder()
quotedAttachmentProto.contentType = attachmentProto.contentType quotedAttachmentProto.contentType = attachmentProto.contentType
val fileName = attachmentProto.fileName val fileName = attachmentProto.fileName?.get()
fileName?.let { quotedAttachmentProto.fileName = fileName } fileName?.let { quotedAttachmentProto.fileName = fileName }
quotedAttachmentProto.thumbnail = attachmentProto.toProto() quotedAttachmentProto.thumbnail = attachmentProto.toProto()
try { try {

View File

@ -90,7 +90,7 @@ class VisibleMessage : Message() {
} }
} }
//Attachments //Attachments
val attachments = attachmentIDs.mapNotNull { MessagingConfiguration.shared.messageDataProvider.getAttachment(it) } val attachments = attachmentIDs.mapNotNull { MessagingConfiguration.shared.messageDataProvider.getAttachmentStream(it) }
if (!attachments.all { it.isUploaded }) { if (!attachments.all { it.isUploaded }) {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
//TODO equivalent to iOS's preconditionFailure //TODO equivalent to iOS's preconditionFailure