-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathForumController.cs
More file actions
684 lines (592 loc) · 32.1 KB
/
Copy pathForumController.cs
File metadata and controls
684 lines (592 loc) · 32.1 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
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
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);
var lastPost = thread.Created;
foreach (var 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.SaveChanges();
}
[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;
var threadID = thread.ForumThreadID;
//int index = post.ForumThread.ForumPosts.IndexOf(post);
var 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.SaveChanges();
return RedirectToAction("Index");
}
db.SaveChanges();
ResetThreadLastPostTime(threadID);
return RedirectToAction("Thread", new { id = threadID, page });
}
[Auth(Role = AuthRole.ZkAdmin)]
public ActionResult DeleteAllPostsByUser(int accountID, string accountName) {
var db = new ZkDataContext();
var acc = db.Accounts.FirstOrDefault(x => x.AccountID == accountID);
if (acc.Name != accountName) return Content("Invalid safety code");
foreach (var p in acc.ForumPosts) DeletePost(p.ForumPostID);
return RedirectToAction("Index");
}
/// <summary>
/// Go to forum index, or a subforum
/// </summary>
public ActionResult Index(IndexResult model) {
var db = new ZkDataContext();
model = model ?? new IndexResult();
model.Categories = db.ForumCategories.Where(x => x.ParentForumCategoryID == model.CategoryID).OrderBy(x => x.SortOrder);
model.CurrentCategory = db.ForumCategories.FirstOrDefault(x => x.ForumCategoryID == model.CategoryID);
model.Path = model.CurrentCategory?.GetPath() ?? new List<ForumCategory>();
var threads = db.ForumThreads.AsQueryable();
if (model.CategoryID != null) threads = threads.Where(x => x.ForumCategoryID == model.CategoryID);
else threads = threads.Where(x => x.ForumCategory.ForumMode != ForumMode.Archive);
threads = threads.Where(x => x.RestrictedClanID == null || x.RestrictedClanID == Global.ClanID);
int? filterAccountID = null;
if (!string.IsNullOrEmpty(model.User))
{
filterAccountID =
(db.Accounts.FirstOrDefault(x => x.Name == model.User) ?? db.Accounts.FirstOrDefault(x => x.Name.ToLower().Contains(model.User)))?.AccountID;
}
if (filterAccountID.HasValue) threads = threads.Where(x => x.CreatedAccountID == filterAccountID || x.ForumPosts.Any(y => y.AuthorAccountID == filterAccountID));
if (model.OnlyUnread && Global.IsAccountAuthorized)
{
threads = from t in threads
let read = t.ForumThreadLastReads.FirstOrDefault(x => x.AccountID == Global.AccountID)
let readForum = t.ForumCategory.ForumLastReads.FirstOrDefault(x => x.AccountID == Global.AccountID)
where (read == null || t.LastPost > read.LastRead) && (readForum == null || t.LastPost > readForum.LastRead)
select t;
}
if (!string.IsNullOrEmpty(model.Search))
{
var threadList =
Global.ForumPostIndexer.FilterPosts(db.ForumPosts, model.Search).Select(x => x.ForumThreadID).Distinct().Take(1000).ToList();
threads = threads.Where(x => x.Title.Contains(model.Search) || x.WikiKey.Contains(model.Search) || threadList.Contains(x.ForumThreadID));
}
model.Threads = threads.OrderByDescending(x => x.ForumCategoryID == model.CategoryID && x.IsPinned).ThenByDescending(x => x.LastPost);
return View("ForumIndex", model);
}
public ActionResult GetPostList(PostListModel model) {
var db = new ZkDataContext();
model = model ?? new PostListModel();
var thread = db.ForumThreads.First(x => x.ForumThreadID == model.ThreadID && (x.RestrictedClanID == null || x.RestrictedClanID == Global.ClanID));
var posts = thread.ForumPosts.AsQueryable();
if (!string.IsNullOrEmpty(model.Search))
{
posts = Global.ForumPostIndexer.FilterPosts(posts, model.Search);
}
if (!string.IsNullOrEmpty(model.User))
{
var filterAccountID = (db.Accounts.FirstOrDefault(x => x.Name == model.User) ?? db.Accounts.FirstOrDefault(x => x.Name.Contains(model.User)))?.AccountID;
if (filterAccountID.HasValue) posts = posts.Where(x => x.AuthorAccountID == filterAccountID);
}
model.Data = posts.OrderBy(x=>x.ForumPostID);
model.Thread = thread;
return View("PostList", model);
}
public class PostListModel
{
public int ThreadID { get; set; }
public string Search { get; set; }
public int GoToPost { get; set; }
public string User { get; set; }
public ForumThread Thread;
public IQueryable<ForumPost> Data;
}
/// <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, string wikiKey) {
var res = new NewPostResult();
var db = new ZkDataContext();
var penalty = Punishment.GetActivePunishment(Global.AccountID, Request.ServerVariables["REMOTE_ADDR"], 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;
}
if (!categoryID.HasValue)
{
categoryID =
db.ForumCategories.Where(x => !x.IsLocked && x.ForumMode == ForumMode.General).OrderBy(x => x.SortOrder).First().ForumCategoryID;
// post in general by default
}
var category = db.ForumCategories.FirstOrDefault(x => x.ForumCategoryID == categoryID);
res.Path = category?.GetPath() ?? new List<ForumCategory>();
res.CurrentCategory = category;
if (forumPostID != null)
{
var post = db.ForumPosts.Single(x => x.ForumPostID == forumPostID);
if (!post.CanEdit(Global.Account)) 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.ForumMode == ForumMode.General || category.ForumMode == ForumMode.Wiki || category.ForumMode == ForumMode.Archive));
} else res.CanSetTopic = true;
res.WikiKey = wikiKey;
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]
[ValidateInput(false)]
public ActionResult SubmitPost(
int? threadID,
int? categoryID,
int? resourceID,
int? missionID,
int? springBattleID,
int? clanID,
int? planetID,
string text,
string title,
string wikiKey,
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 = Punishment.GetActivePunishment(Global.AccountID, MvcApplication.GetUserIP(Request), 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);
var category = thread?.ForumCategory;
if (category == null && categoryID != null) category = db.ForumCategories.FirstOrDefault(x => x.ForumCategoryID == categoryID);
string currentTitle = null;
// update title
if (thread != null && !string.IsNullOrEmpty(title))
{
currentTitle = thread.Title;
thread.Title = title;
thread.WikiKey = wikiKey;
}
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 && category != null) // new thread
{
if (category.IsLocked) return Content("Thread is locked");
if (category.ForumMode == ForumMode.Wiki)
{
if (string.IsNullOrEmpty(wikiKey) || !Account.IsValidLobbyName(wikiKey))
{
return Content("You need to set a valid wiki key");
}
if (db.ForumThreads.Any(y => y.WikiKey == wikiKey))
{
return Content("This wiki key already exists");
}
}
if (string.IsNullOrEmpty(title)) return Content("Title cannot be empty");
thread = new ForumThread();
thread.CreatedAccountID = Global.AccountID;
thread.Title = title;
thread.WikiKey = wikiKey;
thread.ForumCategoryID = category.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.ForumMode == ForumMode.Maps);
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.ForumMode == ForumMode.SpringBattles);
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.ForumMode == ForumMode.Clans);
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.ForumMode == ForumMode.Planets);
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();
int? gotoPostId = null;
//double post preventer
if (lastPost == null || lastPost.AuthorAccountID != Global.AccountID || lastPost.Text != text ||
(!string.IsNullOrEmpty(title) && title != currentTitle))
{
if (forumPostID != null)
{
var post = thread.ForumPosts.Single(x => x.ForumPostID == forumPostID);
if (!post.CanEdit(Global.Account)) 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
{
var p = new ForumPost { AuthorAccountID = Global.AccountID, Text = text, Created = DateTime.UtcNow };
thread.ForumPosts.Add(p);
db.SaveChanges();
gotoPostId = p.ForumPostID;
}
thread.LastPost = DateTime.UtcNow;
thread.LastPostAccountID = Global.AccountID;
thread.PostCount = thread.ForumPosts.Count();
thread.UpdateLastRead(Global.AccountID, true, thread.LastPost);
db.SaveChanges();
}
scope.Complete();
if (missionID.HasValue) return RedirectToAction("Detail", "Missions", new { id = missionID });
if (resourceID.HasValue) return RedirectToAction("Detail", "Maps", new { id = resourceID });
if (springBattleID.HasValue) return RedirectToAction("Detail", "Battles", new { id = springBattleID });
if (clanID.HasValue) return RedirectToAction("Detail", "Clans", new { id = clanID });
if (planetID.HasValue) return RedirectToAction("Planet", "Planetwars", new { id = planetID });
if (forumPostID.HasValue) return RedirectToAction("Thread","Forum", new { id = thread.ForumThreadID, postID = forumPostID });
return RedirectToAction("Thread", "Forum", new { id = thread.ForumThreadID, postID = gotoPostId });
}
}
/// <summary>
/// Redirects to a thread page given a specified <see cref="ForumPost" /> ID
/// </summary>
public ActionResult Post(int id) {
var db = new ZkDataContext();
var post = db.ForumPosts.FirstOrDefault(x => x.ForumPostID == id);
var thread = post.ForumThread;
return RedirectToAction("Thread", new { id = thread.ForumThreadID, postID= id });
}
/// <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, int? postID) {
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");
var cat = t.ForumCategory;
if (cat != null)
{
if (cat.ForumMode == ForumMode.Missions && t.Missions.Any()) return RedirectToAction("Detail", "Missions", new { id = t.Missions.First().MissionID });
if (cat.ForumMode == ForumMode.Maps && t.Resources.Any()) return RedirectToAction("Detail", "Maps", new { id = t.Resources.First().ResourceID });
if (cat.ForumMode == ForumMode.SpringBattles && t.SpringBattles.Any()) return RedirectToAction("Detail", "Battles", new { id = t.SpringBattles.First().SpringBattleID });
if (cat.ForumMode == ForumMode.Clans && t.Clan!=null) return RedirectToAction("Detail", "Clans", new { id = t.RestrictedClanID });
if (cat.ForumMode == ForumMode.Planets && t.Planets.Any()) return RedirectToAction("Planet", "Planetwars", new { id = t.Planets.First().PlanetID });
}
var res = new ThreadResult();
res.GoToPost = postID ?? t.UpdateLastRead(Global.AccountID, false);
db.SaveChanges();
res.Path = cat?.GetPath() ?? new List<ForumCategory>();
res.CurrentThread = t;
return View(res);
}
[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.SaveChanges();
return RedirectToAction("Index", new { categoryID = newcat });
}
/// <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();
var 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;
var post = db.ForumPosts.First(x => x.ForumPostID == forumPostID);
var 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");
var existingVote = db.AccountForumVotes.SingleOrDefault(x => x.ForumPostID == forumPostID && x.AccountID == Global.AccountID);
if (existingVote != null) // clear existing vote
{
var 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)
{
var voteEntry = new AccountForumVote { AccountID = Global.AccountID, ForumPostID = forumPostID, Vote = delta };
db.AccountForumVotes.InsertOnSubmit(voteEntry);
myAcc.VotesAvailable--;
}
db.SaveChanges();
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();
var existingVote = db.AccountForumVotes.SingleOrDefault(x => x.ForumPostID == forumPostID && x.AccountID == Global.AccountID);
if (existingVote == null) return Content("No existing vote to remove");
var post = db.ForumPosts.First(x => x.ForumPostID == forumPostID);
var author = post.Account;
var 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.SaveChanges();
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");
var 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));
if (firstPostOnly)
posts = posts.Where(x => x.ForumThread.ForumPosts.FirstOrDefault() == x);
posts = Global.ForumPostIndexer.FilterPosts(posts, keywords);
return View("SearchResults", new SearchResult { Posts = posts.OrderByDescending(x=>x.Created).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) {
var 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 (var 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.SaveChanges();
return RedirectToAction("Index", new { categoryID });
}
public class IndexResult
{
public IEnumerable<ForumCategory> Categories;
public ForumCategory CurrentCategory;
public List<ForumCategory> Path = new List<ForumCategory>();
public IQueryable<ForumThread> Threads;
public int? CategoryID { get; set; }
public string Search { get; set; }
public bool OnlyUnread { get; set; }
public string User { get; set; }
}
public class NewPostResult
{
public bool CanSetTopic;
public ForumCategory CurrentCategory;
public ForumThread CurrentThread;
public ForumPost EditedPost;
public IEnumerable<ForumPost> LastPosts;
public List<ForumCategory> Path = new List<ForumCategory>();
public string WikiKey;
}
public class ThreadResult
{
public ForumThread CurrentThread;
public int GoToPost;
public List<ForumCategory> Path = new List<ForumCategory>();
}
public class SearchResult
{
public bool DisplayAsPosts;
public List<ForumPost> Posts;
}
[ValidateInput(false)]
public ActionResult Preview(string text) {
return View("Preview",(object)text);
}
}
}