Skip to content

Commit 70b5df3

Browse files
authored
Merge pull request #682 from poppastring/pin-to-home
Pin to home - Allows you to pin a single post to the home page for a more static entry point in the site.
2 parents df36b23 + 1ee225e commit 70b5df3

File tree

12 files changed

+88
-14
lines changed

12 files changed

+88
-14
lines changed

Diff for: azure-pipelines.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pool:
1010

1111
variables:
1212
buildConfiguration: 'Release'
13-
version: 3.3
13+
version: 3.4
1414

1515
steps:
1616
- task: UseDotNet@2

Diff for: source/DasBlog.Services/ConfigFile/Interfaces/ISiteConfig.cs

+2
Original file line numberDiff line numberDiff line change
@@ -370,5 +370,7 @@ public interface ISiteConfig
370370
bool EnableRewritingHashtagsToCategoryLinks { get; set; }
371371
bool EnableRewritingBareLinksToEmbeddings { get; set; }
372372
bool EnableRewritingBareLinksToIcons { get; set; }
373+
374+
string PostPinnedToHomePage { get; set; }
373375
}
374376
}

Diff for: source/DasBlog.Services/ConfigFile/SiteConfig.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,6 @@ public string Root {
251251
public string MastodonServerUrl { get; set; }
252252

253253
public string MastodonAccount { get; set; }
254-
254+
public string PostPinnedToHomePage { get; set; }
255255
}
256256
}

Diff for: source/DasBlog.Tests/UnitTests/SiteConfigTest.cs

