-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewModel.cs
More file actions
362 lines (315 loc) · 12.2 KB
/
ViewModel.cs
File metadata and controls
362 lines (315 loc) · 12.2 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
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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Threading;
using System.Windows.Data;
using System.Windows.Input;
using System.Xml;
using MVVM;
namespace Hackathon2020
{
class PostProxy
{
public PostProxy(int postID, int posterID, string body, Status status)
{
ResponseIDs = new List<int>();
PostID = postID;
PosterID = posterID;
Body = body;
Status = status;
ParentID = -1;
}
public readonly int PostID;
public readonly int PosterID;
public readonly List<int> ResponseIDs;
public readonly Status Status;
public readonly string Body;
public int ParentID;
}
public class ViewModel : ViewModelBase
{
public ViewModel()
: base(null)
{
ComposeCommand = new RelayCommand((_)=>true, composePost);
HomeCommand = new RelayCommand((_)=>true, goHome);
loadKnownDomains();
loadKeywords();
loadUsers();
ActiveUser = _users[0];
loadPosts();
checkPosts();
}
public ICommand ComposeCommand { get; }
public ICommand HomeCommand { get; }
public HashSet<string> Keywords => _keywords;
public Post SelectedPost
{
get => _selectedPost;
set {
if (value != _selectedPost) {
_selectedPost = value;
OnPropertyChanged("SelectedPost");
Responses.Clear();
Responses.AddRange(_selectedPost.Responses);
OnPropertyChanged("SelectedResponses");
}
}
}
public int SelectedPostIndex
{
get => _selectedPostIndex;
set {
if (_selectedPostIndex != value) {
_selectedPostIndex = value;
OnPropertyChanged("SelectedPostIndex");
SelectedPost = Posts[_selectedPostIndex];
}
}
}
public int SelectedResponseIndex
{
get => -1;
set {
var post = Responses[value];
Posts.Clear();
Posts.Add(post);
SelectedPost = post;
}
}
public ChitterUser ActiveUser
{
get => _currentUser;
set {
if (_currentUser != value) {
_currentUser = value;
OnPropertyChanged("ActiveUser");
}
}
}
public bool UnreadPosts
{
get => _unreadPosts;
set {
if (_unreadPosts != value) {
_unreadPosts = value;
OnPropertyChanged("UnreadPosts");
}
}
}
public Dictionary<string, int> KnownDomainScores => _knownDomainScores;
public List<ChitterUser> Users => _users;
public ObservableCollection<Post> Posts { get; } = new ObservableCollection<Post>();
public ObservableCollection<Post> Responses { get; } = new ObservableCollection<Post>();
private void goHome(object arg)
{
if (SelectedPost?.Parent != null) {
Posts.Clear();
Posts.AddRange(_rootLevelPosts);
if (Posts.Count > 0) {
SelectedPost = Posts[0];
}
}
}
private void composePost(object arg)
{
var viewModel = new ComposeViewModel(this, _currentUser);
DialogService.Instance.ShowDialog(viewModel);
if (viewModel.Result) {
var post = new Post(null, _nextPostID, _currentUser) {
BodyText = viewModel.Message,
Quality = viewModel.Quality
};
++_nextPostID;
Posts.Add(post);
CollectionViewSource.GetDefaultView(Posts).MoveCurrentTo(post);
}
}
private void loadKnownDomains()
{
var doc = new XmlDocument();
doc.Load("./Data/urls.xml");
// Whitelist.
var whiteNodes = doc.SelectNodes("/urls/whitelist/url");
if (whiteNodes != null) {
foreach (var node in whiteNodes) {
if (node is XmlElement urlElement) {
var domain = urlElement.GetAttribute("domain").ToLower();
Debug.Assert(!_knownDomainScores.ContainsKey(domain));
var score = int.Parse(urlElement.GetAttribute("score"));
// Good sites have negative weight!
_knownDomainScores[domain] = -Math.Abs(score);
}
}
}
var blackNodes = doc.SelectNodes("urls/blacklist/url");
if (blackNodes != null) {
foreach (var node in blackNodes) {
if (node is XmlElement urlElement) {
var domain = urlElement.GetAttribute("domain").ToLower();
Debug.Assert(!_knownDomainScores.ContainsKey(domain));
var score = int.Parse(urlElement.GetAttribute("score"));
// Bad sites have positive weight!
_knownDomainScores[domain] = Math.Abs(score);
}
}
}
}
private void loadKeywords()
{
var doc = new XmlDocument();
doc.Load("./Data/keywords.xml");
var wordNodes = doc.SelectNodes("/keywords/word");
if (wordNodes != null) {
foreach (var node in wordNodes) {
if (node is XmlElement wordElement) {
var word = wordElement.InnerText.ToLower();
_keywords.Add(word);
}
}
}
}
private void loadUsers()
{
var doc = new XmlDocument();
doc.Load("./Data/users.xml");
var userNodes = doc.SelectNodes("/users/user");
if (userNodes != null) {
foreach (var node in userNodes) {
if (node is XmlElement userElement) {
var user = new ChitterUser(this) {
ID = int.Parse(userElement.GetAttribute("ID")),
Avatar = int.Parse(userElement.GetAttribute("avatar")),
RealName = userElement.GetAttribute("name"),
UserName = userElement.GetAttribute("username")
};
_users.Add(user);
Debug.WriteLine($"Chitter: added user '@{user.UserName}'");
}
}
}
}
private void loadPosts()
{
var doc = new XmlDocument();
doc.Load("./Data/posts.xml");
var postNodes = doc.SelectNodes("/posts/post");
var proxies = new List<PostProxy>();
var maxPostID = -1;
if (postNodes != null) {
foreach (var node in postNodes) {
if (node is XmlElement postElement) {
var postID = int.Parse(postElement.GetAttribute("ID"));
if (postID > maxPostID) {
maxPostID = postID;
}
var posterID = int.Parse(postElement.GetAttribute("user"));
var body = readPostBody(postElement);
var status = (Status) Enum.Parse(typeof(Status), postElement.GetAttribute("status"));
var postProxy = new PostProxy(postID, posterID, body, status);
readResponses(postElement, postProxy.ResponseIDs);
proxies.Add(postProxy);
}
}
}
_nextPostID = maxPostID + 1;
// Patch up parents.
foreach(var postProxy in proxies) {
foreach (var responseID in postProxy.ResponseIDs) {
var responseProxy = proxies.Find(p => p.PostID == responseID);
if (responseProxy != null) {
responseProxy.ParentID = postProxy.PostID;
}
}
}
// Now generate the actual posts. First fix up the parents (needed because of the ViewModelBase ctor)...
var postList = new List<Post>();
foreach (var proxy in proxies) {
Post parent = null;
if (proxy.ParentID >= 0) {
parent = postList.Find(p => p.ID == proxy.ParentID);
}
var user = _users.Find(u => u.ID == proxy.PosterID);
Debug.Assert(user != null);
var post = new Post(parent, proxy.PostID, user) {BodyText = proxy.Body};
// Only add root level posts to the visible post list. Child posts appear in the response panel.
if (proxy.ParentID < 0) {
Posts.Add(post);
_rootLevelPosts.Add(post);
}
postList.Add(post);
// If quality not determined, add to the list to be checked later.
if (post.Quality == Status.Unclassified) {
_postsToCheck.Add(post);
}
}
// ...and the children.
foreach (var proxy in proxies) {
var post = postList.Find(p => p.ID == proxy.PostID);
foreach (var responseID in proxy.ResponseIDs) {
var response = postList.Find(p => p.ID == responseID);
Debug.Assert(response != null);
post.Responses.Add(response);
}
}
// Select the first post.
if (Posts.Count > 0) {
SelectedPost = Posts[0];
}
}
private void checkPosts()
{
foreach (var post in _postsToCheck) {
validatePost(post);
}
_postsToCheck.Clear();
}
private void validatePost(Post post)
{
ThreadPool.QueueUserWorkItem(runPostChecks, post);
}
private void runPostChecks(object arg)
{
var post = (Post) arg;
var status = PostChecker.Run(this, post.BodyText);
post.Quality = status;
var user = post.User;
++user.PostCount;
if (status == Status.Deplorable) {
++user.RedCount;
}
}
private static void readResponses(XmlElement postElement, List<int> responses)
{
var responsesNode = postElement.SelectSingleNode("responses");
if (responsesNode is XmlElement responsesElement) {
var idList = responsesElement.GetAttribute("IDs");
if (!string.IsNullOrEmpty(idList)) {
var idStrs = idList.Split(',');
foreach (var idStr in idStrs) {
responses.Add(int.Parse(idStr));
}
}
}
}
private static string readPostBody(XmlElement node)
{
var bodyNode = node.SelectSingleNode("body");
if (bodyNode is XmlElement bodyElement) {
return bodyElement.InnerText;
}
return string.Empty;
}
private Post _selectedPost;
private int _selectedPostIndex;
private bool _unreadPosts;
private readonly List<ChitterUser> _users = new List<ChitterUser>();
private ChitterUser _currentUser;
private readonly List<Post> _postsToCheck = new List<Post>();
private int _nextPostID;
private readonly List<Post> _rootLevelPosts = new List<Post>();
private readonly Dictionary<string, int> _knownDomainScores = new Dictionary<string, int>();
private readonly HashSet<string> _keywords = new HashSet<string>();
}
}