-
Notifications
You must be signed in to change notification settings - Fork 51
/
ForumController.cs
618 lines (544 loc) · 27 KB
/
ForumController.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web.Mvc;
using ZkData;
namespace ZeroKWeb.Controllers
{
public class ForumController: Controller
{
public const int PageSize = GlobalConst.ForumPostsPerPage;
/// <summary>
/// Returns false for <see cref="News"/> posts and comment threads on <see cref="Clan"/>s, <see cref="Mission"/>s, PlanetWars <see cref="Planet"/>s and <see cref="SpringBattle"/>s; true otherwise
/// </summary>
/// <param name="thread"></param>
/// <returns></returns>
bool IsNormalThread(ForumThread thread)
{
if (thread.Clans != null && thread.Clans.Count > 0) return false;
if (thread.Missions != null && thread.Missions.Count > 0) return false;
if (thread.Planets != null && thread.Planets.Count > 0) return false;
if (thread.SpringBattles != null && thread.SpringBattles.Count > 0) return false;
if (thread.News != null && thread.News.Count > 0) return false;
return true;
}
/// <summary>
/// Set the last post time of the thread to current time (includes edits)
/// </summary>
void ResetThreadLastPostTime(int threadID)
{
var db = new ZkDataContext();
var thread = db.ForumThreads.FirstOrDefault(x=> x.ForumThreadID == threadID);
DateTime lastPost = thread.Created;
foreach (ForumPost p in thread.ForumPosts.Reverse())
{
if (p.ForumPostEdits.Count > 0)
{
var lastEdit = p.ForumPostEdits.Last().EditTime;
if (lastEdit > lastPost) lastPost = lastEdit;
}
else if (p.Created > lastPost) lastPost = p.Created;
}
thread.LastPost = lastPost;
db.SubmitChanges();
}
[Auth(Role = AuthRole.ZkAdmin)]
public ActionResult DeletePost(int? postID)
{
var db = new ZkDataContext();
var post = db.ForumPosts.Single(x => x.ForumPostID == postID);
var thread = post.ForumThread;
int threadID = thread.ForumThreadID;
//int index = post.ForumThread.ForumPosts.IndexOf(post);
int page = GetPostPage(post);
db.ForumPosts.DeleteOnSubmit(post);
if (thread.ForumPosts.Count() <= 1 && IsNormalThread(thread)) {
db.ForumThreadLastReads.DeleteAllOnSubmit(db.ForumThreadLastReads.Where(x => x.ForumThreadID == thread.ForumThreadID).ToList());
db.ForumThreads.DeleteOnSubmit(thread);
db.SubmitChanges();
return RedirectToAction("Index");
}
db.SubmitChanges();
ResetThreadLastPostTime(threadID);
return RedirectToAction("Thread", new { id = threadID, page = page });
}
[Auth(Role = AuthRole.ZkAdmin)]
public ActionResult DeleteAllPostsByUser(int accountID, string accountName)
{
var db = new ZkDataContext();
Account acc = db.Accounts.FirstOrDefault(x => x.AccountID == accountID);
if (acc.Name != accountName) return Content("Invalid safety code");
foreach (ForumPost p in acc.ForumPosts) DeletePost(p.ForumPostID);
return RedirectToAction("Index");
}
/// <summary>
/// Go to forum index, or a subforum
/// </summary>
/// <param name="categoryID">Subforum category ID</param>
/// <param name="page">Page of subforum (i.e. display older threads)</param>
public ActionResult Index(int? categoryID, int? page = null)
{
var db = new ZkDataContext();
var res = new IndexResult();
res.Categories = db.ForumCategories.Where(x => x.ParentForumCategoryID == categoryID).OrderBy(x => x.SortOrder);
res.Path = GetCategoryPath(categoryID, db);
res.CurrentCategory = res.Path.LastOrDefault();
var threads = db.ForumThreads.Where(x => x.ForumCategoryID == categoryID).OrderByDescending(x => x.IsPinned).ThenByDescending(x => x.LastPost);
res.Page = page ?? 0;
res.PageCount = ((threads.Count() - 1) / PageSize) + 1;
res.Threads = threads.Skip((page ?? 0) * PageSize).Take(PageSize).ToList();
return View(res);
}
/// <summary>
/// Make a new post or edit an existing one
/// </summary>
/// <param name="categoryID">The ID of the subforum the <see cref="ForumPost"/> is/will be in</param>
/// <param name="threadID">The <see cref="ForumThread"/> ID, if not a new thread</param>
/// <param name="forumPostID">The <see cref="ForumPost"/> ID, if editing an existing post</param>
/// <returns></returns>
[Auth]
public ActionResult NewPost(int? categoryID, int? threadID, int? forumPostID)
{
var res = new NewPostResult();
var db = new ZkDataContext();
var penalty = ZkData.Punishment.GetActivePunishment(Global.AccountID, "", 0, x => x.BanForum);
if (penalty != null)
{
return Content(string.Format("You cannot post while banned from forum!\nExpires: {0} UTC\nReason: {1}", penalty.BanExpires, penalty.Reason));
}
if (threadID.HasValue && threadID > 0)
{
var clan = db.Clans.FirstOrDefault(x => x.ForumThreadID == threadID);
if (clan != null && Global.ClanID != clan.ClanID)
{
return Content(string.Format("You are not a member of {0}, you cannot post in their clan thread", clan.ClanName));
}
var t = db.ForumThreads.Single(x => x.ForumThreadID == threadID.Value);
res.CurrentThread = t;
res.LastPosts = res.CurrentThread.ForumPosts.OrderByDescending(x => x.ForumPostID).Take(20);
if (!categoryID.HasValue) categoryID = t.ForumCategoryID;
}
res.Path = GetCategoryPath(categoryID, db);
var category = res.Path.LastOrDefault();
res.CurrentCategory = category;
if (forumPostID != null) {
ForumPost post = db.ForumPosts.Single(x=>x.ForumPostID == forumPostID);
if (!Global.IsZeroKAdmin && Global.AccountID != post.AuthorAccountID)
{
return Content("You cannot edit this post");
}
res.EditedPost= post;
}
if (threadID != null)
{
var thread = res.CurrentThread;
res.CanSetTopic = (thread.ForumPosts.Count > 0 && thread.ForumPosts.First().ForumPostID == forumPostID
&& !category.IsClans && !category.IsMaps && !category.IsMissions && !category.IsPlanets && !category.IsSpringBattles);
}
else res.CanSetTopic = true;
return View(res);
}
/// <summary>
/// Try to make a new post or edit an existing one; make a new thread if approriate
/// </summary>
/// <param name="threadID">The <see cref="ForumThread"/> ID, if not a new thread</param>
/// <param name="categoryID">The ID of the subforum the <see cref="ForumPost"/> is/will be in</param>
/// <param name="forumPostID">The <see cref="ForumPost"/> ID, if editing an existing post</param>
[Auth]
public ActionResult SubmitPost(int? threadID, int? categoryID, int? resourceID, int? missionID, int? springBattleID, int? clanID, int? planetID, string text, string title, int? forumPostID)
{
if (threadID == null && missionID == null && resourceID == null && springBattleID == null && clanID ==null && planetID == null && forumPostID==null && string.IsNullOrWhiteSpace(title)) return Content("Cannot post new thread with blank title");
if (string.IsNullOrWhiteSpace(text)) return Content("Please type some text :)");
var penalty = ZkData.Punishment.GetActivePunishment(Global.AccountID, "", 0, x => x.BanForum);
if (penalty != null)
{
return Content(string.Format("You cannot post while banned from forum!\nExpires: {0} UTC\nReason: {1}", penalty.BanExpires, penalty.Reason));
}
var db = new ZkDataContext();
using (var scope = new TransactionScope())
{
var thread = db.ForumThreads.SingleOrDefault(x => x.ForumThreadID == threadID);
// update title
if (thread != null && !String.IsNullOrEmpty(title)) thread.Title = title;
if (thread != null && planetID != null)
{
var planet = db.Planets.Single(x => x.PlanetID == planetID);
thread.Title = "Planet " + planet.Name;
}
if (thread != null && clanID != null)
{
var clan = db.Clans.Single(x => x.ClanID == clanID);
thread.Title = "Clan " + clan.ClanName;
}
if (thread != null && missionID != null)
{
var mission = db.Missions.Single(x => x.MissionID == missionID);
thread.Title = "Mission " +mission.Name;
}
if (thread != null && resourceID != null) {
var map = db.Resources.Single(x => x.ResourceID == resourceID);
thread.Title = "Map " + map.InternalName;
}
if (threadID == null && categoryID.HasValue) // new thread
{
var cat = db.ForumCategories.Single(x => x.ForumCategoryID == categoryID.Value);
if (cat.IsLocked) return Content("Thread is locked");
if (string.IsNullOrEmpty(title)) return Content("Title cannot be empty");
thread = new ForumThread();
thread.CreatedAccountID = Global.AccountID;
thread.Title = title;
thread.ForumCategoryID = cat.ForumCategoryID;
db.ForumThreads.InsertOnSubmit(thread);
}
if (thread == null && resourceID != null) // non existing thread, we posted new post on map
{
var res = db.Resources.Single(x => x.ResourceID == resourceID);
if (res.ForumThread != null) return Content("Double post");
thread = new ForumThread() { Title = "Map " +res.InternalName, CreatedAccountID = Global.AccountID, LastPostAccountID = Global.AccountID };
thread.ForumCategory = db.ForumCategories.FirstOrDefault(x => x.IsMaps);
res.ForumThread = thread;
db.ForumThreads.InsertOnSubmit(thread);
}
if (thread == null && springBattleID != null) // non existing thread, we posted new post on battle
{
var bat = db.SpringBattles.Single(x => x.SpringBattleID == springBattleID);
if (bat.ForumThread != null) return Content("Double post");
thread = new ForumThread() { Title = bat.FullTitle, CreatedAccountID = Global.AccountID, LastPostAccountID = Global.AccountID };
thread.ForumCategory = db.ForumCategories.FirstOrDefault(x => x.IsSpringBattles);
bat.ForumThread = thread;
db.ForumThreads.InsertOnSubmit(thread);
}
if (thread == null && clanID != null)
{
var clan = db.Clans.Single(x => x.ClanID == clanID);
if (clan.ForumThread != null) return Content("Double post");
thread = new ForumThread() { Title = "Clan " +clan.ClanName, CreatedAccountID = Global.AccountID, LastPostAccountID = Global.AccountID };
thread.ForumCategory = db.ForumCategories.FirstOrDefault(x => x.IsClans);
clan.ForumThread = thread;
thread.Clan = clan;
db.ForumThreads.InsertOnSubmit(thread);
}
if (thread == null && planetID != null)
{
var planet = db.Planets.Single(x => x.PlanetID == planetID);
if (planet.ForumThread != null) return Content("Double post");
thread = new ForumThread() { Title = "Planet " +planet.Name, CreatedAccountID = Global.AccountID, LastPostAccountID = Global.AccountID };
thread.ForumCategory = db.ForumCategories.FirstOrDefault(x => x.IsPlanets);
planet.ForumThread = thread;
db.ForumThreads.InsertOnSubmit(thread);
}
if (thread == null) return Content("Thread not found");
if (thread.IsLocked) return Content("Thread is locked");
var lastPost = thread.ForumPosts.OrderByDescending(x => x.ForumPostID).FirstOrDefault();
//double post preventer
if (lastPost == null || lastPost.AuthorAccountID != Global.AccountID || lastPost.Text != text)
{
if (forumPostID != null) {
var post = thread.ForumPosts.Single(x => x.ForumPostID == forumPostID);
if (post.AuthorAccountID != Global.AccountID && !Global.Account.IsZeroKAdmin) throw new ApplicationException("Not authorized to edit the post");
post.ForumPostEdits.Add(new ForumPostEdit() {
EditorAccountID = Global.AccountID,
EditTime = DateTime.UtcNow,
OriginalText = post.Text,
NewText = text
});
post.Text = text;
} else thread.ForumPosts.Add(new ForumPost() { AuthorAccountID = Global.AccountID, Text = text, Created = DateTime.UtcNow});
thread.LastPost = DateTime.UtcNow;
thread.LastPostAccountID = Global.AccountID;
thread.PostCount = thread.ForumPosts.Count();
thread.UpdateLastRead(Global.AccountID, true, thread.LastPost);
db.SubmitChanges();
}
int lastPage = ((thread.PostCount - 1) / PageSize);
scope.Complete();
if (missionID.HasValue) return RedirectToAction("Detail", "Missions", new { id = missionID });
else if (resourceID.HasValue) return RedirectToAction("Detail", "Maps", new { id = resourceID });
else if (springBattleID.HasValue) return RedirectToAction("Detail", "Battles", new { id = springBattleID });
else if (clanID.HasValue) return RedirectToAction("Detail", "Clans", new { id = clanID });
else if (planetID.HasValue) return RedirectToAction("Planet", "Planetwars", new { id = planetID });
else if (forumPostID.HasValue) return RedirectToAction("Thread", new { id = thread.ForumThreadID, postID = forumPostID });
else return RedirectToAction("Thread", new { id = thread.ForumThreadID, page = lastPage});
}
}
/// <summary>
/// Redirects to a thread page given a specified <see cref="ForumPost"/> ID
/// </summary>
public ActionResult Post(int id)
{
var db = new ZkDataContext();
ForumPost post = db.ForumPosts.FirstOrDefault(x => x.ForumPostID == id);
int? page = GetPostPage(post);
if (page == 0) page = null;
ForumThread thread = post.ForumThread;
return RedirectToAction("Thread", new { id = thread.ForumThreadID, page = page});
}
/// <summary>
/// Go to a specific <see cref="ForumThread"/>
/// </summary>
/// <param name="lastPost">Go to last post</param>
/// <param name="lastSeen">UNUSED</param>
/// <param name="postID">A specific <see cref="ForumPost"/> ID to go to</param>
/// <returns></returns>
public ActionResult Thread(int id, bool? lastPost, bool? lastSeen, int? postID, int? page)
{
var db = new ZkDataContext();
var t = db.ForumThreads.FirstOrDefault(x => x.ForumThreadID == id);
// TODO - indicate thread has been deleted
if (t == null) return RedirectToAction("Index");
if (page == null) {
if (postID == null) page = 0;
else {
var post = t.ForumPosts.FirstOrDefault(x => x.ForumPostID == postID);
page = GetPostPage(post);
}
}
var cat = t.ForumCategory;
if (cat != null)
{
if (cat.IsMissions) return RedirectToAction("Detail", "Missions", new { id = t.Missions.First().MissionID });
if (cat.IsMaps) return RedirectToAction("Detail", "Maps", new { id = t.Resources.First().ResourceID });
if (cat.IsSpringBattles) return RedirectToAction("Detail", "Battles", new { id = t.SpringBattles.First().SpringBattleID });
if (cat.IsClans) return RedirectToAction("Detail", "Clans", new { id = t.RestrictedClanID});
if (cat.IsPlanets) return RedirectToAction("Planet", "Planetwars", new { id = t.Planets.First().PlanetID});
}
var res = new ThreadResult();
res.GoToPost = postID ?? t.UpdateLastRead(Global.AccountID, false);
db.SubmitChanges();
res.Path = GetCategoryPath(t.ForumCategoryID, db);
res.CurrentThread = t;
res.PageCount = ((t.PostCount-1)/PageSize) + 1;
res.Page = page;
res.Posts = t.ForumPosts.AsQueryable().Skip((page ?? 0)*PageSize).Take(PageSize).ToList();
return View(res);
}
static IEnumerable<ForumCategory> GetCategoryPath(int? categoryID, ZkDataContext db)
{
var path = new List<ForumCategory>();
var id = categoryID;
while (id != null)
{
var cat = db.ForumCategories.SingleOrDefault(x => x.ForumCategoryID == id);
path.Add(cat);
id = cat.ParentForumCategoryID;
}
path.Reverse();
return path;
}
public class IndexResult
{
public IEnumerable<ForumCategory> Categories;
public ForumCategory CurrentCategory;
public int? Page;
public int PageCount;
public IEnumerable<ForumCategory> Path;
public IEnumerable<ForumThread> Threads;
}
public class NewPostResult
{
public ForumCategory CurrentCategory;
public ForumThread CurrentThread;
public IEnumerable<ForumPost> LastPosts;
public IEnumerable<ForumCategory> Path;
public ForumPost EditedPost;
public bool CanSetTopic;
}
public class ThreadResult
{
public ForumThread CurrentThread;
public int GoToPost;
public int? Page;
public int PageCount;
public IEnumerable<ForumCategory> Path;
public List<ForumPost> Posts;
}
public class SearchResult
{
public List<ForumPost> Posts;
public bool DisplayAsPosts;
}
[Auth(Role = AuthRole.ZkAdmin)]
public ActionResult AdminThread(int threadID, int newcat, bool isPinned, bool isLocked)
{
var db = new ZkDataContext();
var thread = db.ForumThreads.Single(x => x.ForumThreadID == threadID);
thread.ForumCategoryID = newcat;
thread.IsPinned = isPinned;
thread.IsLocked = isLocked;
db.SubmitChanges();
return RedirectToAction("Index", new { categoryID = newcat });
}
public ActionResult EditHistory(int forumPostID)
{
var db = new ZkDataContext();
var post = db.ForumPosts.First(x => x.ForumPostID == forumPostID);
return View("EditHistory", post);
}
/// <summary>
/// Upvote or downvote a post
/// </summary>
/// <param name="delta">+1 or -1</param>
/// <returns></returns>
[Auth]
public ActionResult VotePost(int forumPostID, int delta)
{
var db = new ZkDataContext();
Account myAcc = Global.Account;
if (myAcc.Level < GlobalConst.MinLevelForForumVote)
{
return Content(string.Format("You cannot vote until you are level {0} or higher", GlobalConst.MinLevelForForumVote));
}
if ((Global.Account.ForumTotalUpvotes - Global.Account.ForumTotalDownvotes) < GlobalConst.MinNetKarmaToVote)
{
return Content("Your net karma is too low to vote");
}
if (delta > 1) delta = 1;
else if (delta < -1) delta = -1;
ForumPost post = db.ForumPosts.First(x => x.ForumPostID == forumPostID);
Account author = post.Account;
if (author.AccountID == Global.AccountID) return Content("Cannot vote for your own posts");
if (myAcc.VotesAvailable <= 0) return Content("Out of votes");
AccountForumVote existingVote = db.AccountForumVotes.SingleOrDefault(x => x.ForumPostID == forumPostID && x.AccountID == Global.AccountID);
if (existingVote != null) // clear existing vote
{
int oldDelta = existingVote.Vote;
// reverse vote effects
if (oldDelta > 0)
{
author.ForumTotalUpvotes = author.ForumTotalUpvotes - oldDelta;
post.Upvotes = post.Upvotes - oldDelta;
}
else if (oldDelta < 0)
{
author.ForumTotalDownvotes = author.ForumTotalDownvotes + oldDelta;
post.Downvotes = post.Downvotes + oldDelta;
}
db.AccountForumVotes.DeleteOnSubmit(existingVote);
}
if (delta > 0)
{
author.ForumTotalUpvotes = author.ForumTotalUpvotes + delta;
post.Upvotes = post.Upvotes + delta;
}
else if (delta < 0)
{
author.ForumTotalDownvotes = author.ForumTotalDownvotes - delta;
post.Downvotes = post.Downvotes - delta;
}
if (delta != 0) {
AccountForumVote voteEntry = new AccountForumVote { AccountID = Global.AccountID, ForumPostID = forumPostID, Vote = delta };
db.AccountForumVotes.InsertOnSubmit(voteEntry);
myAcc.VotesAvailable--;
}
db.SubmitChanges();
return RedirectToAction("Thread", new { id = post.ForumThreadID, postID = forumPostID});
}
/// <summary>
/// Removes an existing vote on a post
/// </summary>
[Auth]
public ActionResult CancelVotePost(int forumPostID)
{
var db = new ZkDataContext();
AccountForumVote existingVote = db.AccountForumVotes.SingleOrDefault(x => x.ForumPostID == forumPostID && x.AccountID == Global.AccountID);
if (existingVote == null) return Content("No existing vote to remove");
ForumPost post = db.ForumPosts.First(x => x.ForumPostID == forumPostID);
Account author = post.Account;
int delta = existingVote.Vote;
// reverse vote effects
if (delta > 0)
{
author.ForumTotalUpvotes = author.ForumTotalUpvotes - delta;
post.Upvotes = post.Upvotes - delta;
}
else if (delta < 0)
{
author.ForumTotalDownvotes = author.ForumTotalDownvotes + delta;
post.Downvotes = post.Downvotes + delta;
}
db.AccountForumVotes.DeleteOnSubmit(existingVote);
db.SubmitChanges();
return RedirectToAction("Thread", new { id = post.ForumThreadID, postID = forumPostID });
}
public static int GetPostPage(ForumPost post)
{
if (post == null) return 0;
var index = post.ForumThread.ForumPosts.Count(x=>x.ForumPostID < post.ForumPostID);
return index / PageSize;
}
public ActionResult Search()
{
return View("Search");
}
public ActionResult SubmitSearch(string keywords, string username, List<int> categoryIDs, bool firstPostOnly = false, bool resultsAsPosts = true)
{
if (String.IsNullOrEmpty(keywords) && String.IsNullOrEmpty(username)) return Content("You must enter keywords and/or username");
ZkDataContext db = new ZkDataContext();
if (categoryIDs == null) categoryIDs = new List<int>();
var posts = db.ForumPosts.Where(x=> (String.IsNullOrEmpty(username) || x.Account.Name == username && !x.Account.IsDeleted)
&& (categoryIDs.Count == 0 || categoryIDs.Contains((int)x.ForumThread.ForumCategoryID))
&& (x.ForumThread.RestrictedClanID == null || x.ForumThread.RestrictedClanID == Global.ClanID)
).OrderByDescending(x=> x.Created).ToList();
if (firstPostOnly) posts = posts.Where(x => x.ForumThread.ForumPosts.First() == x).ToList();
var invalidResults = new List<ForumPost>();
if (!String.IsNullOrEmpty(keywords))
{
string[] keywordArray = keywords.Split(null as string[], StringSplitOptions.RemoveEmptyEntries);
foreach (ForumPost p in posts)
{
/* // use this for an OR search
bool success = false;
foreach (string word in keywordArray)
{
if (p.Text.Contains(word))
{
success = true;
break;
}
}
if (!success) invalidResults.Add(p);
*/
// AND search
foreach (string word in keywordArray)
{
if (!p.Text.Contains(word))
{
invalidResults.Add(p);
}
}
}
}
posts = posts.Where(x => !invalidResults.Contains(x)).ToList();
return View("SearchResults", new SearchResult {Posts = posts.Take(100).ToList(), DisplayAsPosts = resultsAsPosts});
}
/// <summary>
/// Marks all threads as read
/// </summary>
/// <param name="categoryID">The subforum category ID; will be applied to all subforums if null</param>
/// <returns></returns>
/// <remarks>Unlike the normal system that tags a thread as read, this one sets a single date value for one or all subforums; a thread is read if its last post is older than this date</remarks>
[Auth]
public ActionResult MarkAllAsRead(int? categoryID)
{
ZkDataContext db = new ZkDataContext();
if (categoryID != null)
{
var lastRead = db.ForumLastReads.FirstOrDefault(x => x.AccountID == Global.AccountID && x.ForumCategoryID == categoryID);
if (lastRead == null) db.ForumLastReads.InsertOnSubmit(new ForumLastRead { AccountID = Global.AccountID, ForumCategoryID = (int)categoryID, LastRead = DateTime.UtcNow });
else lastRead.LastRead = DateTime.UtcNow;
}
else
{
foreach (int categoryID2 in db.ForumCategories.Select(x=> x.ForumCategoryID))
{
var lastRead = db.ForumLastReads.FirstOrDefault(x => x.AccountID == Global.AccountID && x.ForumCategoryID == categoryID2);
if (lastRead == null) db.ForumLastReads.InsertOnSubmit(new ForumLastRead { AccountID = Global.AccountID, ForumCategoryID = categoryID2, LastRead = DateTime.UtcNow });
else lastRead.LastRead = DateTime.UtcNow;
}
}
db.SubmitChanges();
return RedirectToAction("Index", new { categoryID = categoryID });
}
}
}