2019-12-02 15:34:05 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2020-09-01 13:27:01 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common"
|
2019-12-02 15:34:05 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
v1protocol "github.com/status-im/status-go/protocol/v1"
|
|
|
|
)
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
var defaultSystemMessagesTranslations = new(systemMessageTranslationsMap)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
defaultSystemMessagesTranslationSet := map[protobuf.MembershipUpdateEvent_EventType]string{
|
|
|
|
protobuf.MembershipUpdateEvent_CHAT_CREATED: "{{from}} created the group {{name}}",
|
|
|
|
protobuf.MembershipUpdateEvent_NAME_CHANGED: "{{from}} changed the group's name to {{name}}",
|
2022-05-20 10:53:28 +00:00
|
|
|
protobuf.MembershipUpdateEvent_MEMBERS_ADDED: "{{from}} has added {{members}}",
|
2021-03-29 15:41:30 +00:00
|
|
|
protobuf.MembershipUpdateEvent_ADMINS_ADDED: "{{from}} has made {{members}} admin",
|
|
|
|
protobuf.MembershipUpdateEvent_MEMBER_REMOVED: "{{member}} left the group",
|
|
|
|
protobuf.MembershipUpdateEvent_ADMIN_REMOVED: "{{member}} is not admin anymore",
|
2022-07-12 07:48:58 +00:00
|
|
|
protobuf.MembershipUpdateEvent_COLOR_CHANGED: "{{from}} changed the group's color",
|
2022-08-08 15:22:22 +00:00
|
|
|
protobuf.MembershipUpdateEvent_IMAGE_CHANGED: "{{from}} changed the group's image",
|
2021-03-29 15:41:30 +00:00
|
|
|
}
|
|
|
|
defaultSystemMessagesTranslations.Init(defaultSystemMessagesTranslationSet)
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func tsprintf(format string, params map[string]string) string {
|
|
|
|
for key, val := range params {
|
2020-02-10 11:22:37 +00:00
|
|
|
format = strings.Replace(format, "{{"+key+"}}", val, -1)
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
2020-02-10 11:22:37 +00:00
|
|
|
return format
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
func eventToSystemMessage(e v1protocol.MembershipUpdateEvent, translations *systemMessageTranslationsMap) *common.Message {
|
2019-12-02 15:34:05 +00:00
|
|
|
var text string
|
|
|
|
switch e.Type {
|
|
|
|
case protobuf.MembershipUpdateEvent_CHAT_CREATED:
|
2021-03-29 15:41:30 +00:00
|
|
|
message, _ := translations.Load(protobuf.MembershipUpdateEvent_CHAT_CREATED)
|
|
|
|
text = tsprintf(message, map[string]string{"from": "@" + e.From, "name": e.Name})
|
2019-12-02 15:34:05 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_NAME_CHANGED:
|
2021-03-29 15:41:30 +00:00
|
|
|
message, _ := translations.Load(protobuf.MembershipUpdateEvent_NAME_CHANGED)
|
|
|
|
text = tsprintf(message, map[string]string{"from": "@" + e.From, "name": e.Name})
|
2022-07-12 07:48:58 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_COLOR_CHANGED:
|
|
|
|
message, _ := translations.Load(protobuf.MembershipUpdateEvent_COLOR_CHANGED)
|
2022-08-08 15:22:22 +00:00
|
|
|
text = tsprintf(message, map[string]string{"from": "@" + e.From})
|
|
|
|
case protobuf.MembershipUpdateEvent_IMAGE_CHANGED:
|
|
|
|
message, _ := translations.Load(protobuf.MembershipUpdateEvent_IMAGE_CHANGED)
|
|
|
|
text = tsprintf(message, map[string]string{"from": "@" + e.From})
|
2019-12-02 15:34:05 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_MEMBERS_ADDED:
|
|
|
|
|
|
|
|
var memberMentions []string
|
|
|
|
for _, s := range e.Members {
|
|
|
|
memberMentions = append(memberMentions, "@"+s)
|
|
|
|
}
|
2021-03-29 15:41:30 +00:00
|
|
|
message, _ := translations.Load(protobuf.MembershipUpdateEvent_MEMBERS_ADDED)
|
|
|
|
text = tsprintf(message, map[string]string{"from": "@" + e.From, "members": strings.Join(memberMentions, ", ")})
|
2019-12-02 15:34:05 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_ADMINS_ADDED:
|
|
|
|
var memberMentions []string
|
|
|
|
for _, s := range e.Members {
|
|
|
|
memberMentions = append(memberMentions, "@"+s)
|
|
|
|
}
|
2021-03-29 15:41:30 +00:00
|
|
|
message, _ := translations.Load(protobuf.MembershipUpdateEvent_ADMINS_ADDED)
|
|
|
|
text = tsprintf(message, map[string]string{"from": "@" + e.From, "members": strings.Join(memberMentions, ", ")})
|
2019-12-02 15:34:05 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_MEMBER_REMOVED:
|
2021-03-29 15:41:30 +00:00
|
|
|
message, _ := translations.Load(protobuf.MembershipUpdateEvent_MEMBER_REMOVED)
|
|
|
|
text = tsprintf(message, map[string]string{"member": "@" + e.Members[0]})
|
2019-12-02 15:34:05 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_ADMIN_REMOVED:
|
2021-03-29 15:41:30 +00:00
|
|
|
message, _ := translations.Load(protobuf.MembershipUpdateEvent_ADMIN_REMOVED)
|
|
|
|
text = tsprintf(message, map[string]string{"member": "@" + e.Members[0]})
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
timestamp := v1protocol.TimestampInMsFromTime(time.Now())
|
2020-09-01 13:27:01 +00:00
|
|
|
message := &common.Message{
|
2019-12-02 15:34:05 +00:00
|
|
|
ChatMessage: protobuf.ChatMessage{
|
|
|
|
ChatId: e.ChatID,
|
|
|
|
Text: text,
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_SYSTEM_MESSAGE_PRIVATE_GROUP,
|
2020-01-28 11:16:28 +00:00
|
|
|
ContentType: protobuf.ChatMessage_SYSTEM_MESSAGE_CONTENT_PRIVATE_GROUP,
|
2019-12-02 15:34:05 +00:00
|
|
|
Clock: e.ClockValue,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
},
|
|
|
|
From: e.From,
|
|
|
|
WhisperTimestamp: timestamp,
|
|
|
|
LocalChatID: e.ChatID,
|
|
|
|
Seen: true,
|
|
|
|
ID: types.EncodeHex(crypto.Keccak256(e.Signature)),
|
|
|
|
}
|
2021-05-25 09:40:02 +00:00
|
|
|
// We don't pass an identity here as system messages don't need the mentioned flag
|
|
|
|
_ = message.PrepareContent("")
|
2019-12-02 15:34:05 +00:00
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
func buildSystemMessages(events []v1protocol.MembershipUpdateEvent, translations *systemMessageTranslationsMap) []*common.Message {
|
2020-09-01 13:27:01 +00:00
|
|
|
var messages []*common.Message
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
for _, e := range events {
|
2022-05-20 10:53:28 +00:00
|
|
|
if e.Type == protobuf.MembershipUpdateEvent_MEMBER_JOINED {
|
|
|
|
// explicit join has been removed, ignore this event
|
|
|
|
continue
|
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
|
2022-05-20 10:53:28 +00:00
|
|
|
messages = append(messages, eventToSystemMessage(e, translations))
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return messages
|
|
|
|
}
|