classes structure redesign + LinkPreview & BaseVisibleMessage implementations

pull/420/head
Brice 4 years ago
parent 3f0e456002
commit f5a583e7c8

@ -0,0 +1,98 @@
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
val quote = Quote.fromProto(quoteProto)
quote?.let { result.quote = quote }
val linkPreviewProto = dataMessage.previewList.first()
val linkPreview = LinkPreview.fromProto(linkPreviewProto)
linkPreview?.let { result.linkPreview = linkPreview }
// TODO Contact
val profile = Profile.fromProto(dataMessage)
if (profile != null) { 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.isEmpty()) 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?.toProto("") //TODO
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) }
// 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
}
}
}

@ -1,9 +1,8 @@
package org.session.libsession.messaging.messages.visible
import org.session.libsession.messaging.messages.control.ExpirationTimerUpdate
import org.session.libsignal.service.internal.push.SignalServiceProtos
internal class Contact : VisibleMessage() {
class Contact : VisibleMessage<SignalServiceProtos.DataMessage.Contact?>() {
companion object {
fun fromProto(proto: SignalServiceProtos.Content): Contact? {
@ -11,7 +10,7 @@ internal class Contact : VisibleMessage() {
}
}
override fun toProto(): SignalServiceProtos.Content? {
override fun toProto(transaction: String): SignalServiceProtos.DataMessage.Contact? {
TODO("Not yet implemented")
}
}

@ -1,17 +1,58 @@
package org.session.libsession.messaging.messages.visible
import org.session.libsession.messaging.messages.control.ExpirationTimerUpdate
import org.session.libsession.messaging.messages.control.TypingIndicator
import org.session.libsignal.libsignal.logging.Log
import org.session.libsignal.service.internal.push.SignalServiceProtos
internal class LinkPreview : VisibleMessage(){
class LinkPreview() : VisibleMessage<SignalServiceProtos.DataMessage.Preview?>(){
var title: String? = null
var url: String? = null
var attachmentID: String? = null
companion object {
fun fromProto(proto: SignalServiceProtos.Content): LinkPreview? {
TODO("Not yet implemented")
const val TAG = "LinkPreview"
fun fromProto(proto: SignalServiceProtos.DataMessage.Preview): LinkPreview? {
val title = proto.title
val url = proto.url
return LinkPreview(title, url, null)
}
}
override fun toProto(): SignalServiceProtos.Content? {
TODO("Not yet implemented")
//constructor
internal constructor(title: String?, url: String, attachmentID: String?) : this() {
this.title = title
this.url = url
this.attachmentID = attachmentID
}
// validation
override fun isValid(): Boolean {
if (!super.isValid()) return false
return (title != null && url != null && attachmentID != null)
}
override fun toProto(transaction: String): SignalServiceProtos.DataMessage.Preview? {
val url = url
if (url == null) {
Log.w(TAG, "Couldn't construct link preview proto from: $this")
return null
}
val linkPreviewProto = SignalServiceProtos.DataMessage.Preview.newBuilder()
linkPreviewProto.url = url
title?. let { linkPreviewProto.title = title }
val attachmentID = attachmentID
attachmentID?.let {
//TODO database stuff
}
// Build
try {
return linkPreviewProto.build()
} catch (e: Exception) {
Log.w(TAG, "Couldn't construct link preview proto from: $this")
return null
}
}
}

@ -1,17 +1,16 @@
package org.session.libsession.messaging.messages.visible
import org.session.libsession.messaging.messages.control.ExpirationTimerUpdate
import org.session.libsignal.service.internal.push.SignalServiceProtos
internal class Profile : VisibleMessage() {
class Profile() : VisibleMessage<SignalServiceProtos.DataMessage?>() {
companion object {
fun fromProto(proto: SignalServiceProtos.Content): Profile? {
fun fromProto(proto: SignalServiceProtos.DataMessage): Profile? {
TODO("Not yet implemented")
}
}
override fun toProto(): SignalServiceProtos.Content? {
TODO("Not yet implemented")
override fun toProto(transaction: String): SignalServiceProtos.DataMessage? {
return null
}
}

@ -1,17 +1,21 @@
package org.session.libsession.messaging.messages.visible
import org.session.libsession.messaging.messages.control.ExpirationTimerUpdate
import org.session.libsignal.service.internal.push.SignalServiceProtos
internal class Quote : VisibleMessage() {
class Quote() : VisibleMessage<SignalServiceProtos.DataMessage.Quote?>() {
var timestamp: Long? = 0
var publicKey: String? = null
var text: String? = null
var attachmentID: String? = null
companion object {
fun fromProto(proto: SignalServiceProtos.Content): Quote? {
fun fromProto(proto: SignalServiceProtos.DataMessage.Quote): Quote? {
TODO("Not yet implemented")
}
}
override fun toProto(): SignalServiceProtos.Content? {
TODO("Not yet implemented")
override fun toProto(transaction: String): SignalServiceProtos.DataMessage.Quote? {
return null
}
}

@ -1,6 +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 VisibleMessage : Message() {
abstract class VisibleMessage<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:) instead.")
TODO("Not yet implemented")
}
}

@ -1,10 +1,9 @@
package org.session.libsession.messaging.messages.visible.attachments
import org.session.libsession.messaging.messages.control.ExpirationTimerUpdate
import org.session.libsession.messaging.messages.visible.VisibleMessage
import org.session.libsession.messaging.messages.visible.BaseVisibleMessage
import org.session.libsignal.service.internal.push.SignalServiceProtos
internal class Attachment : VisibleMessage() {
internal class Attachment : BaseVisibleMessage() {
companion object {
fun fromProto(proto: SignalServiceProtos.Content): Attachment? {

Loading…
Cancel
Save