forked from CommentViewerCollection/MultiCommentViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCommentProvider.cs
More file actions
174 lines (165 loc) · 5.38 KB
/
TestCommentProvider.cs
File metadata and controls
174 lines (165 loc) · 5.38 KB
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
using Common;
using SitePlugin;
using SitePluginCommon;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace TestSitePlugin
{
public class TestCommentProvider : ICommentProvider
{
#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
public event EventHandler<ConnectedEventArgs> Connected;
public event EventHandler<IMessageContext> MessageReceived;
public event EventHandler<IMetadata> MetadataUpdated;
public event EventHandler CanConnectChanged;
public event EventHandler CanDisconnectChanged;
CancellationTokenSource _cts;
private readonly ICommentOptions _options;
private readonly IUserStore _userStore;
public async Task ConnectAsync(string input, global::ryu_s.BrowserCookie.IBrowserProfile browserProfile)
{
if (_cts != null)
{
throw new InvalidOperationException("_cts is not null!");
}
_cts = new CancellationTokenSource();
CanConnect = false;
CanDisconnect = true;
Connected?.Invoke(this, new ConnectedEventArgs
{
IsInputStoringNeeded = false,
});
while (!_cts.IsCancellationRequested)
{
try
{
await Task.Delay(1000, _cts.Token);
}
catch (TaskCanceledException) { break; }
}
CanConnect = true;
CanDisconnect = false;
_cts = null;
}
public void Disconnect()
{
_cts?.Cancel();
}
class CurrentUserInfo : ICurrentUserInfo
{
public string Username { get; set; }
public bool IsLoggedIn { get; set; }
}
public Task<ICurrentUserInfo> GetCurrentUserInfo(global::ryu_s.BrowserCookie.IBrowserProfile browserProfile)
{
ICurrentUserInfo info = new CurrentUserInfo
{
Username = "test",
IsLoggedIn = true,
};
return Task.FromResult(info);
}
public IUser GetUser(string userId)
{
return _userStore.GetUser(userId);
}
public Task PostCommentAsync(string text)
{
var arr = text.Split(',');
if (arr.Length == 2)
{
var userId = arr[0];
var content = arr[1];
var user = _userStore.GetUser(userId);
var comment = new TestComment
{
UserId = userId,
Text = content,
};
var metadata = new TestMetadata(user)
{
SiteContextGuid = SiteContextGuid,
};
var methods = new TestMethods();
var context = new MessageContext(comment, metadata, methods);
MessageReceived?.Invoke(this, context);
}
else if (arr.Length == 3)
{
var userId = arr[0];
var name = arr[1];
var content = arr[2];
var user = _userStore.GetUser(userId);
var comment = new TestComment
{
UserId = userId,
UserName = name,
Text = content,
};
var metadata = new TestMetadata(user)
{
SiteContextGuid = SiteContextGuid,
};
var methods = new TestMethods();
var context = new MessageContext(comment, metadata, methods);
MessageReceived?.Invoke(this, context);
}
else
{
SendSystemInfo(text, InfoType.Notice);
}
return Task.CompletedTask;
}
private void SendSystemInfo(string message, InfoType type)
{
var context = InfoMessageContext.Create(new InfoMessage
{
Text = message,
SiteType = SiteType.Unknown,
Type = type,
}, _options);
MessageReceived?.Invoke(this, context);
}
public void SetMessage(string raw)
{
throw new NotImplementedException();
}
public Guid SiteContextGuid { get; set; }
public TestCommentProvider(ICommentOptions options, IUserStore userStore)
{
_options = options;
_userStore = userStore;
CanConnect = true;
CanDisconnect = false;
}
}
}