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

@ -1,104 +0,0 @@
package org.thoughtcrime.securesms.loki.utilities
import android.content.Context
import org.session.libsession.messaging.threads.Address
import org.session.libsession.messaging.threads.recipients.Recipient
import org.session.libsession.messaging.threads.recipients.RecipientModifiedListener
import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsignal.utilities.Base64
import org.session.libsignal.service.internal.push.SignalServiceProtos
import java.util.*
import network.loki.messenger.R
import org.session.libsignal.utilities.logging.Log
import java.io.IOException
//TODO that class isn't used anymore
class GroupDescription(context: Context, groupContext: SignalServiceProtos.GroupContext?) {
private val context: Context
private val groupContext: SignalServiceProtos.GroupContext?
private val newMembers: MutableList<Recipient>
private val removedMembers: MutableList<Recipient>
private var wasCurrentUserRemoved: Boolean = false
private fun toRecipient(hexEncodedPublicKey: String): Recipient {
val address = Address.fromSerialized(hexEncodedPublicKey)
return Recipient.from(context, address, false)
}
fun toString(sender: Recipient): String {
if (wasCurrentUserRemoved) {
return context.getString(R.string.GroupUtil_you_were_removed_from_group)
}
val description = StringBuilder()
description.append(context.getString(R.string.MessageRecord_s_updated_group, sender.toShortString()))
if (groupContext == null) {
return description.toString()
}
val title = groupContext.name
if (!newMembers.isEmpty()) {
description.append("\n")
description.append(context.resources.getQuantityString(R.plurals.GroupUtil_joined_the_group,
newMembers.size, toString(newMembers)))
}
if (!removedMembers.isEmpty()) {
description.append("\n")
description.append(context.resources.getQuantityString(R.plurals.GroupUtil_removed_from_the_group,
removedMembers.size, toString(removedMembers)))
}
if (title != null && !title.trim { it <= ' ' }.isEmpty()) {
val separator = if (!newMembers.isEmpty() || !removedMembers.isEmpty()) " " else "\n"
description.append(separator)
description.append(context.getString(R.string.GroupUtil_group_name_is_now, title))
}
return description.toString()
}
fun addListener(listener: RecipientModifiedListener?) {
if (!newMembers.isEmpty()) {
for (member in newMembers) {
member.addListener(listener)
}
}
}
private fun toString(recipients: List<Recipient>): String {
var result = ""
for (i in recipients.indices) {
result += recipients[i].toShortString()
if (i != recipients.size - 1) result += ", "
}
return result
}
init {
this.context = context.applicationContext
this.groupContext = groupContext
newMembers = LinkedList()
removedMembers = LinkedList()
if (groupContext != null) {
val newMembers = groupContext.newMembersList
for (member in newMembers) {
this.newMembers.add(toRecipient(member))
}
val removedMembers = groupContext.removedMembersList
for (member in removedMembers) {
this.removedMembers.add(toRecipient(member))
}
wasCurrentUserRemoved = removedMembers.contains(TextSecurePreferences.getLocalNumber(context))
}
}
companion object {
fun getDescription(context: Context, encodedGroup: String?): GroupDescription {
return if (encodedGroup == null) {
GroupDescription(context, null)
} else try {
val groupContext = SignalServiceProtos.GroupContext.parseFrom(Base64.decode(encodedGroup))
GroupDescription(context, groupContext)
} catch (e: IOException) {
Log.w("Loki", e)
GroupDescription(context, null)
}
}
}
}

View File

@ -427,6 +427,7 @@
<string name="MessageRecord_you_removed_s_from_the_group">Vous avez supprimé %1$s du groupe.</string> <string name="MessageRecord_you_removed_s_from_the_group">Vous avez supprimé %1$s du groupe.</string>
<string name="MessageRecord_s_removed_s_from_the_group">%1$s a supprimé %2$s du groupe.</string> <string name="MessageRecord_s_removed_s_from_the_group">%1$s a supprimé %2$s du groupe.</string>
<string name="MessageRecord_you_were_removed_from_the_group">Vous avez été supprimé du groupe.</string> <string name="MessageRecord_you_were_removed_from_the_group">Vous avez été supprimé du groupe.</string>
<string name="MessageRecord_you">Vous</string>
<string name="MessageRecord_you_called">Vous avez appelé</string> <string name="MessageRecord_you_called">Vous avez appelé</string>
<string name="MessageRecord_called_you">Contact appelé</string> <string name="MessageRecord_called_you">Contact appelé</string>
<string name="MessageRecord_missed_call">Appel manqué</string> <string name="MessageRecord_missed_call">Appel manqué</string>

View File

@ -502,6 +502,7 @@
<string name="MessageRecord_you_removed_s_from_the_group">You removed %1$s from the group.</string> <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_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_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_you_called">You called</string>
<string name="MessageRecord_called_you">Contact called</string> <string name="MessageRecord_called_you">Contact called</string>
<string name="MessageRecord_missed_call">Missed call</string> <string name="MessageRecord_missed_call">Missed call</string>

View File

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

View File

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