2019-11-21 16:19:22 +00:00
|
|
|
package protocol
|
2019-07-17 22:25:42 +00:00
|
|
|
|
2019-08-29 06:33:46 +00:00
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2019-10-14 14:10:48 +00:00
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/hex"
|
2019-08-29 06:33:46 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2019-11-21 16:19:22 +00:00
|
|
|
protocol "github.com/status-im/status-go/protocol/types"
|
|
|
|
v1protocol "github.com/status-im/status-go/protocol/v1"
|
2019-08-29 06:33:46 +00:00
|
|
|
)
|
2019-07-17 22:25:42 +00:00
|
|
|
|
2019-07-30 18:39:16 +00:00
|
|
|
type ChatType int
|
|
|
|
|
|
|
|
const (
|
2019-10-09 14:22:53 +00:00
|
|
|
ChatTypeOneToOne ChatType = iota + 1
|
2019-07-30 18:39:16 +00:00
|
|
|
ChatTypePublic
|
|
|
|
ChatTypePrivateGroupChat
|
|
|
|
)
|
|
|
|
|
|
|
|
type Chat struct {
|
|
|
|
// ID is the id of the chat, for public chats it is the name e.g. status, for one-to-one
|
|
|
|
// is the hex encoded public key and for group chats is a random uuid appended with
|
|
|
|
// the hex encoded pk of the creator of the chat
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Color string `json:"color"`
|
|
|
|
// Active indicates whether the chat has been soft deleted
|
|
|
|
Active bool `json:"active"`
|
|
|
|
|
|
|
|
ChatType ChatType `json:"chatType"`
|
|
|
|
|
|
|
|
// Only filled for one to one chats
|
|
|
|
PublicKey *ecdsa.PublicKey `json:"-"`
|
|
|
|
|
|
|
|
// Timestamp indicates the last time this chat has received/sent a message
|
|
|
|
Timestamp int64 `json:"timestamp"`
|
|
|
|
// LastClockValue indicates the last clock value to be used when sending messages
|
|
|
|
LastClockValue uint64 `json:"lastClockValue"`
|
|
|
|
// DeletedAtClockValue indicates the clock value at time of deletion, messages
|
|
|
|
// with lower clock value of this should be discarded
|
|
|
|
DeletedAtClockValue uint64 `json:"deletedAtClockValue"`
|
|
|
|
|
|
|
|
// Denormalized fields
|
|
|
|
UnviewedMessagesCount uint `json:"unviewedMessagesCount"`
|
|
|
|
LastMessageContentType string `json:"lastMessageContentType"`
|
|
|
|
LastMessageContent string `json:"lastMessageContent"`
|
2019-09-02 09:29:06 +00:00
|
|
|
LastMessageTimestamp int64 `json:"lastMessageTimestamp"`
|
2019-11-04 10:08:22 +00:00
|
|
|
LastMessageClockValue int64 `json:"lastMessageClockValue"`
|
2019-07-30 18:39:16 +00:00
|
|
|
|
|
|
|
// Group chat fields
|
|
|
|
// Members are the members who have been invited to the group chat
|
|
|
|
Members []ChatMember `json:"members"`
|
|
|
|
// MembershipUpdates is all the membership events in the chat
|
|
|
|
MembershipUpdates []ChatMembershipUpdate `json:"membershipUpdates"`
|
|
|
|
}
|
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
func (c *Chat) MembersAsPublicKeys() ([]*ecdsa.PublicKey, error) {
|
|
|
|
publicKeys := make([]string, len(c.Members))
|
|
|
|
for idx, item := range c.Members {
|
|
|
|
publicKeys[idx] = item.ID
|
|
|
|
}
|
|
|
|
return stringSliceToPublicKeys(publicKeys, true)
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:19:22 +00:00
|
|
|
func (c *Chat) updateChatFromProtocolGroup(g *v1protocol.Group) {
|
2019-10-14 14:10:48 +00:00
|
|
|
// ID
|
|
|
|
c.ID = g.ChatID()
|
|
|
|
|
|
|
|
// Name
|
|
|
|
c.Name = g.Name()
|
|
|
|
|
|
|
|
// Members
|
|
|
|
members := g.Members()
|
|
|
|
admins := g.Admins()
|
|
|
|
joined := g.Joined()
|
|
|
|
chatMembers := make([]ChatMember, 0, len(members))
|
|
|
|
for _, m := range members {
|
|
|
|
chatMember := ChatMember{
|
|
|
|
ID: m,
|
|
|
|
}
|
|
|
|
chatMember.Admin = stringSliceContains(admins, m)
|
|
|
|
chatMember.Joined = stringSliceContains(joined, m)
|
|
|
|
chatMembers = append(chatMembers, chatMember)
|
|
|
|
}
|
|
|
|
c.Members = chatMembers
|
|
|
|
|
|
|
|
// MembershipUpdates
|
|
|
|
updates := g.Updates()
|
|
|
|
membershipUpdates := make([]ChatMembershipUpdate, 0, len(updates))
|
|
|
|
for _, update := range updates {
|
|
|
|
membershipUpdate := ChatMembershipUpdate{
|
|
|
|
Type: update.Type,
|
|
|
|
Name: update.Name,
|
|
|
|
ClockValue: uint64(update.ClockValue), // TODO: get rid of type casting
|
|
|
|
Signature: update.Signature,
|
|
|
|
From: update.From,
|
|
|
|
Member: update.Member,
|
|
|
|
Members: update.Members,
|
|
|
|
}
|
|
|
|
membershipUpdate.setID()
|
|
|
|
membershipUpdates = append(membershipUpdates, membershipUpdate)
|
|
|
|
}
|
|
|
|
c.MembershipUpdates = membershipUpdates
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:39:16 +00:00
|
|
|
// ChatMembershipUpdate represent an event on membership of the chat
|
|
|
|
type ChatMembershipUpdate struct {
|
|
|
|
// Unique identifier for the event
|
|
|
|
ID string `json:"id"`
|
|
|
|
// Type indicates the kind of event (i.e changed-name, added-member, etc)
|
|
|
|
Type string `json:"type"`
|
|
|
|
// Name represents the name in the event of changing name events
|
2019-08-20 11:20:25 +00:00
|
|
|
Name string `json:"name,omitempty"`
|
2019-07-30 18:39:16 +00:00
|
|
|
// Clock value of the event
|
|
|
|
ClockValue uint64 `json:"clockValue"`
|
|
|
|
// Signature of the event
|
|
|
|
Signature string `json:"signature"`
|
|
|
|
// Hex encoded public key of the creator of the event
|
|
|
|
From string `json:"from"`
|
|
|
|
// Target of the event for single-target events
|
2019-08-20 11:20:25 +00:00
|
|
|
Member string `json:"member,omitempty"`
|
2019-07-30 18:39:16 +00:00
|
|
|
// Target of the event for multi-target events
|
2019-08-20 11:20:25 +00:00
|
|
|
Members []string `json:"members,omitempty"`
|
2019-07-30 18:39:16 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
func (u *ChatMembershipUpdate) setID() {
|
|
|
|
sum := sha1.Sum([]byte(u.Signature))
|
|
|
|
u.ID = hex.EncodeToString(sum[:])
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:39:16 +00:00
|
|
|
// ChatMember represents a member who participates in a group chat
|
|
|
|
type ChatMember struct {
|
|
|
|
// ID is the hex encoded public key of the member
|
|
|
|
ID string `json:"id"`
|
|
|
|
// Admin indicates if the member is an admin of the group chat
|
|
|
|
Admin bool `json:"admin"`
|
|
|
|
// Joined indicates if the member has joined the group chat
|
|
|
|
Joined bool `json:"joined"`
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
2019-08-29 06:33:46 +00:00
|
|
|
|
|
|
|
func (c ChatMember) PublicKey() (*ecdsa.PublicKey, error) {
|
2019-11-21 16:19:22 +00:00
|
|
|
b, err := protocol.DecodeHex(c.ID)
|
2019-08-29 06:33:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return crypto.UnmarshalPubkey(b)
|
|
|
|
}
|
2019-09-26 07:01:17 +00:00
|
|
|
|
|
|
|
func oneToOneChatID(publicKey *ecdsa.PublicKey) string {
|
2019-11-21 16:19:22 +00:00
|
|
|
return protocol.EncodeHex(crypto.FromECDSAPub(publicKey))
|
2019-09-26 07:01:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateOneToOneChat(name string, publicKey *ecdsa.PublicKey) Chat {
|
|
|
|
return Chat{
|
|
|
|
ID: oneToOneChatID(publicKey),
|
|
|
|
Name: name,
|
|
|
|
Active: true,
|
|
|
|
ChatType: ChatTypeOneToOne,
|
|
|
|
PublicKey: publicKey,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreatePublicChat(name string) Chat {
|
|
|
|
return Chat{
|
|
|
|
ID: name,
|
|
|
|
Name: name,
|
|
|
|
Active: true,
|
|
|
|
ChatType: ChatTypePublic,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
func createGroupChat() Chat {
|
2019-10-09 14:22:53 +00:00
|
|
|
return Chat{
|
|
|
|
Active: true,
|
|
|
|
ChatType: ChatTypePrivateGroupChat,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 07:01:17 +00:00
|
|
|
func findChatByID(chatID string, chats []*Chat) *Chat {
|
|
|
|
for _, c := range chats {
|
|
|
|
if c.ID == chatID {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-10-14 14:10:48 +00:00
|
|
|
|
|
|
|
func stringSliceToPublicKeys(slice []string, prefixed bool) ([]*ecdsa.PublicKey, error) {
|
|
|
|
result := make([]*ecdsa.PublicKey, len(slice))
|
|
|
|
for idx, item := range slice {
|
|
|
|
var (
|
|
|
|
b []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if prefixed {
|
2019-11-21 16:19:22 +00:00
|
|
|
b, err = protocol.DecodeHex(item)
|
2019-10-14 14:10:48 +00:00
|
|
|
} else {
|
|
|
|
b, err = hex.DecodeString(item)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result[idx], err = crypto.UnmarshalPubkey(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringSliceContains(slice []string, item string) bool {
|
|
|
|
for _, s := range slice {
|
|
|
|
if s == item {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|