From 13bfa46c012c0409b3da11f2f9c7e57e534900fa Mon Sep 17 00:00:00 2001 From: Sebastiaan Janssen Date: Sat, 7 Jan 2017 12:48:23 +0100 Subject: [PATCH] Don't make the twitter searcher easy to alter Get the twitter filters from the settings on the home node Makes latest forum activity on home async too --- OurUmbraco.Site/OurUmbraco.Site.csproj | 3 +- .../Views/MacroPartials/Community/Home.cshtml | 79 +++++++++++++++++-- .../MacroPartials/Forum/LatestActivity.cshtml | 54 ------------- .../Views/Partials/Home/ForumActivity.cshtml | 26 ------ .../Partials/Home/LatestForumActivity.cshtml | 40 ++++++++++ .../Partials/Home/TwitterSearchUmbraco.cshtml | 51 +++--------- .../Controllers/TwitterSearchController.cs | 30 ++++--- .../Controllers/LatestActivityController.cs | 31 ++++++++ OurUmbraco/Forum/Models/ReadOnlyTopic.cs | 6 ++ OurUmbraco/Our/MigrationsHandler.cs | 45 +++++++++++ OurUmbraco/OurUmbraco.csproj | 1 + 11 files changed, 227 insertions(+), 139 deletions(-) delete mode 100644 OurUmbraco.Site/Views/MacroPartials/Forum/LatestActivity.cshtml delete mode 100644 OurUmbraco.Site/Views/Partials/Home/ForumActivity.cshtml create mode 100644 OurUmbraco.Site/Views/Partials/Home/LatestForumActivity.cshtml create mode 100644 OurUmbraco/Forum/Controllers/LatestActivityController.cs diff --git a/OurUmbraco.Site/OurUmbraco.Site.csproj b/OurUmbraco.Site/OurUmbraco.Site.csproj index b5942a3a..2fd03c6e 100644 --- a/OurUmbraco.Site/OurUmbraco.Site.csproj +++ b/OurUmbraco.Site/OurUmbraco.Site.csproj @@ -1761,7 +1761,6 @@ - @@ -1792,6 +1791,7 @@ + @@ -4434,7 +4434,6 @@ - diff --git a/OurUmbraco.Site/Views/MacroPartials/Community/Home.cshtml b/OurUmbraco.Site/Views/MacroPartials/Community/Home.cshtml index ea9d0deb..ab23a26e 100644 --- a/OurUmbraco.Site/Views/MacroPartials/Community/Home.cshtml +++ b/OurUmbraco.Site/Views/MacroPartials/Community/Home.cshtml @@ -57,24 +57,89 @@ @if (Members.IsLoggedIn()) { - @Html.Partial("home/forumactivity") - + @ForumActivity() + @TwitterSearch() @Html.Partial("home/documentation") } else { @Html.Partial("home/documentation") - @Html.Partial("home/forumactivity") - + @ForumActivity() + @TwitterSearch() +} + +@helper ForumActivity() +{ +
+
+
+ +
+

Forum Activity

+

+ Recent activity in the forums, where people are helping each other with current problems. +

+
+
+ Recent posts +
+ +
+

Loading latest forum activity...

