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
72 es := mla.EpisodesWithState[*set.State]
73 es.Add(f.EpisodeNumber)
74 mla.EpisodesWithState[*set.State] = es
77 if set.Watched != nil && *set.Watched ||
78 set.ViewDate != nil && !set.ViewDate.IsZero() {
79 mla.WatchedEpisodes.Add(f.EpisodeNumber)
83 Cache.Set(mla, "mylist-anime", uid, f.AID)
84 Cache.Chtime(mla.Cached, "mylist-anime", uid, f.AID)
87 (set.ViewDate == nil && set.Watched == nil && set.State == nil &&
88 set.Source == nil && set.Storage == nil && set.Other == nil) {
92 e := lid.MyListEntry()
93 if set.ViewDate != nil {
94 e.DateWatched = *set.ViewDate
95 } else if set.Watched != nil {
97 e.DateWatched = time.Now()
99 e.DateWatched = time.Time{}
102 if set.State != nil {
103 e.MyListState = *set.State
105 if set.Source != nil {
106 e.Source = *set.Source
108 if set.Storage != nil {
109 e.Storage = *set.Storage
111 if set.Other != nil {
114 Cache.Set(e, "mylist", lid)
115 Cache.Chtime(e.Cached, "mylist", lid)
118 func (adb *AniDB) MyListAdd(f *File, set *MyListSet) <-chan LID {
119 ch := make(chan LID, 1)
127 user := <-adb.GetCurrentUser()
128 if user == nil || user.UID < 1 {
134 // for the intent map; doesn't get cached
135 key := []fscache.CacheKey{"mylist-add", user.UID, f.FID}
137 ic := make(chan notification, 1)
138 go func() { ch <- (<-ic).(LID); close(ch) }()
139 if intentMap.Intent(ic, key...) {
143 pm := set.toParamMap()
146 reply := <-adb.udp.SendRecv("MYLISTADD", pm)
150 switch reply.Code() {
152 e := adb.parseMylistReply(reply)
157 id, _ := strconv.ParseInt(reply.Lines()[1], 10, 64)
160 // the 310 case does this in parseMylistReply
161 set.update(user.UID, f, lid)
164 intentMap.NotifyClose(lid, key...)
170 func (adb *AniDB) MyListAddByEd2kSize(ed2k string, size int64, set *MyListSet) <-chan LID {
171 ch := make(chan LID, 1)
172 if size < 1 || !validEd2kHash.MatchString(ed2k) {
179 ch <- <-adb.MyListAdd(<-adb.FileByEd2kSize(ed2k, size), set)
185 func (adb *AniDB) MyListEdit(f *File, set *MyListSet) <-chan bool {
186 ch := make(chan bool, 1)
194 user := <-adb.GetCurrentUser()
195 if user == nil || user.UID < 1 {
201 // for the intent map; doesn't get cached
202 key := []fscache.CacheKey{"mylist-edit", user.UID, f.FID}
204 ic := make(chan notification, 1)
205 go func() { ch <- (<-ic).(bool); close(ch) }()
206 if intentMap.Intent(ic, key...) {
210 pm := set.toParamMap()
212 if lid := f.LID[user.UID]; lid > 0 {
218 reply := <-adb.udp.SendRecv("MYLISTADD", pm)
220 switch reply.Code() {
222 intentMap.NotifyClose(true, key...)
224 set.update(user.UID, f, 0)
226 intentMap.NotifyClose(false, key...)
233 func (adb *AniDB) MyListDel(f *File) <-chan bool {
234 ch := make(chan bool)
242 user := <-adb.GetCurrentUser()
243 if user == nil || user.UID < 1 {
249 // for the intent map; doesn't get cached
250 key := []fscache.CacheKey{"mylist-del", user.UID, f.FID}
252 ic := make(chan notification, 1)
253 go func() { ch <- (<-ic).(bool); close(ch) }()
254 if intentMap.Intent(ic, key...) {
259 if lid := f.LID[user.UID]; lid > 0 {
265 reply := <-adb.udp.SendRecv("MYLISTDEL", pm)
267 switch reply.Code() {
269 delete(f.LID, user.UID)
270 Cache.Set(f, "fid", f.FID)
271 Cache.Chtime(f.Cached, "fid", f.FID)
273 intentMap.NotifyClose(true, key...)
275 intentMap.NotifyClose(false, key...)