forked from diamondburned/smolboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
253 lines (210 loc) · 7.44 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package client
import (
"fmt"
"net/url"
"strconv"
"github.com/diamondburned/smolboard/smolboard"
)
// Session is the current smolboard HTTP session.
type Session struct {
Client *Client
}
// NewSession creates a new session with the given endpoint.
func NewSession(host string) (*Session, error) {
c, err := NewClient(host)
if err != nil {
return nil, err
}
return NewSessionWithClient(c), nil
}
// NewSessionWithClient creates a new session with a client. Refer to
// NewSession.
func NewSessionWithClient(c *Client) *Session {
return &Session{
Client: c,
}
}
// AllowedTypes returns a list of allowed MIME types.
func (s *Session) AllowedTypes() (ts []string, err error) {
return ts, s.Client.Get("/filetypes", &ts, nil)
}
// Endpoint returns the current endpoint with the appended path. The path is
// optional, but it must start with a slash ("/") otherwise.
func (s *Session) Endpoint(path string) string {
return s.Client.Endpoint() + path
}
// Signin signs a new user in.
func (s *Session) Signin(username, password string) (sm smolboard.Session, err error) {
return sm, s.Client.Post("/signin", &sm, url.Values{
"username": {username},
"password": {password},
})
}
// Signup registers a new user.
func (s *Session) Signup(username, password, token string) (sm smolboard.Session, err error) {
return sm, s.Client.Post("/signup", &sm, url.Values{
"username": {username},
"password": {password},
"token": {token},
})
}
// Signout signs the current user out, which will invalidate their current
// token.
func (s *Session) Signout() error {
return s.Client.Post("/signout", nil, nil)
}
// Me returns the current user.
func (s *Session) Me() (u smolboard.UserPart, err error) {
return u, s.Client.Get("/users/@me", &u, nil)
}
// UserEditParams is the parameters for EditMe. All fields are optional.
type UserEditParams struct {
Password string
}
// EditMe edits the current user. All parameters in the given UserEditParams are
// optional with their respective zero values.
func (s *Session) EditMe(pp UserEditParams) (u smolboard.UserPart, err error) {
return u, s.Client.Request("PATCH", "/users/@me", &u, url.Values{
"password": {pp.Password},
})
}
// DeleteMe deletes the currenet user.
func (s *Session) DeleteMe() error {
return s.Client.Delete("/users/@me", nil, nil)
}
// User gets a user with the given username.
func (s *Session) User(username string) (u smolboard.UserPart, err error) {
return u, s.Client.Get(fmt.Sprintf("/users/%s", url.PathEscape(username)), &u, nil)
}
// SetUserPermission sets the given user's permission.
func (s *Session) SetUserPermission(username string, p smolboard.Permission) error {
return s.Client.Request(
"PATCH",
fmt.Sprintf("/users/%s/permission", url.PathEscape(username)),
nil,
url.Values{"p": {p.StringInt()}},
)
}
// Users gets a paginated list of users. The default value for count is 50. This
// endpoint is only allowed for the owner and admins.
func (s *Session) Users(count, page int) (u smolboard.UserList, err error) {
return s.SearchUsers("", count, page)
}
// SearchUsers searches for a user. The default values from Users applies here
// as well. If q is empty, then all users are listed.
func (s *Session) SearchUsers(q string, count, page int) (u smolboard.UserList, err error) {
if count == 0 {
count = 50
}
return u, s.Client.Get("/users", &u, url.Values{
"q": {q},
"c": {strconv.Itoa(count)},
"p": {strconv.Itoa(page)},
})
}
// DeleteUser deletes a user.
func (s *Session) DeleteUser(username string) error {
return s.Client.Delete("/users/"+url.PathEscape(username), nil, nil)
}
// Post returns a post with the given ID. It returns extra information such as
// tags.
func (s *Session) Post(id int64) (p smolboard.PostExtended, err error) {
return p, s.Client.Get(fmt.Sprintf("/posts/%d", id), &p, nil)
}
// Posts returns the paginated post list. Count is defaulted to 25.
func (s *Session) Posts(count, page int) (p smolboard.SearchResults, err error) {
return s.PostSearch("", count, page)
}
// PostSearch is similar to Posts but with searching.
func (s *Session) PostSearch(q string, count, page int) (p smolboard.SearchResults, err error) {
if count == 0 {
count = 25
}
return p, s.Client.Get("/posts", &p, url.Values{
"q": {q},
"c": {strconv.Itoa(count)},
"p": {strconv.Itoa(page)},
})
}
// PostDirectPath returns the direct path to the post's content.
func (s *Session) PostDirectPath(post smolboard.Post) string {
return fmt.Sprintf("/api/v1/images/%s", url.PathEscape(post.Filename()))
}
// PostThumbPathRL returns the JPEG path to the thumbnail of the given post.
func (s *Session) PostThumbPath(post smolboard.Post) string {
return fmt.Sprintf("/api/v1/images/%s/thumb.jpg", url.PathEscape(post.Filename()))
}
// DeletePost deletes the given post.
func (s *Session) DeletePost(id int64) error {
return s.Client.Delete(fmt.Sprintf("/posts/%d", id), nil, nil)
}
// SetPostPermission sets the given post's permission.
func (s *Session) SetPostPermission(postID int64, p smolboard.Permission) error {
return s.Client.Request(
"PATCH",
fmt.Sprintf("/posts/%d/permission", postID),
nil,
url.Values{"p": {p.StringInt()}},
)
}
// TagPost adds a tag to a post.
func (s *Session) TagPost(postID int64, tag string) error {
if err := smolboard.TagIsValid(tag); err != nil {
return err
}
return s.Client.Post(fmt.Sprintf("/posts/%d/tags", postID), nil, url.Values{
"t": {tag},
})
}
// UntagPost removes a tag from a post.
func (s *Session) UntagPost(postID int64, tag string) error {
if err := smolboard.TagIsValid(tag); err != nil {
return err
}
return s.Client.Delete(fmt.Sprintf("/posts/%d/tags", postID), nil, url.Values{
"t": {tag},
})
}
// Tokens returns a list of tokens along with extra bits returned from the
// server to assist in getting information without extra queries.
func (s *Session) Tokens() (tl smolboard.TokenList, err error) {
return tl, s.Client.Get("/tokens", &tl, nil)
}
// CreateToken creates a token with the given uses count. This function returns
// ErrZeroNotAllowed if uses is 0.
func (s *Session) CreateToken(uses int) (t smolboard.Token, err error) {
if uses == 0 {
return t, smolboard.ErrZeroNotAllowed
}
return t, s.Client.Post("/tokens", &t, url.Values{
"uses": {strconv.Itoa(uses)},
})
}
// DeleteToken deletes the token with the given token string.
func (s *Session) DeleteToken(token string) error {
return s.Client.Delete("/tokens/"+token, nil, nil)
}
// GetSessions returns a list of sessions.
func (s *Session) GetSessions() (ses []smolboard.Session, err error) {
return ses, s.Client.Get("/users/@me/sessions", &ses, nil)
}
// Session returns the session with the given ID.
func (s *Session) Session(id int64) (ses smolboard.Session, err error) {
return ses, s.Client.Get(fmt.Sprintf("/users/@me/sessions/%d", id), &ses, nil)
}
// CurrentSession returns the current session.
func (s *Session) CurrentSession() (ses smolboard.Session, err error) {
return ses, s.Client.Get("/users/@me/sessions/@this", &ses, nil)
}
// DeleteSession deletes the session with the given ID.
func (s *Session) DeleteSession(id int64) error {
return s.Client.Delete(fmt.Sprintf("/users/@me/sessions/%d", id), nil, nil)
}
// DeleteCurrentSession deletes the current session.
func (s *Session) DeleteCurrentSession() error {
return s.Client.Delete("/users/@me/sessions/@this", nil, nil)
}
// DeleteAllSessions deletes all sessions except for the current one.
func (s *Session) DeleteAllSessions() error {
return s.Client.Delete("/users/@me/sessions", nil, nil)
}