This repository has been archived by the owner on Sep 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
comment.go
286 lines (241 loc) Β· 8.24 KB
/
comment.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package crunchyroll
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
// Comment contains all information about a comment.
type Comment struct {
crunchy *Crunchyroll
EpisodeID string `json:"episode_id"`
CommentID string `json:"comment_id"`
DomainID string `json:"domain_id"`
GuestbookKey string `json:"guestbook_key"`
User struct {
UserKey string `json:"user_key"`
UserAttributes struct {
Username string `json:"username"`
Avatar struct {
Locked []Image `json:"locked"`
Unlocked []Image `json:"unlocked"`
} `json:"avatar"`
} `json:"user_attributes"`
UserFlags []any `json:"user_flags"`
} `json:"user"`
Message string `json:"message"`
ParentCommentID int `json:"parent_comment_id"`
Locale LOCALE `json:"locale"`
UserVotes []string `json:"user_votes"`
Flags []string `json:"flags"`
Votes struct {
Inappropriate int `json:"inappropriate"`
Like int `json:"like"`
Spoiler int `json:"spoiler"`
} `json:"votes"`
DeleteReason any `json:"delete_reason"`
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
IsOwner bool `json:"is_owner"`
RepliesCount int `json:"replies_count"`
}
// Delete deleted the current comment. Works only if the user has written the comment.
func (c *Comment) Delete() error {
if !c.IsOwner {
return fmt.Errorf("cannot delete, user is not the comment author")
}
endpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/talkbox/guestbooks/%s/comments/%s/flags?locale=%s", c.EpisodeID, c.CommentID, c.crunchy.Locale)
resp, err := c.crunchy.request(endpoint, http.MethodDelete)
if err != nil {
return err
}
defer resp.Body.Close()
// the api returns a new comment object when modifying it.
// hopefully this does not change
json.NewDecoder(resp.Body).Decode(c)
return nil
}
// IsSpoiler returns if the comment is marked as spoiler or not.
func (c *Comment) IsSpoiler() bool {
for _, flag := range c.Flags {
if flag == "spoiler" {
return true
}
}
return false
}
// MarkAsSpoiler marks the current comment as spoiler. Works only if the user has written the comment,
// and it isn't already marked as spoiler.
func (c *Comment) MarkAsSpoiler() error {
if !c.IsOwner {
return fmt.Errorf("cannot mark as spoiler, user is not the comment author")
} else if c.votedAs("spoiler") {
return fmt.Errorf("comment is already marked as spoiler")
}
endpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/talkbox/guestbooks/%s/comments/%s/flags?locale=%s", c.EpisodeID, c.CommentID, c.crunchy.Locale)
body, _ := json.Marshal(map[string][]string{"add": {"spoiler"}})
req, err := http.NewRequest(http.MethodPatch, endpoint, bytes.NewBuffer(body))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.crunchy.requestFull(req)
if err != nil {
return err
}
defer resp.Body.Close()
json.NewDecoder(resp.Body).Decode(c)
return nil
}
// UnmarkAsSpoiler unmarks the current comment as spoiler. Works only if the user has written the comment,
// and it is already marked as spoiler.
func (c *Comment) UnmarkAsSpoiler() error {
if !c.IsOwner {
return fmt.Errorf("cannot mark as spoiler, user is not the comment author")
} else if !c.votedAs("spoiler") {
return fmt.Errorf("comment is not marked as spoiler")
}
endpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/talkbox/guestbooks/%s/comments/%s/flags?locale=%s", c.EpisodeID, c.CommentID, c.crunchy.Locale)
body, _ := json.Marshal(map[string][]string{"remove": {"spoiler"}})
req, err := http.NewRequest(http.MethodPatch, endpoint, bytes.NewBuffer(body))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.crunchy.requestFull(req)
if err != nil {
return err
}
defer resp.Body.Close()
json.NewDecoder(resp.Body).Decode(c)
return nil
}
// Like likes the comment. Works only if the user hasn't already liked it.
func (c *Comment) Like() error {
if err := c.vote("like", "liked"); err != nil {
return err
}
c.Votes.Like += 1
return nil
}
// Liked returns if the user has liked the comment.
func (c *Comment) Liked() bool {
return c.votedAs("liked")
}
// RemoveLike removes the like from the comment. Works only if the user has liked it.
func (c *Comment) RemoveLike() error {
if err := c.unVote("like", "liked"); err != nil {
return err
}
c.Votes.Like -= 1
return nil
}
// Reply replies to the current comment.
func (c *Comment) Reply(message string, spoiler bool) (*Comment, error) {
endpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/talkbox/guestbooks/%s/comments?locale=%s", c.EpisodeID, c.crunchy.Locale)
var flags []string
if spoiler {
flags = append(flags, "spoiler")
}
body, _ := json.Marshal(map[string]any{"locale": string(c.crunchy.Locale), "message": message, "flags": flags, "parent_id": c.CommentID})
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.crunchy.requestFull(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
reply := &Comment{}
if err = json.NewDecoder(resp.Body).Decode(reply); err != nil {
return nil, err
}
return reply, nil
}
// Replies shows all replies to the current comment.
func (c *Comment) Replies(page uint, size uint) ([]*Comment, error) {
if c.RepliesCount == 0 {
return []*Comment{}, nil
}
endpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/talkbox/guestbooks/%s/comments/%s/replies?page_size=%d&page=%d&locale=%s", c.EpisodeID, c.CommentID, size, page, c.Locale)
resp, err := c.crunchy.request(endpoint, http.MethodGet)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var jsonBody map[string]any
json.NewDecoder(resp.Body).Decode(&jsonBody)
var comments []*Comment
if err = decodeMapToStruct(jsonBody["items"].([]any), &comments); err != nil {
return nil, err
}
return comments, nil
}
// Report reports the comment. Only works if the comment hasn't been reported yet.
func (c *Comment) Report() error {
return c.vote("inappropriate", "reported")
}
func (c *Comment) IsReported() bool {
return c.votedAs("reported")
}
// RemoveReport removes the report request from the comment. Only works if the user
// has reported the comment.
func (c *Comment) RemoveReport() error {
return c.unVote("inappropriate", "reported")
}
// FlagAsSpoiler sends a request to the user (and / or crunchyroll?) to mark the comment
// as spoiler. Only works if the comment hasn't been flagged as spoiler yet.
func (c *Comment) FlagAsSpoiler() error {
return c.vote("spoiler", "spoiler")
}
func (c *Comment) IsFlaggedAsSpoiler() bool {
return c.votedAs("spoiler")
}
// UnflagAsSpoiler rewokes the request to the user (and / or crunchyroll?) to mark the
// comment as spoiler. Only works if the user has flagged the comment as spoiler.
func (c *Comment) UnflagAsSpoiler() error {
return c.unVote("spoiler", "spoiler")
}
func (c *Comment) votedAs(voteType string) bool {
for _, userVote := range c.UserVotes {
if userVote == voteType {
return true
}
}
return false
}
func (c *Comment) vote(voteType, readableName string) error {
if c.votedAs(voteType) {
return fmt.Errorf("comment is already marked as %s", readableName)
}
endpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/talkbox/guestbooks/%s/comments/%s/votes?locale=%s", c.EpisodeID, c.CommentID, c.crunchy.Locale)
body, _ := json.Marshal(map[string]string{"vote_type": voteType})
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(body))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
_, err = c.crunchy.requestFull(req)
if err != nil {
return err
}
c.UserVotes = append(c.UserVotes, voteType)
return nil
}
func (c *Comment) unVote(voteType, readableName string) error {
for i, userVote := range c.UserVotes {
if userVote == voteType {
endpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/talkbox/guestbooks/%s/comments/%s/votes?vote_type=%s&locale=%s", c.EpisodeID, c.CommentID, voteType, c.crunchy.Locale)
_, err := c.crunchy.request(endpoint, http.MethodDelete)
if err != nil {
return err
}
c.UserVotes = append(c.UserVotes[:i], c.UserVotes[i+1:]...)
return nil
}
}
return fmt.Errorf("comment is not marked as %s", readableName)
}