/
CommandRequest_AddFileToMyList.cs
211 lines (181 loc) · 8.26 KB
/
CommandRequest_AddFileToMyList.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Shoko.Commons.Queue;
using Shoko.Models.Enums;
using Shoko.Models.Queue;
using Shoko.Models.Server;
using Shoko.Server.Extensions;
using Shoko.Server.Models;
using Shoko.Server.Repositories;
namespace Shoko.Server.Commands
{
[Serializable]
[Command(CommandRequestType.AniDB_AddFileUDP)]
public class CommandRequest_AddFileToMyList : CommandRequestImplementation
{
public string Hash { get; set; }
public bool ReadStates { get; set; } = true;
[NonSerialized]
private SVR_VideoLocal vid;
public override CommandRequestPriority DefaultPriority => CommandRequestPriority.Priority6;
public override QueueStateStruct PrettyDescription
{
get
{
if (vid != null)
return new QueueStateStruct
{
queueState = QueueStateEnum.AniDB_MyListAdd,
extraParams = new[] {vid.FileName}
};
return new QueueStateStruct
{
queueState = QueueStateEnum.AniDB_MyListAdd,
extraParams = new[] {Hash}
};
}
}
public CommandRequest_AddFileToMyList()
{
}
public CommandRequest_AddFileToMyList(string hash, bool readstate = true)
{
Hash = hash;
ReadStates = readstate;
Priority = (int) DefaultPriority;
GenerateCommandID();
}
public override void ProcessCommand()
{
logger.Info($"Processing CommandRequest_AddFileToMyList: {vid?.FileName} - {Hash} - {ReadStates}");
try
{
if (vid == null) return;
// when adding a file via the API, newWatchedStatus will return with current watched status on AniDB
// if the file is already on the user's list
bool isManualLink = false;
List<CrossRef_File_Episode> xrefs = vid.EpisodeCrossRefs;
if (xrefs.Count > 0)
isManualLink = xrefs[0].CrossRefSource != (int) CrossRefSource.AniDB;
// mark the video file as watched
List<SVR_JMMUser> aniDBUsers = RepoFactory.JMMUser.GetAniDBUsers();
SVR_JMMUser juser = aniDBUsers.FirstOrDefault();
DateTime? originalWatchedDate = null;
if (juser != null)
originalWatchedDate = vid.GetUserRecord(juser.JMMUserID)?.WatchedDate;
DateTime? newWatchedDate;
int? lid;
AniDBFile_State? state = null;
if (isManualLink)
(lid, newWatchedDate) = ShokoService.AnidbProcessor.AddFileToMyList(xrefs[0].AnimeID,
xrefs[0].GetEpisode().EpisodeNumber, originalWatchedDate, ref state);
else
(lid, newWatchedDate) =
ShokoService.AnidbProcessor.AddFileToMyList(vid, originalWatchedDate, ref state);
if (lid != null && lid.Value > 0)
{
vid.MyListID = lid.Value;
RepoFactory.VideoLocal.Save(vid);
}
if (juser != null)
{
string datemessage = newWatchedDate?.ToShortDateString() ?? "Not Watched";
if (newWatchedDate?.Equals(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime()) ?? false)
datemessage = "No Watch Date Specified";
logger.Info($"Adding file to list: {vid.FileName} - {datemessage}");
bool watched = newWatchedDate != null;
bool watchedLocally = originalWatchedDate != null;
bool watchedChanged = watched != watchedLocally;
if (ReadStates)
{
// handle import watched settings. Don't update AniDB in either case, we'll do that with the storage state
if (ServerSettings.AniDB_MyList_ReadWatched && watched && !watchedLocally)
{
vid.ToggleWatchedStatus(true, false, newWatchedDate, false, juser.JMMUserID,
false, false);
}
else if (ServerSettings.AniDB_MyList_ReadUnwatched && !watched && watchedLocally)
{
vid.ToggleWatchedStatus(false, false, null, false, juser.JMMUserID,
false, false);
}
}
// We should have a MyListID at this point, so hopefully this will prevent looping
if (vid.MyListID > 0 && (watchedChanged || state != ServerSettings.AniDB_MyList_StorageState))
{
if (ServerSettings.AniDB_MyList_SetWatched && watchedLocally)
ShokoService.AnidbProcessor.UpdateMyListFileStatus(vid, true, originalWatchedDate);
else if (ServerSettings.AniDB_MyList_SetUnwatched && !watchedLocally)
ShokoService.AnidbProcessor.UpdateMyListFileStatus(vid, false);
}
}
// if we don't have xrefs, then no series or eps.
if (xrefs.Count <= 0) return;
SVR_AnimeSeries ser = RepoFactory.AnimeSeries.GetByAnimeID(xrefs[0].AnimeID);
// all the eps should belong to the same anime
ser.QueueUpdateStats();
//StatsCache.Instance.UpdateUsingSeries(ser.AnimeSeriesID);
// lets also try adding to the users trakt collecion
if (ServerSettings.Trakt_IsEnabled &&
!string.IsNullOrEmpty(ServerSettings.Trakt_AuthToken))
{
foreach (SVR_AnimeEpisode aep in vid.GetAnimeEpisodes())
{
CommandRequest_TraktCollectionEpisode cmdSyncTrakt =
new CommandRequest_TraktCollectionEpisode(aep.AnimeEpisodeID, TraktSyncAction.Add);
cmdSyncTrakt.Save();
}
}
}
catch (Exception ex)
{
logger.Error($"Error processing CommandRequest_AddFileToMyList: {Hash} - {ex}");
}
}
/// <summary>
/// This should generate a unique key for a command
/// It will be used to check whether the command has already been queued before adding it
/// </summary>
public override void GenerateCommandID()
{
CommandID = $"CommandRequest_AddFileToMyList_{Hash}";
}
public override bool LoadFromDBCommand(CommandRequest cq)
{
CommandID = cq.CommandID;
CommandRequestID = cq.CommandRequestID;
Priority = cq.Priority;
CommandDetails = cq.CommandDetails;
DateTimeUpdated = cq.DateTimeUpdated;
// read xml to get parameters
if (CommandDetails.Trim().Length > 0)
{
XmlDocument docCreator = new XmlDocument();
docCreator.LoadXml(CommandDetails);
// populate the fields
Hash = TryGetProperty(docCreator, "CommandRequest_AddFileToMyList", "Hash");
string read = TryGetProperty(docCreator, "CommandRequest_AddFileToMyList", "ReadStates");
if (!bool.TryParse(read, out bool read_states)) read_states = true;
ReadStates = read_states;
}
if (Hash.Trim().Length <= 0) return false;
vid = RepoFactory.VideoLocal.GetByHash(Hash);
return true;
}
public override CommandRequest ToDatabaseObject()
{
GenerateCommandID();
CommandRequest cq = new CommandRequest
{
CommandID = CommandID,
CommandType = CommandType,
Priority = Priority,
CommandDetails = ToXML(),
DateTimeUpdated = DateTime.Now
};
return cq;
}
}
}