-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathfriends.go
87 lines (70 loc) · 1.84 KB
/
friends.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
package vkapi
import (
"encoding/json"
"net/url"
"strconv"
)
type FriendsRequests struct {
Count int `json:"count"`
Requests []*Request `json:"items"`
}
type Friends struct {
Count int `json:"count"`
Users []*User `json:"items"`
}
type Request struct {
UserID int `json:"user_id"`
MutualFriends *Mutual `json:"mutual"`
}
type Mutual struct {
Count int `json:"count"`
Users []int `json:"users"`
}
func (client *VKClient) FriendsGet(uid int, count int) (int, []*User, error) {
params := url.Values{}
params.Set("user_id", strconv.Itoa(uid))
params.Set("count", strconv.Itoa(count))
params.Set("fields", userFields)
resp, err := client.MakeRequest("friends.get", params)
if err != nil {
return 0, nil, err
}
var friends *Friends
json.Unmarshal(resp.Response, &friends)
return friends.Count, friends.Users, nil
}
func (client *VKClient) FriendsGetRequests(count int, out int) (int, []*Request, error) {
params := url.Values{}
params.Set("count", strconv.Itoa(count))
params.Set("out", strconv.Itoa(out))
params.Set("extended", "1")
resp, err := client.MakeRequest("friends.getRequests", params)
if err != nil {
return 0, nil, err
}
var reqs *FriendsRequests
json.Unmarshal(resp.Response, &reqs)
return reqs.Count, reqs.Requests, nil
}
func (client *VKClient) FriendsAdd(userID int, text string, follow int) error {
params := url.Values{}
params.Set("user_id", strconv.Itoa(userID))
params.Set("follow", strconv.Itoa(follow))
if text != "" {
params.Set("text", text)
}
_, err := client.MakeRequest("friends.add", params)
if err != nil {
return err
}
return nil
}
func (client *VKClient) FriendsDelete(userID int) error {
params := url.Values{}
params.Set("user_id", strconv.Itoa(userID))
_, err := client.MakeRequest("friends.delete", params)
if err != nil {
return err
}
return nil
}