+
+ + + +
+
+
+} + +@helper TwitterSearch() +{ +
+
+
+ +
+

Twitter Activity

+

+ Recent activity on twitter, where people are talking about Umbraco. +

+
+
+ Recent tweets +
+ + + + + +
+
+
} \ No newline at end of file diff --git a/OurUmbraco.Site/Views/MacroPartials/Forum/LatestActivity.cshtml b/OurUmbraco.Site/Views/MacroPartials/Forum/LatestActivity.cshtml deleted file mode 100644 index 127e3def..00000000 --- a/OurUmbraco.Site/Views/MacroPartials/Forum/LatestActivity.cshtml +++ /dev/null @@ -1,54 +0,0 @@ -@using OurUmbraco.Forum.Extensions -@using OurUmbraco.Forum.Models -@using OurUmbraco.Forum.Services -@using OurUmbraco.Our -@inherits Umbraco.Web.Macros.PartialViewMacroPage - -@{ - var ts = new TopicService(ApplicationContext.DatabaseContext); -} - -
- @Column(ts.GetLatestTopics(6, 1)) -
- -@helper Column(IEnumerable topics) -{ - foreach (var topic in topics.ToArray()) - { - var cat = Umbraco.TypedContent(topic.ParentId); - var mem = Members.GetById(topic.LatestReplyAuthor) ?? Members.GetById(topic.MemberId); - - - -
- @if(mem != null) - { - @Html.Raw(Utils.GetMemberAvatar(mem, 100)) - } -
- -
-
-

@topic.Title

-

- @if (topic.Replies != 0 && !string.IsNullOrEmpty(topic.LastReplyAuthorName)) - { - last edited by @topic.LastReplyAuthorName - } - else - { - created by @topic.AuthorName - } - - @topic.Updated.ConvertToRelativeTime() -

-
- -
- @cat.Name -
-
-
- } -} \ No newline at end of file diff --git a/OurUmbraco.Site/Views/Partials/Home/ForumActivity.cshtml b/OurUmbraco.Site/Views/Partials/Home/ForumActivity.cshtml deleted file mode 100644 index c33dd314..00000000 --- a/OurUmbraco.Site/Views/Partials/Home/ForumActivity.cshtml +++ /dev/null @@ -1,26 +0,0 @@ -@inherits Umbraco.Web.Macros.PartialViewMacroPage - -
-
-
- -
-

Forum Activity

-

- Recent activity in the forums, where people are helping each other with current problems. -

