-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_friends.go
145 lines (133 loc) · 4.3 KB
/
user_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
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
package deviantart
import (
"fmt"
"github.com/dghubble/sling"
)
type friendsService struct {
sling *sling.Sling
}
func newFriendsService(sling *sling.Sling) *friendsService {
return &friendsService{
sling: sling.Path("friends/"),
}
}
type Friend struct {
User *User `json:"user"`
IsWatching bool `json:"is_watching"`
WatchesYou bool `json:"watches_you"`
LastVisit string `json:"lastvisit,omitempty"` // TODO: Parse time.
Watch UserWatch `json:"watch"`
}
type UserWatch struct {
Friend bool `json:"friend" url:"friend"`
Deviations bool `json:"deviations" url:"deviations"`
Journals bool `json:"journals" url:"journals"`
ForumThreads bool `json:"forum_threads" url:"forum_threads"`
Critiques bool `json:"critiques" url:"critiques"`
Scraps bool `json:"scraps" url:"scraps"`
Activity bool `json:"activity" url:"activity"`
Collections bool `json:"collections" url:"collections"`
}
// Get gets the users list of friends.
//
// To connect to this endpoint OAuth2 Access Token from the Client Credentials
// Grant, or Authorization Code Grant is required.
//
// The following scopes are required to access this resource:
//
// - browse
func (s *friendsService) Get(username string, page *OffsetParams) (OffsetResponse[Friend], error) {
var (
success OffsetResponse[Friend]
failure Error
)
_, err := s.sling.New().Get(username).QueryStruct(page).Receive(&success, &failure)
if err := relevantError(err, failure); err != nil {
return OffsetResponse[Friend]{}, fmt.Errorf("unable to get friends: %w", err)
}
return success, nil
}
type FriendsSearchParams struct {
Username string `url:"username,omitempty"`
Search string `url:"search,omitempty"`
Query string `url:"query,omitempty"`
}
// Search searches friends by username.
//
// To connect to this endpoint OAuth2 Access Token from the Client Credentials
// Grant, or Authorization Code Grant is required.
//
// The following scopes are required to access this resource:
//
// - browse
func (s *friendsService) Search(params *FriendsSearchParams) ([]User, error) {
var (
success singleResponse[User]
failure Error
)
_, err := s.sling.New().Get("search").QueryStruct(params).Receive(&success, &failure)
if err := relevantError(err, failure); err != nil {
return nil, fmt.Errorf("unable to search friends: %w", err)
}
return success.Results, nil
}
// Watch watches a user.
//
// Requires Authorization Code grant with the following scopes access this resource:
//
// - browse
// - user.manage
//
// TODO: Make better comments for access types and scopes (make it in a single paragraph/sentence).
func (s *friendsService) Watch(username string, params *UserWatch) (bool, error) {
type watch struct {
Watch UserWatch `url:"watch"`
}
var (
success map[string]any
failure Error
)
_, err := s.sling.New().Post("watch/").Path(username).BodyForm(&watch{Watch: *params}).Receive(&success, &failure)
if err := relevantError(err, failure); err != nil {
return false, fmt.Errorf("unable to watch a user: %w", err)
}
return success["success"].(bool), nil
}
// Unwatch unwatches a user.
//
// Requires Authorization Code grant with the following scopes access this resource:
//
// - browse
// - user.manage
//
// TODO: Make better comments for access types and scopes (make it in a single paragraph/sentence).
func (s *friendsService) Unwatch(username string) (bool, error) {
var (
success map[string]any
failure Error
)
_, err := s.sling.New().Get("unwatch/").Path(username).Receive(&success, &failure)
if err := relevantError(err, failure); err != nil {
return false, fmt.Errorf("unable to unwatch a user: %w", err)
}
return success["success"].(bool), nil
}
// Watching checks if user is being watched by the given user.
//
// Requires Authorization Code grant with the following scopes access this resource:
//
// - browse
// - user
//
// TODO: Make better comments for access types and scopes (make it in a single paragraph/sentence).
func (s *friendsService) Watching(username string) (bool, error) {
var (
success map[string]any
failure Error
)
_, err := s.sling.New().Get("watching/").Path(username).Receive(&success, &failure)
if err := relevantError(err, failure); err != nil {
return false, fmt.Errorf("unable to check watching for a user: %w", err)
}
return success["watching"].(bool), nil
}