-
Notifications
You must be signed in to change notification settings - Fork 13
/
user.go
171 lines (145 loc) · 5.06 KB
/
user.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package mixin
import (
"context"
"crypto"
"crypto/ed25519"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"time"
)
type User struct {
UserID string `json:"user_id,omitempty"`
IdentityNumber string `json:"identity_number,omitempty"`
Phone string `json:"phone,omitempty"`
FullName string `json:"full_name,omitempty"`
Biography string `json:"biography,omitempty"`
AvatarURL string `json:"avatar_url,omitempty"`
Relationship string `json:"relationship,omitempty"`
MuteUntil time.Time `json:"mute_until,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
IsVerified bool `json:"is_verified,omitempty"`
IsScam bool `json:"is_scam,omitempty"`
IsDeactivated bool `json:"is_deactivated,omitempty"`
CodeID string `json:"code_id,omitempty"`
CodeURL string `json:"code_url,omitempty"`
SessionID string `json:"session_id,omitempty"`
DeviceStatus string `json:"device_status,omitempty"`
HasSafe bool `json:"has_safe,omitempty"`
HasPin bool `json:"has_pin,omitempty"`
HasEmergencyContact bool `json:"has_emergency_contact,omitempty"`
ReceiveMessageSource string `json:"receive_message_source,omitempty"`
AcceptConversationSource string `json:"accept_conversation_source,omitempty"`
AcceptSearchSource string `json:"accept_search_source,omitempty"`
FiatCurrency string `json:"fiat_currency,omitempty"`
PinToken string `json:"pin_token,omitempty"`
PinTokenBase64 string `json:"pin_token_base64,omitempty"`
SaltBase64 string `json:"salt_base64,omitempty"`
TipKeyBase64 string `json:"tip_key_base64,omitempty"`
TipCounter int `json:"tip_counter,omitempty"`
App *App `json:"app,omitempty"`
}
type UserUpdate struct {
FullName string `json:"full_name,omitempty"`
AvatarBase64 string `json:"avatar_base64,omitempty"`
Biography string `json:"biography,omitempty"`
}
func (c *Client) UserMe(ctx context.Context) (*User, error) {
var user User
if err := c.Get(ctx, "/me", nil, &user); err != nil {
return nil, err
}
return &user, nil
}
func UserMe(ctx context.Context, accessToken string) (*User, error) {
return NewFromAccessToken(accessToken).UserMe(ctx)
}
func (c *Client) ReadUser(ctx context.Context, userIdOrIdentityNumber string) (*User, error) {
uri := fmt.Sprintf("/users/%s", userIdOrIdentityNumber)
var user User
if err := c.Get(ctx, uri, nil, &user); err != nil {
return nil, err
}
return &user, nil
}
func (c *Client) ReadUsers(ctx context.Context, ids ...string) ([]*User, error) {
if len(ids) == 0 {
return nil, nil
}
var users []*User
if err := c.Post(ctx, "/users/fetch", ids, &users); err != nil {
return nil, err
}
return users, nil
}
func (c *Client) ReadFriends(ctx context.Context) ([]*User, error) {
var users []*User
if err := c.Get(ctx, "/friends", nil, &users); err != nil {
return nil, err
}
return users, nil
}
func (c *Client) SearchUser(ctx context.Context, identityNumberOrPhoneNumber string) (*User, error) {
uri := fmt.Sprintf("/search/%s", identityNumberOrPhoneNumber)
var user User
if err := c.Get(ctx, uri, nil, &user); err != nil {
return nil, err
}
return &user, nil
}
func (c *Client) CreateUser(ctx context.Context, key crypto.Signer, fullname string) (*User, *Keystore, error) {
publicKey := key.Public()
var pub []byte
switch k := publicKey.(type) {
case ed25519.PublicKey:
pub = k
case *rsa.PublicKey:
var err error
pub, err = x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return nil, nil, err
}
default:
return nil, nil, fmt.Errorf("unexpected public key type: %T", key)
}
paras := map[string]interface{}{
"session_secret": base64.StdEncoding.EncodeToString(pub),
"full_name": fullname,
}
var user User
if err := c.Post(ctx, "/users", paras, &user); err != nil {
return nil, nil, err
}
return &user, newKeystoreFromUser(&user, key), nil
}
func newKeystoreFromUser(user *User, privateKey crypto.PrivateKey) *Keystore {
var pk string
switch k := privateKey.(type) {
case ed25519.PrivateKey:
pk = ed25519Encoding.EncodeToString(k)
case *rsa.PrivateKey:
s := pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(k),
})
pk = string(s)
}
return &Keystore{
ClientID: user.UserID,
SessionID: user.SessionID,
PinToken: user.PinToken,
PrivateKey: pk,
}
}
func (c *Client) ModifyProfile(ctx context.Context, fullname, avatarBase64 string) (*User, error) {
return c.UpdateProfile(ctx, UserUpdate{FullName: fullname, AvatarBase64: avatarBase64})
}
func (c *Client) UpdateProfile(ctx context.Context, input UserUpdate) (*User, error) {
var user User
if err := c.Post(ctx, "/me", input, &user); err != nil {
return nil, err
}
return &user, nil
}