4 "github.com/Kovensky/go-fscache"
9 // These are all pointers because they're not
10 // sent at all if they're nil
11 type MyListSet struct {
20 func (set *MyListSet) toParamMap() (pm paramMap) {
27 pm["state"] = *set.State
29 if set.Watched != nil {
30 pm["viewed"] = *set.Watched
32 if set.ViewDate != nil {
33 if set.ViewDate.IsZero() {
36 pm["viewdate"] = int(int32(set.ViewDate.Unix()))
39 if set.Source != nil {
40 pm["source"] = *set.Source
42 if set.Storage != nil {
43 pm["storage"] = *set.Storage
46 pm["other"] = *set.Other
51 func (set *MyListSet) update(uid UID, f *File, lid LID) {
52 if f.LID[uid] != lid {
54 Cache.Set(f, "fid", f.FID)
55 Cache.Chtime(f.Cached, "fid", f.FID)
58 mla := uid.MyListAnime(f.AID)
61 EpisodesWithState: MyListStateMap{},
62 EpisodesPerGroup: GroupEpisodes{},
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
70 newState := MyListStateUnknown
76 if set.Watched != nil && *set.Watched ||
77 set.ViewDate != nil && !set.ViewDate.IsZero() {
78 mla.WatchedEpisodes.Add(f.EpisodeNumber)
82 es := mla.EpisodesWithState[newState]
83 es.Add(f.EpisodeNumber)
84 mla.EpisodesWithState[newState] = es
86 Cache.Set(mla, "mylist-anime", uid, f.AID)
87 Cache.Chtime(mla.Cached, "mylist-anime", uid, f.AID)
89 e := lid.MyListEntry()
91 (set.ViewDate == nil && set.Watched == nil && set.State == nil &&
92 set.Source == nil && set.Storage == nil && set.Other == nil) {
107 // make sure it's expired
108 Cached: time.Unix(0, 0),
112 if set.ViewDate != nil {
113 e.DateWatched = *set.ViewDate
114 } else if set.Watched != nil {
116 e.DateWatched = time.Now()
118 e.DateWatched = time.Time{}
121 if set.State != nil {
122 e.MyListState = *set.State
124 if set.Source != nil {
125 e.Source = *set.Source
127 if set.Storage != nil {
128 e.Storage = *set.Storage
130 if set.Other != nil {
133 Cache.Set(e, "mylist", lid)
134 Cache.Chtime(e.Cached, "mylist", lid)
137 func (adb *AniDB) MyListAdd(f *File, set *MyListSet) <-chan LID {
138 ch := make(chan LID, 1)
146 user := <-adb.GetCurrentUser()
147 if user == nil || user.UID < 1 {
153 // for the intent map; doesn't get cached
154 key := []fscache.CacheKey{"mylist-add", user.UID, f.FID}
156 ic := make(chan notification, 1)
157 go func() { ch <- (<-ic).(LID); close(ch) }()
158 if intentMap.Intent(ic, key...) {
162 pm := set.toParamMap()
165 reply := <-adb.udp.SendRecv("MYLISTADD", pm)
169 switch reply.Code() {
171 e := adb.parseMylistReply(reply)
176 id, _ := strconv.ParseInt(reply.Lines()[1], 10, 64)
179 // the 310 case does this in parseMylistReply
180 set.update(user.UID, f, lid)
183 intentMap.NotifyClose(lid, key...)
189 func (adb *AniDB) MyListAddByEd2kSize(ed2k string, size int64, set *MyListSet) <-chan LID {
190 ch := make(chan LID, 1)
191 if size < 1 || !validEd2kHash.MatchString(ed2k) {
198 ch <- <-adb.MyListAdd(<-adb.FileByEd2kSize(ed2k, size), set)
204 func (adb *AniDB) MyListEdit(f *File, set *MyListSet) <-chan bool {
205 ch := make(chan bool, 1)
213 user := <-adb.GetCurrentUser()
214 if user == nil || user.UID < 1 {
220 // for the intent map; doesn't get cached
221 key := []fscache.CacheKey{"mylist-edit", user.UID, f.FID}
223 ic := make(chan notification, 1)
224 go func() { ch <- (<-ic).(bool); close(ch) }()
225 if intentMap.Intent(ic, key...) {
229 pm := set.toParamMap()
231 if lid := f.LID[user.UID]; lid > 0 {
237 reply := <-adb.udp.SendRecv("MYLISTADD", pm)
239 switch reply.Code() {
241 intentMap.NotifyClose(true, key...)
243 set.update(user.UID, f, 0)
245 intentMap.NotifyClose(false, key...)
252 func (adb *AniDB) MyListDel(f *File) <-chan bool {
253 ch := make(chan bool)
261 user := <-adb.GetCurrentUser()
262 if user == nil || user.UID < 1 {
268 // for the intent map; doesn't get cached
269 key := []fscache.CacheKey{"mylist-del", user.UID, f.FID}
271 ic := make(chan notification, 1)
272 go func() { ch <- (<-ic).(bool); close(ch) }()
273 if intentMap.Intent(ic, key...) {
278 if lid := f.LID[user.UID]; lid > 0 {
284 reply := <-adb.udp.SendRecv("MYLISTDEL", pm)
286 switch reply.Code() {
288 delete(f.LID, user.UID)
289 Cache.Set(f, "fid", f.FID)
290 Cache.Chtime(f.Cached, "fid", f.FID)
292 intentMap.NotifyClose(true, key...)
294 intentMap.NotifyClose(false, key...)