This commit is contained in:
Ryan ZHAO 2021-01-29 11:59:26 +11:00
commit 2d80f38e61
7 changed files with 43 additions and 10 deletions

View File

@ -438,6 +438,30 @@ public class AttachmentDatabase extends Database {
return insertedAttachments; return insertedAttachments;
} }
/**
* Insert attachments in database and return the IDs of the inserted attachments
*
* @param mmsId message ID
* @param attachments attachments to persist
* @return IDs of the persisted attachments
* @throws MmsException
*/
@NonNull List<Long> insertAttachments(long mmsId, @NonNull List<Attachment> attachments)
throws MmsException
{
Log.d(TAG, "insertParts(" + attachments.size() + ")");
List<Long> insertedAttachmentsIDs = new LinkedList<>();
for (Attachment attachment : attachments) {
AttachmentId attachmentId = insertAttachment(mmsId, attachment, attachment.isQuote());
insertedAttachmentsIDs.add(attachmentId.getRowId());
Log.i(TAG, "Inserted attachment at ID: " + attachmentId);
}
return insertedAttachmentsIDs;
}
public @NonNull Attachment updateAttachmentData(@NonNull Attachment attachment, public @NonNull Attachment updateAttachmentData(@NonNull Attachment attachment,
@NonNull MediaStream mediaStream) @NonNull MediaStream mediaStream)
throws MmsException throws MmsException

View File

