rename and minor refactor to match the api

This commit is contained in:
Ryan ZHAO 2020-12-02 17:06:28 +11:00
parent bd96342a16
commit 295eb3b52f
12 changed files with 123 additions and 130 deletions

View File

@ -2,9 +2,9 @@ package org.session.libsession.messaging.messages
sealed class Destination {
class Contact(val publicKey: String)
class ClosedGroup(val groupPublicKey: String)
class OpenGroup(val channel: Long, val server: String)
class Contact(val publicKey: String) : Destination()
class ClosedGroup(val groupPublicKey: String) : Destination()
class OpenGroup(val channel: Long, val server: String) : Destination()
companion object {
//TODO need to implement the equivalent to TSThread and then implement from(...)

View File

@ -12,11 +12,7 @@ abstract class Message {
var sender: String? = null
var groupPublicKey: String? = null
var openGroupServerMessageID: Long? = null
companion object {
@JvmStatic
val ttl = 2 * 24 * 60 * 60 * 1000 //TODO not sure about that declaration
}
val ttl: Long = 2 * 24 * 60 * 60 * 1000
// validation
open fun isValid(): Boolean {

View File

@ -4,10 +4,8 @@ import android.util.Size
import android.webkit.MimeTypeMap
import org.session.libsignal.service.internal.push.SignalServiceProtos
import java.io.File
import java.net.URL
import kotlin.math.absoluteValue
class Attachment : VisibleMessage<SignalServiceProtos.AttachmentPointer?>() {
class Attachment : VisibleMessageProto<SignalServiceProtos.AttachmentPointer?>() {
var fileName: String? = null
var contentType: String? = null

View File

@ -1,102 +0,0 @@
package org.session.libsession.messaging.messages.visible
import org.session.libsignal.libsignal.logging.Log
import org.session.libsignal.service.internal.push.SignalServiceProtos
class BaseVisibleMessage() : VisibleMessage<SignalServiceProtos.Content?>() {
var text: String? = null
var attachmentIDs = ArrayList<String>()
var quote: Quote? = null
var linkPreview: LinkPreview? = null
var contact: Contact? = null
var profile: Profile? = null
companion object {
const val TAG = "BaseVisibleMessage"
fun fromProto(proto: SignalServiceProtos.Content): BaseVisibleMessage? {
val dataMessage = proto.dataMessage ?: return null
val result = BaseVisibleMessage()
result.text = dataMessage.body
// Attachments are handled in MessageReceiver
val quoteProto = dataMessage.quote
quoteProto?.let {
val quote = Quote.fromProto(quoteProto)
quote?.let { result.quote = quote }
}
val linkPreviewProto = dataMessage.previewList.first()
linkPreviewProto?.let {
val linkPreview = LinkPreview.fromProto(linkPreviewProto)
linkPreview?.let { result.linkPreview = linkPreview }
}
// TODO Contact
val profile = Profile.fromProto(dataMessage)
profile?.let { result.profile = profile }
return result
}
}
// validation
override fun isValid(): Boolean {
if (!super.isValid()) return false
if (attachmentIDs.isNotEmpty()) return true
val text = text?.trim() ?: return false
if (text.isNotEmpty()) return true
return false
}
override fun toProto(transaction: String): SignalServiceProtos.Content? {
val proto = SignalServiceProtos.Content.newBuilder()
var attachmentIDs = this.attachmentIDs
val dataMessage: SignalServiceProtos.DataMessage.Builder
// Profile
val profile = profile
val profileProto = profile?.toSSProto()
if (profileProto != null) {
dataMessage = profileProto.toBuilder()
} else {
dataMessage = SignalServiceProtos.DataMessage.newBuilder()
}
// Text
text?.let { dataMessage.body = text }
// Quote
val quotedAttachmentID = quote?.attachmentID
quotedAttachmentID?.let {
val index = attachmentIDs.indexOf(quotedAttachmentID)
if (index >= 0) { attachmentIDs.removeAt(index) }
}
val quote = quote
quote?.let {
val quoteProto = quote.toProto(transaction)
if (quoteProto != null) dataMessage.quote = quoteProto
}
//Link preview
val linkPreviewAttachmentID = linkPreview?.attachmentID
linkPreviewAttachmentID?.let {
val index = attachmentIDs.indexOf(quotedAttachmentID)
if (index >= 0) { attachmentIDs.removeAt(index) }
}
val linkPreview = linkPreview
linkPreview?.let {
val linkPreviewProto = linkPreview.toProto(transaction)
linkPreviewProto?.let {
dataMessage.addAllPreview(listOf(linkPreviewProto))
}
}
//Attachments
// TODO I'm blocking on that one...
//swift: let attachments = attachmentIDs.compactMap { TSAttachmentStream.fetch(uniqueId: $0, transaction: transaction) }
// TODO Contact
// Build
try {
proto.dataMessage = dataMessage.build()
return proto.build()
} catch (e: Exception) {
Log.w(TAG, "Couldn't construct visible message proto from: $this")
return null
}
}
}

View File

@ -2,7 +2,7 @@ package org.session.libsession.messaging.messages.visible
import org.session.libsignal.service.internal.push.SignalServiceProtos
class Contact : VisibleMessage<SignalServiceProtos.DataMessage.Contact?>() {
class Contact : VisibleMessageProto<SignalServiceProtos.DataMessage.Contact?>() {
companion object {
fun fromProto(proto: SignalServiceProtos.Content): Contact? {

View File

@ -1,10 +1,9 @@
package org.session.libsession.messaging.messages.visible
import org.session.libsession.messaging.messages.control.TypingIndicator
import org.session.libsignal.libsignal.logging.Log
import org.session.libsignal.service.internal.push.SignalServiceProtos
class LinkPreview() : VisibleMessage<SignalServiceProtos.DataMessage.Preview?>(){
class LinkPreview() : VisibleMessageProto<SignalServiceProtos.DataMessage.Preview?>(){
var title: String? = null
var url: String? = null

View File

@ -4,7 +4,7 @@ import com.google.protobuf.ByteString
import org.session.libsignal.libsignal.logging.Log
import org.session.libsignal.service.internal.push.SignalServiceProtos
class Profile() : VisibleMessage<SignalServiceProtos.DataMessage?>() {
class Profile() : VisibleMessageProto<SignalServiceProtos.DataMessage?>() {
var displayName: String? = null
var profileKey: ByteArray? = null

View File

@ -3,7 +3,7 @@ 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<SignalServiceProtos.DataMessage.Quote?>() {
class Quote() : VisibleMessageProto<SignalServiceProtos.DataMessage.Quote?>() {
var timestamp: Long? = 0
var publicKey: String? = null

View File

@ -1,15 +1,102 @@
package org.session.libsession.messaging.messages.visible
import org.session.libsession.messaging.messages.Message
import org.session.libsignal.libsignal.logging.Log
import org.session.libsignal.service.internal.push.SignalServiceProtos
abstract class VisibleMessage<out T: com.google.protobuf.MessageOrBuilder?> : Message() {
class VisibleMessage() : VisibleMessageProto<SignalServiceProtos.Content?>() {
abstract fun toProto(transaction: String): T
var text: String? = null
var attachmentIDs = ArrayList<String>()
var quote: Quote? = null
var linkPreview: LinkPreview? = null
var contact: Contact? = null
var profile: Profile? = null
final override fun toProto(): SignalServiceProtos.Content? {
//we don't need to implement this method in subclasses
//TODO it just needs an equivalent to swift: preconditionFailure("Use toProto(using:) if that exists...
TODO("Not implemented")
companion object {
const val TAG = "BaseVisibleMessage"
fun fromProto(proto: SignalServiceProtos.Content): VisibleMessage? {
val dataMessage = proto.dataMessage ?: return null
val result = VisibleMessage()
result.text = dataMessage.body
// Attachments are handled in MessageReceiver
val quoteProto = dataMessage.quote
quoteProto?.let {
val quote = Quote.fromProto(quoteProto)
quote?.let { result.quote = quote }
}
val linkPreviewProto = dataMessage.previewList.first()
linkPreviewProto?.let {
val linkPreview = LinkPreview.fromProto(linkPreviewProto)
linkPreview?.let { result.linkPreview = linkPreview }
}
// TODO Contact
val profile = Profile.fromProto(dataMessage)
profile?.let { result.profile = profile }
return result
}
}
// validation
override fun isValid(): Boolean {
if (!super.isValid()) return false
if (attachmentIDs.isNotEmpty()) return true
val text = text?.trim() ?: return false
if (text.isNotEmpty()) return true
return false
}
override fun toProto(transaction: String): SignalServiceProtos.Content? {
val proto = SignalServiceProtos.Content.newBuilder()
var attachmentIDs = this.attachmentIDs
val dataMessage: SignalServiceProtos.DataMessage.Builder
// Profile
val profile = profile
val profileProto = profile?.toSSProto()
if (profileProto != null) {
dataMessage = profileProto.toBuilder()
} else {
dataMessage = SignalServiceProtos.DataMessage.newBuilder()
}
// Text
text?.let { dataMessage.body = text }
// Quote
val quotedAttachmentID = quote?.attachmentID
quotedAttachmentID?.let {
val index = attachmentIDs.indexOf(quotedAttachmentID)
if (index >= 0) { attachmentIDs.removeAt(index) }
}
val quote = quote
quote?.let {
val quoteProto = quote.toProto(transaction)
if (quoteProto != null) dataMessage.quote = quoteProto
}
//Link preview
val linkPreviewAttachmentID = linkPreview?.attachmentID
linkPreviewAttachmentID?.let {
val index = attachmentIDs.indexOf(quotedAttachmentID)
if (index >= 0) { attachmentIDs.removeAt(index) }
}
val linkPreview = linkPreview
linkPreview?.let {
val linkPreviewProto = linkPreview.toProto(transaction)
linkPreviewProto?.let {
dataMessage.addAllPreview(listOf(linkPreviewProto))
}
}
//Attachments
// TODO I'm blocking on that one...
//swift: let attachments = attachmentIDs.compactMap { TSAttachmentStream.fetch(uniqueId: $0, transaction: transaction) }
// TODO Contact
// Build
try {
proto.dataMessage = dataMessage.build()
return proto.build()
} catch (e: Exception) {
Log.w(TAG, "Couldn't construct visible message proto from: $this")
return null
}
}
}

View File

@ -0,0 +1,15 @@
package org.session.libsession.messaging.messages.visible
import org.session.libsession.messaging.messages.Message
import org.session.libsignal.service.internal.push.SignalServiceProtos
abstract class VisibleMessageProto<out T: com.google.protobuf.MessageOrBuilder?> : Message() {
abstract fun toProto(transaction: String): T
final override fun toProto(): SignalServiceProtos.Content? {
//we don't need to implement this method in subclasses
//TODO it just needs an equivalent to swift: preconditionFailure("Use toProto(using:) if that exists...
TODO("Not implemented")
}
}

View File

@ -27,11 +27,11 @@ public data class OpenGroupMessage(
val storage = Configuration.shared.storage
val userPublicKey = storage.getUserPublicKey() ?: return null
// Validation
if (!message.isValid) { return null } // Should be valid at this point
if (!message.isValid()) { return null } // Should be valid at this point
// Quote
val quote: OpenGroupMessage.Quote? = {
val quote = message.quote
if (quote != null && quote.isValid) {
if (quote != null && quote.isValid()) {
val quotedMessageServerID = storage.getQuoteServerID(quote.id, quote.publicKey)
OpenGroupMessage.Quote(quote.timestamp, quote.publicKey, quote.text, quotedMessageServerID)
} else {
@ -45,7 +45,7 @@ public data class OpenGroupMessage(
// Link preview
val linkPreview = message.linkPreview
linkPreview?.let {
if (!linkPreview.isValid) { return@let }
if (!linkPreview.isValid()) { return@let }
val attachment = linkPreview.getImage() ?: return@let
val openGroupLinkPreview = OpenGroupMessage.Attachment(
OpenGroupMessage.Attachment.Kind.LinkPreview,

View File

@ -69,7 +69,7 @@ object MessageSender {
is Destination.OpenGroup -> throw preconditionFailure
}
// Validate the message
message.isValid ?: throw Error.InvalidMessage
if (!message.isValid()) { throw Error.InvalidMessage }
// Convert it to protobuf
val proto = message.toProto() ?: throw Error.ProtoConversionFailed
// Serialize the protobuf
@ -162,7 +162,7 @@ object MessageSender {
}
}
// Validate the message
if (message !is VisibleMessage || !message.isValid) {
if (message !is VisibleMessage || !message.isValid()) {
throw Error.InvalidMessage
}
// Convert the message to an open group message