change package name + start of implementation

This commit is contained in:
Brice 2020-11-27 15:56:16 +11:00
parent 742370b5fa
commit 888eda4ba9
16 changed files with 303 additions and 29 deletions

View File

@ -38,7 +38,10 @@ dependencies {
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation "com.google.protobuf:protobuf-java:$protobufVersion"
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation project(":libsignal")
}

View File

@ -1,5 +1,9 @@
package org.session.messaging.messages
package org.session.libsession.messaging.messages
enum class Destination {
sealed class Destination {
class Contact(val publicKey: String)
class ClosedGroup(val groupPublicKey: String)
class OpenGroup(val channel: Long, val server: String)
}

View File

@ -1,5 +1,33 @@
package org.session.messaging.messages
package org.session.libsession.messaging.messages
open class Message {
import org.session.libsignal.service.internal.push.SignalServiceProtos
abstract class Message {
var id: String? = null
var threadID: String? = null
var sentTimestamp: Long? = null
var receivedTimestamp: Long? = null
var recipient: String? = null
var sender: String? = null
var groupPublicKey: String? = null
var openGroupServerMessageID: Long? = null
companion object {
@JvmStatic
val ttl = 2 * 24 * 60 * 60 * 1000
//fun fromProto(proto: SignalServiceProtos.Content): Message? {}
}
open fun isValid(): Boolean {
sentTimestamp = if (sentTimestamp!! > 0) sentTimestamp else return false
receivedTimestamp = if (receivedTimestamp!! > 0) receivedTimestamp else return false
return sender != null && recipient != null
}
abstract fun toProto(): SignalServiceProtos.Content?
}

View File

@ -1,4 +1,154 @@
package org.session.messaging.messages.control
package org.session.libsession.messaging.messages.control
class ClosedGroupUpdate : ControlMessage() {
import com.google.protobuf.ByteString
import org.session.libsignal.libsignal.logging.Log
import org.session.libsignal.service.internal.push.SignalServiceProtos
import org.session.libsignal.service.loki.protocol.closedgroups.ClosedGroupSenderKey
class ClosedGroupUpdate() : ControlMessage() {
companion object {
const val TAG = "ClosedGroupUpdate"
fun fromProto(proto: SignalServiceProtos.Content): ClosedGroupUpdate? {
val closedGroupUpdateProto = proto.dataMessage?.closedGroupUpdate ?: return null
val groupPublicKey = closedGroupUpdateProto.groupPublicKey
var kind: Kind? = null
when(closedGroupUpdateProto.type) {
SignalServiceProtos.ClosedGroupUpdate.Type.NEW -> {
val name = closedGroupUpdateProto.name ?: return null
val groupPrivateKey = closedGroupUpdateProto.groupPrivateKey ?: return null
val senderKeys = closedGroupUpdateProto.senderKeysList.map { ClosedGroupSenderKey.fromProto(it) }
kind = Kind.New(
groupPublicKey = groupPublicKey.toByteArray(),
name = name,
groupPrivateKey = groupPrivateKey.toByteArray(),
senderKeys = senderKeys,
members = closedGroupUpdateProto.membersList.map { it.toByteArray() },
admins = closedGroupUpdateProto.adminsList.map { it.toByteArray() }
)
}
SignalServiceProtos.ClosedGroupUpdate.Type.INFO -> {
val name = closedGroupUpdateProto.name ?: return null
val senderKeys = closedGroupUpdateProto.senderKeysList.map { ClosedGroupSenderKey.fromProto(it) }
kind = Kind.Info(
groupPublicKey = groupPublicKey.toByteArray(),
name = name,
senderKeys = senderKeys,
members = closedGroupUpdateProto.membersList.map { it.toByteArray() },
admins = closedGroupUpdateProto.adminsList.map { it.toByteArray() }
)
}
SignalServiceProtos.ClosedGroupUpdate.Type.SENDER_KEY_REQUEST -> {
kind = Kind.SenderKeyRequest(groupPublicKey = groupPublicKey.toByteArray())
}
SignalServiceProtos.ClosedGroupUpdate.Type.SENDER_KEY -> {
val senderKeyProto = closedGroupUpdateProto.senderKeysList?.first() ?: return null
kind = Kind.SenderKey(
groupPublicKey = groupPublicKey.toByteArray(),
senderKey = ClosedGroupSenderKey.fromProto(senderKeyProto)
)
}
}
return ClosedGroupUpdate(kind)
}
}
//private val TAG: String = ClosedGroupUpdate::class.java.simpleName
// Kind enum
sealed class Kind {
class New(val groupPublicKey: ByteArray, val name: String, val groupPrivateKey: ByteArray, val senderKeys: Collection<org.session.libsignal.service.loki.protocol.closedgroups.ClosedGroupSenderKey>, val members: Collection<ByteArray>, val admins: Collection<ByteArray>) : Kind()
class Info(val groupPublicKey: ByteArray, val name: String, val senderKeys: Collection<org.session.libsignal.service.loki.protocol.closedgroups.ClosedGroupSenderKey>, val members: Collection<ByteArray>, val admins: Collection<ByteArray>) : Kind()
class SenderKeyRequest(val groupPublicKey: ByteArray) : Kind()
class SenderKey(val groupPublicKey: ByteArray, val senderKey: org.session.libsignal.service.loki.protocol.closedgroups.ClosedGroupSenderKey) : Kind()
}
var kind: Kind? = null
// constructors
internal constructor(kind: Kind?) : this() {
this.kind = kind
}
override fun isValid(): Boolean {
if (!super.isValid() || kind == null) return false
val kind = kind ?: return false
when(kind) {
is Kind.New -> {
return !kind.groupPublicKey.isEmpty() && !kind.name.isEmpty() && !kind.groupPrivateKey.isEmpty() && !kind.members.isEmpty() && !kind.admins.isEmpty()
}
is Kind.Info -> {
return !kind.groupPublicKey.isEmpty() && !kind.name.isEmpty() && !kind.members.isEmpty() && !kind.admins.isEmpty()
}
is Kind.SenderKeyRequest -> {
return !kind.groupPublicKey.isEmpty()
}
is Kind.SenderKey -> {
return !kind.groupPublicKey.isEmpty()
}
}
}
override fun toProto(): SignalServiceProtos.Content? {
val kind = kind
if (kind == null) {
Log.w(TAG, "Couldn't construct closed group update proto from: $this")
return null
}
try {
val closedGroupUpdate: SignalServiceProtos.ClosedGroupUpdate.Builder = SignalServiceProtos.ClosedGroupUpdate.newBuilder()
when (kind) {
is Kind.New -> {
closedGroupUpdate.groupPublicKey = ByteString.copyFrom(kind.groupPublicKey)
closedGroupUpdate.type = SignalServiceProtos.ClosedGroupUpdate.Type.NEW
closedGroupUpdate.name = kind.name
closedGroupUpdate.groupPrivateKey = ByteString.copyFrom(kind.groupPrivateKey)
closedGroupUpdate.addAllSenderKeys(kind.senderKeys.map { it.toProto() })
closedGroupUpdate.addAllMembers(kind.members.map { ByteString.copyFrom(it) })
closedGroupUpdate.addAllAdmins(kind.admins.map { ByteString.copyFrom(it) })
}
is Kind.Info -> {
closedGroupUpdate.groupPublicKey = ByteString.copyFrom(kind.groupPublicKey)
closedGroupUpdate.type = SignalServiceProtos.ClosedGroupUpdate.Type.INFO
closedGroupUpdate.name = kind.name
closedGroupUpdate.addAllSenderKeys(kind.senderKeys.map { it.toProto() })
closedGroupUpdate.addAllMembers(kind.members.map { ByteString.copyFrom(it) })
closedGroupUpdate.addAllAdmins(kind.admins.map { ByteString.copyFrom(it) })
}
is Kind.SenderKeyRequest -> {
closedGroupUpdate.groupPublicKey = ByteString.copyFrom(kind.groupPublicKey)
closedGroupUpdate.type = SignalServiceProtos.ClosedGroupUpdate.Type.SENDER_KEY_REQUEST
}
is Kind.SenderKey -> {
closedGroupUpdate.groupPublicKey = ByteString.copyFrom(kind.groupPublicKey)
closedGroupUpdate.type = SignalServiceProtos.ClosedGroupUpdate.Type.SENDER_KEY
closedGroupUpdate.addAllSenderKeys(listOf( kind.senderKey.toProto() ))
}
}
val contentProto = SignalServiceProtos.Content.newBuilder()
val dataMessageProto = SignalServiceProtos.DataMessage.newBuilder()
dataMessageProto.closedGroupUpdate = closedGroupUpdate.build()
contentProto.dataMessage = dataMessageProto.build()
return contentProto.build()
} catch (e: Exception) {
Log.w(TAG, "Couldn't construct closed group update proto from: $this")
return null
}
return null
}
}
// extension functions to class ClosedGroupSenderKey
private fun ClosedGroupSenderKey.Companion.fromProto(proto: SignalServiceProtos.ClosedGroupUpdate.SenderKey): org.session.libsignal.service.loki.protocol.closedgroups.ClosedGroupSenderKey {
return ClosedGroupSenderKey(chainKey = proto.chainKey.toByteArray(), keyIndex = proto.keyIndex, publicKey = proto.publicKey.toByteArray())
}
private fun ClosedGroupSenderKey.toProto(): SignalServiceProtos.ClosedGroupUpdate.SenderKey {
val proto = SignalServiceProtos.ClosedGroupUpdate.SenderKey.newBuilder()
proto.chainKey = ByteString.copyFrom(chainKey)
proto.keyIndex = keyIndex
proto.publicKey = ByteString.copyFrom(publicKey)
return proto.build()
}

View File

@ -1,6 +1,6 @@
package org.session.messaging.messages.control
package org.session.libsession.messaging.messages.control
import org.session.messaging.messages.Message
import org.session.libsession.messaging.messages.Message
open class ControlMessage : Message() {
abstract class ControlMessage : Message() {
}

View File

@ -1,4 +1,13 @@
package org.session.messaging.messages.control
package org.session.libsession.messaging.messages.control
import org.session.libsignal.service.internal.push.SignalServiceProtos
class ExpirationTimerUpdate : ControlMessage() {
override fun fromProto(proto: SignalServiceProtos.Content): ExpirationTimerUpdate? {
TODO("Not yet implemented")
}
override fun toProto(): SignalServiceProtos.Content? {
TODO("Not yet implemented")
}
}

View File

@ -1,4 +1,13 @@
package org.session.messaging.messages.control
package org.session.libsession.messaging.messages.control
import org.session.libsignal.service.internal.push.SignalServiceProtos
class ReadReceipt : ControlMessage() {
override fun fromProto(proto: SignalServiceProtos.Content): ReadReceipt? {
TODO("Not yet implemented")
}
override fun toProto(): SignalServiceProtos.Content? {
TODO("Not yet implemented")
}
}

View File

@ -1,4 +1,13 @@
package org.session.messaging.messages.control
package org.session.libsession.messaging.messages.control
import org.session.libsignal.service.internal.push.SignalServiceProtos
class TypingIndicator : ControlMessage() {
override fun fromProto(proto: SignalServiceProtos.Content): TypingIndicator?{
TODO("Not yet implemented")
}
override fun toProto(): SignalServiceProtos.Content? {
TODO("Not yet implemented")
}
}

View File

@ -1,6 +1,14 @@
package org.session.messaging.messages.control.unused
package org.session.libsession.messaging.messages.control.unused
import org.session.messaging.messages.control.ControlMessage
import org.session.libsession.messaging.messages.control.ControlMessage
import org.session.libsignal.service.internal.push.SignalServiceProtos
class NullMessage : ControlMessage() {
override fun fromProto(proto: SignalServiceProtos.Content): NullMessage? {
TODO("Not yet implemented")
}
override fun toProto(): SignalServiceProtos.Content? {
TODO("Not yet implemented")
}
}

View File

@ -1,6 +1,14 @@
package org.session.messaging.messages.control.unused
package org.session.libsession.messaging.messages.control.unused
import org.session.messaging.messages.control.ControlMessage
import org.session.libsession.messaging.messages.control.ControlMessage
import org.session.libsignal.service.internal.push.SignalServiceProtos
class SessionRequest : ControlMessage() {
override fun fromProto(proto: SignalServiceProtos.Content): SessionRequest? {
TODO("Not yet implemented")
}
override fun toProto(): SignalServiceProtos.Content? {
TODO("Not yet implemented")
}
}

View File

@ -1,4 +1,13 @@
package org.session.messaging.messages.visible
package org.session.libsession.messaging.messages.visible
internal class Contact {
import org.session.libsignal.service.internal.push.SignalServiceProtos
internal class Contact : VisibleMessage() {
override fun fromProto(proto: SignalServiceProtos.Content): Contact? {
TODO("Not yet implemented")
}
override fun toProto(): SignalServiceProtos.Content? {
TODO("Not yet implemented")
}
}

View File

@ -1,4 +1,13 @@
package org.session.messaging.messages.visible
package org.session.libsession.messaging.messages.visible
internal class LinkPreview {
import org.session.libsignal.service.internal.push.SignalServiceProtos
internal class LinkPreview : VisibleMessage(){
override fun fromProto(proto: SignalServiceProtos.Content): LinkPreview? {
TODO("Not yet implemented")
}
override fun toProto(): SignalServiceProtos.Content? {
TODO("Not yet implemented")
}
}

View File

@ -1,4 +1,13 @@
package org.session.messaging.messages.visible
package org.session.libsession.messaging.messages.visible
internal class Profile {
import org.session.libsignal.service.internal.push.SignalServiceProtos
internal class Profile : VisibleMessage() {
override fun fromProto(proto: SignalServiceProtos.Content): Profile? {
TODO("Not yet implemented")
}
override fun toProto(): SignalServiceProtos.Content? {
TODO("Not yet implemented")
}
}

View File

@ -1,4 +1,13 @@
package org.session.messaging.messages.visible
package org.session.libsession.messaging.messages.visible
internal class Quote {
import org.session.libsignal.service.internal.push.SignalServiceProtos
internal class Quote : VisibleMessage() {
override fun fromProto(proto: SignalServiceProtos.Content): Quote? {
TODO("Not yet implemented")
}
override fun toProto(): SignalServiceProtos.Content? {
TODO("Not yet implemented")
}
}

View File

@ -1,6 +1,6 @@
package org.session.messaging.messages.visible
package org.session.libsession.messaging.messages.visible
import org.session.messaging.messages.Message
import org.session.libsession.messaging.messages.Message
class VisibleMessage : Message() {
abstract class VisibleMessage : Message() {
}

View File

@ -1,4 +1,14 @@
package org.session.messaging.messages.visible.attachments
package org.session.libsession.messaging.messages.visible.attachments
internal class Attachment {
import org.session.libsession.messaging.messages.visible.VisibleMessage
import org.session.libsignal.service.internal.push.SignalServiceProtos
internal class Attachment : VisibleMessage() {
override fun fromProto(proto: SignalServiceProtos.Content): Attachment? {
TODO("Not yet implemented")
}
override fun toProto(): SignalServiceProtos.Content? {
TODO("Not yet implemented")
}
}