@ -81,8 +81,10 @@ class Storage(context: Context, helper: SQLCipherOpenHelper) : Database(context,
return registrationID return registrationID
} }
override fun persist(attachments: List<Attachment>): List<Long> { override fun persistAttachments(messageId: Long, attachments: List<Attachment>): List<Long> {
TODO("Not yet implemented") val database = DatabaseFactory.getAttachmentDatabase(context)
val databaseAttachments = attachments.map { it.toDatabaseAttachment() }
return database.insertAttachments(messageId, databaseAttachments)
} }
override fun persist(message: VisibleMessage, quotes: QuoteModel?, linkPreview: List<LinkPreview?>, groupPublicKey: String?, openGroupID: String?): Long? { override fun persist(message: VisibleMessage, quotes: QuoteModel?, linkPreview: List<LinkPreview?>, groupPublicKey: String?, openGroupID: String?): Long? {
@ -127,7 +129,7 @@ class Storage(context: Context, helper: SQLCipherOpenHelper) : Database(context,
// JOBS // JOBS
override fun persist(job: Job) { override fun persistJob(job: Job) {
DatabaseFactory.getSessionJobDatabase(context).persistJob(job) DatabaseFactory.getSessionJobDatabase(context).persistJob(job)
} }

View File

@ -39,7 +39,7 @@ interface StorageProtocol {
fun getOrGenerateRegistrationID(): Int fun getOrGenerateRegistrationID(): Int
// Jobs // Jobs
fun persist(job: Job) fun persistJob(job: Job)
fun markJobAsSucceeded(job: Job) fun markJobAsSucceeded(job: Job)
fun markJobAsFailed(job: Job) fun markJobAsFailed(job: Job)
fun getAllPendingJobs(type: String): List<Job> fun getAllPendingJobs(type: String): List<Job>
@ -86,7 +86,7 @@ interface StorageProtocol {
fun getReceivedMessageTimestamps(): Set<Long> fun getReceivedMessageTimestamps(): Set<Long>
fun addReceivedMessageTimestamp(timestamp: Long) fun addReceivedMessageTimestamp(timestamp: Long)
// Returns the IDs of the saved attachments. // Returns the IDs of the saved attachments.
fun persist(attachments: List<Attachment>): List<Long> fun persistAttachments(messageId: Long, attachments: List<Attachment>): List<Long>
fun getMessageIdInDatabase(timestamp: Long, author: String): Long? fun getMessageIdInDatabase(timestamp: Long, author: String): Long?
fun setOpenGroupServerMessageID(messageID: Long, serverID: Long) fun setOpenGroupServerMessageID(messageID: Long, serverID: Long)

View File

@ -25,7 +25,7 @@ class JobQueue : JobDelegate {
fun addWithoutExecuting(job: Job) { fun addWithoutExecuting(job: Job) {
job.id = System.currentTimeMillis().toString() job.id = System.currentTimeMillis().toString()
MessagingConfiguration.shared.storage.persist(job) MessagingConfiguration.shared.storage.persistJob(job)
job.delegate = this job.delegate = this
} }
@ -54,7 +54,7 @@ class JobQueue : JobDelegate {
job.failureCount += 1 job.failureCount += 1
val storage = MessagingConfiguration.shared.storage val storage = MessagingConfiguration.shared.storage
if (storage.isJobCanceled(job)) { return Log.i("Jobs", "${job::class.simpleName} canceled.")} if (storage.isJobCanceled(job)) { return Log.i("Jobs", "${job::class.simpleName} canceled.")}
storage.persist(job) storage.persistJob(job)
if (job.failureCount == job.maxFailureCount) { if (job.failureCount == job.maxFailureCount) {
storage.markJobAsFailed(job) storage.markJobAsFailed(job)
} else { } else {
@ -70,7 +70,7 @@ class JobQueue : JobDelegate {
override fun handleJobFailedPermanently(job: Job, error: Exception) { override fun handleJobFailedPermanently(job: Job, error: Exception) {
job.failureCount += 1 job.failureCount += 1
val storage = MessagingConfiguration.shared.storage val storage = MessagingConfiguration.shared.storage
storage.persist(job) storage.persistJob(job)
storage.markJobAsFailed(job) storage.markJobAsFailed(job)
} }

View File

@ -3,6 +3,7 @@ package org.session.libsession.messaging.messages.visible
import android.util.Size import android.util.Size
import android.webkit.MimeTypeMap import android.webkit.MimeTypeMap
import org.session.libsession.database.MessageDataProvider import org.session.libsession.database.MessageDataProvider
import org.session.libsession.messaging.sending_receiving.attachments.DatabaseAttachment
import org.session.libsignal.service.internal.push.SignalServiceProtos import org.session.libsignal.service.internal.push.SignalServiceProtos
import java.io.File import java.io.File
@ -64,4 +65,10 @@ class Attachment {
fun toProto(): SignalServiceProtos.AttachmentPointer? { fun toProto(): SignalServiceProtos.AttachmentPointer? {
TODO("Not implemented") TODO("Not implemented")
} }
fun toDatabaseAttachment(): org.session.libsession.messaging.sending_receiving.attachments.Attachment {
return DatabaseAttachment(null, 0, true, true, contentType, 0,
sizeInBytes?.toLong() ?: 0, fileName, null, key.toString(), null, digest, null, kind == Kind.VOICE_MESSAGE,
size?.width ?: 0, size?.height ?: 0, false, caption, null, url)
}
} }

View File

@ -117,7 +117,7 @@ fun MessageReceiver.handleVisibleMessage(message: VisibleMessage, proto: SignalS
return@mapNotNull attachment return@mapNotNull attachment
} }
} }
val attachmentIDs = storage.persist(attachments) val attachmentIDs = storage.persistAttachments(message.id ?: 0, attachments)
message.attachmentIDs = attachmentIDs as ArrayList<Long> message.attachmentIDs = attachmentIDs as ArrayList<Long>
var attachmentsToDownload = attachmentIDs var attachmentsToDownload = attachmentIDs
// Update profile if needed // Update profile if needed

View File

@ -72,7 +72,7 @@ object MessageSender {
attachments.add(attachment) attachments.add(attachment)
} }
} }
val attachmentIDs = MessagingConfiguration.shared.storage.persist(attachments) val attachmentIDs = MessagingConfiguration.shared.storage.persistAttachments(message.id ?: 0, attachments)
message.attachmentIDs.addAll(attachmentIDs) message.attachmentIDs.addAll(attachmentIDs)
} }