summaryrefslogtreecommitdiff
path: root/Project-Unite/Controllers/ForumController.cs
blob: f64bcfbb4e9f194950be8bf74c7be71f0748dd2f (plain) (blame)
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Project_Unite.Models;

namespace Project_Unite.Controllers
{
    public class ForumController : Controller
    {
        public ApplicationDbContext db = new ApplicationDbContext();

        // GET: Forum
        public ActionResult Index()
        {
            var toplevels = (db.ForumCategories.FirstOrDefault(x => x.Id == "root").Children);


            return View(toplevels);
        }

        //GET: ViewForum
        public ActionResult ViewForum(string id)
        {
            try
            {
                var cat = db.ForumCategories.FirstOrDefault(m => m.Id == id);
                if (!ACL.CanSee(User.Identity.Name, id))
                    return new HttpStatusCodeResult(403);

                return View(cat);
            }
            catch
            {
                return new HttpStatusCodeResult(404);
            }
        }

        public const string BadIdChars = "~`!@#$%^&*()-+={}[]|\\:;'\"<,>.?/";

        public ActionResult PostReply(string id, string fid)
        {
            if (!ACL.CanReply(User.Identity.Name, fid))
                return new HttpStatusCodeResult(403);

            var model = new CreateTopicViewModel();
            model.Category = id;
            return View(model);
        }

        [Authorize]
        public ActionResult Dislike(string id)
        {
            var db = new ApplicationDbContext();
            var topic = db.ForumTopics.FirstOrDefault(x => x.Discriminator == id);
            var uid = User.Identity.GetUserId();
            if (topic == null)
                return new HttpStatusCodeResult(404);
            if (topic.AuthorId == User.Identity.GetUserId())
                return RedirectToAction("ViewTopic", new { id = id, triedtolikeowntopic = true });
            var like = db.Likes.Where(x => x.Topic == topic.Id).FirstOrDefault(x => x.User == uid);
            if (like != null)
            {
                if (like.IsDislike == false)
                {
                    like.IsDislike = true;
                }
                else
                {
                    db.Likes.Remove(like);
                }
            }
            else
            {
                like = new Models.Like();
                like.Id = Guid.NewGuid().ToString();
                like.User = User.Identity.GetUserId();
                like.Topic = topic.Id;
                like.LikedAt = DateTime.Now;
                like.IsDislike = true;
                db.Likes.Add(like);
            }
            db.SaveChanges();
            return RedirectToAction("ViewTopic", new { id = id });
        }

        [Authorize]
        public ActionResult Like(string id)
        {
            var db = new ApplicationDbContext();
            var topic = db.ForumTopics.FirstOrDefault(x => x.Discriminator == id);
            var uid = User.Identity.GetUserId();
            if (topic == null)
                return new HttpStatusCodeResult(404);
            if (topic.AuthorId == User.Identity.GetUserId())
                return RedirectToAction("ViewTopic", new { id = id, triedtolikeowntopic = true });
            var like = db.Likes.Where(x=>x.Topic==topic.Id).FirstOrDefault(x => x.User == uid);
            if (like != null)
            {
                if (like.IsDislike == true)
                {
                    like.IsDislike = false;
                }
                else
                {
                    db.Likes.Remove(like);
                }
            }
            else
            {
                like = new Models.Like();
                like.Id = Guid.NewGuid().ToString();
                like.User = User.Identity.GetUserId();
                like.Topic = topic.Id;
                like.LikedAt = DateTime.Now;
                like.IsDislike = false;
                db.Likes.Add(like);
            }
            db.SaveChanges();
            return RedirectToAction("ViewTopic", new { id = id });
        }

        [Authorize]
        public ActionResult EditPost(string id)
        {
            var db = new ApplicationDbContext();
            var topic = db.ForumPosts.FirstOrDefault(x => x.Id == id);
            var uid = User.Identity.GetUserId();
            string acl_perm = "CanEditPosts";
            if (topic == null)
                return new HttpStatusCodeResult(404);
            if (topic.AuthorId == User.Identity.GetUserId())
                acl_perm = "CanEditOwnPosts";
            if (!ACL.Granted(User.Identity.Name, acl_perm))
                return new HttpStatusCodeResult(403);
            var model = new EditPostViewModel();
            model.Id = topic.Id;
            model.Contents = topic.Body;
            return View(model);
        }

