2019-11-21 16:19:22 +00:00
|
|
|
package protocol
|
2019-09-26 07:01:17 +00:00
|
|
|
|
2019-10-09 14:22:53 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/ecdsa"
|
2019-10-14 14:10:48 +00:00
|
|
|
"fmt"
|
2019-10-09 14:22:53 +00:00
|
|
|
"sort"
|
2019-10-14 14:10:48 +00:00
|
|
|
"strings"
|
2019-10-09 14:22:53 +00:00
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2019-10-14 14:10:48 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/pkg/errors"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2019-12-02 15:34:05 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2019-10-09 14:22:53 +00:00
|
|
|
)
|
2019-09-26 07:01:17 +00:00
|
|
|
|
|
|
|
// MembershipUpdateMessage is a message used to propagate information
|
|
|
|
// about group membership changes.
|
|
|
|
// For more information, see https://github.com/status-im/specs/blob/master/status-group-chats-spec.md.
|
|
|
|
type MembershipUpdateMessage struct {
|
2020-07-25 21:25:40 +00:00
|
|
|
ChatID string `json:"chatId"` // UUID concatenated with hex-encoded public key of the creator for the chat
|
|
|
|
Events []MembershipUpdateEvent `json:"events"`
|
|
|
|
Message *protobuf.ChatMessage `json:"-"`
|
|
|
|
EmojiReaction *protobuf.EmojiReaction `json:"-"`
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
const signatureLength = 65
|
2019-09-26 07:01:17 +00:00
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func MembershipUpdateEventFromProtobuf(chatID string, raw []byte) (*MembershipUpdateEvent, error) {
|
|
|
|
if len(raw) <= signatureLength {
|
|
|
|
return nil, errors.New("invalid payload length")
|
2019-10-09 14:22:53 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
decodedEvent := protobuf.MembershipUpdateEvent{}
|
|
|
|
signature := raw[:signatureLength]
|
|
|
|
encodedEvent := raw[signatureLength:]
|
2019-10-14 14:10:48 +00:00
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
signatureMaterial := append([]byte(chatID), encodedEvent...)
|
|
|
|
publicKey, err := crypto.ExtractSignature(signatureMaterial, signature)
|
2019-10-14 14:10:48 +00:00
|
|
|
if err != nil {
|
2019-12-02 15:34:05 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to extract signature")
|
2019-10-09 14:22:53 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
|
2021-01-08 11:26:15 +00:00
|
|
|
from := publicKeyToString(publicKey)
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
err = proto.Unmarshal(encodedEvent, &decodedEvent)
|
2019-10-09 14:22:53 +00:00
|
|
|
if err != nil {
|
2019-12-02 15:34:05 +00:00
|
|
|
return nil, err
|
2019-10-09 14:22:53 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
return &MembershipUpdateEvent{
|
|
|
|
ClockValue: decodedEvent.Clock,
|
|
|
|
ChatID: chatID,
|
|
|
|
Members: decodedEvent.Members,
|
|
|
|
Name: decodedEvent.Name,
|
|
|
|
Type: decodedEvent.Type,
|
2022-07-12 07:48:58 +00:00
|
|
|
Color: decodedEvent.Color,
|
2022-08-08 15:22:22 +00:00
|
|
|
Image: decodedEvent.Image,
|
2019-12-02 15:34:05 +00:00
|
|
|
Signature: signature,
|
|
|
|
RawPayload: encodedEvent,
|
|
|
|
From: from,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-07-25 21:25:40 +00:00
|
|
|
func (m *MembershipUpdateMessage) ToProtobuf() (*protobuf.MembershipUpdateMessage, error) {
|
2019-12-02 15:34:05 +00:00
|
|
|
var rawEvents [][]byte
|
|
|
|
for _, e := range m.Events {
|
|
|
|
var encodedEvent []byte
|
|
|
|
encodedEvent = append(encodedEvent, e.Signature...)
|
|
|
|
encodedEvent = append(encodedEvent, e.RawPayload...)
|
|
|
|
rawEvents = append(rawEvents, encodedEvent)
|
|
|
|
}
|
2020-07-25 16:13:08 +00:00
|
|
|
|
2020-07-25 21:25:40 +00:00
|
|
|
mUM := &protobuf.MembershipUpdateMessage{
|
|
|
|
ChatId: m.ChatID,
|
|
|
|
Events: rawEvents,
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
2020-07-25 21:25:40 +00:00
|
|
|
|
2020-07-27 10:13:22 +00:00
|
|
|
// If message is not piggybacking anything, that's a valid case and we just return
|
2020-07-25 21:25:40 +00:00
|
|
|
switch {
|
|
|
|
case m.Message != nil:
|
|
|
|
mUM.ChatEntity = &protobuf.MembershipUpdateMessage_Message{Message: m.Message}
|
|
|
|
case m.EmojiReaction != nil:
|
|
|
|
mUM.ChatEntity = &protobuf.MembershipUpdateMessage_EmojiReaction{EmojiReaction: m.EmojiReaction}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mUM, nil
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func MembershipUpdateMessageFromProtobuf(raw *protobuf.MembershipUpdateMessage) (*MembershipUpdateMessage, error) {
|
|
|
|
var events []MembershipUpdateEvent
|
|
|
|
for _, e := range raw.Events {
|
|
|
|
verifiedEvent, err := MembershipUpdateEventFromProtobuf(raw.ChatId, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
events = append(events, *verifiedEvent)
|
2019-10-09 14:22:53 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
return &MembershipUpdateMessage{
|
2020-07-28 12:23:34 +00:00
|
|
|
ChatID: raw.ChatId,
|
|
|
|
Events: events,
|
|
|
|
Message: raw.GetMessage(),
|
|
|
|
EmojiReaction: raw.GetEmojiReaction(),
|
2019-12-02 15:34:05 +00:00
|
|
|
}, nil
|
2019-10-09 14:22:53 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
// EncodeMembershipUpdateMessage encodes a MembershipUpdateMessage using protobuf serialization.
|
|
|
|
func EncodeMembershipUpdateMessage(value MembershipUpdateMessage) ([]byte, error) {
|
2020-07-25 21:25:40 +00:00
|
|
|
pb, err := value.ToProtobuf()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return proto.Marshal(pb)
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MembershipUpdateEvent contains an event information.
|
|
|
|
// Member and Members are hex-encoded values with 0x prefix.
|
2019-09-26 07:01:17 +00:00
|
|
|
type MembershipUpdateEvent struct {
|
2019-12-02 15:34:05 +00:00
|
|
|
Type protobuf.MembershipUpdateEvent_EventType `json:"type"`
|
|
|
|
ClockValue uint64 `json:"clockValue"`
|
|
|
|
Members []string `json:"members,omitempty"` // in "members-added" and "admins-added" events
|
|
|
|
Name string `json:"name,omitempty"` // name of the group chat
|
2022-07-12 07:48:58 +00:00
|
|
|
Color string `json:"color,omitempty"` // color of the group chat
|
2022-08-08 15:22:22 +00:00
|
|
|
Image []byte `json:"image,omitempty"` // image of the group chat
|
2020-04-06 12:08:53 +00:00
|
|
|
From string `json:"from,omitempty"`
|
|
|
|
Signature []byte `json:"signature,omitempty"`
|
|
|
|
ChatID string `json:"chatId"`
|
|
|
|
RawPayload []byte `json:"rawPayload"`
|
2019-09-26 07:01:17 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (u *MembershipUpdateEvent) Equal(update MembershipUpdateEvent) bool {
|
2020-02-10 11:22:37 +00:00
|
|
|
return bytes.Equal(u.Signature, update.Signature)
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (u *MembershipUpdateEvent) Sign(key *ecdsa.PrivateKey) error {
|
|
|
|
if len(u.ChatID) == 0 {
|
|
|
|
return errors.New("can't sign with empty chatID")
|
|
|
|
}
|
|
|
|
encodedEvent, err := proto.Marshal(u.ToProtobuf())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u.RawPayload = encodedEvent
|
|
|
|
var signatureMaterial []byte
|
|
|
|
signatureMaterial = append(signatureMaterial, []byte(u.ChatID)...)
|
|
|
|
signatureMaterial = crypto.Keccak256(append(signatureMaterial, u.RawPayload...))
|
|
|
|
signature, err := crypto.Sign(signatureMaterial, key)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u.Signature = signature
|
2021-01-08 11:26:15 +00:00
|
|
|
u.From = publicKeyToString(&key.PublicKey)
|
2019-12-02 15:34:05 +00:00
|
|
|
return nil
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (u *MembershipUpdateEvent) ToProtobuf() *protobuf.MembershipUpdateEvent {
|
|
|
|
return &protobuf.MembershipUpdateEvent{
|
|
|
|
Clock: u.ClockValue,
|
|
|
|
Name: u.Name,
|
2022-07-12 07:48:58 +00:00
|
|
|
Color: u.Color,
|
2022-08-08 15:22:22 +00:00
|
|
|
Image: u.Image,
|
2019-12-02 15:34:05 +00:00
|
|
|
Members: u.Members,
|
|
|
|
Type: u.Type,
|
|
|
|
}
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func MergeMembershipUpdateEvents(dest []MembershipUpdateEvent, src []MembershipUpdateEvent) []MembershipUpdateEvent {
|
2019-10-14 14:10:48 +00:00
|
|
|
for _, update := range src {
|
|
|
|
var exists bool
|
|
|
|
for _, existing := range dest {
|
|
|
|
if existing.Equal(update) {
|
|
|
|
exists = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
dest = append(dest, update)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dest
|
|
|
|
}
|
|
|
|
|
2022-07-12 07:48:58 +00:00
|
|
|
func NewChatCreatedEvent(name string, color string, clock uint64) MembershipUpdateEvent {
|
2019-10-09 14:22:53 +00:00
|
|
|
return MembershipUpdateEvent{
|
2019-12-02 15:34:05 +00:00
|
|
|
Type: protobuf.MembershipUpdateEvent_CHAT_CREATED,
|
2019-10-09 14:22:53 +00:00
|
|
|
Name: name,
|
|
|
|
ClockValue: clock,
|
2022-07-12 07:48:58 +00:00
|
|
|
Color: color,
|
2019-10-09 14:22:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func NewNameChangedEvent(name string, clock uint64) MembershipUpdateEvent {
|
2019-10-09 14:22:53 +00:00
|
|
|
return MembershipUpdateEvent{
|
2019-12-02 15:34:05 +00:00
|
|
|
Type: protobuf.MembershipUpdateEvent_NAME_CHANGED,
|
2019-10-09 14:22:53 +00:00
|
|
|
Name: name,
|
|
|
|
ClockValue: clock,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 07:48:58 +00:00
|
|
|
func NewColorChangedEvent(color string, clock uint64) MembershipUpdateEvent {
|
|
|
|
return MembershipUpdateEvent{
|
|
|
|
Type: protobuf.MembershipUpdateEvent_COLOR_CHANGED,
|
|
|
|
Color: color,
|
|
|
|
ClockValue: clock,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-08 15:22:22 +00:00
|
|
|
func NewImageChangedEvent(image []byte, clock uint64) MembershipUpdateEvent {
|
|
|
|
return MembershipUpdateEvent{
|
|
|
|
Type: protobuf.MembershipUpdateEvent_IMAGE_CHANGED,
|
|
|
|
Image: image,
|
|
|
|
ClockValue: clock,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func NewMembersAddedEvent(members []string, clock uint64) MembershipUpdateEvent {
|
2019-10-09 14:22:53 +00:00
|
|
|
return MembershipUpdateEvent{
|
2019-12-02 15:34:05 +00:00
|
|
|
Type: protobuf.MembershipUpdateEvent_MEMBERS_ADDED,
|
2019-10-09 14:22:53 +00:00
|
|
|
Members: members,
|
|
|
|
ClockValue: clock,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func NewMemberJoinedEvent(clock uint64) MembershipUpdateEvent {
|
2019-10-09 14:22:53 +00:00
|
|
|
return MembershipUpdateEvent{
|
2019-12-02 15:34:05 +00:00
|
|
|
Type: protobuf.MembershipUpdateEvent_MEMBER_JOINED,
|
2019-10-09 14:22:53 +00:00
|
|
|
ClockValue: clock,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func NewAdminsAddedEvent(admins []string, clock uint64) MembershipUpdateEvent {
|
2019-10-09 14:22:53 +00:00
|
|
|
return MembershipUpdateEvent{
|
2019-12-02 15:34:05 +00:00
|
|
|
Type: protobuf.MembershipUpdateEvent_ADMINS_ADDED,
|
2019-10-09 14:22:53 +00:00
|
|
|
Members: admins,
|
|
|
|
ClockValue: clock,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func NewMemberRemovedEvent(member string, clock uint64) MembershipUpdateEvent {
|
2019-10-09 14:22:53 +00:00
|
|
|
return MembershipUpdateEvent{
|
2019-12-02 15:34:05 +00:00
|
|
|
Type: protobuf.MembershipUpdateEvent_MEMBER_REMOVED,
|
|
|
|
Members: []string{member},
|
2019-10-09 14:22:53 +00:00
|
|
|
ClockValue: clock,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func NewAdminRemovedEvent(admin string, clock uint64) MembershipUpdateEvent {
|
2019-10-09 14:22:53 +00:00
|
|
|
return MembershipUpdateEvent{
|
2019-12-02 15:34:05 +00:00
|
|
|
Type: protobuf.MembershipUpdateEvent_ADMIN_REMOVED,
|
|
|
|
Members: []string{admin},
|
2019-10-09 14:22:53 +00:00
|
|
|
ClockValue: clock,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Group struct {
|
2019-10-14 14:10:48 +00:00
|
|
|
chatID string
|
|
|
|
name string
|
2022-07-12 07:48:58 +00:00
|
|
|
color string
|
2022-08-08 15:22:22 +00:00
|
|
|
image []byte
|
2019-12-02 15:34:05 +00:00
|
|
|
events []MembershipUpdateEvent
|
2019-10-14 14:10:48 +00:00
|
|
|
admins *stringSet
|
|
|
|
members *stringSet
|
|
|
|
}
|
|
|
|
|
|
|
|
func groupChatID(creator *ecdsa.PublicKey) string {
|
2021-01-08 11:26:15 +00:00
|
|
|
return uuid.New().String() + "-" + publicKeyToString(creator)
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func NewGroupWithEvents(chatID string, events []MembershipUpdateEvent) (*Group, error) {
|
|
|
|
return newGroup(chatID, events)
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 07:48:58 +00:00
|
|
|
func NewGroupWithCreator(name string, color string, clock uint64, creator *ecdsa.PrivateKey) (*Group, error) {
|
2019-10-14 14:10:48 +00:00
|
|
|
chatID := groupChatID(&creator.PublicKey)
|
2022-07-12 07:48:58 +00:00
|
|
|
chatCreated := NewChatCreatedEvent(name, color, clock)
|
2019-12-02 15:34:05 +00:00
|
|
|
chatCreated.ChatID = chatID
|
|
|
|
err := chatCreated.Sign(creator)
|
|
|
|
if err != nil {
|
2019-10-14 14:10:48 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
return newGroup(chatID, []MembershipUpdateEvent{chatCreated})
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func newGroup(chatID string, events []MembershipUpdateEvent) (*Group, error) {
|
2019-10-14 14:10:48 +00:00
|
|
|
g := Group{
|
|
|
|
chatID: chatID,
|
2019-12-02 15:34:05 +00:00
|
|
|
events: events,
|
2019-10-14 14:10:48 +00:00
|
|
|
admins: newStringSet(),
|
|
|
|
members: newStringSet(),
|
|
|
|
}
|
|
|
|
if err := g.init(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &g, nil
|
2019-10-09 14:22:53 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
func (g *Group) init() error {
|
|
|
|
g.sortEvents()
|
|
|
|
|
|
|
|
var chatID string
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
for _, event := range g.events {
|
2019-10-14 14:10:48 +00:00
|
|
|
if chatID == "" {
|
2019-12-02 15:34:05 +00:00
|
|
|
chatID = event.ChatID
|
|
|
|
} else if event.ChatID != chatID {
|
2019-10-14 14:10:48 +00:00
|
|
|
return errors.New("updates contain different chat IDs")
|
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
valid := g.validateEvent(event)
|
2019-10-14 14:10:48 +00:00
|
|
|
if !valid {
|
2019-12-02 15:34:05 +00:00
|
|
|
return fmt.Errorf("invalid event %#+v from %s", event, event.From)
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
g.processEvent(event)
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
valid := g.validateChatID(g.chatID)
|
|
|
|
if !valid {
|
|
|
|
return fmt.Errorf("invalid chat ID: %s", g.chatID)
|
|
|
|
}
|
|
|
|
if chatID != g.chatID {
|
|
|
|
return fmt.Errorf("expected chat ID equal %s, got %s", g.chatID, chatID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g Group) ChatID() string {
|
|
|
|
return g.chatID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g Group) Name() string {
|
|
|
|
return g.name
|
|
|
|
}
|
|
|
|
|
2022-07-12 07:48:58 +00:00
|
|
|
func (g Group) Color() string {
|
|
|
|
return g.color
|
|
|
|
}
|
|
|
|
|
2022-08-08 15:22:22 +00:00
|
|
|
func (g Group) Image() []byte {
|
|
|
|
return g.image
|
|
|
|
}
|
|
|
|
|
2020-03-09 06:19:23 +00:00
|
|
|
func (g Group) Events() []MembershipUpdateEvent {
|
|
|
|
return g.events
|
|
|
|
}
|
|
|
|
|
2021-01-08 11:26:15 +00:00
|
|
|
// AbridgedEvents returns the minimum set of events for a user to publish a post
|
2022-09-13 16:07:34 +00:00
|
|
|
// The events we want to keep:
|
|
|
|
// 1) Chat created
|
|
|
|
// 2) Latest color changed
|
|
|
|
// 3) Latest image changed
|
|
|
|
// 4) For each admin, the latest admins added event that contains them
|
|
|
|
// 5) For each member, the latest members added event that contains them
|
|
|
|
// 4 & 5, might bring removed admins or removed members, for those, we also need to
|
|
|
|
// keep the event that removes them
|
|
|
|
func (g Group) AbridgedEvents() []MembershipUpdateEvent {
|
2021-01-08 11:26:15 +00:00
|
|
|
var events []MembershipUpdateEvent
|
|
|
|
var nameChangedEventFound bool
|
2022-07-12 07:48:58 +00:00
|
|
|
var colorChangedEventFound bool
|
2022-08-08 15:22:22 +00:00
|
|
|
var imageChangedEventFound bool
|
2022-09-13 16:07:34 +00:00
|
|
|
removedMembers := make(map[string]*MembershipUpdateEvent)
|
|
|
|
addedMembers := make(map[string]bool)
|
|
|
|
extraMembers := make(map[string]bool)
|
|
|
|
admins := make(map[string]bool)
|
2021-01-08 11:26:15 +00:00
|
|
|
// Iterate in reverse
|
|
|
|
for i := len(g.events) - 1; i >= 0; i-- {
|
|
|
|
event := g.events[i]
|
|
|
|
switch event.Type {
|
|
|
|
case protobuf.MembershipUpdateEvent_CHAT_CREATED:
|
|
|
|
events = append(events, event)
|
|
|
|
case protobuf.MembershipUpdateEvent_NAME_CHANGED:
|
|
|
|
if nameChangedEventFound {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
events = append(events, event)
|
|
|
|
nameChangedEventFound = true
|
2022-07-12 07:48:58 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_COLOR_CHANGED:
|
|
|
|
if colorChangedEventFound {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
events = append(events, event)
|
|
|
|
colorChangedEventFound = true
|
2022-08-08 15:22:22 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_IMAGE_CHANGED:
|
|
|
|
if imageChangedEventFound {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
events = append(events, event)
|
|
|
|
imageChangedEventFound = true
|
2022-09-13 16:07:34 +00:00
|
|
|
|
2021-01-08 11:26:15 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_MEMBERS_ADDED:
|
2022-09-13 16:07:34 +00:00
|
|
|
var shouldAddEvent bool
|
|
|
|
for _, m := range event.Members {
|
|
|
|
// If it's adding a current user, and we don't have a more
|
|
|
|
// recent event
|
|
|
|
// if it's an admin, we track it
|
|
|
|
if admins[m] || (g.members.Has(m) && !addedMembers[m]) {
|
|
|
|
addedMembers[m] = true
|
|
|
|
shouldAddEvent = true
|
|
|
|
}
|
2021-01-08 11:26:15 +00:00
|
|
|
}
|
2022-09-13 16:07:34 +00:00
|
|
|
if shouldAddEvent {
|
|
|
|
// Append the event and check the not current members that are also
|
|
|
|
// added
|
|
|
|
for _, m := range event.Members {
|
|
|
|
if !g.members.Has(m) && !admins[m] {
|
|
|
|
extraMembers[m] = true
|
|
|
|
}
|
|
|
|
}
|
2021-01-08 11:26:15 +00:00
|
|
|
events = append(events, event)
|
|
|
|
}
|
2022-09-13 16:07:34 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_ADMIN_REMOVED:
|
|
|
|
// We add it always for now
|
2021-01-08 11:26:15 +00:00
|
|
|
events = append(events, event)
|
|
|
|
case protobuf.MembershipUpdateEvent_ADMINS_ADDED:
|
2022-09-13 16:07:34 +00:00
|
|
|
// We track admins in full
|
|
|
|
admins[event.Members[0]] = true
|
|
|
|
events = append(events, event)
|
|
|
|
case protobuf.MembershipUpdateEvent_MEMBER_REMOVED:
|
|
|
|
// Save member removed events, as we might need it
|
|
|
|
// to remove members who have been added but subsequently left
|
|
|
|
if removedMembers[event.Members[0]] == nil || removedMembers[event.Members[0]].ClockValue < event.ClockValue {
|
|
|
|
removedMembers[event.Members[0]] = &event
|
|
|
|
}
|
|
|
|
|
|
|
|
case protobuf.MembershipUpdateEvent_MEMBER_JOINED:
|
|
|
|
if g.members.Has(event.From) {
|
2021-01-08 11:26:15 +00:00
|
|
|
events = append(events, event)
|
|
|
|
}
|
|
|
|
|
2022-09-13 16:07:34 +00:00
|
|
|
}
|
2021-01-08 11:26:15 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 16:07:34 +00:00
|
|
|
for m := range extraMembers {
|
|
|
|
if removedMembers[m] != nil {
|
|
|
|
events = append(events, *removedMembers[m])
|
|
|
|
}
|
2021-01-08 11:26:15 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 16:07:34 +00:00
|
|
|
sort.Slice(events, func(i, j int) bool {
|
|
|
|
return events[i].ClockValue < events[j].ClockValue
|
|
|
|
})
|
|
|
|
|
2021-01-08 11:26:15 +00:00
|
|
|
return events
|
|
|
|
}
|
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
func (g Group) Members() []string {
|
|
|
|
return g.members.List()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g Group) Admins() []string {
|
|
|
|
return g.admins.List()
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (g *Group) ProcessEvents(events []MembershipUpdateEvent) error {
|
2019-10-14 14:10:48 +00:00
|
|
|
for _, event := range events {
|
2019-12-02 15:34:05 +00:00
|
|
|
err := g.ProcessEvent(event)
|
2019-10-14 14:10:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (g *Group) ProcessEvent(event MembershipUpdateEvent) error {
|
|
|
|
if !g.validateEvent(event) {
|
|
|
|
return fmt.Errorf("invalid event %#+v", event)
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
// Check if exists
|
|
|
|
g.events = append(g.events, event)
|
|
|
|
g.processEvent(event)
|
2019-10-14 14:10:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (g Group) LastClockValue() uint64 {
|
|
|
|
if len(g.events) == 0 {
|
2019-10-14 14:10:48 +00:00
|
|
|
return 0
|
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
return g.events[len(g.events)-1].ClockValue
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g Group) creator() (string, error) {
|
2019-12-02 15:34:05 +00:00
|
|
|
if len(g.events) == 0 {
|
2019-10-14 14:10:48 +00:00
|
|
|
return "", errors.New("no events in the group")
|
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
first := g.events[0]
|
|
|
|
if first.Type != protobuf.MembershipUpdateEvent_CHAT_CREATED {
|
2019-10-14 14:10:48 +00:00
|
|
|
return "", fmt.Errorf("expected first event to be 'chat-created', got %s", first.Type)
|
|
|
|
}
|
|
|
|
return first.From, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g Group) validateChatID(chatID string) bool {
|
|
|
|
creator, err := g.creator()
|
|
|
|
if err != nil || creator == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// TODO: It does not verify that the prefix is a valid UUID.
|
|
|
|
// Improve it so that the prefix follows UUIDv4 spec.
|
|
|
|
return strings.HasSuffix(chatID, creator) && chatID != creator
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (g Group) IsMember(id string) bool {
|
|
|
|
return g.members.Has(id)
|
|
|
|
}
|
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
// validateEvent returns true if a given event is valid.
|
2019-12-02 15:34:05 +00:00
|
|
|
func (g Group) validateEvent(event MembershipUpdateEvent) bool {
|
|
|
|
if len(event.From) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2019-10-09 14:22:53 +00:00
|
|
|
switch event.Type {
|
2019-12-02 15:34:05 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_CHAT_CREATED:
|
2019-10-14 14:10:48 +00:00
|
|
|
return g.admins.Empty() && g.members.Empty()
|
2019-12-02 15:34:05 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_NAME_CHANGED:
|
2022-08-29 14:07:32 +00:00
|
|
|
return (g.admins.Has(event.From) || g.members.Has(event.From)) && len(event.Name) > 0
|
2022-07-12 07:48:58 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_COLOR_CHANGED:
|
2022-08-29 14:07:32 +00:00
|
|
|
return (g.admins.Has(event.From) || g.members.Has(event.From)) && len(event.Color) > 0
|
2022-08-08 15:22:22 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_IMAGE_CHANGED:
|
2022-08-29 14:07:32 +00:00
|
|
|
return (g.admins.Has(event.From) || g.members.Has(event.From)) && len(event.Image) > 0
|
2019-12-02 15:34:05 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_MEMBERS_ADDED:
|
2022-08-26 09:25:54 +00:00
|
|
|
return g.admins.Has(event.From) || g.members.Has(event.From)
|
2019-12-02 15:34:05 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_MEMBER_JOINED:
|
|
|
|
return g.members.Has(event.From)
|
|
|
|
case protobuf.MembershipUpdateEvent_MEMBER_REMOVED:
|
2019-10-09 14:22:53 +00:00
|
|
|
// Member can remove themselves or admin can remove a member.
|
2019-12-02 15:34:05 +00:00
|
|
|
return len(event.Members) == 1 && (event.From == event.Members[0] || (g.admins.Has(event.From) && !g.admins.Has(event.Members[0])))
|
|
|
|
case protobuf.MembershipUpdateEvent_ADMINS_ADDED:
|
|
|
|
return g.admins.Has(event.From) && stringSliceSubset(event.Members, g.members.List())
|
|
|
|
case protobuf.MembershipUpdateEvent_ADMIN_REMOVED:
|
|
|
|
return len(event.Members) == 1 && g.admins.Has(event.From) && event.From == event.Members[0]
|
2019-10-09 14:22:53 +00:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (g *Group) processEvent(event MembershipUpdateEvent) {
|
2019-10-14 14:10:48 +00:00
|
|
|
switch event.Type {
|
2019-12-02 15:34:05 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_CHAT_CREATED:
|
2019-10-14 14:10:48 +00:00
|
|
|
g.name = event.Name
|
2022-07-12 07:48:58 +00:00
|
|
|
g.color = event.Color
|
2019-12-02 15:34:05 +00:00
|
|
|
g.members.Add(event.From)
|
|
|
|
g.admins.Add(event.From)
|
|
|
|
case protobuf.MembershipUpdateEvent_NAME_CHANGED:
|
2019-10-14 14:10:48 +00:00
|
|
|
g.name = event.Name
|
2022-07-12 07:48:58 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_COLOR_CHANGED:
|
|
|
|
g.color = event.Color
|
2022-08-08 15:22:22 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_IMAGE_CHANGED:
|
|
|
|
g.image = event.Image
|
2019-12-02 15:34:05 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_ADMINS_ADDED:
|
2019-10-14 14:10:48 +00:00
|
|
|
g.admins.Add(event.Members...)
|
2019-12-02 15:34:05 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_ADMIN_REMOVED:
|
|
|
|
g.admins.Remove(event.Members[0])
|
|
|
|
case protobuf.MembershipUpdateEvent_MEMBERS_ADDED:
|
2019-10-14 14:10:48 +00:00
|
|
|
g.members.Add(event.Members...)
|
2019-12-02 15:34:05 +00:00
|
|
|
case protobuf.MembershipUpdateEvent_MEMBER_REMOVED:
|
|
|
|
g.admins.Remove(event.Members[0])
|
|
|
|
g.members.Remove(event.Members[0])
|
2019-10-09 14:22:53 +00:00
|
|
|
}
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Group) sortEvents() {
|
2019-12-02 15:34:05 +00:00
|
|
|
sort.Slice(g.events, func(i, j int) bool {
|
|
|
|
return g.events[i].ClockValue < g.events[j].ClockValue
|
2019-10-14 14:10:48 +00:00
|
|
|
})
|
2019-10-09 14:22:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func stringSliceSubset(subset []string, set []string) bool {
|
|
|
|
for _, item1 := range set {
|
|
|
|
var found bool
|
|
|
|
for _, item2 := range subset {
|
|
|
|
if item1 == item2 {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2019-10-14 14:10:48 +00:00
|
|
|
|
|
|
|
func publicKeyToString(publicKey *ecdsa.PublicKey) string {
|
2019-11-23 17:57:05 +00:00
|
|
|
return types.EncodeHex(crypto.FromECDSAPub(publicKey))
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type stringSet struct {
|
|
|
|
m map[string]struct{}
|
|
|
|
items []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStringSet() *stringSet {
|
|
|
|
return &stringSet{
|
|
|
|
m: make(map[string]struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStringSetFromSlice(s []string) *stringSet {
|
|
|
|
set := newStringSet()
|
|
|
|
if len(s) > 0 {
|
|
|
|
set.Add(s...)
|
|
|
|
}
|
|
|
|
return set
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stringSet) Add(items ...string) {
|
|
|
|
for _, item := range items {
|
|
|
|
if _, ok := s.m[item]; !ok {
|
|
|
|
s.m[item] = struct{}{}
|
|
|
|
s.items = append(s.items, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stringSet) Remove(items ...string) {
|
|
|
|
for _, item := range items {
|
|
|
|
if _, ok := s.m[item]; ok {
|
|
|
|
delete(s.m, item)
|
|
|
|
s.removeFromItems(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stringSet) Has(item string) bool {
|
|
|
|
_, ok := s.m[item]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stringSet) Empty() bool {
|
|
|
|
return len(s.items) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stringSet) List() []string {
|
|
|
|
return s.items
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stringSet) removeFromItems(dropped string) {
|
|
|
|
n := 0
|
|
|
|
for _, item := range s.items {
|
|
|
|
if item != dropped {
|
|
|
|
s.items[n] = item
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.items = s.items[:n]
|
|
|
|
}
|