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
71 es := mla.EpisodesWithState[*set.State]
72 es.Add(f.EpisodeNumber)
73 mla.EpisodesWithState[*set.State] = es
76 if set.Watched != nil && *set.Watched ||
77 set.ViewDate != nil && !set.ViewDate.IsZero() {
78 mla.WatchedEpisodes.Add(f.EpisodeNumber)
81 Cache.Set(mla, "mylist-anime", uid, f.AID)
82 Cache.Chtime(mla.Cached, "mylist-anime", uid, f.AID)
84 if set.ViewDate == nil && set.Watched == nil && set.State == nil &&
85 set.Source == nil && set.Storage == nil && set.Other == nil {
89 e := lid.MyListEntry()
90 if set.ViewDate != nil {
91 e.DateWatched = *set.ViewDate
92 } else if set.Watched != nil {
94 e.DateWatched = time.Now()
96 e.DateWatched = time.Time{}
100 e.MyListState = *set.State
102 if set.Source != nil {
103 e.Source = *set.Source
105 if set.Storage != nil {
106 e.Storage = *set.Storage
108 if set.Other != nil {
111 Cache.Set(e, "mylist", lid)
112 Cache.Chtime(e.Cached, "mylist", lid)
115 func (adb *AniDB) MyListAdd(f *File, set *MyListSet) <-chan LID {
116 ch := make(chan LID, 1)
124 user := <-adb.GetCurrentUser()
125 if user == nil || user.UID < 1 {
131 // for the intent map; doesn't get cached
132 key := []fscache.CacheKey{"mylist-add", user.UID, f.FID}
134 ic := make(chan notification, 1)
135 go func() { ch <- (<-ic).(LID); close(ch) }()
136 if intentMap.Intent(ic, key...) {
140 pm := set.toParamMap()
143 reply := <-adb.udp.SendRecv("MYLISTADD", pm)
147 switch reply.Code() {
149 e := adb.parseMylistReply(reply)
154 id, _ := strconv.ParseInt(reply.Lines()[1], 10, 64)
157 // the 310 case does this in parseMylistReply
158 set.update(user.UID, f, lid)
161 intentMap.NotifyClose(lid, key...)
167 func (adb *AniDB) MyListAddByEd2kSize(ed2k string, size int64, set *MyListSet) <-chan LID {
168 ch := make(chan LID, 1)
169 if size < 1 || !validEd2kHash.MatchString(ed2k) {
176 ch <- <-adb.MyListAdd(<-adb.FileByEd2kSize(ed2k, size), set)
182 func (adb *AniDB) MyListEdit(f *File, set *MyListSet) <-chan bool {
183 ch := make(chan bool, 1)
191 user := <-adb.GetCurrentUser()
192 if user == nil || user.UID < 1 {
198 // for the intent map; doesn't get cached
199 key := []fscache.CacheKey{"mylist-edit", user.UID, f.FID}
201 ic := make(chan notification, 1)
202 go func() { ch <- (<-ic).(bool); close(ch) }()
203 if intentMap.Intent(ic, key...) {
207 pm := set.toParamMap()
209 if lid := f.LID[user.UID]; lid > 0 {
215 reply := <-adb.udp.SendRecv("MYLISTADD", pm)
217 switch reply.Code() {
219 intentMap.NotifyClose(true, key...)
221 set.update(user.UID, f, 0)
223 intentMap.NotifyClose(false, key...)
230 func (adb *AniDB) MyListDel(f *File) <-chan bool {
231 ch := make(chan bool)
239 user := <-adb.GetCurrentUser()
240 if user == nil || user.UID < 1 {
246 // for the intent map; doesn't get cached
247 key := []fscache.CacheKey{"mylist-del", user.UID, f.FID}
249 ic := make(chan notification, 1)
250 go func() { ch <- (<-ic).(bool); close(ch) }()
251 if intentMap.Intent(ic, key...) {
256 if lid := f.LID[user.UID]; lid > 0 {
262 reply := <-adb.udp.SendRecv("MYLISTDEL", pm)
264 switch reply.Code() {
266 delete(f.LID, user.UID)
267 Cache.Set(f, "fid", f.FID)
268 Cache.Chtime(f.Cached, "fid", f.FID)
270 intentMap.NotifyClose(true, key...)
272 intentMap.NotifyClose(false, key...)