implementation of persistAttachments + refactoring

pull/420/head
Brice 3 years ago
parent ebad701378
commit 49f3e0cfca

@ -438,6 +438,30 @@ public class AttachmentDatabase extends Database {
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,
@NonNull MediaStream mediaStream)
throws MmsException

@ -81,8 +81,10 @@ class Storage(context: Context, helper: SQLCipherOpenHelper) : Database(context,
return registrationID
}
override fun persist(attachments: List<Attachment>): List<Long> {
TODO("Not yet implemented")
override fun persistAttachments(messageId: Long, attachments: List<Attachment>): List<Long> {
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? {
@ -127,7 +129,7 @@ class Storage(context: Context, helper: SQLCipherOpenHelper) : Database(context,
// JOBS
override fun persist(job: Job) {
override fun persistJob(job: Job) {
DatabaseFactory.getSessionJobDatabase(context).persistJob(job)
}

@ -39,7 +39,7 @@ interface StorageProtocol {
fun getOrGenerateRegistrationID(): Int
// Jobs
fun persist(job: Job)
fun persistJob(job: Job)
fun markJobAsSucceeded(job: Job)
fun markJobAsFailed(job: Job)
fun getAllPendingJobs(type: String): List<Job>
@ -86,7 +86,7 @@ interface StorageProtocol {
fun getReceivedMessageTimestamps(): Set<Long>
fun addReceivedMessageTimestamp(timestamp: Long)
// 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 setOpenGroupServerMessageID(messageID: Long, serverID: Long)

@ -25,7 +25,7 @@ class JobQueue : JobDelegate {
fun addWithoutExecuting(job: Job) {
job.id = System.currentTimeMillis().toString()
MessagingConfiguration.shared.storage.persist(job)
MessagingConfiguration.shared.storage.persistJob(job)
job.delegate = this
}
@ -35,7 +35,7 @@ class JobQueue : JobDelegate {
return
}
hasResumedPendingJobs = true
val allJobTypes = listOf(AttachmentDownloadJob.collection, AttachmentDownloadJob.collection, MessageReceiveJob.collection, MessageSendJob.collection, NotifyPNServerJob.collection)
val allJobTypes = listOf(AttachmentDownloadJob.KEY, AttachmentDownloadJob.KEY, MessageReceiveJob.KEY, MessageSendJob.KEY, NotifyPNServerJob.KEY)
allJobTypes.forEach { type ->
val allPendingJobs = MessagingConfiguration.shared.storage.getAllPendingJobs(type)
allPendingJobs.sortedBy { it.id }.forEach { job ->
@ -54,7 +54,7 @@ class JobQueue : JobDelegate {
job.failureCount += 1
val storage = MessagingConfiguration.shared.storage
if (storage.isJobCanceled(job)) { return Log.i("Jobs", "${job::class.simpleName} canceled.")}
storage.persist(job)
storage.persistJob(job)
if (job.failureCount == job.maxFailureCount) {
storage.markJobAsFailed(job)
} else {
@ -70,7 +70,7 @@ class JobQueue : JobDelegate {
override fun handleJobFailedPermanently(job: Job, error: Exception) {
job.failureCount += 1
val storage = MessagingConfiguration.shared.storage
storage.persist(job)
storage.persistJob(job)
storage.markJobAsFailed(job)
}

@ -3,6 +3,7 @@ package org.session.libsession.messaging.messages.visible
import android.util.Size
import android.webkit.MimeTypeMap
import org.session.libsession.database.MessageDataProvider
import org.session.libsession.messaging.sending_receiving.attachments.DatabaseAttachment
import org.session.libsignal.service.internal.push.SignalServiceProtos
import java.io.File
@ -64,4 +65,10 @@ class Attachment {
fun toProto(): SignalServiceProtos.AttachmentPointer? {
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)
}
}

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

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

Loading…
Cancel
Save