-
Notifications
You must be signed in to change notification settings - Fork 13
/
session.go
61 lines (50 loc) · 1.14 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package mixin
import (
"context"
"crypto/md5"
"encoding/hex"
"io"
"sort"
)
const (
SessionPlatformIOS = "iOS"
SessionPlatformAndroid = "Android"
SessionPlatformDesktop = "Desktop"
)
type Session struct {
UserID string `json:"user_id,omitempty"`
SessionID string `json:"session_id,omitempty"`
PublicKey string `json:"public_key,omitempty"`
Platform string `json:"platform,omitempty"`
}
func IsEncryptedMessageSupported(sessions []*Session) bool {
for _, session := range sessions {
if session.PublicKey == "" {
return false
}
}
return true
}
func GenerateSessionChecksum(sessions []*Session) string {
ids := make([]string, len(sessions))
for i, session := range sessions {
ids[i] = session.SessionID
}
if len(ids) == 0 {
return ""
}
sort.Strings(ids)
h := md5.New()
for _, id := range ids {
_, _ = io.WriteString(h, id)
}
sum := h.Sum(nil)
return hex.EncodeToString(sum[:])
}
func (c *Client) FetchSessions(ctx context.Context, ids []string) ([]*Session, error) {
var sessions []*Session
if err := c.Post(ctx, "/sessions/fetch", ids, &sessions); err != nil {
return nil, err
}
return sessions, nil
}