mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-05-25 18:22:27 +00:00
Add user status api endpoint
This commit is contained in:
parent
8fdd464e2e
commit
2b521be45c
6 changed files with 83 additions and 22 deletions
|
@ -1,10 +1,13 @@
|
||||||
#nullable enable
|
#nullable enable
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using LBPUnion.ProjectLighthouse.Types;
|
using LBPUnion.ProjectLighthouse.Types;
|
||||||
|
using LBPUnion.ProjectLighthouse.Types.Profiles;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
// ReSharper disable RouteTemplates.ActionRoutePrefixCanBeExtractedToControllerRoute
|
||||||
|
|
||||||
namespace LBPUnion.ProjectLighthouse.Controllers.Api;
|
namespace LBPUnion.ProjectLighthouse.Controllers.Api;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -36,4 +39,21 @@ public class UserEndpoints : ApiEndpointController
|
||||||
|
|
||||||
return this.Ok(user);
|
return this.Ok(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a user and their information from the database.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The ID of the user</param>
|
||||||
|
/// <returns>The user's status</returns>
|
||||||
|
/// <response code="200">The user's status, if successful.</response>
|
||||||
|
/// <response code="404">The user could not be found.</response>
|
||||||
|
[HttpGet("user/{id:int}/status")]
|
||||||
|
[ProducesResponseType(typeof(UserStatus), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
public async Task<IActionResult> GetUserStatus(int id)
|
||||||
|
{
|
||||||
|
UserStatus userStatus = new(this.database, id);
|
||||||
|
|
||||||
|
return this.Ok(userStatus);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -5,7 +5,6 @@ using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using LBPUnion.ProjectLighthouse.Helpers;
|
|
||||||
using LBPUnion.ProjectLighthouse.Serialization;
|
using LBPUnion.ProjectLighthouse.Serialization;
|
||||||
using LBPUnion.ProjectLighthouse.Types;
|
using LBPUnion.ProjectLighthouse.Types;
|
||||||
using LBPUnion.ProjectLighthouse.Types.Levels;
|
using LBPUnion.ProjectLighthouse.Types.Levels;
|
||||||
|
@ -39,14 +38,13 @@ public class CommentController : ControllerBase
|
||||||
return this.Ok();
|
return this.Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[HttpGet("comments/user/{slotId:int}")]
|
[HttpGet("comments/user/{slotId:int}")]
|
||||||
[HttpGet("userComments/{username}")]
|
[HttpGet("userComments/{username}")]
|
||||||
public async Task<IActionResult> GetComments([FromQuery] int pageStart, [FromQuery] int pageSize, string? username, int? slotId)
|
public async Task<IActionResult> GetComments([FromQuery] int pageStart, [FromQuery] int pageSize, string? username, int? slotId)
|
||||||
{
|
{
|
||||||
User? user = await this.database.UserFromGameRequest(this.Request);
|
User? user = await this.database.UserFromGameRequest(this.Request);
|
||||||
if (user == null) return this.StatusCode(403, "");
|
if (user == null) return this.StatusCode(403, "");
|
||||||
|
|
||||||
int targetId = slotId.GetValueOrDefault();
|
int targetId = slotId.GetValueOrDefault();
|
||||||
CommentType type = CommentType.Level;
|
CommentType type = CommentType.Level;
|
||||||
if (!string.IsNullOrWhiteSpace(username))
|
if (!string.IsNullOrWhiteSpace(username))
|
||||||
|
@ -55,24 +53,24 @@ public class CommentController : ControllerBase
|
||||||
type = CommentType.Profile;
|
type = CommentType.Profile;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Comment> comments = await this.database.Comments
|
List<Comment> comments = await this.database.Comments.Include
|
||||||
.Include(c => c.Poster)
|
(c => c.Poster)
|
||||||
.Where(c => c.TargetId == targetId && c.Type == type)
|
.Where(c => c.TargetId == targetId && c.Type == type)
|
||||||
.OrderByDescending(c => c.Timestamp)
|
.OrderByDescending(c => c.Timestamp)
|
||||||
.Skip(pageStart - 1)
|
.Skip(pageStart - 1)
|
||||||
.Take(Math.Min(pageSize,
|
.Take(Math.Min(pageSize, 30))
|
||||||
30))
|
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
string outputXml = comments.Aggregate(string.Empty, (current, comment) => current +
|
string outputXml = comments.Aggregate
|
||||||
comment.Serialize(this.getReaction(user.UserId, comment.CommentId).Result));
|
(string.Empty, (current, comment) => current + comment.Serialize(this.getReaction(user.UserId, comment.CommentId).Result));
|
||||||
return this.Ok(LbpSerializer.StringElement("comments", outputXml));
|
return this.Ok(LbpSerializer.StringElement("comments", outputXml));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<int> getReaction(int userId, int commentId)
|
private async Task<int> getReaction(int userId, int commentId)
|
||||||
{
|
{
|
||||||
Reaction? reaction = await this.database.Reactions.FirstOrDefaultAsync(r => r.UserId == userId && r.TargetId == commentId);
|
Reaction? reaction = await this.database.Reactions.FirstOrDefaultAsync(r => r.UserId == userId && r.TargetId == commentId);
|
||||||
if (reaction == null) return 0;
|
if (reaction == null) return 0;
|
||||||
|
|
||||||
return reaction.Rating;
|
return reaction.Rating;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,11 +78,11 @@ public class CommentController : ControllerBase
|
||||||
[HttpPost("postComment/user/{slotId:int}")]
|
[HttpPost("postComment/user/{slotId:int}")]
|
||||||
public async Task<IActionResult> PostComment(string? username, int? slotId)
|
public async Task<IActionResult> PostComment(string? username, int? slotId)
|
||||||
{
|
{
|
||||||
this.Request.Body.Position = 0;
|
this.Request.Body.Position = 0;
|
||||||
string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync();
|
string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync();
|
||||||
|
|
||||||
XmlSerializer serializer = new(typeof(Comment));
|
XmlSerializer serializer = new(typeof(Comment));
|
||||||
Comment? comment = (Comment?) serializer.Deserialize(new StringReader(bodyString));
|
Comment? comment = (Comment?)serializer.Deserialize(new StringReader(bodyString));
|
||||||
|
|
||||||
CommentType type = (slotId.GetValueOrDefault() == 0 ? CommentType.Profile : CommentType.Level);
|
CommentType type = (slotId.GetValueOrDefault() == 0 ? CommentType.Profile : CommentType.Level);
|
||||||
|
|
||||||
|
@ -112,6 +110,7 @@ public class CommentController : ControllerBase
|
||||||
|
|
||||||
Comment? comment = await this.database.Comments.FirstOrDefaultAsync(c => c.CommentId == commentId);
|
Comment? comment = await this.database.Comments.FirstOrDefaultAsync(c => c.CommentId == commentId);
|
||||||
if (comment == null) return this.NotFound();
|
if (comment == null) return this.NotFound();
|
||||||
|
|
||||||
// if you are not the poster
|
// if you are not the poster
|
||||||
if (comment.PosterUserId != user.UserId)
|
if (comment.PosterUserId != user.UserId)
|
||||||
{
|
{
|
||||||
|
|
|
@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||||
namespace LBPUnion.ProjectLighthouse.Types;
|
namespace LBPUnion.ProjectLighthouse.Types;
|
||||||
|
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("/api/v1/")]
|
[Route("/api/v1")]
|
||||||
[Produces("application/json")]
|
[Produces("application/json")]
|
||||||
public class ApiEndpointController : ControllerBase
|
public class ApiEndpointController : ControllerBase
|
||||||
{}
|
{}
|
7
ProjectLighthouse/Types/Profiles/StatusType.cs
Normal file
7
ProjectLighthouse/Types/Profiles/StatusType.cs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
namespace LBPUnion.ProjectLighthouse.Types.Profiles;
|
||||||
|
|
||||||
|
public enum StatusType
|
||||||
|
{
|
||||||
|
Offline = 0,
|
||||||
|
Online = 1,
|
||||||
|
}
|
42
ProjectLighthouse/Types/Profiles/UserStatus.cs
Normal file
42
ProjectLighthouse/Types/Profiles/UserStatus.cs
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
#nullable enable
|
||||||
|
using System.Linq;
|
||||||
|
using LBPUnion.ProjectLighthouse.Helpers;
|
||||||
|
|
||||||
|
namespace LBPUnion.ProjectLighthouse.Types.Profiles;
|
||||||
|
|
||||||
|
public class UserStatus
|
||||||
|
{
|
||||||
|
public StatusType StatusType { get; set; }
|
||||||
|
public GameVersion? CurrentVersion { get; set; }
|
||||||
|
|
||||||
|
public UserStatus()
|
||||||
|
{}
|
||||||
|
|
||||||
|
public UserStatus(Database database, int userId)
|
||||||
|
{
|
||||||
|
LastContact? lastContact = database.LastContacts.Where(l => l.UserId == userId).FirstOrDefault(l => TimestampHelper.Timestamp - l.Timestamp < 300);
|
||||||
|
|
||||||
|
if (lastContact == null)
|
||||||
|
{
|
||||||
|
StatusType = StatusType.Offline;
|
||||||
|
CurrentVersion = null;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
StatusType = StatusType.Online;
|
||||||
|
CurrentVersion = lastContact.GameVersion;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
CurrentVersion ??= GameVersion.Unknown;
|
||||||
|
|
||||||
|
return this.StatusType switch
|
||||||
|
{
|
||||||
|
StatusType.Online => $"Currently online on {((GameVersion)this.CurrentVersion).ToPrettyString()}",
|
||||||
|
StatusType.Offline => "Offline",
|
||||||
|
_ => "Unknown",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,7 +2,6 @@ using System.ComponentModel.DataAnnotations.Schema;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
using LBPUnion.ProjectLighthouse.Helpers;
|
|
||||||
using LBPUnion.ProjectLighthouse.Serialization;
|
using LBPUnion.ProjectLighthouse.Serialization;
|
||||||
using LBPUnion.ProjectLighthouse.Types.Profiles;
|
using LBPUnion.ProjectLighthouse.Types.Profiles;
|
||||||
using LBPUnion.ProjectLighthouse.Types.Settings;
|
using LBPUnion.ProjectLighthouse.Types.Settings;
|
||||||
|
@ -153,16 +152,10 @@ public class User
|
||||||
#nullable enable
|
#nullable enable
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public string Status {
|
public UserStatus Status {
|
||||||
get {
|
get {
|
||||||
using Database database = new();
|
using Database database = new();
|
||||||
LastContact? lastMatch = database.LastContacts.Where
|
return new UserStatus(database, this.UserId);
|
||||||
(l => l.UserId == this.UserId)
|
|
||||||
.FirstOrDefault(l => TimestampHelper.Timestamp - l.Timestamp < 300);
|
|
||||||
|
|
||||||
if (lastMatch == null) return "Offline";
|
|
||||||
|
|
||||||
return "Currently online on " + lastMatch.GameVersion.ToPrettyString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#nullable disable
|
#nullable disable
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue