Handy Twitter spam tools for HQ
This commit is contained in:
@@ -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)
|
||||
{
|
||||
<a href="@tweet.Url" class="forum-thread">
|
||||
foreach (var tweet in Model.Tweets)
|
||||
{
|
||||
<a href="@tweet.Url" class="forum-thread">
|
||||
|
||||
<div class="avatar">
|
||||
<img src="@tweet.CreatedBy.ProfileImageUrl400x400.Replace("http://", "https://")" />
|
||||
</div>
|
||||
|
||||
<div class="meta">
|
||||
<div class="forum-thread-text">
|
||||
<h3>@tweet.Text</h3>
|
||||
<p>@tweet.CreatedAt.ConvertToRelativeTime() by @@@tweet.CreatedBy.UserIdentifier.ScreenName (@tweet.CreatedBy.Name)</p>
|
||||
<div class="avatar">
|
||||
<img src="@tweet.CreatedBy.ProfileImageUrl400x400.Replace("http://", "https://")" />
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
|
||||
<div class="meta">
|
||||
<div class="forum-thread-text">
|
||||
<h3>@tweet.Text</h3>
|
||||
<p>@tweet.CreatedAt.ConvertToRelativeTime() by @@@tweet.CreatedBy.UserIdentifier.ScreenName (@tweet.CreatedBy.Name)</p>
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var tweet in Model.Tweets)
|
||||
{
|
||||
var twitterHandle = tweet.CreatedBy.UserIdentifier.ScreenName;
|
||||
|
||||
<img src="@tweet.CreatedBy.ProfileImageUrl400x400.Replace("http://", "https://")" /><br />
|
||||
<h3>@tweet.Text</h3>
|
||||
<p>@tweet.CreatedAt.ConvertToRelativeTime() by <a href="https://twitter.com/@twitterHandle/with_replies" target="_blank">@@@twitterHandle (@tweet.CreatedBy.Name)</a></p><br />
|
||||
<a href="@tweet.Url" target="_blank">@tweet.Url</a><br />
|
||||
|
||||
using (Html.BeginForm("MarkAsSpam", "TwitterSearch"))
|
||||
{
|
||||
@Html.AntiForgeryToken()
|
||||
|
||||
<input type="hidden" name="TwitterHandle" value="@twitterHandle" />
|
||||
<input type="submit" value="Mark as spam" />
|
||||
}
|
||||
|
||||
<hr />
|
||||
}
|
||||
}
|
||||
|
||||
@if (Model.Any() == false)
|
||||
@if (Model.Tweets.Any() == false)
|
||||
{
|
||||
<h2>Could not load recent tweets.</h2>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<ITweet[]>("UmbracoSearchedTweets",
|
||||
() =>
|
||||
{
|
||||
Auth.SetUserCredentials(ConfigurationManager.AppSettings["twitterConsumerKey"],
|
||||
ConfigurationManager.AppSettings["twitterConsumerSecret"],
|
||||
ConfigurationManager.AppSettings["twitterUserAccessToken"],
|
||||
ConfigurationManager.AppSettings["twitterUserAccessSecret"]);
|
||||
Tweetinvi.User.GetAuthenticatedUser();
|
||||
var tweets =
|
||||
ApplicationContext.ApplicationCache.RuntimeCache.GetCacheItem<ITweet[]>("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<string>("twitterFilterAccounts")
|
||||
.ToLowerInvariant().Split(',').Where(x => x != string.Empty);
|
||||
.ToLowerInvariant().Split(',').Where(x => x != string.Empty).ToArray();
|
||||
var wordFilter = settingsNode.GetPropertyValue<string>("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<ITweet>("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<string>("twitterFilterAccounts");
|
||||
settingsContent.SetValue("twitterFilterAccounts", string.Format("{0},{1}", twitterFilterAccountsValue, twitterHandle));
|
||||
var publishStatus = contentService.PublishWithStatus(settingsContent);
|
||||
if (publishStatus.Exception == null)
|
||||
LogHelper.Info<TwitterSearchController>(string.Format("Twitter handle {0} marked as spam by {1} (id: {2})", twitterHandle, member.Name, member.Id));
|
||||
|
||||
return Redirect(redirectUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
using Tweetinvi.Models;
|
||||
|
||||
namespace OurUmbraco.Community.Models
|
||||
{
|
||||
public class TweetsModel
|
||||
{
|
||||
public ITweet[] Tweets { get; set; }
|
||||
public bool ShowAdminOverView { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -392,6 +392,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="CamelCaseFormatter.cs" />
|
||||
<Compile Include="Community\Controllers\TwitterSearchController.cs" />
|
||||
<Compile Include="Community\Models\TweetsModel.cs" />
|
||||
<Compile Include="CustomDateTimeConvertor.cs" />
|
||||
<Compile Include="Documentation\Busineslogic\ConventionExtensions.cs" />
|
||||
<Compile Include="Documentation\Busineslogic\DefaultVersion.cs" />
|
||||
|
||||
Reference in New Issue
Block a user