Skip to content

Commit

Permalink
Adds preview capabilities for new message/edit message/new thread. For
Browse files Browse the repository at this point in the history
  • Loading branch information
FransBouma committed Apr 7, 2021
1 parent b001e39 commit 28f4ff9
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 50 deletions.
99 changes: 76 additions & 23 deletions GuiCore/Controllers/MessageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ public async Task<ActionResult> Add(int threadId = 0, int messageIdToQuote = 0)
ThreadSubject = thread.Subject,
PageNo = 1,
LastMessageInThread = await ThreadGuiHelper.GetLastMessageInThreadDtoAsync(threadId),
PreviewTextAsHtml = string.Empty,
};
return View(messageData);
}
Expand All @@ -203,7 +204,7 @@ public async Task<ActionResult> Add([Bind(nameof(MessageData.MessageText), nameo
MessageData messageData, string submitButton,
int threadId = 0)
{
if(submitButton != "Post")
if(submitButton != "Post" && submitButton!= "Preview")
{
return threadId <= 0 ? RedirectToAction("Index", "Home") : RedirectToAction("Index", "Thread", new {threadId = threadId});
}
Expand All @@ -220,23 +221,49 @@ public async Task<ActionResult> Add([Bind(nameof(MessageData.MessageText), nameo
}

int newMessageId = 0;
if(submitButton == "Post")
switch(submitButton)
{
// allowed, proceed
// parse message text to html
var messageAsHtml = HnDGeneralUtils.TransformMarkdownToHtml(messageData.MessageText, ApplicationAdapter.GetEmojiFilenamesPerName(),
ApplicationAdapter.GetSmileyMappings());
var systemData = await _cache.GetSystemDataAsync();
var remoteIPAddress = HnDGeneralUtils.GetRemoteIPAddressAsIP4String(this.HttpContext.Connection.RemoteIpAddress);
newMessageId = await ThreadManager.CreateNewMessageInThreadAsync(threadId, this.HttpContext.Session.GetUserID(), messageData.MessageText, messageAsHtml,
remoteIPAddress, messageData.Subscribe,
ApplicationAdapter.GetEmailData(this.Request.Host.Host, EmailTemplate.ThreadUpdatedNotification),
systemData.SendReplyNotifications);
ApplicationAdapter.InvalidateCachedNumberOfThreadsInSupportQueues();
if(this.HttpContext.Session.CheckIfNeedsAuditing(AuditActions.AuditNewMessage))
case "Post":
{
await SecurityManager.AuditNewMessageAsync(this.HttpContext.Session.GetUserID(), newMessageId);
// allowed, proceed
// parse message text to html
var messageAsHtml = HnDGeneralUtils.TransformMarkdownToHtml(messageData.MessageText, ApplicationAdapter.GetEmojiFilenamesPerName(),
ApplicationAdapter.GetSmileyMappings());
var systemData = await _cache.GetSystemDataAsync();
var remoteIPAddress = HnDGeneralUtils.GetRemoteIPAddressAsIP4String(this.HttpContext.Connection.RemoteIpAddress);
newMessageId = await ThreadManager.CreateNewMessageInThreadAsync(threadId, this.HttpContext.Session.GetUserID(), messageData.MessageText, messageAsHtml,
remoteIPAddress, messageData.Subscribe,
ApplicationAdapter.GetEmailData(this.Request.Host.Host, EmailTemplate.ThreadUpdatedNotification),
systemData.SendReplyNotifications);
ApplicationAdapter.InvalidateCachedNumberOfThreadsInSupportQueues();
if(this.HttpContext.Session.CheckIfNeedsAuditing(AuditActions.AuditNewMessage))
{
await SecurityManager.AuditNewMessageAsync(this.HttpContext.Session.GetUserID(), newMessageId);
}

break;
}
case "Preview":
var forum = await _cache.GetForumAsync(thread.ForumID);
if(forum == null)
{
return RedirectToAction("Index", "Home");
}
var newMessageData = new MessageData()
{
MessageText = messageData.MessageText,
CurrentUserID = this.HttpContext.Session.GetUserID(),
ForumID = thread.ForumID,
ThreadID = thread.ThreadID,
ForumName = forum.ForumName,
SectionName = await _cache.GetSectionNameAsync(forum.SectionID),
ThreadSubject = thread.Subject,
PageNo = 1,
LastMessageInThread = await ThreadGuiHelper.GetLastMessageInThreadDtoAsync(threadId),
PreviewTextAsHtml = HnDGeneralUtils.TransformMarkdownToHtml(messageData.MessageText, ApplicationAdapter.GetEmojiFilenamesPerName(),
ApplicationAdapter.GetSmileyMappings()),
};
return View(newMessageData);
}

return await CalculateRedirectToMessageAsync(thread.ThreadID, newMessageId);
Expand All @@ -260,17 +287,43 @@ public async Task<ActionResult> Edit([Bind(nameof(MessageData.MessageText))]
return RedirectToAction("Index", "Home");
}

if(submitButton == "Post")
switch(submitButton)
{
// parse message text to html
var messageAsHtml = HnDGeneralUtils.TransformMarkdownToHtml(messageData.MessageText, ApplicationAdapter.GetEmojiFilenamesPerName(),
ApplicationAdapter.GetSmileyMappings());
await MessageManager.UpdateEditedMessageAsync(this.HttpContext.Session.GetUserID(), message.MessageID, messageData.MessageText, messageAsHtml,
this.Request.Host.Host, string.Empty);
if(this.HttpContext.Session.CheckIfNeedsAuditing(AuditActions.AuditAlteredMessage))
case "Post":
{
await SecurityManager.AuditAlteredMessageAsync(this.HttpContext.Session.GetUserID(), message.MessageID);
// parse message text to html
var messageAsHtml = HnDGeneralUtils.TransformMarkdownToHtml(messageData.MessageText, ApplicationAdapter.GetEmojiFilenamesPerName(),
ApplicationAdapter.GetSmileyMappings());
await MessageManager.UpdateEditedMessageAsync(this.HttpContext.Session.GetUserID(), message.MessageID, messageData.MessageText, messageAsHtml,
this.Request.Host.Host, string.Empty);
if(this.HttpContext.Session.CheckIfNeedsAuditing(AuditActions.AuditAlteredMessage))
{
await SecurityManager.AuditAlteredMessageAsync(this.HttpContext.Session.GetUserID(), message.MessageID);
}
break;
}

case "Preview":
var thread = message.Thread;
var forum = await _cache.GetForumAsync(thread.ForumID);
if(forum == null)
{
return RedirectToAction("Index", "Home");
}
var newMessageData = new MessageData()
{
MessageText = messageData.MessageText,
CurrentUserID = this.HttpContext.Session.GetUserID(),
ForumID = thread.ForumID,
ThreadID = thread.ThreadID,
ForumName = forum.ForumName,
SectionName = await _cache.GetSectionNameAsync(forum.SectionID),
ThreadSubject = thread.Subject,
PageNo = 1,
PreviewTextAsHtml = HnDGeneralUtils.TransformMarkdownToHtml(messageData.MessageText, ApplicationAdapter.GetEmojiFilenamesPerName(),
ApplicationAdapter.GetSmileyMappings()),
};
return View(newMessageData);
}

return await CalculateRedirectToMessageAsync(message.ThreadID, message.MessageID);
Expand Down
62 changes: 41 additions & 21 deletions GuiCore/Controllers/ThreadController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,9 @@ public async Task<ActionResult> Add(int forumId = 0)
[ValidateAntiForgeryToken]
[HttpPost]
public async Task<ActionResult> Add([Bind(nameof(NewThreadData.MessageText), nameof(NewThreadData.ThreadSubject), nameof(NewThreadData.IsSticky),
nameof(NewThreadData.Subscribe))]
NewThreadData newThreadData, string submitButton, int forumId = 0)
nameof(NewThreadData.Subscribe))] NewThreadData newThreadData, string submitButton, int forumId = 0)
{
if(submitButton != "Post")
if(submitButton != "Post" && submitButton!="Preview")
{
// apparently canceled
if(forumId <= 0)
Expand All @@ -342,32 +341,53 @@ public async Task<ActionResult> Add([Bind(nameof(NewThreadData.MessageText), nam
return RedirectToAction("Index", "Home");
}

var (userMayAddThread, forum, userMayAddStickThread) = await PerformAddThreadSecurityChecksAsync(forumId);
var (userMayAddThread, forum, userMayAddStickyThread) = await PerformAddThreadSecurityChecksAsync(forumId);
if(!userMayAddThread)
{
return RedirectToAction("Index", "Home");
}

int newThreadId = 0;
if(submitButton == "Post")
{
// allowed, proceed
// parse message text to html
var messageAsHtml = HnDGeneralUtils.TransformMarkdownToHtml(newThreadData.MessageText, ApplicationAdapter.GetEmojiFilenamesPerName(),
ApplicationAdapter.GetSmileyMappings());
var (newThreadIdFromCall, newMessageId) = await ForumManager.CreateNewThreadInForumAsync(forumId, this.HttpContext.Session.GetUserID(),
newThreadData.ThreadSubject, newThreadData.MessageText,
messageAsHtml, userMayAddStickThread && newThreadData.IsSticky,
this.Request.Host.Host, forum.DefaultSupportQueueID,
newThreadData.Subscribe);
newThreadId = newThreadIdFromCall;
ApplicationAdapter.InvalidateCachedNumberOfThreadsInSupportQueues();
if(this.HttpContext.Session.CheckIfNeedsAuditing(AuditActions.AuditNewThread))
switch(submitButton)
{
case "Post":
{
await SecurityManager.AuditNewThreadAsync(this.HttpContext.Session.GetUserID(), newThreadId);
// allowed, proceed
// parse message text to html
var messageAsHtml = HnDGeneralUtils.TransformMarkdownToHtml(newThreadData.MessageText, ApplicationAdapter.GetEmojiFilenamesPerName(),
ApplicationAdapter.GetSmileyMappings());
var (newThreadIdFromCall, newMessageId) = await ForumManager.CreateNewThreadInForumAsync(forumId, this.HttpContext.Session.GetUserID(),
newThreadData.ThreadSubject, newThreadData.MessageText,
messageAsHtml, userMayAddStickyThread && newThreadData.IsSticky,
this.Request.Host.Host, forum.DefaultSupportQueueID,
newThreadData.Subscribe);
newThreadId = newThreadIdFromCall;
ApplicationAdapter.InvalidateCachedNumberOfThreadsInSupportQueues();
if(this.HttpContext.Session.CheckIfNeedsAuditing(AuditActions.AuditNewThread))
{
await SecurityManager.AuditNewThreadAsync(this.HttpContext.Session.GetUserID(), newThreadId);
}

_cache.Remove(CacheManager.ProduceCacheKey(CacheKeys.SingleForum, forumId));
break;
}

_cache.Remove(CacheManager.ProduceCacheKey(CacheKeys.SingleForum, forumId));
case "Preview":
var newThreadDataPreview = new NewThreadData()
{
CurrentUserID = this.HttpContext.Session.GetUserID(),
ForumID = forumId,
ForumName = forum.ForumName,
SectionName = await _cache.GetSectionNameAsync(forum.SectionID),
MessageText = newThreadData.MessageText,
PreviewTextAsHtml = HnDGeneralUtils.TransformMarkdownToHtml(newThreadData.MessageText, ApplicationAdapter.GetEmojiFilenamesPerName(),
ApplicationAdapter.GetSmileyMappings()),
ThreadSubject = newThreadData.ThreadSubject,
IsSticky = newThreadData.IsSticky && userMayAddStickyThread,
UserCanAddStickyThread = userMayAddStickyThread,
NewThreadWelcomeTextAsHTML = forum.NewThreadWelcomeTextAsHTML,
Subscribe = newThreadData.Subscribe,
};
return View(newThreadDataPreview);
}

return Redirect(this.Url.Action("Index", "Thread", new {threadId = newThreadId, pageNo = 1}));
Expand Down
1 change: 1 addition & 0 deletions GuiCore/Models/MessageData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ public class MessageData
public string MessageText { get; set; }
public bool Subscribe { get; set; }
public MessageInThreadDto LastMessageInThread { get; set; }
public string PreviewTextAsHtml { get; set; }
}
}
19 changes: 18 additions & 1 deletion GuiCore/Views/Message/Add.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,29 @@
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@await Html.PartialAsync("MarkdownEditor", Model)
<div class="mt20">
<input type="submit" name="submitButton" value="Post" class="btn btn-primary btn-default"/>&nbsp;&nbsp;&nbsp;
<input type="submit" name="submitButton" value="Post" class="btn btn-primary btn-default"/>&nbsp;&nbsp;
<input type="submit" name="submitButton" value="Preview" class="btn btn-default" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" name="submitButton" formnovalidate value="Cancel" class="btn btn-default" />
</div>
}
</div>
</div>
@if(!string.IsNullOrWhiteSpace(Model.PreviewTextAsHtml))
{
<div class="row ps ps-lastmessage-header">
<div class="col-sm-12 fs-header fs-header-slim">
Preview
</div>
<div class="col-sm-12 plr0">
<div class="ps-message">
<div class="ps-message-content" id="MessagePreview">
@Html.Raw(Model.PreviewTextAsHtml)
</div>
</div>
</div>
</div>
}

@if(Model.LastMessageInThread != null)
{
var currentMessage = Model.LastMessageInThread;
Expand Down
21 changes: 18 additions & 3 deletions GuiCore/Views/Message/Edit.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,32 @@
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@await Html.PartialAsync("MarkdownEditor", Model)
<div class="mt20">
<input type="submit" name="submitButton" value="Post" class="btn btn-primary btn-default" />&nbsp;&nbsp;&nbsp;
<input type="submit" name="submitButton" value="Cancel" class="btn btn-default" />
<input type="submit" name="submitButton" value="Post" class="btn btn-primary btn-default"/>&nbsp;&nbsp;
<input type="submit" name="submitButton" value="Preview" class="btn btn-default" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" name="submitButton" formnovalidate value="Cancel" class="btn btn-default" />
</div>
}
</div>
</div>
@if(!string.IsNullOrWhiteSpace(Model.PreviewTextAsHtml))
{
<div class="row ps ps-lastmessage-header">
<div class="col-sm-12 fs-header fs-header-slim">
Preview
</div>
<div class="col-sm-12 plr0">
<div class="ps-message">
<div class="ps-message-content" id="MessagePreview">
@Html.Raw(Model.PreviewTextAsHtml)
</div>
</div>
</div>
</div>
}
</div>
</section>



@section Scripts {
@await Html.PartialAsync("MarkdownEditor_JS")
}
19 changes: 17 additions & 2 deletions GuiCore/Views/Thread/Add.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,31 @@
<div class="col-sm-12">
@await Html.PartialAsync("MarkdownEditor", Model)
<div class="mt20">
<input type="submit" name="submitButton" value="Post" class="btn btn-primary btn-default" />&nbsp;&nbsp;&nbsp;
<input type="submit" name="submitButton" value="Post" class="btn btn-primary btn-default"/>&nbsp;&nbsp;
<input type="submit" name="submitButton" value="Preview" class="btn btn-default" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" name="submitButton" formnovalidate value="Cancel" class="btn btn-default" />
</div>
</div>
</div>
}
@if(!string.IsNullOrWhiteSpace(Model.PreviewTextAsHtml))
{
<div class="row ps ps-lastmessage-header">
<div class="col-sm-12 fs-header fs-header-slim">
Preview
</div>
<div class="col-sm-12 plr0">
<div class="ps-message">
<div class="ps-message-content" id="MessagePreview">
@Html.Raw(Model.PreviewTextAsHtml)
</div>
</div>
</div>
</div>
}
</div>
</section>


@section Scripts {
@await Html.PartialAsync("MarkdownEditor_JS")
}

0 comments on commit 28f4ff9

Please sign in to comment.