        [Authorize]
        public ActionResult DeletePost(string id)
        {
            var db = new ApplicationDbContext();
            var topic = db.ForumPosts.FirstOrDefault(x => x.Id == id);
            var uid = User.Identity.GetUserId();
            string acl_perm = "CanDeletePosts";
            if (topic == null)
                return new HttpStatusCodeResult(404);
            if (topic.AuthorId == User.Identity.GetUserId())
                acl_perm = "CanDeleteOwnPosts";
            if (!ACL.Granted(User.Identity.Name, acl_perm))
                return new HttpStatusCodeResult(403);
            var parent = db.ForumTopics.FirstOrDefault(x => x.Id == topic.Parent);
            bool redirectToParent = false;
            string cat = "";
            if (parent.Posts.Length - 1 == 0)
            {
                cat = parent.Parent;
                db.ForumTopics.Remove(parent);
                db.SaveChanges();
                redirectToParent = true;
            }
            db.ForumPosts.Remove(topic);
            db.SaveChanges();
            if(redirectToParent)
                return RedirectToAction("ViewForum", new { id = cat });

            return RedirectToAction("ViewTopic", new { id = db.ForumTopics.FirstOrDefault(x => x.Id == topic.Parent).Discriminator});
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize]
        public ActionResult EditPost(EditPostViewModel model)
        {
            var db = new ApplicationDbContext();
            var topic = db.ForumPosts.FirstOrDefault(x => x.Id == model.Id);
            var uid = User.Identity.GetUserId();
            string acl_perm = "CanEditPosts";
            if (topic == null)
                return new HttpStatusCodeResult(404);
            if (topic.AuthorId == User.Identity.GetUserId())
                acl_perm = "CanEditOwnPosts";
            if (!ACL.Granted(User.Identity.Name, acl_perm))
                return new HttpStatusCodeResult(403);
            var edit = new ForumPostEdit();
            edit.EditedAt = DateTime.Now;
            edit.EditReason = model.EditReason;
            edit.Id = Guid.NewGuid().ToString();
            edit.Parent = topic.Id;
            edit.PreviousState = topic.Body;
            edit.UserId = uid;
            db.ForumPostEdits.Add(edit);
            topic.Body = model.Contents;
            db.SaveChanges();
            return RedirectToAction("ViewTopic", new { id = db.ForumTopics.FirstOrDefault(x => x.Id == topic.Parent).Discriminator });
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize]
        public ActionResult PostReply(CreateTopicViewModel model)
        {


            var db = new ApplicationDbContext();
            var topic = db.ForumTopics.FirstOrDefault(x => x.Discriminator == model.Category);
            if (topic == null)
                return new HttpStatusCodeResult(404);
            if (!ACL.CanReply(User.Identity.Name, topic.Parent))
                return new HttpStatusCodeResult(403);

            var post = new ForumPost();
            post.AuthorId = User.Identity.GetUserId();
            post.Body = model.Body;
            post.Id = Guid.NewGuid().ToString();
            post.Parent = topic.Id;
            post.PostedAt = DateTime.Now;
            db.ForumPosts.Add(post);
            db.SaveChanges();
            NotificationDaemon.NotifyFollowers(User.Identity.GetUserId(), "New post from " + ACL.UserNameRaw(User.Identity.GetUserId()) + "!", ACL.UserNameRaw(User.Identity.GetUserId()) + " just posted a reply to " + topic.Subject + ".", Url.Action("ViewTopic", new { id = topic.Discriminator }));
            NotificationDaemon.NotifyUser(User.Identity.GetUserId(), topic.AuthorId, "New post from " + ACL.UserNameRaw(User.Identity.GetUserId()) + "!", ACL.UserNameRaw(User.Identity.GetUserId()) + " just posted a reply to " + topic.Subject + ".", Url.Action("ViewTopic", new { id = topic.Discriminator }));

            return RedirectToAction("ViewTopic", new { id = topic.Discriminator });

        }


        public ActionResult CreateTopic(string id)
        {
            if (!ACL.CanPost(User.Identity.Name, id))
                return new HttpStatusCodeResult(403);

            var model = new CreateTopicViewModel();
            model.Category = id;
            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize]
        public ActionResult CreateTopic(CreateTopicViewModel model)
        {
            if (!ACL.CanPost(User.Identity.Name, model.Category))
                return new HttpStatusCodeResult(403);


            var db = new ApplicationDbContext();
            var forum = db.ForumCategories.FirstOrDefault(x => x.Id == model.Category);
            if (forum == null)
                return new HttpStatusCodeResult(404);

            string subjectId = model.Subject;

            if (String.IsNullOrWhiteSpace(model.Subject) == true)
            {
                ViewBag.Error = "hey no null subjects";
                return new HttpStatusCodeResult(404);
            }

            char[] badChars = subjectId.Where(x => !AllowedChars.Contains(x)).ToArray();

            foreach(var c in badChars)
            {
                subjectId = subjectId.Replace(c, '_');
            }

            while (subjectId.Contains("__"))
            {
                subjectId = subjectId.Replace("__", "_");
            }

            var topic = new ForumTopic();
            topic.AuthorId = User.Identity.GetUserId();
            topic.Id = Guid.NewGuid().ToString();
            topic.Discriminator = subjectId + "_" + db.ForumTopics.Count().ToString();
            topic.StartedAt = DateTime.Now;
            topic.Parent = forum.Id;
            topic.IsAnnounce = model.IsAnnounce;
            topic.IsSticky = model.IsSticky;
            topic.IsGlobal = model.IsGlobal;
            topic.Subject = model.Subject;
            var post = new ForumPost();
            post.AuthorId = topic.AuthorId;
            post.Body = model.Body;
            post.Id = Guid.NewGuid().ToString();
            post.Parent = topic.Id;
            post.PostedAt = topic.StartedAt;
            db.ForumTopics.Add(topic);
            db.ForumPosts.Add(post);
            db.SaveChanges();
            NotificationDaemon.NotifyFollowers(User.Identity.GetUserId(), "New topic started by " + ACL.UserNameRaw(User.Identity.GetUserId()) + "!", ACL.UserNameRaw(User.Identity.GetUserId()) + " just started a topic: " + topic.Subject + ".", Url.Action("ViewTopic", new { id = topic.Discriminator }));

            return RedirectToAction("ViewTopic", new { id = topic.Discriminator });

        }

        const string AllowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz";

        [Authorize]
        public ActionResult ViewTopic(string id, bool triedtolikeowntopic = false)
        {
            if (triedtolikeowntopic)
                ViewBag.Error = "You cannot like or dislike your own topic!";
            var db = new ApplicationDbContext();
            var topic = db.ForumTopics.FirstOrDefault(x => x.Discriminator == id);
            if (topic == null)
                return new HttpStatusCodeResult(404);
            if (!ACL.CanSee(User.Identity.Name, topic.Parent))
                return new HttpStatusCodeResult(403);

            return View(topic);
        }
    }
}