Add comment logging, move comment submissions to POST request

Closes #185
This commit is contained in:
jvyden 2022-02-22 16:25:46 -05:00
commit c8595f5522
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
4 changed files with 45 additions and 24 deletions

View file

@ -1,5 +1,7 @@
#nullable enable #nullable enable
using System.Threading.Tasks; using System.Threading.Tasks;
using Kettu;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Types; using LBPUnion.ProjectLighthouse.Types;
using LBPUnion.ProjectLighthouse.Types.Levels; using LBPUnion.ProjectLighthouse.Types.Levels;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -34,15 +36,20 @@ public class SlotPageController : ControllerBase
return this.Redirect($"~/slot/{id}#{commentId}"); return this.Redirect($"~/slot/{id}#{commentId}");
} }
[HttpGet("postComment")] [HttpPost("postComment")]
public async Task<IActionResult> PostComment([FromRoute] int id, [FromQuery] string? msg) public async Task<IActionResult> PostComment([FromRoute] int id, [FromForm] string? msg)
{ {
User? user = this.database.UserFromWebRequest(this.Request); User? user = this.database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login"); if (user == null) return this.Redirect("~/login");
if (msg == null) return this.Redirect("~/slot/" + id); if (msg == null)
{
Logger.Log($"Refusing to post comment from {user.UserId} on user {id}, {nameof(msg)} is null", LoggerLevelComments.Instance);
return this.Redirect("~/slot/" + id);
}
await this.database.PostComment(user, id, CommentType.Level, msg); await this.database.PostComment(user, id, CommentType.Level, msg);
Logger.Log($"Posted comment from {user.UserId}: \"{msg}\" on user {id}", LoggerLevelComments.Instance);
return this.Redirect("~/slot/" + id); return this.Redirect("~/slot/" + id);
} }

View file

@ -1,10 +1,9 @@
#nullable enable #nullable enable
using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Kettu;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Types; using LBPUnion.ProjectLighthouse.Types;
using LBPUnion.ProjectLighthouse.Types.Profiles;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace LBPUnion.ProjectLighthouse.Controllers.Website; namespace LBPUnion.ProjectLighthouse.Controllers.Website;
@ -20,20 +19,6 @@ public class UserPageController : ControllerBase
this.database = database; this.database = database;
} }
[HttpGet("heart")]
public async Task<IActionResult> HeartUser([FromRoute] int id)
{
User? user = this.database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login");
User? heartedUser = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == id);
if (heartedUser == null) return this.NotFound();
await this.database.HeartUser(user, heartedUser);
return this.Redirect("~/user/" + id);
}
[HttpGet("rateComment")] [HttpGet("rateComment")]
public async Task<IActionResult> RateComment([FromRoute] int id, [FromQuery] int? commentId, [FromQuery] int? rating) public async Task<IActionResult> RateComment([FromRoute] int id, [FromQuery] int? commentId, [FromQuery] int? rating)
{ {
@ -45,15 +30,34 @@ public class UserPageController : ControllerBase
return this.Redirect($"~/user/{id}#{commentId}"); return this.Redirect($"~/user/{id}#{commentId}");
} }
[HttpGet("postComment")] [HttpPost("postComment")]
public async Task<IActionResult> PostComment([FromRoute] int id, [FromQuery] string? msg) public async Task<IActionResult> PostComment([FromRoute] int id, [FromForm] string? msg)
{ {
User? user = this.database.UserFromWebRequest(this.Request); User? user = this.database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login"); if (user == null) return this.Redirect("~/login");
if (msg == null) return this.Redirect("~/user/" + id); if (msg == null)
{
Logger.Log($"Refusing to post comment from {user.UserId} on user {id}, {nameof(msg)} is null", LoggerLevelComments.Instance);
return this.Redirect("~/user/" + id);
}
await this.database.PostComment(user, id, CommentType.Profile, msg); await this.database.PostComment(user, id, CommentType.Profile, msg);
Logger.Log($"Posted comment from {user.UserId}: \"{msg}\" on user {id}", LoggerLevelComments.Instance);
return this.Redirect("~/user/" + id);
}
[HttpGet("heart")]
public async Task<IActionResult> HeartUser([FromRoute] int id)
{
User? user = this.database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login");
User? heartedUser = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == id);
if (heartedUser == null) return this.NotFound();
await this.database.HeartUser(user, heartedUser);
return this.Redirect("~/user/" + id); return this.Redirect("~/user/" + id);
} }

View file

@ -63,6 +63,12 @@ public class LoggerLevelInflux : LoggerLevel
public override string Name => "Influx"; public override string Name => "Influx";
} }
public class LoggerLevelComments : LoggerLevel
{
public static readonly LoggerLevelComments Instance = new();
public override string Name => "Comments";
}
public class LoggerLevelAspNet : LoggerLevel public class LoggerLevelAspNet : LoggerLevel
{ {

View file

@ -30,12 +30,13 @@
@if (Model.CommentsEnabled && Model.User != null) @if (Model.CommentsEnabled && Model.User != null)
{ {
<div class="ui divider"></div> <div class="ui divider"></div>
<form class="ui reply form" action="@Url.RouteUrl(ViewContext.RouteData.Values)/postComment"> <form class="ui reply form" action="@Url.RouteUrl(ViewContext.RouteData.Values)/postComment" method="post">
<div class="field"> <div class="field">
<textarea style="min-height: 70px; height: 70px; max-height:120px" name="msg"></textarea> <textarea style="min-height: 70px; height: 70px; max-height:120px" name="msg"></textarea>
</div> </div>
<input type="submit" class="ui blue button"> <input type="submit" class="ui blue button">
</form> </form>
<br>
} }
@for(int i = 0; i < Model.Comments.Count; i++) @for(int i = 0; i < Model.Comments.Count; i++)
@ -44,9 +45,12 @@
DateTimeOffset timestamp = DateTimeOffset.FromUnixTimeSeconds(comment.Timestamp / 1000); DateTimeOffset timestamp = DateTimeOffset.FromUnixTimeSeconds(comment.Timestamp / 1000);
StringWriter messageWriter = new(); StringWriter messageWriter = new();
HttpUtility.HtmlDecode(comment.getComment(), messageWriter); HttpUtility.HtmlDecode(comment.getComment(), messageWriter);
string decodedMessage = messageWriter.ToString(); string decodedMessage = messageWriter.ToString();
string url = Url.RouteUrl(ViewContext.RouteData.Values); string url = Url.RouteUrl(ViewContext.RouteData.Values);
int rating = comment.ThumbsUp - comment.ThumbsDown; int rating = comment.ThumbsUp - comment.ThumbsDown;
<div style="display: flex" id="@comment.CommentId"> <div style="display: flex" id="@comment.CommentId">
<div class="voting"> <div class="voting">
<a href="@url/rateComment?commentId=@(comment.CommentId)&rating=@(comment.YourThumb == 1 ? 0 : 1)"> <a href="@url/rateComment?commentId=@(comment.CommentId)&rating=@(comment.YourThumb == 1 ? 0 : 1)">