]> git.lizzy.rs Git - go-anidb.git/blob - mylistmanip.go
anidb: Allow setting the Logger to use for logging traffic
[go-anidb.git] / mylistmanip.go
1 package anidb
2
3 import (
4         "github.com/Kovensky/go-fscache"
5         "strconv"
6         "time"
7 )
8
9 // These are all pointers because they're not
10 // sent at all if they're nil
11 type MyListSet struct {
12         State    *MyListState
13         Watched  *bool
14         ViewDate *time.Time
15         Source   *string
16         Storage  *string
17         Other    *string
18 }
19
20 func (set *MyListSet) toParamMap() (pm paramMap) {
21         pm = paramMap{}
22         if set == nil {
23                 return
24         }
25
26         if set.State != nil {
27                 pm["state"] = *set.State
28         }
29         if set.Watched != nil {
30                 pm["viewed"] = *set.Watched
31         }
32         if set.ViewDate != nil {
33                 if set.ViewDate.IsZero() {
34                         pm["viewdate"] = 0
35                 } else {
36                         pm["viewdate"] = int(int32(set.ViewDate.Unix()))
37                 }
38         }
39         if set.Source != nil {
40                 pm["source"] = *set.Source
41         }
42         if set.Storage != nil {
43                 pm["storage"] = *set.Storage
44         }
45         if set.Other != nil {
46                 pm["other"] = *set.Other
47         }
48         return
49 }
50
51 func (set *MyListSet) update(uid UID, f *File, lid LID) {
52         if f.LID[uid] != lid {
53                 f.LID[uid] = lid
54                 Cache.Set(f, "fid", f.FID)
55                 Cache.Chtime(f.Cached, "fid", f.FID)
56         }
57
58         mla := uid.MyListAnime(f.AID)
59         if mla == nil {
60                 mla = &MyListAnime{
61                         EpisodesWithState: MyListStateMap{},
62                         EpisodesPerGroup:  GroupEpisodes{},
63                 }
64         }
65         // We only ever add, not remove -- we don't know if other files also satisfy the list
66         eg := mla.EpisodesPerGroup[f.GID]
67         eg.Add(f.EpisodeNumber)
68         mla.EpisodesPerGroup[f.GID] = eg
69
70         newState := MyListStateUnknown
71         if set != nil {
72                 if set.State != nil {
73                         newState = *set.State
74                 }
75
76                 if set.Watched != nil && *set.Watched ||
77                         set.ViewDate != nil && !set.ViewDate.IsZero() {
78                         mla.WatchedEpisodes.Add(f.EpisodeNumber)
79                 }
80         }
81
82         es := mla.EpisodesWithState[newState]
83         es.Add(f.EpisodeNumber)
84         mla.EpisodesWithState[newState] = es
85
86         Cache.Set(mla, "mylist-anime", uid, f.AID)
87         Cache.Chtime(mla.Cached, "mylist-anime", uid, f.AID)
88
89         if set == nil ||
90                 (set.ViewDate == nil && set.Watched == nil && set.State == nil &&
91                         set.Source == nil && set.Storage == nil && set.Other == nil) {
92                 return
93         }
94
95         e := lid.MyListEntry()
96         if set.ViewDate != nil {
97                 e.DateWatched = *set.ViewDate
98         } else if set.Watched != nil {
99                 if *set.Watched {
100                         e.DateWatched = time.Now()
101                 } else {
102                         e.DateWatched = time.Time{}
103                 }
104         }
105         if set.State != nil {
106                 e.MyListState = *set.State
107         }
108         if set.Source != nil {
109                 e.Source = *set.Source
110         }
111         if set.Storage != nil {
112                 e.Storage = *set.Storage
113         }
114         if set.Other != nil {
115                 e.Other = *set.Other
116         }
117         Cache.Set(e, "mylist", lid)
118         Cache.Chtime(e.Cached, "mylist", lid)
119 }
120
121 func (adb *AniDB) MyListAdd(f *File, set *MyListSet) <-chan LID {
122         ch := make(chan LID, 1)
123         if f == nil {
124                 ch <- 0
125                 close(ch)
126                 return ch
127         }
128
129         go func() {
130                 user := <-adb.GetCurrentUser()
131                 if user == nil || user.UID < 1 {
132                         ch <- 0
133                         close(ch)
134                         return
135                 }
136
137                 // for the intent map; doesn't get cached
138                 key := []fscache.CacheKey{"mylist-add", user.UID, f.FID}
139
140                 ic := make(chan notification, 1)
141                 go func() { ch <- (<-ic).(LID); close(ch) }()
142                 if intentMap.Intent(ic, key...) {
143                         return
144                 }
145
146                 pm := set.toParamMap()
147                 pm["fid"] = f.FID
148
149                 reply := <-adb.udp.SendRecv("MYLISTADD", pm)
150
151                 lid := LID(0)
152
153                 switch reply.Code() {
154                 case 310:
155                         e := adb.parseMylistReply(reply)
156                         if e != nil {
157                                 lid = e.LID
158                         }
159                 case 210:
160                         id, _ := strconv.ParseInt(reply.Lines()[1], 10, 64)
161                         lid = LID(id)
162
163                         // the 310 case does this in parseMylistReply
164                         set.update(user.UID, f, lid)
165                 }
166
167                 intentMap.NotifyClose(lid, key...)
168         }()
169
170         return ch
171 }
172
173 func (adb *AniDB) MyListAddByEd2kSize(ed2k string, size int64, set *MyListSet) <-chan LID {
174         ch := make(chan LID, 1)
175         if size < 1 || !validEd2kHash.MatchString(ed2k) {
176                 ch <- 0
177                 close(ch)
178                 return ch
179         }
180
181         go func() {
182                 ch <- <-adb.MyListAdd(<-adb.FileByEd2kSize(ed2k, size), set)
183                 close(ch)
184         }()
185         return ch
186 }
187
188 func (adb *AniDB) MyListEdit(f *File, set *MyListSet) <-chan bool {
189         ch := make(chan bool, 1)
190         if f == nil {
191                 ch <- false
192                 close(ch)
193                 return ch
194         }
195
196         go func() {
197                 user := <-adb.GetCurrentUser()
198                 if user == nil || user.UID < 1 {
199                         ch <- false
200                         close(ch)
201                         return
202                 }
203
204                 // for the intent map; doesn't get cached
205                 key := []fscache.CacheKey{"mylist-edit", user.UID, f.FID}
206
207                 ic := make(chan notification, 1)
208                 go func() { ch <- (<-ic).(bool); close(ch) }()
209                 if intentMap.Intent(ic, key...) {
210                         return
211                 }
212
213                 pm := set.toParamMap()
214                 pm["edit"] = 1
215                 if lid := f.LID[user.UID]; lid > 0 {
216                         pm["lid"] = lid
217                 } else {
218                         pm["fid"] = f.FID
219                 }
220
221                 reply := <-adb.udp.SendRecv("MYLISTADD", pm)
222
223                 switch reply.Code() {
224                 case 311:
225                         intentMap.NotifyClose(true, key...)
226
227                         set.update(user.UID, f, 0)
228                 default:
229                         intentMap.NotifyClose(false, key...)
230                 }
231         }()
232
233         return ch
234 }
235
236 func (adb *AniDB) MyListDel(f *File) <-chan bool {
237         ch := make(chan bool)
238         if f == nil {
239                 ch <- false
240                 close(ch)
241                 return ch
242         }
243
244         go func() {
245                 user := <-adb.GetCurrentUser()
246                 if user == nil || user.UID < 1 {
247                         ch <- false
248                         close(ch)
249                         return
250                 }
251
252                 // for the intent map; doesn't get cached
253                 key := []fscache.CacheKey{"mylist-del", user.UID, f.FID}
254
255                 ic := make(chan notification, 1)
256                 go func() { ch <- (<-ic).(bool); close(ch) }()
257                 if intentMap.Intent(ic, key...) {
258                         return
259                 }
260
261                 pm := paramMap{}
262                 if lid := f.LID[user.UID]; lid > 0 {
263                         pm["lid"] = lid
264                 } else {
265                         pm["fid"] = f.FID
266                 }
267
268                 reply := <-adb.udp.SendRecv("MYLISTDEL", pm)
269
270                 switch reply.Code() {
271                 case 211:
272                         delete(f.LID, user.UID)
273                         Cache.Set(f, "fid", f.FID)
274                         Cache.Chtime(f.Cached, "fid", f.FID)
275
276                         intentMap.NotifyClose(true, key...)
277                 default:
278                         intentMap.NotifyClose(false, key...)
279                 }
280         }()
281
282         return ch
283 }