diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/visible/Profile.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/visible/Profile.kt index 417599962c..5a2590b56d 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/visible/Profile.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/messages/visible/Profile.kt @@ -23,20 +23,19 @@ class Profile() : VisibleMessage() { profilePictureURL?.let { return Profile(displayName = displayName, profileKey = profileKey.toByteArray(), profilePictureURL = profilePictureURL) } - return Profile(displayName) } - + return Profile(displayName) } } //constructor - internal constructor(displayName: String, profileKey: ByteArray? = nil, profilePictureURL: String? = nil) : this() { + internal constructor(displayName: String, profileKey: ByteArray? = null, profilePictureURL: String? = null) : this() { this.displayName = displayName this.profileKey = profileKey this.profilePictureURL = profilePictureURL } - fun toProto(): SignalServiceProtos.DataMessage? { + fun toSSProto(): SignalServiceProtos.DataMessage? { return this.toProto("") } diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/visible/Quote.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/visible/Quote.kt index 0f60db1865..47a9e99626 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/visible/Quote.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/messages/visible/Quote.kt @@ -1,5 +1,6 @@ package org.session.libsession.messaging.messages.visible +import org.session.libsignal.libsignal.logging.Log import org.session.libsignal.service.internal.push.SignalServiceProtos class Quote() : VisibleMessage() { @@ -10,12 +11,62 @@ class Quote() : VisibleMessage() { var attachmentID: String? = null companion object { + const val TAG = "Quote" + fun fromProto(proto: SignalServiceProtos.DataMessage.Quote): Quote? { - TODO("Not yet implemented") + val timestamp = proto.id + val publicKey = proto.author + val text = proto.text + return Quote(timestamp, publicKey, text, null) } } + //constructor + internal constructor(timestamp: Long, publicKey: String, text: String?, attachmentID: String?) : this() { + this.timestamp = timestamp + this.publicKey = publicKey + this.text = text + this.attachmentID = attachmentID + } + + + // validation + override fun isValid(): Boolean { + if (!super.isValid()) return false + return (timestamp != null && publicKey != null) + } + override fun toProto(transaction: String): SignalServiceProtos.DataMessage.Quote? { - return null + val timestamp = timestamp + val publicKey = publicKey + if (timestamp == null || publicKey == null) { + Log.w(TAG, "Couldn't construct quote proto from: $this") + return null + } + val quoteProto = SignalServiceProtos.DataMessage.Quote.newBuilder() + quoteProto.id = timestamp + quoteProto.author = publicKey + text?.let { quoteProto.text = text } + //TODO addAttachmentsIfNeeded(quoteProto, transaction) + // Build + try { + return quoteProto.build() + } catch (e: Exception) { + Log.w(TAG, "Couldn't construct quote proto from: $this") + return null + } + } + + private fun addAttachmentsIfNeeded(quoteProto: SignalServiceProtos.DataMessage.Quote.Builder, transaction: String) { + val attachmentID = attachmentID ?: return + //TODO databas stuff + val quotedAttachmentProto = SignalServiceProtos.DataMessage.Quote.QuotedAttachment.newBuilder() + //TODO more database related stuff + //quotedAttachmentProto.contentType = + try { + quoteProto.addAttachments(quotedAttachmentProto.build()) + } catch (e: Exception) { + Log.w(TAG, "Couldn't construct quoted attachment proto from: $this") + } } } \ No newline at end of file