/
wall.go
152 lines (129 loc) · 3.89 KB
/
wall.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
package vkapi
import (
"encoding/json"
"errors"
"net/url"
"strconv"
)
type Wall struct {
Count int `json:"count"`
Posts []*WallPost `json:"items"`
Profiles []*User `json:"profiles"`
Groups []*Group `json:"groups"`
}
type WallPost struct {
ID int `json:"id"`
FromID int `json:"from_id"`
OwnerID int `json:"owner_id"`
ToID int `json:"to_id"`
Date int64 `json:"date"`
MarkedAsAd int `json:"marked_as_ads"`
IsPinned int `json:"is_pinned"`
PostType string `json:"post_type"`
CopyPostDate int64 `json:"copy_post_date"`
CopyPostType string `json:"copy_post_type"`
CopyOwnerID int `json:"copy_owner_id"`
CopyPostID int `json:"copy_post_id"`
CopyHistory []*WallPost `json:"copy_history"`
CreatedBy int `json:"created_by"`
Text string `json:"text"`
CanDelete int `json:"can_delete"`
CanPin int `json:"can_pin"`
Attachments []*MessageAttachment `json:"attachments"`
PostSource *Source `json:"post_source"`
Comments *Comment `json:"comments"`
Likes *Like `json:"likes"`
Reposts *Repost `json:"reposts"`
Online int `json:"online"`
ReplyCount int `json:"reply_count"`
SignerID int `json:"signer_id"`
}
type Comment struct {
Count int `json:"count"`
CanPost int `json:"can_post"`
}
type Like struct {
Count int `json:"count"`
UserLikes int `json:"user_likes"`
CanLike int `json:"can_like"`
CanPublish int `json:"can_publish"`
}
type Repost struct {
Count int `json:"count"`
UserReposted int `json:"user_reposted"`
}
type Source struct {
Type string `json:"type"`
}
func (client *VKClient) WallGet(id interface{}, count int, params url.Values) (*Wall, error) {
if params == nil {
params = url.Values{}
}
params.Set("count", strconv.Itoa(count))
switch id.(type) {
case int:
params.Set("owner_id", strconv.Itoa(id.(int)))
case string:
params.Set("domain", id.(string))
default:
return nil, errors.New("unexpected type in id filed")
}
resp, err := client.MakeRequest("wall.get", params)
if err != nil {
return nil, err
}
var wall *Wall
json.Unmarshal(resp.Response, &wall)
return wall, nil
}
func (client *VKClient) WallGetByID(id string, params url.Values) (*Wall, error) {
if params == nil {
params = url.Values{}
}
params.Set("posts", id)
resp, err := client.MakeRequest("wall.getById", params)
if err != nil {
return nil, err
}
wall := &Wall{}
if params.Get(`extended`) == `1` {
json.Unmarshal(resp.Response, &wall)
} else {
json.Unmarshal(resp.Response, &wall.Posts)
}
wall.Count = len(wall.Posts)
return wall, nil
}
func (client *VKClient) WallPost(ownerID int, message string, params url.Values) (int, error) {
if params == nil {
params = url.Values{}
}
params.Set("owner_id", strconv.Itoa(ownerID))
params.Set("message", message)
resp, err := client.MakeRequest("wall.post", params)
if err != nil {
return 0, err
}
m := map[string]int{}
if err = json.Unmarshal(resp.Response, &m); err != nil {
return 0, err
}
return m["post_id"], nil
}
func (client *VKClient) WallPostComment(ownerID int, postID int, message string, params url.Values) (int, error) {
if params == nil {
params = url.Values{}
}
params.Set("owner_id", strconv.Itoa(ownerID))
params.Set("post_id", strconv.Itoa(postID))
params.Set("message", message)
resp, err := client.MakeRequest("wall.createComment", params)
if err != nil {
return 0, err
}
m := map[string]int{}
if err = json.Unmarshal(resp.Response, &m); err != nil {
return 0, err
}
return m["comment_id"], nil
}