+1
Original file line numberDiff line numberDiff line change
@@ -173,5 +173,6 @@ public class SiteConfigTest : ISiteConfig
173173
public bool EnableRewritingHashtagsToCategoryLinks { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
174174
public bool EnableRewritingBareLinksToEmbeddings { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
175175
public bool EnableRewritingBareLinksToIcons { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
176+
public string PostPinnedToHomePage { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
176177
}
177178
}

Diff for: source/DasBlog.Web.Repositories/BlogManager.cs

+5
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ public void DeleteEntry(string postid)
205205
LogEvent(EventCodes.EntryDeleted, entry);
206206
}
207207

208+
public EntryCollection GetAllEntries()
209+
{
210+
return dataService.GetEntries(false);
211+
}
212+
208213
private static StringCollection GetSearchWords(string searchString)
209214
{
210215
var searchWords = new StringCollection();

Diff for: source/DasBlog.Web.Repositories/Interfaces/IBlogManager.cs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public interface IBlogManager
1717

1818
EntryCollection GetEntriesForPage(int pageIndex, string acceptLanguageHeader);
1919

20+
EntryCollection GetAllEntries();
21+
2022
EntrySaveState CreateEntry(Entry entry);
2123

2224
EntrySaveState UpdateEntry(Entry entry);

Diff for: source/DasBlog.Web.UI/Controllers/AdminController.cs

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public IActionResult Settings()
4646
var dbsvm = new DasBlogSettingsViewModel();
4747
dbsvm.MetaConfig = mapper.Map<MetaViewModel>(dasBlogSettings.MetaTags);
4848
dbsvm.SiteConfig = mapper.Map<SiteViewModel>(dasBlogSettings.SiteConfiguration);
49+
dbsvm.Posts = blogManager.GetAllEntries()
50+
.Select(entry => mapper.Map<PostViewModel>(entry)).ToList();
4951

5052
return View(dbsvm);
5153
}

Diff for: source/DasBlog.Web.UI/Controllers/HomeController.cs

+26-5
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
using Microsoft.AspNetCore.Mvc;
1212
using Microsoft.Extensions.Caching.Memory;
1313
using Microsoft.Extensions.Logging;
14+
using Quartz.Util;
1415
using System;
16+
using System.Collections.Generic;
1517
using System.Diagnostics;
1618
using System.Linq;
17-
using System.Threading.Tasks;
1819

1920
namespace DasBlog.Web.Controllers
2021
{
@@ -47,9 +48,7 @@ public IActionResult Index()
4748
{
4849
lpvm = new ListPostsViewModel
4950
{
50-
Posts = blogManager.GetFrontPagePosts(Request.Headers["Accept-Language"])
51-
.Select(entry => mapper.Map<PostViewModel>(entry))
52-
.Select(editentry => editentry).ToList()
51+
Posts = HomePagePosts()
5352
};
5453

5554
foreach( var post in lpvm.Posts )
@@ -92,7 +91,6 @@ public IActionResult Page(int index)
9291
return Index();
9392
}
9493

95-
9694
var lpvm = new ListPostsViewModel
9795
{
9896
Posts = blogManager.GetEntriesForPage(index, Request.Headers["Accept-Language"])
@@ -157,6 +155,29 @@ private ListPostsViewModel AddComments(ListPostsViewModel listPostsViewModel)
157155

158156
return listPostsViewModel;
159157
}
158+
159+
private IList<PostViewModel> HomePagePosts()
160+
{
161+
IList<PostViewModel> posts = new List<PostViewModel>();
162+
163+
if (!dasBlogSettings.SiteConfiguration.PostPinnedToHomePage.IsNullOrWhiteSpace() &&
164+
Guid.TryParse(dasBlogSettings.SiteConfiguration.PostPinnedToHomePage, out var results))
165+
{
166+
var entry = blogManager.GetBlogPostByGuid(results);
167+
168+
if (entry != null)
169+
{
170+
posts.Add(mapper.Map<PostViewModel>(entry));
171+
}
172+
}
173+
else
174+
{
175+
posts = blogManager.GetFrontPagePosts(Request.Headers["Accept-Language"])
176+
.Select(entry => mapper.Map<PostViewModel>(entry)).ToList();
177+
}
178+
179+
return posts;
180+
}
160181
}
161182
}
162183

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using DasBlog.Web.Models.BlogViewModels;
5+
6+
namespace DasBlog.Web.Models.AdminViewModels
7+
{
8+
public class BlogPostListViewModel
9+
{
10+
public string Name { get; set; }
11+
12+
public string Id { get; set; }
13+
14+
public List<BlogPostListViewModel> Init(List<PostViewModel> posts)
15+
{
16+
var allposts = posts.Select(p => new BlogPostListViewModel { Name = p.Title, Id = p.EntryId }).ToList();
17+
18+
allposts.Insert(0, new BlogPostListViewModel { Name = "--Disable Pinning--", Id = "" });
19+
20+
return allposts;
21+
}
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
1+
using System.Collections.Generic;
2+
using DasBlog.Web.Models.BlogViewModels;
53

64
namespace DasBlog.Web.Models.AdminViewModels
75
{
86
public class DasBlogSettingsViewModel
97
{
108
public MetaViewModel MetaConfig { get; set; }
119
public SiteViewModel SiteConfig { get; set; }
10+
public List<PostViewModel> Posts { get; set; }
1211
}
1312
}

Diff for: source/DasBlog.Web.UI/Models/AdminViewModels/SiteViewModel.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -302,21 +302,25 @@ public class SiteViewModel
302302
[Description("Help meet some of the EU General Data Protection Regulation (GDPR) requirements")]
303303
public bool CookieConsentEnabled { get; set; }
304304

305-
[DisplayName("Default Sources (seperated by semi colon")]
305+
[DisplayName("Default Sources (separated by semi colon")]
306306
[Description("")]
307307
[StringLength(50, MinimumLength = 1, ErrorMessage = "{0} should be between 1 to 50 characters")]
308308
public string DefaultSources { get; set; }
309309

310-
[DisplayName("Mastadon Server")]
310+
[DisplayName("Mastodon Server")]
311311
[Description("")]
312312
[DataType(DataType.Url, ErrorMessage = "Invalid URL format")]
313313
public string MastodonServerUrl { get; set; }
314314

315-
[DisplayName("Mastadon Account (@username)")]
315+
[DisplayName("Mastodon Account (@username)")]
316316
[Description("")]
317317
[RegularExpression("(@)((?:[A-Za-z0-9-_]*))")]
318318
public string MastodonAccount { get; set; }
319319

320+
[DisplayName("Pin this Post to the Home Page")]
321+
[Description("")]
322+
[DataType(DataType.Text, ErrorMessage = "Invalid Guid format")]
323+
public string PostPinnedToHomePage { get; set; }
320324

321325
public bool EntryTitleAsLink { get; set; }
322326
public bool ObfuscateEmail { get; set; }

Diff for: source/DasBlog.Web.UI/Views/Admin/Settings.cshtml

+15
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,21 @@
157157

158158
</div>
159159

160+
<div class="form-group row mb-3">
161+
162+
@Html.LabelFor(m => @Model.SiteConfig.PostPinnedToHomePage, null, new { @class = "col-form-label col-sm-2" })
163+
164+
<div class="col-sm-3">
165+
@Html.DropDownListFor(n => n.SiteConfig.PostPinnedToHomePage,
166+
new SelectList(new BlogPostListViewModel().Init(Model.Posts), "Id", "Name"),
167+
new { @class = "form-select col-1" })
168+
</div>
169+
170+
171+
@Html.ValidationMessageFor(m => m.SiteConfig.PostPinnedToHomePage, null, new { @class = "text-danger" })
172+
173+
</div>
174+
160175
<div class="form-check row mb-3">
161176
<div class="col-sm-10 offset-sm-2">
162177
<div class="col-sm-2">

0 commit comments

Comments
 (0)