mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-05-25 10:22:26 +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
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using LBPUnion.ProjectLighthouse.Types.Profiles;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
// ReSharper disable RouteTemplates.ActionRoutePrefixCanBeExtractedToControllerRoute
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Controllers.Api;
|
||||
|
||||
/// <summary>
|
||||
|
@ -36,4 +39,21 @@ public class UserEndpoints : ApiEndpointController
|
|||
|
||||
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.Threading.Tasks;
|
||||
using System.Xml.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using LBPUnion.ProjectLighthouse.Types.Levels;
|
||||
|
@ -39,7 +38,6 @@ public class CommentController : ControllerBase
|
|||
return this.Ok();
|
||||
}
|
||||
|
||||
|
||||
[HttpGet("comments/user/{slotId:int}")]
|
||||
[HttpGet("userComments/{username}")]
|
||||
public async Task<IActionResult> GetComments([FromQuery] int pageStart, [FromQuery] int pageSize, string? username, int? slotId)
|
||||
|
@ -55,24 +53,24 @@ public class CommentController : ControllerBase
|
|||
type = CommentType.Profile;
|
||||
}
|
||||
|
||||
List<Comment> comments = await this.database.Comments
|
||||
.Include(c => c.Poster)
|
||||
List<Comment> comments = await this.database.Comments.Include
|
||||
(c => c.Poster)
|
||||
.Where(c => c.TargetId == targetId && c.Type == type)
|
||||
.OrderByDescending(c => c.Timestamp)
|
||||
.Skip(pageStart - 1)
|
||||
.Take(Math.Min(pageSize,
|
||||
30))
|
||||
.Take(Math.Min(pageSize, 30))
|
||||
.ToListAsync();
|
||||
|
||||
string outputXml = comments.Aggregate(string.Empty, (current, comment) => current +
|
||||
comment.Serialize(this.getReaction(user.UserId, comment.CommentId).Result));
|
||||
string outputXml = comments.Aggregate
|
||||
(string.Empty, (current, comment) => current + comment.Serialize(this.getReaction(user.UserId, comment.CommentId).Result));
|
||||
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);
|
||||
if (reaction == null) return 0;
|
||||
|
||||
return reaction.Rating;
|
||||
}
|
||||
|
||||
|
@ -84,7 +82,7 @@ public class CommentController : ControllerBase
|
|||
string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync();
|
||||
|
||||
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);
|
||||
|
||||
|
@ -112,6 +110,7 @@ public class CommentController : ControllerBase
|
|||
|
||||
Comment? comment = await this.database.Comments.FirstOrDefaultAsync(c => c.CommentId == commentId);
|
||||
if (comment == null) return this.NotFound();
|
||||
|
||||
// if you are not the poster
|
||||
if (comment.PosterUserId != user.UserId)
|
||||
{
|
||||
|
|
|
@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Mvc;
|
|||
namespace LBPUnion.ProjectLighthouse.Types;
|
||||
|
||||
[ApiController]
|
||||
[Route("/api/v1/")]
|
||||
[Route("/api/v1")]
|
||||
[Produces("application/json")]
|
||||
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.Linq;
|
||||
using System.Text.Json.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Types.Profiles;
|
||||
using LBPUnion.ProjectLighthouse.Types.Settings;
|
||||
|
@ -153,16 +152,10 @@ public class User
|
|||
#nullable enable
|
||||
[NotMapped]
|
||||
[JsonIgnore]
|
||||
public string Status {
|
||||
public UserStatus Status {
|
||||
get {
|
||||
using Database database = new();
|
||||
LastContact? lastMatch = database.LastContacts.Where
|
||||
(l => l.UserId == this.UserId)
|
||||
.FirstOrDefault(l => TimestampHelper.Timestamp - l.Timestamp < 300);
|
||||
|
||||
if (lastMatch == null) return "Offline";
|
||||
|
||||
return "Currently online on " + lastMatch.GameVersion.ToPrettyString();
|
||||
return new UserStatus(database, this.UserId);
|
||||
}
|
||||
}
|
||||
#nullable disable
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue