From 85ab0bd867704e70299c4cce7e598d2121f9e1a5 Mon Sep 17 00:00:00 2001 From: Sebastiaan Janssen Date: Sat, 28 Jan 2017 12:43:18 +0100 Subject: [PATCH] Handy Twitter spam tools for HQ --- .../Partials/Home/TwitterSearchUmbraco.cshtml | 56 +++++++++---- .../Controllers/TwitterSearchController.cs | 79 ++++++++++++++----- OurUmbraco/Community/Models/TweetsModel.cs | 10 +++ OurUmbraco/OurUmbraco.csproj | 1 + 4 files changed, 111 insertions(+), 35 deletions(-) create mode 100644 OurUmbraco/Community/Models/TweetsModel.cs diff --git a/OurUmbraco.Site/Views/Partials/Home/TwitterSearchUmbraco.cshtml b/OurUmbraco.Site/Views/Partials/Home/TwitterSearchUmbraco.cshtml index 691ccd7a..d43abda8 100644 --- a/OurUmbraco.Site/Views/Partials/Home/TwitterSearchUmbraco.cshtml +++ b/OurUmbraco.Site/Views/Partials/Home/TwitterSearchUmbraco.cshtml @@ -1,25 +1,49 @@ @using OurUmbraco.Forum.Extensions -@using Tweetinvi.Models -@model ITweet[] +@model OurUmbraco.Community.Models.TweetsModel -@foreach (var tweet in Model) +@if (Model.ShowAdminOverView == false) { - + foreach (var tweet in Model.Tweets) + { + -
- -
- -
-
-

@tweet.Text

-

@tweet.CreatedAt.ConvertToRelativeTime() by @@@tweet.CreatedBy.UserIdentifier.ScreenName (@tweet.CreatedBy.Name)

+
+
-
-
+ +
+
+

@tweet.Text

+

@tweet.CreatedAt.ConvertToRelativeTime() by @@@tweet.CreatedBy.UserIdentifier.ScreenName (@tweet.CreatedBy.Name)

+
+
+ + } +} +else +{ + foreach (var tweet in Model.Tweets) + { + var twitterHandle = tweet.CreatedBy.UserIdentifier.ScreenName; + +
+

@tweet.Text

+

@tweet.CreatedAt.ConvertToRelativeTime() by @@@twitterHandle (@tweet.CreatedBy.Name)


+ @tweet.Url
+ + using (Html.BeginForm("MarkAsSpam", "TwitterSearch")) + { + @Html.AntiForgeryToken() + + + + } + +
+ } } -@if (Model.Any() == false) +@if (Model.Tweets.Any() == false) {

Could not load recent tweets.

-} \ No newline at end of file +} diff --git a/OurUmbraco/Community/Controllers/TwitterSearchController.cs b/OurUmbraco/Community/Controllers/TwitterSearchController.cs index 59d21c80..783a13fa 100644 --- a/OurUmbraco/Community/Controllers/TwitterSearchController.cs +++ b/OurUmbraco/Community/Controllers/TwitterSearchController.cs @@ -2,6 +2,8 @@ using System.Configuration; using System.Linq; using System.Web.Mvc; +using OurUmbraco.Community.Models; +using OurUmbraco.Forum.Extensions; using Tweetinvi; using Tweetinvi.Models; using Tweetinvi.Parameters; @@ -15,49 +17,88 @@ namespace OurUmbraco.Community.Controllers { public class TwitterSearchController : SurfaceController { - public ActionResult TwitterSearchResult(int numberOfResults = 6) + public ActionResult TwitterSearchResult(int numberOfResults = 6, bool adminOverview = false) { - if (numberOfResults > 30) + var model = new TweetsModel { ShowAdminOverView = adminOverview }; + var member = Members.GetCurrentMember(); + if (member == null || member.IsHq() == false) + model.ShowAdminOverView = false; + + if (member.IsHq() == false && numberOfResults > 30) numberOfResults = 6; - - ITweet[] filteredTweets = { }; + + ITweet[] filteredTweets = {}; try { - var tweets = ApplicationContext.ApplicationCache.RuntimeCache.GetCacheItem("UmbracoSearchedTweets", - () => - { - Auth.SetUserCredentials(ConfigurationManager.AppSettings["twitterConsumerKey"], - ConfigurationManager.AppSettings["twitterConsumerSecret"], - ConfigurationManager.AppSettings["twitterUserAccessToken"], - ConfigurationManager.AppSettings["twitterUserAccessSecret"]); - Tweetinvi.User.GetAuthenticatedUser(); + var tweets = + ApplicationContext.ApplicationCache.RuntimeCache.GetCacheItem("UmbracoSearchedTweets", + () => + { + Auth.SetUserCredentials(ConfigurationManager.AppSettings["twitterConsumerKey"], + ConfigurationManager.AppSettings["twitterConsumerSecret"], + ConfigurationManager.AppSettings["twitterUserAccessToken"], + ConfigurationManager.AppSettings["twitterUserAccessSecret"]); + Tweetinvi.User.GetAuthenticatedUser(); - var searchParameter = new SearchTweetsParameters("umbraco") { SearchType = SearchResultType.Recent }; - return Search.SearchTweets(searchParameter).ToArray(); + var searchParameter = new SearchTweetsParameters("umbraco") + { + SearchType = SearchResultType.Recent + }; + return Search.SearchTweets(searchParameter).ToArray(); - }, TimeSpan.FromMinutes(2)); + }, TimeSpan.FromMinutes(2)); var settingsNode = Umbraco.TypedContentAtRoot().FirstOrDefault(); if (settingsNode != null) { var usernameFilter = settingsNode.GetPropertyValue("twitterFilterAccounts") - .ToLowerInvariant().Split(',').Where(x => x != string.Empty); + .ToLowerInvariant().Split(',').Where(x => x != string.Empty).ToArray(); var wordFilter = settingsNode.GetPropertyValue("twitterFilterWords") .ToLowerInvariant().Split(',').Where(x => x != string.Empty); - filteredTweets = tweets.Where(x => - x.CreatedBy.UserIdentifier.ScreenName.ToLowerInvariant().ContainsAny(usernameFilter) == false + filteredTweets = tweets.Where(x => + x.CreatedBy.UserIdentifier.ScreenName.ToLowerInvariant().ContainsAny(usernameFilter) == + false + && x.UserMentions.Any(m => m.ScreenName.ContainsAny(usernameFilter)) == false && x.Text.ToLowerInvariant().ContainsAny(wordFilter) == false) .Take(numberOfResults) .ToArray(); } + + model.Tweets = filteredTweets; } catch (Exception ex) { LogHelper.Error("Could not get tweets", ex); } + + return PartialView("~/Views/Partials/Home/TwitterSearchUmbraco.cshtml", model); + } - return PartialView("~/Views/Partials/Home/TwitterSearchUmbraco.cshtml", filteredTweets); + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult MarkAsSpam(string twitterHandle) + { + var redirectUrl = HttpContext.Request.UrlReferrer.AbsoluteUri; + + var member = Members.GetCurrentMember(); + var settingsNode = Umbraco.TypedContentAtRoot().FirstOrDefault(); + if (string.IsNullOrEmpty(twitterHandle) + || member == null + || member.IsHq() == false + || ModelState.IsValid == false + || settingsNode == null) + return Redirect(redirectUrl); + + var contentService = ApplicationContext.Services.ContentService; + var settingsContent = contentService.GetById(settingsNode.Id); + var twitterFilterAccountsValue = settingsContent.GetValue("twitterFilterAccounts"); + settingsContent.SetValue("twitterFilterAccounts", string.Format("{0},{1}", twitterFilterAccountsValue, twitterHandle)); + var publishStatus = contentService.PublishWithStatus(settingsContent); + if (publishStatus.Exception == null) + LogHelper.Info(string.Format("Twitter handle {0} marked as spam by {1} (id: {2})", twitterHandle, member.Name, member.Id)); + + return Redirect(redirectUrl); } } } diff --git a/OurUmbraco/Community/Models/TweetsModel.cs b/OurUmbraco/Community/Models/TweetsModel.cs new file mode 100644 index 00000000..6b0d1b47 --- /dev/null +++ b/OurUmbraco/Community/Models/TweetsModel.cs @@ -0,0 +1,10 @@ +using Tweetinvi.Models; + +namespace OurUmbraco.Community.Models +{ + public class TweetsModel + { + public ITweet[] Tweets { get; set; } + public bool ShowAdminOverView { get; set; } + } +} diff --git a/OurUmbraco/OurUmbraco.csproj b/OurUmbraco/OurUmbraco.csproj index cc7eb674..288f9d4f 100644 --- a/OurUmbraco/OurUmbraco.csproj +++ b/OurUmbraco/OurUmbraco.csproj @@ -392,6 +392,7 @@ +