added Kind sealed class in UpdateMessageData + minor fixes

This commit is contained in:
Brice-W
2021-04-16 15:54:39 +10:00
parent 2a1dfff8c4
commit 2b7cf7c1b4
6 changed files with 29 additions and 120 deletions

View File

@@ -8,13 +8,14 @@ import org.session.libsignal.service.api.messages.SignalServiceGroup
object UpdateMessageBuilder {
fun buildGroupUpdateMessage(context: Context, updateData: UpdateMessageData, sender: String? = null, isOutgoing: Boolean = false): String {
val updateType = updateData.type
fun buildGroupUpdateMessage(context: Context, updateMessageData: UpdateMessageData, sender: String? = null, isOutgoing: Boolean = false): String {
var message: String = ""
val updateData = updateMessageData.kind as? UpdateMessageData.Kind.GroupUpdate ?: return message
val updateType = updateData.type
if (!isOutgoing && sender == null) return message
val senderName: String? = if (!isOutgoing) {
MessagingConfiguration.shared.storage.getDisplayNameForRecipient(sender!!) ?: sender
} else { sender }
} else { context.getString(R.string.MessageRecord_you) }
when (updateType) {
SignalServiceGroup.Type.CREATION -> {

View File

@@ -1,5 +1,7 @@
package org.session.libsession.messaging.utilities
import com.fasterxml.jackson.annotation.JsonSubTypes
import com.fasterxml.jackson.annotation.JsonTypeInfo
import com.fasterxml.jackson.core.JsonParseException
import org.session.libsignal.service.api.messages.SignalServiceGroup
import org.session.libsignal.utilities.JsonUtil
@@ -9,14 +11,21 @@ import java.util.*
// class used to save update messages details
class UpdateMessageData () {
var type: SignalServiceGroup.Type = SignalServiceGroup.Type.UNKNOWN
var groupName: String? = null
var updatedMembers: Collection<String> = Collections.emptyList()
var kind: Kind? = null
constructor(type: SignalServiceGroup.Type, groupName: String?, updatedMembers: Collection<String>): this() {
this.type = type
this.groupName = groupName
this.updatedMembers = updatedMembers
//the annotations below are required for serialization. Any new Kind class MUST be declared as JsonSubTypes as well
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
@JsonSubTypes(
JsonSubTypes.Type(Kind.GroupUpdate::class, name = "GroupUpdate")
)
sealed class Kind {
class GroupUpdate( var type: SignalServiceGroup.Type, var groupName: String?, var updatedMembers: Collection<String>): Kind() {
constructor(): this(SignalServiceGroup.Type.UNKNOWN, null, Collections.emptyList()) //default constructor required for json serialization
}
}
constructor(kind: Kind): this() {
this.kind = kind
}
companion object {
@@ -24,19 +33,19 @@ class UpdateMessageData () {
fun buildGroupUpdate(type: SignalServiceGroup.Type, name: String, members: Collection<String>): UpdateMessageData {
return when(type) {
SignalServiceGroup.Type.NAME_CHANGE -> UpdateMessageData(type, name, Collections.emptyList())
SignalServiceGroup.Type.MEMBER_ADDED -> UpdateMessageData(type,null, members)
SignalServiceGroup.Type.MEMBER_REMOVED -> UpdateMessageData(type,null, members)
else -> UpdateMessageData(type,null, Collections.emptyList())
SignalServiceGroup.Type.NAME_CHANGE -> UpdateMessageData(Kind.GroupUpdate(type, name, Collections.emptyList()))
SignalServiceGroup.Type.MEMBER_ADDED -> UpdateMessageData(Kind.GroupUpdate(type,null, members))
SignalServiceGroup.Type.MEMBER_REMOVED -> UpdateMessageData(Kind.GroupUpdate(type,null, members))
else -> UpdateMessageData(Kind.GroupUpdate(type,null, Collections.emptyList()))
}
}
fun fromJSON(json: String): UpdateMessageData {
return try {
return try {
JsonUtil.fromJson(json, UpdateMessageData::class.java)
} catch (e: JsonParseException) {
Log.e(TAG, "${e.message}")
UpdateMessageData(SignalServiceGroup.Type.UNKNOWN, null, Collections.emptyList())
UpdateMessageData(Kind.GroupUpdate(SignalServiceGroup.Type.UNKNOWN, null, Collections.emptyList()))
}
}
}

View File

@@ -496,6 +496,7 @@
<string name="MessageRecord_you_removed_s_from_the_group">You removed %1$s from the group.</string>
<string name="MessageRecord_s_removed_s_from_the_group">%1$s removed %2$s from the group.</string>
<string name="MessageRecord_you_were_removed_from_the_group">You were removed from the group.</string>
<string name="MessageRecord_you">You</string>
<string name="MessageRecord_you_called">You called</string>
<string name="MessageRecord_called_you">Contact called</string>
<string name="MessageRecord_missed_call">Missed call</string>