-
-
- Recent posts -
- - @Umbraco.RenderMacro("ForumLatestActivity") - - - - -
-
-
\ No newline at end of file diff --git a/OurUmbraco.Site/Views/Partials/Home/LatestForumActivity.cshtml b/OurUmbraco.Site/Views/Partials/Home/LatestForumActivity.cshtml new file mode 100644 index 00000000..e3846dd6 --- /dev/null +++ b/OurUmbraco.Site/Views/Partials/Home/LatestForumActivity.cshtml @@ -0,0 +1,40 @@ +@using OurUmbraco.Forum.Extensions +@using OurUmbraco.Forum.Models +@model ReadOnlyTopic[] + +@foreach (var topic in Model) +{ + + +
+ @Html.Raw(topic.LastReplyAuthorAvatar) +
+ +
+
+

@topic.Title

+

+ @if (topic.Replies != 0 && !string.IsNullOrEmpty(topic.LastReplyAuthorName)) + { + last updated by @topic.LastReplyAuthorName + } + else + { + created by @topic.AuthorName + } + + @topic.Updated.ConvertToRelativeTime() +

+
+ +
+ @topic.CategoryName +
+
+
+} + +@if (Model.Any() == false) +{ +

Could not load recent forum activity.

+} \ No newline at end of file diff --git a/OurUmbraco.Site/Views/Partials/Home/TwitterSearchUmbraco.cshtml b/OurUmbraco.Site/Views/Partials/Home/TwitterSearchUmbraco.cshtml index 35a4e201..d0f2d36a 100644 --- a/OurUmbraco.Site/Views/Partials/Home/TwitterSearchUmbraco.cshtml +++ b/OurUmbraco.Site/Views/Partials/Home/TwitterSearchUmbraco.cshtml @@ -2,51 +2,24 @@ @using Tweetinvi.Models @model ITweet[] -@if (Model.Any()) +@foreach (var tweet in Model) { -
-
-
+ -
-

Twitter Activity

-

- Recent activity on twitter, where people are talking about Umbraco. -

-
-
- Recent tweets -
- -
- @Column(Model) -
- -
+
+ +
+
+
+

@tweet.Text

+

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

-
+ } -@helper Column(ITweet[] tweets) +@if (Model.Any() == false) { - foreach (var tweet in tweets.Take(6)) - { - - -
- -
- -
-
-

@tweet.Text

-

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

-
-
-
- } +

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 e3aab551..59d21c80 100644 --- a/OurUmbraco/Community/Controllers/TwitterSearchController.cs +++ b/OurUmbraco/Community/Controllers/TwitterSearchController.cs @@ -1,9 +1,6 @@ using System; -using System.Collections.Generic; using System.Configuration; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Web.Mvc; using Tweetinvi; using Tweetinvi.Models; @@ -11,14 +8,18 @@ using Tweetinvi.Parameters; using Umbraco.Core; using Umbraco.Core.Cache; using Umbraco.Core.Logging; +using Umbraco.Web; using Umbraco.Web.Mvc; namespace OurUmbraco.Community.Controllers { public class TwitterSearchController : SurfaceController { - public ActionResult TwitterSearchResult(string searchWord = "umbraco") + public ActionResult TwitterSearchResult(int numberOfResults = 6) { + if (numberOfResults > 30) + numberOfResults = 6; + ITweet[] filteredTweets = { }; try { @@ -31,18 +32,25 @@ namespace OurUmbraco.Community.Controllers ConfigurationManager.AppSettings["twitterUserAccessSecret"]); Tweetinvi.User.GetAuthenticatedUser(); - var searchParameter = new SearchTweetsParameters(searchWord) { SearchType = SearchResultType.Recent }; + var searchParameter = new SearchTweetsParameters("umbraco") { SearchType = SearchResultType.Recent }; return Search.SearchTweets(searchParameter).ToArray(); }, TimeSpan.FromMinutes(2)); - var usernameFilter = "Technologx,Technologx4Real,SOAzure,AdamSmith1,AdamSmitht1,UriSamuels,coding_jobfeeds,TechSparkUk,ItProjectBoard,liveedutv,AdeboyejoAde,VivacitySocial,CHHCInc,DevOpsBlogs,ItCrowdSource,EquiKey".ToLowerInvariant().Split(','); - var wordFilter = "exegesis,#Job Alert:,#pigefodboldbsf".ToLowerInvariant().Split(','); + var settingsNode = Umbraco.TypedContentAtRoot().FirstOrDefault(); + if (settingsNode != null) + { + var usernameFilter = settingsNode.GetPropertyValue("twitterFilterAccounts") + .ToLowerInvariant().Split(',').Where(x => x != string.Empty); + var wordFilter = settingsNode.GetPropertyValue("twitterFilterWords") + .ToLowerInvariant().Split(',').Where(x => x != string.Empty); - filteredTweets = tweets.Where( - x => x.Urls.Any(u => u.ExpandedURL.Contains("umbraco-proxy.com")) == false - && x.CreatedBy.UserIdentifier.ScreenName.ToLowerInvariant().ContainsAny(usernameFilter) == false - && x.Text.ToLowerInvariant().ContainsAny(wordFilter) == false).ToArray(); + filteredTweets = tweets.Where(x => + x.CreatedBy.UserIdentifier.ScreenName.ToLowerInvariant().ContainsAny(usernameFilter) == false + && x.Text.ToLowerInvariant().ContainsAny(wordFilter) == false) + .Take(numberOfResults) + .ToArray(); + } } catch (Exception ex) { diff --git a/OurUmbraco/Forum/Controllers/LatestActivityController.cs b/OurUmbraco/Forum/Controllers/LatestActivityController.cs new file mode 100644 index 00000000..f355cf4b --- /dev/null +++ b/OurUmbraco/Forum/Controllers/LatestActivityController.cs @@ -0,0 +1,31 @@ +using System.Linq; +using System.Web.Mvc; +using OurUmbraco.Forum.Services; +using Umbraco.Web.Mvc; + +namespace OurUmbraco.Forum.Controllers +{ + public class LatestActivityController : SurfaceController + { + public ActionResult LatestActivity(int numberOfTopics = 6) + { + if (numberOfTopics > 30) + numberOfTopics = 6; + + var ts = new TopicService(ApplicationContext.DatabaseContext); + var topics = ts.GetLatestTopics(numberOfTopics).ToArray(); + foreach (var topic in topics) + { + var category = Umbraco.TypedContent(topic.ParentId); + if(category != null) + topic.CategoryName = category.Name; + + var member = Members.GetById(topic.LatestReplyAuthor) ?? Members.GetById(topic.MemberId); + if (member != null) + topic.LastReplyAuthorAvatar = Our.Utils.GetMemberAvatar(member, 100); + } + + return PartialView("~/Views/Partials/Home/LatestForumActivity.cshtml", topics); + } + } +} diff --git a/OurUmbraco/Forum/Models/ReadOnlyTopic.cs b/OurUmbraco/Forum/Models/ReadOnlyTopic.cs index b7f8ac1c..127e8028 100644 --- a/OurUmbraco/Forum/Models/ReadOnlyTopic.cs +++ b/OurUmbraco/Forum/Models/ReadOnlyTopic.cs @@ -16,5 +16,11 @@ namespace OurUmbraco.Forum.Models [ResultColumn("LastReplyAuthorName")] public string LastReplyAuthorName { get; set; } + + [ResultColumn("LastReplyAuthorAvatar")] + public string LastReplyAuthorAvatar { get; set; } + + [ResultColumn("CategoryName")] + public string CategoryName { get; set; } } } \ No newline at end of file diff --git a/OurUmbraco/Our/MigrationsHandler.cs b/OurUmbraco/Our/MigrationsHandler.cs index 840542d3..7a7044cb 100644 --- a/OurUmbraco/Our/MigrationsHandler.cs +++ b/OurUmbraco/Our/MigrationsHandler.cs @@ -37,6 +37,7 @@ namespace OurUmbraco.Our AddMarkAsSolutionReminderSent(); UseNewLoginForm(); UseNewForgotPasswordForm(); + AddTwitterFilters(); } private void EnsureMigrationsMarkerPathExists() @@ -637,5 +638,49 @@ namespace OurUmbraco.Our LogHelper.Error(string.Format("Migration: '{0}' failed", migrationName), ex); } } + + + private void AddTwitterFilters() + { + var migrationName = MethodBase.GetCurrentMethod().Name; + + try + { + var path = HostingEnvironment.MapPath(MigrationMarkersPath + migrationName + ".txt"); + if (File.Exists(path)) + return; + + var contentTypeService = ApplicationContext.Current.Services.ContentTypeService; + var communityContentType = contentTypeService.GetContentType("Community"); + var propertyTypeAlias = "twitterFilterAccounts"; + var textboxMultiple = new DataTypeDefinition("Umbraco.TextboxMultiple"); + + var tabName = "Settings"; + if (communityContentType.PropertyGroups.Contains(tabName) == false) + communityContentType.AddPropertyGroup(tabName); + + if (communityContentType.PropertyTypeExists(propertyTypeAlias) == false) + { + var textboxAccountsFilter = new PropertyType(textboxMultiple, propertyTypeAlias) { Name = "CSV of Twitter accounts to filter" }; + communityContentType.AddPropertyType(textboxAccountsFilter, tabName); + } + + propertyTypeAlias = "twitterFilterWords"; + if (communityContentType.PropertyTypeExists(propertyTypeAlias) == false) + { + var textboxWordFilter = new PropertyType(textboxMultiple, propertyTypeAlias) { Name = "CSV of words filter tweets out" }; + communityContentType.AddPropertyType(textboxWordFilter, tabName); + } + + contentTypeService.Save(communityContentType); + + string[] lines = { "" }; + File.WriteAllLines(path, lines); + } + catch (Exception ex) + { + LogHelper.Error(string.Format("Migration: '{0}' failed", migrationName), ex); + } + } } } diff --git a/OurUmbraco/OurUmbraco.csproj b/OurUmbraco/OurUmbraco.csproj index 6936bd47..74865bc5 100644 --- a/OurUmbraco/OurUmbraco.csproj +++ b/OurUmbraco/OurUmbraco.csproj @@ -374,6 +374,7 @@ +