From 82c0215b9743c8cc7e90a054b6cff9fb8f6cebe7 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 31 May 2017 18:41:36 -0400 Subject: [PATCH] User-submitted quotes. --- Project-Unite/Controllers/QuotesController.cs | 82 +++++++++++++++++++ Project-Unite/Models/IdentityModels.cs | 10 +++ Project-Unite/Project-Unite.csproj | 3 + Project-Unite/Views/Quotes/Index.cshtml | 67 +++++++++++++++ Project-Unite/Views/Quotes/ReviewAll.cshtml | 36 ++++++++ 5 files changed, 198 insertions(+) create mode 100644 Project-Unite/Controllers/QuotesController.cs create mode 100644 Project-Unite/Views/Quotes/Index.cshtml create mode 100644 Project-Unite/Views/Quotes/ReviewAll.cshtml diff --git a/Project-Unite/Controllers/QuotesController.cs b/Project-Unite/Controllers/QuotesController.cs new file mode 100644 index 0000000..cce8f24 --- /dev/null +++ b/Project-Unite/Controllers/QuotesController.cs @@ -0,0 +1,82 @@ +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 +{ + [Authorize] + public class QuotesController : Controller + { + // GET: Quotes + public ActionResult Index() + { + return View(); + } + + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult Index(Models.Quote model) + { + if (!ModelState.IsValid) + return View(model); + + var db = new Models.ApplicationDbContext(); + model.Id = (db.Quotes.Count() + 1).ToString(); + model.IsApproved = false; + db.Quotes.Add(model); + db.SaveChanges(); + + var users = db.Users.ToArray(); + foreach (var user in users) + { + try + { + if (user.HighestRole.IsAdmin) + { + NotificationDaemon.NotifyUser(User.Identity.GetUserId(), user.Id, "New quote submitted.", "Please review user-submitted quotes.", Url.Action("ReviewAll")); + } + } + catch { } + } + return View(model); + } + + [RequiresModerator] + public ActionResult ReviewAll() + { + var db = new ApplicationDbContext(); + return View(db.Quotes.Where(x => x.IsApproved == false)); + } + + [RequiresModerator] + public ActionResult Deny(string id) + { + var db = new ApplicationDbContext(); + var quote = db.Quotes.FirstOrDefault(x => x.Id == id); + if (quote == null) + return new HttpStatusCodeResult(404); + if (quote.IsApproved == true) + return new HttpStatusCodeResult(403); + db.Quotes.Remove(quote); + db.SaveChanges(); + return RedirectToAction("ReviewAll"); + + } + + [RequiresModerator] + public ActionResult Approve(string id) + { + var db = new ApplicationDbContext(); + var quote = db.Quotes.FirstOrDefault(x => x.Id == id); + if (quote == null) + return new HttpStatusCodeResult(404); + quote.IsApproved = true; + db.SaveChanges(); + return RedirectToAction("ReviewAll"); + } + } +} \ No newline at end of file diff --git a/Project-Unite/Models/IdentityModels.cs b/Project-Unite/Models/IdentityModels.cs index 3b534ec..6783e7c 100644 --- a/Project-Unite/Models/IdentityModels.cs +++ b/Project-Unite/Models/IdentityModels.cs @@ -228,10 +228,20 @@ public UserFollow[] Followers public class Quote { public string Id { get; set; } + + [Required(ErrorMessage ="Your quote must have an author name.", AllowEmptyStrings =false)] + [MaxLength(50, ErrorMessage ="Your author name must not exceed 50 characters.")] public string AuthorId { get; set; } + + [AllowHtml] + [Required(AllowEmptyStrings =false, ErrorMessage ="Trying to submit an empty quote, I see?")] public string Body { get; set; } + public string AuthorAvatar { get; set; } + + [Required(ErrorMessage ="We must know what year this quote was said in.")] public long Year { get; set; } + public bool IsApproved { get; set; } } public class BannedIP diff --git a/Project-Unite/Project-Unite.csproj b/Project-Unite/Project-Unite.csproj index 7ddcc26..9172afa 100644 --- a/Project-Unite/Project-Unite.csproj +++ b/Project-Unite/Project-Unite.csproj @@ -638,6 +638,7 @@ + @@ -981,6 +982,8 @@ + + diff --git a/Project-Unite/Views/Quotes/Index.cshtml b/Project-Unite/Views/Quotes/Index.cshtml new file mode 100644 index 0000000..4bea2d6 --- /dev/null +++ b/Project-Unite/Views/Quotes/Index.cshtml @@ -0,0 +1,67 @@ +@model Project_Unite.Models.Quote +@{ + ViewBag.Title = "Quotes"; +} + +

Quotes

+ +

Welcome to the ShiftOS Quotes Database! This page allows you to submit a quote to our Discord bot.

+ +@if (User.Identity.IsModerator()) +{ +

Click here to review all quotes.

+} + +

Submit a quote

+ +@using (Html.BeginForm()) +{ + @Html.AntiForgeryToken() +
+
+ @Html.ValidationSummary() +
+
+ +
+
Quote Author
+
@Html.TextBoxFor(Model=>Model.AuthorId, new{@class="form-control"})
+
Quote Author Image URL (requires http:// or https://)
+
@Html.TextBoxFor(Model => Model.AuthorAvatar, new { @class = "form-control" })
+
Year
+
@Html.TextBoxFor(Model=>Model.Year, new { @type = "number", @class = "form-control" })
+
Quote body
+
@Html.TextAreaFor(Model => Model.AuthorId, new { @class = "form-control" })
+
+ + +} + +
+ +

How to use the quote bot in Discord:

+ +@Html.Markdown(@"To use the quote bot in our Discord server, simply join `#bot`, then type in `!quoteoftheday` and send it as a message and the moderator bot will show you the quote of the day. + +To force a new quote of the day, simply type `!forcequote`. This pulls a random quote from the database, and will keep the quote for 24 hours.") + +

Bot rules

+ +

Here is a quick reminder of the rules regarding both spam and bot usage inside our Discord server.

+ +
+
Section 2: Spam
+
+ 2A. Rapid-fire of messages whether they be different, similar, long or short, is not permitted, and the bot will shun you if you continue.
+ 2B. Disrupting an on-going conversation is not genuinely appreciated. Please move to a different channel, preferrably #off-topic.
+ 2C. Emoji and reaction spam is not appreciated.
+ 2D. Mention-spamming staff and dev roles is not appreciated.
+
+
Section 4: Bots
+
+ 4A. Selfbots must follow the Discord TOS, or we will boot you off the server.
+ 4B. Use of bots must be done in #bot.
+ 4C. If you want your bot on the server, please contact me.
+ 4D. We reserve the right to boot your bot off the server if it breaks any server rules.
+
+
\ No newline at end of file diff --git a/Project-Unite/Views/Quotes/ReviewAll.cshtml b/Project-Unite/Views/Quotes/ReviewAll.cshtml new file mode 100644 index 0000000..bee7f37 --- /dev/null +++ b/Project-Unite/Views/Quotes/ReviewAll.cshtml @@ -0,0 +1,36 @@ +@model IEnumerable +@{ + ViewBag.Title = "Review quotes"; +} + +

Review Quotes

+ +

Below is a list of all quotes that need moderator review.

+ +@if(Model.Count() == 0) +{ +

No quotes to show!

+} +else +{ + foreach (var quote in Model) + { +
+
+

+ @if (!String.IsNullOrWhiteSpace(quote.AuthorAvatar)) + { + + } + @quote.AuthorId

+ +

@Html.Markdown(quote.Body)

+ +

- @quote.Year

+ + Approve + Deny & delete +
+
+ } +} \ No newline at end of file