-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathCommentProvider.cs
459 lines (430 loc) · 15.8 KB
/
CommentProvider.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using SitePlugin;
using ryu_s.BrowserCookie;
using Common;
using System.Threading;
using System.Diagnostics;
using System.Net;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections.Concurrent;
using SitePluginCommon;
namespace MixchSitePlugin
{
[Serializable]
public class InvalidInputException : Exception
{
public InvalidInputException() { }
}
class CommentProvider : ICommentProvider
{
#region ICommentProvider
#region Events
public event EventHandler<IMetadata> MetadataUpdated;
public event EventHandler CanConnectChanged;
public event EventHandler CanDisconnectChanged;
public event EventHandler<ConnectedEventArgs> Connected;
public event EventHandler<IMessageContext> MessageReceived;
#endregion //Events
#region CanConnect
private bool _canConnect;
public bool CanConnect
{
get { return _canConnect; }
set
{
if (_canConnect == value)
return;
_canConnect = value;
CanConnectChanged?.Invoke(this, EventArgs.Empty);
}
}
#endregion //CanConnect
#region CanDisconnect
private bool _canDisconnect;
public bool CanDisconnect
{
get { return _canDisconnect; }
set
{
if (_canDisconnect == value)
return;
_canDisconnect = value;
CanDisconnectChanged?.Invoke(this, EventArgs.Empty);
}
}
#endregion //CanDisconnect
const int LiveStatusUnknown = -1;
const int LiveStatusOnAir = 0;
const int LiveStatusOffAir = 1;
private int _liveStatus = LiveStatusUnknown;
public int LiveStatus
{
get { return _liveStatus; }
set
{
if (_liveStatus == value)
return;
if (_liveStatus != LiveStatusUnknown)
{
if (value == LiveStatusOnAir)
{
SendSystemInfo("配信が開始しました", InfoType.Notice);
}
else if (value == LiveStatusOffAir)
{
SendSystemInfo("配信が終了しました", InfoType.Notice);
}
}
else
{
if (value == LiveStatusOffAir)
{
SendSystemInfo("配信が開始されていません", InfoType.Notice);
}
}
_liveStatus = value;
}
}
private void BeforeConnecting()
{
CanConnect = false;
CanDisconnect = true;
_isExpectedDisconnect = false;
_lastReceiveTime = DateTime.Now;
_keepaliveTimer.Enabled = true;
_poipoiStockTimer.Enabled = true;
}
private void AfterDisconnected()
{
_keepaliveTimer.Enabled = false;
_poipoiStockTimer.Enabled = false;
_ws = null;
CanConnect = true;
CanDisconnect = false;
}
protected virtual List<Cookie> GetCookies(IBrowserProfile browserProfile)
{
List<Cookie> cookies = null;
try
{
cookies = browserProfile.GetCookieCollection("mixch.tv");
}
catch { }
return cookies ?? new List<Cookie>();
}
protected virtual CookieContainer CreateCookieContainer(IBrowserProfile browserProfile)
{
var cc = new CookieContainer();
try
{
var cookies = browserProfile.GetCookieCollection("mixch.tv");
foreach (var cookie in cookies)
{
cc.Add(cookie);
}
}
catch { }
return cc;
}
private async Task ConnectInternalAsync(string input, IBrowserProfile browserProfile)
{
if (_ws != null)
{
throw new InvalidOperationException("");
}
var cookies = GetCookies(browserProfile);
LiveUrlInfo liveUrlInfo;
try
{
liveUrlInfo = await Tools.GetLiveId(_dataSource, input);
}
catch (InvalidInputException ex)
{
_logger.LogException(ex, "無効な入力値", $"input={input}");
SendSystemInfo("無効な入力値です", InfoType.Error);
AfterDisconnected();
return;
}
// TODO: 過去のコメントを取得する
while (!_isExpectedDisconnect)
{
_ws = CreateMixchWebsocket();
_ws.Received += WebSocket_Received;
var wsTask = _ws.ReceiveAsync(liveUrlInfo, "", null);
var tasks = new List<Task> { wsTask };
while (tasks.Count > 0)
{
var t = await Task.WhenAny(tasks);
try
{
await wsTask;
}
catch (Exception ex)
{
_logger.LogException(ex);
}
tasks.Remove(wsTask);
SendSystemInfo("wsタスク終了", InfoType.Debug);
}
_ws.Received -= WebSocket_Received;
}
}
public async Task ConnectAsync(string input, IBrowserProfile browserProfile)
{
BeforeConnecting();
try
{
await ConnectInternalAsync(input, browserProfile);
}
finally
{
AfterDisconnected();
}
}
protected virtual IMixchWebsocket CreateMixchWebsocket()
{
return new MixchWebsocket(_logger);
}
private CookieContainer CreateCookieContainer(List<Cookie> cookies)
{
var cc = new CookieContainer();
try
{
foreach (var cookie in cookies)
{
cc.Add(cookie);
}
}
catch { }
return cc;
}
private MixchMessageContext CreateMessageContext(Packet p, bool isInitialComment)
{
var userId = p.UserId.ToString();
var user = GetUser(userId) as IUser2;
if (!_userDict.ContainsKey(userId))
{
_userDict.AddOrUpdate(user.UserId, user, (id, u) => u);
}
bool isFirstComment;
if (_userCommentCountDict.ContainsKey(userId))
{
_userCommentCountDict[userId]++;
isFirstComment = false;
}
else
{
_userCommentCountDict.Add(userId, 1);
isFirstComment = true;
}
var nameItems = new List<IMessagePart>();
nameItems.Add(MessagePartFactory.CreateMessageText(p.Name));
user.Name = nameItems;
var messageItems = new List<IMessagePart>();
var messageBody = p.Message();
if (!string.IsNullOrEmpty(messageBody))
{
messageItems.Add(MessagePartFactory.CreateMessageText(messageBody));
}
MixchMessageContext messageContext = null;
IMixchMessage message;
message = new MixchMessage("")
{
MixchMessageType = (MixchMessageType)p.Kind,
MessageItems = messageItems,
Id = "", // ミクチャはメッセージにIDが存在しない
NameItems = nameItems,
PostTime = DateTimeOffset.FromUnixTimeSeconds(p.Created).LocalDateTime,
UserId = p.UserId.ToString(),
IsFirstComment = isFirstComment,
};
var metadata = new MessageMetadata(message, _options, _siteOptions, user, this, isFirstComment)
{
IsInitialComment = isInitialComment,
SiteContextGuid = SiteContextGuid,
};
var methods = new MixchMessageMethods();
messageContext = new MixchMessageContext(message, metadata, methods);
return messageContext;
}
IMixchWebsocket _ws;
/// <summary>
/// ユーザが意図した切断か
/// </summary>
bool _isExpectedDisconnect;
public void Disconnect()
{
_isExpectedDisconnect = true;
if (_ws != null)
{
_ws.Disconnect();
}
}
public IUser GetUser(string userId)
{
return _userStoreManager.GetUser(SiteType.Mixch, userId);
}
#endregion //ICommentProvider
#region Fields
private ICommentOptions _options;
private MixchSiteOptions _siteOptions;
private ILogger _logger;
private IUserStoreManager _userStoreManager;
private readonly IDataSource _dataSource;
#endregion //Fields
#region ctors
System.Timers.Timer _keepaliveTimer = new System.Timers.Timer();
System.Timers.Timer _poipoiStockTimer = new System.Timers.Timer();
public CommentProvider(ICommentOptions options, MixchSiteOptions siteOptions, ILogger logger, IUserStoreManager userStoreManager)
{
_options = options;
_siteOptions = siteOptions;
_logger = logger;
_userStoreManager = userStoreManager;
_dataSource = new DataSource();
_keepaliveTimer.Interval = 1000;
_keepaliveTimer.Elapsed += _KeepaliveTimer_Elapsed;
_keepaliveTimer.AutoReset = true;
_poipoiStockTimer.Interval = 1000;
_poipoiStockTimer.Elapsed += _PoipoiStockTimer_Elapsed;
_poipoiStockTimer.AutoReset = true;
CanConnect = true;
CanDisconnect = false;
}
// 指定秒数以上パケットの受信がなければWebSocketを再接続する
// 7秒毎にステータス更新パケットが届くので、コメントがなくても通信が正常なら最終受信時間は更新される
const int secondToReconnect = 15;
private DateTime _lastReceiveTime;
private void _KeepaliveTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
var diffTime = DateTime.Now - _lastReceiveTime;
if (diffTime.Seconds >= secondToReconnect && _ws != null)
{
SendSystemInfo($"{secondToReconnect}秒以上データ受信がなかったので再接続します", InfoType.Notice);
_lastReceiveTime = DateTime.Now;
_ws.Disconnect();
}
}
private void _PoipoiStockTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
var unixTimestampNow = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
List<string> processedKeys = new List<string>();
lock (_poipoiStockDict)
{
foreach (KeyValuePair<string, Packet> _poipoiStock in _poipoiStockDict)
{
if (_poipoiStock.Value.Created + _siteOptions.PoipoiKeepSeconds <= unixTimestampNow)
{
var messageContext = CreateMessageContext(_poipoiStock.Value, false);
if (messageContext != null)
{
MessageReceived?.Invoke(this, messageContext);
}
processedKeys.Add(_poipoiStock.Key);
}
}
foreach (string key in processedKeys)
{
_poipoiStockDict.Remove(key);
}
}
}
#endregion //ctors
#region Events
#endregion //Events
private void SendSystemInfo(string message, InfoType type)
{
var context = InfoMessageContext.Create(new InfoMessage
{
Text = message,
SiteType = SiteType.Mixch,
Type = type,
}, _options);
MessageReceived?.Invoke(this, context);
}
public Guid SiteContextGuid { get; set; }
Dictionary<string, int> _userCommentCountDict = new Dictionary<string, int>();
[Obsolete]
Dictionary<string, UserViewModel> _userViewModelDict = new Dictionary<string, UserViewModel>();
ConcurrentDictionary<string, IUser2> _userDict = new ConcurrentDictionary<string, IUser2>();
Dictionary<string, Packet> _poipoiStockDict = new Dictionary<string, Packet>();
private void WebSocket_Received(object sender, Packet p)
{
try
{
if (p.IsStatus())
{
MetadataUpdated?.Invoke(this, new Metadata
{
Title = p.Title,
Elapsed = Tools.ElapsedToString(p.Elapsed),
Others = p.DisplayPointString(),
});
LiveStatus = p.Status;
}
else if ((MixchMessageType)p.Kind == MixchMessageType.PoiPoi)
{
lock (_poipoiStockDict)
{
if (_poipoiStockDict.ContainsKey(p.PoiPoiKey()))
{
var _p = _poipoiStockDict[p.PoiPoiKey()];
_p.Count += p.Count;
}
else
{
_poipoiStockDict[p.PoiPoiKey()] = p;
}
}
}
else if (p.HasMessage())
{
var messageContext = CreateMessageContext(p, false);
if (messageContext != null)
{
MessageReceived?.Invoke(this, messageContext);
}
}
}
catch (Exception ex)
{
_logger.LogException(ex);
}
_lastReceiveTime = DateTime.Now;
}
public async Task PostCommentAsync(string str)
{
throw new NotImplementedException();
}
public async Task<ICurrentUserInfo> GetCurrentUserInfo(IBrowserProfile browserProfile)
{
// FIXME: コメビュでhttpsアクセスするとWeb版で開いているライブ視聴画面でアイテムの送信ができなくなる。
// リロードで直るので何らかのセッション不整合が起きている模様。
// 今現在はユーザー情報を使っていないのでログイン情報の取得を停止する
// var cc = CreateCookieContainer(browserProfile);
// var me = await API.GetMeAsync(_dataSource, cc);
// return new CurrentUserInfo
// {
// IsLoggedIn = !string.IsNullOrEmpty(me.UserId),
// UserId = me.UserId,
// Username = me.DisplayName,
// };
//
return new CurrentUserInfo { };
}
public void SetMessage(string raw)
{
throw new NotImplementedException();
}
}
class CurrentUserInfo : ICurrentUserInfo
{
public string Username { get; set; }
public string UserId { get; set; }
public bool IsLoggedIn { get; set; }
}
}