mirror of
https://github.com/oxen-io/session-android.git
synced 2025-02-19 19:58:26 +00:00
added specific UpdateMessageData.King types
This commit is contained in:
parent
2b7cf7c1b4
commit
99fa7eb767
@ -405,7 +405,7 @@ class Storage(context: Context, helper: SQLCipherOpenHelper) : Database(context,
|
||||
override fun insertIncomingInfoMessage(context: Context, senderPublicKey: String, groupID: String, type: SignalServiceGroup.Type, name: String, members: Collection<String>, admins: Collection<String>, sentTimestamp: Long) {
|
||||
val group = SignalServiceGroup(type, GroupUtil.getDecodedGroupIDAsData(groupID), SignalServiceGroup.GroupType.SIGNAL, name, members.toList(), null, admins.toList())
|
||||
val m = IncomingTextMessage(Address.fromSerialized(senderPublicKey), 1, sentTimestamp, "", Optional.of(group), 0, true)
|
||||
val updateData = UpdateMessageData.buildGroupUpdate(type, name, members).toJSON()
|
||||
val updateData = UpdateMessageData.buildGroupUpdate(type, name, members)?.toJSON()
|
||||
val infoMessage = IncomingGroupMessage(m, groupID, updateData, true)
|
||||
val smsDB = DatabaseFactory.getSmsDatabase(context)
|
||||
smsDB.insertMessageInbox(infoMessage)
|
||||
@ -415,7 +415,7 @@ class Storage(context: Context, helper: SQLCipherOpenHelper) : Database(context,
|
||||
val userPublicKey = getUserPublicKey()
|
||||
val recipient = Recipient.from(context, Address.fromSerialized(groupID), false)
|
||||
|
||||
val updateData = UpdateMessageData.buildGroupUpdate(type, name, members).toJSON()
|
||||
val updateData = UpdateMessageData.buildGroupUpdate(type, name, members)?.toJSON() ?: ""
|
||||
val infoMessage = OutgoingGroupMediaMessage(recipient, updateData, groupID, null, sentTimestamp, 0, true, null, listOf(), listOf())
|
||||
val mmsDB = DatabaseFactory.getMmsDatabase(context)
|
||||
val mmsSmsDB = DatabaseFactory.getMmsSmsDatabase(context)
|
||||
|
@ -9,30 +9,29 @@ import org.session.libsignal.service.api.messages.SignalServiceGroup
|
||||
object UpdateMessageBuilder {
|
||||
|
||||
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
|
||||
var message = ""
|
||||
val updateData = updateMessageData.kind ?: return message
|
||||
if (!isOutgoing && sender == null) return message
|
||||
val senderName: String? = if (!isOutgoing) {
|
||||
val senderName: String = if (!isOutgoing) {
|
||||
MessagingConfiguration.shared.storage.getDisplayNameForRecipient(sender!!) ?: sender
|
||||
} else { context.getString(R.string.MessageRecord_you) }
|
||||
|
||||
when (updateType) {
|
||||
SignalServiceGroup.Type.CREATION -> {
|
||||
when (updateData) {
|
||||
is UpdateMessageData.Kind.GroupCreation -> {
|
||||
message = if (isOutgoing) {
|
||||
context.getString(R.string.MessageRecord_you_created_a_new_group)
|
||||
} else {
|
||||
context.getString(R.string.MessageRecord_s_added_you_to_the_group, senderName)
|
||||
}
|
||||
}
|
||||
SignalServiceGroup.Type.NAME_CHANGE -> {
|
||||
is UpdateMessageData.Kind.GroupNameChange -> {
|
||||
message = if (isOutgoing) {
|
||||
context.getString(R.string.MessageRecord_you_renamed_the_group_to_s, updateData.groupName)
|
||||
context.getString(R.string.MessageRecord_you_renamed_the_group_to_s, updateData.name)
|
||||
} else {
|
||||
context.getString(R.string.MessageRecord_s_renamed_the_group_to_s, senderName, updateData.groupName)
|
||||
context.getString(R.string.MessageRecord_s_renamed_the_group_to_s, senderName, updateData.name)
|
||||
}
|
||||
}
|
||||
SignalServiceGroup.Type.MEMBER_ADDED -> {
|
||||
is UpdateMessageData.Kind.GroupMemberAdded -> {
|
||||
val members = updateData.updatedMembers.joinToString(", ") {
|
||||
MessagingConfiguration.shared.storage.getDisplayNameForRecipient(it) ?: it
|
||||
}
|
||||
@ -42,7 +41,7 @@ object UpdateMessageBuilder {
|
||||
context.getString(R.string.MessageRecord_s_added_s_to_the_group, senderName, members)
|
||||
}
|
||||
}
|
||||
SignalServiceGroup.Type.MEMBER_REMOVED -> {
|
||||
is UpdateMessageData.Kind.GroupMemberRemoved -> {
|
||||
val storage = MessagingConfiguration.shared.storage
|
||||
val userPublicKey = storage.getUserPublicKey()!!
|
||||
// 1st case: you are part of the removed members
|
||||
@ -64,16 +63,13 @@ object UpdateMessageBuilder {
|
||||
}
|
||||
}
|
||||
}
|
||||
SignalServiceGroup.Type.QUIT -> {
|
||||
is UpdateMessageData.Kind.GroupMemberLeft -> {
|
||||
message = if (isOutgoing) {
|
||||
context.getString(R.string.MessageRecord_left_group)
|
||||
} else {
|
||||
context.getString(R.string.ConversationItem_group_action_left, senderName)
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
message = context.getString(R.string.MessageRecord_s_updated_group, senderName)
|
||||
}
|
||||
}
|
||||
return message
|
||||
}
|
||||
|
@ -16,12 +16,24 @@ class UpdateMessageData () {
|
||||
//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")
|
||||
JsonSubTypes.Type(Kind.GroupCreation::class, name = "GroupCreation"),
|
||||
JsonSubTypes.Type(Kind.GroupNameChange::class, name = "GroupNameChange"),
|
||||
JsonSubTypes.Type(Kind.GroupMemberAdded::class, name = "GroupMemberAdded"),
|
||||
JsonSubTypes.Type(Kind.GroupMemberRemoved::class, name = "GroupMemberRemoved"),
|
||||
JsonSubTypes.Type(Kind.GroupMemberLeft::class, name = "GroupMemberLeft")
|
||||
)
|
||||
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
|
||||
sealed class Kind() {
|
||||
class GroupCreation(): Kind()
|
||||
class GroupNameChange(val name: String): Kind() {
|
||||
constructor(): this("") //default constructor required for json serialization
|
||||
}
|
||||
class GroupMemberAdded(val updatedMembers: Collection<String>): Kind() {
|
||||
constructor(): this(Collections.emptyList())
|
||||
}
|
||||
class GroupMemberRemoved(val updatedMembers: Collection<String>): Kind() {
|
||||
constructor(): this(Collections.emptyList())
|
||||
}
|
||||
class GroupMemberLeft(): Kind()
|
||||
}
|
||||
|
||||
constructor(kind: Kind): this() {
|
||||
@ -31,21 +43,23 @@ class UpdateMessageData () {
|
||||
companion object {
|
||||
val TAG = UpdateMessageData::class.simpleName
|
||||
|
||||
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) {
|
||||
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()))
|
||||
SignalServiceGroup.Type.CREATION -> UpdateMessageData(Kind.GroupCreation())
|
||||
SignalServiceGroup.Type.NAME_CHANGE -> UpdateMessageData(Kind.GroupNameChange(name))
|
||||
SignalServiceGroup.Type.MEMBER_ADDED -> UpdateMessageData(Kind.GroupMemberAdded(members))
|
||||
SignalServiceGroup.Type.MEMBER_REMOVED -> UpdateMessageData(Kind.GroupMemberRemoved(members))
|
||||
SignalServiceGroup.Type.QUIT -> UpdateMessageData(Kind.GroupMemberLeft())
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
fun fromJSON(json: String): UpdateMessageData {
|
||||
fun fromJSON(json: String): UpdateMessageData? {
|
||||
return try {
|
||||
JsonUtil.fromJson(json, UpdateMessageData::class.java)
|
||||
} catch (e: JsonParseException) {
|
||||
Log.e(TAG, "${e.message}")
|
||||
UpdateMessageData(Kind.GroupUpdate(SignalServiceGroup.Type.UNKNOWN, null, Collections.emptyList()))
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user