LuskiServer/LuskiServer/Controllers/v1/SocketProfileController.cs
2024-11-18 23:23:48 -05:00

139 lines
6.5 KiB
C#

using System.Net.Mime;
using Asp.Versioning;
using Luski.Shared.PublicServers.V1.Enums;
using Luski.Shared.PublicServers.V1.ServerToClient.HTTP;
using LuskiServer.Classes;
using LuskiServer.Classes.TableDef;
using Microsoft.AspNetCore.Mvc;
namespace LuskiServer.Controllers.v1;
[ApiVersion(1)]
[ApiController]
public class SocketProfileController : ControllerBase
{
/// <summary>
/// Returns the Avatar for the channel profile.
/// </summary>
/// <param name="id">The ID of the requested profile</param>
/// <returns></returns>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(STC), StatusCodes.Status403Forbidden)]
[Produces(MediaTypeNames.Image.Jpeg, MediaTypeNames.Image.Gif, MediaTypeNames.Image.Tiff, "image/png", MediaTypeNames.Application.Json)]
[Route(LuskiFunctions.Info.Routes.Default.Base + "/Avatar/{id:long}")]
public IActionResult? Avatar(long id)
{
if (Tables.Profiles.TryRead(Profiles.Picture, out byte[]? image, Profiles.ID.CreateParameter(id)))
{
return Tables.Profiles.Read(Profiles.PictureType, Profiles.ID.CreateParameter(id)) switch
{
PictureType.png => File(image, "image/png"),
PictureType.jpeg => File(image, "image/jpeg"),
PictureType.bmp => File(image, "image/bmp"),
PictureType.gif => File(image, "image/gif"),
PictureType.ico => File(image, "image/vnd.microsoft.icon"),
PictureType.svg => File(image, "image/svg+xml"),
PictureType.tif => File(image, "image/tiff"),
PictureType.webp => File(image, "image/webp"),
//should never happen
_ => File(image, "image/png"),
};
}
else
{
return StatusCode(403, new STC()
{
Error = ErrorCode.Forbidden,
ErrorMessage = "the user you have given does not exist"
});
}
}
[HttpGet]
[Route(LuskiFunctions.Info.Routes.Default.Base)]
public IActionResult Get([FromHeader(Name = "id")]long Person)
{
try
{
if (!this.CanTokenRequest(out long ID, out long SID, out IActionResult? toc) && toc != null) return toc;
if (!Tables.Profiles.TryReadRow(out ProfileRow PR, Profiles.ID.CreateParameter(Person))) return this.ShowError(ErrorCode.Forbidden, "Profile does not exist");
return this.ResponseToResult(new ProfileSTC()
{
ID = PR.ID,
DisplayName = PR.DisplayName,
PictureType = PR.PictureType,
Controllers = Tables.ProfileControllers.ReadColumn(ProfileControllers.User, ProfileControllers.Profile.CreateParameter(Person)),
RoleControllers = Tables.ProfileRoleControllers.ReadColumn(ProfileRoleControllers.Role, ProfileRoleControllers.Profile.CreateParameter(Person)),
Channels = Tables.ChannelProfiles.ReadColumn(ChannelProfiles.Channel, ChannelProfiles.Profile.CreateParameter(Person)),
Categories = Tables.CategoryProfiles.ReadColumn(CategoryProfiles.Category, CategoryProfiles.Profile.CreateParameter(Person)),
});
}
catch (Exception e)
{
return this.ShowError(e);
}
}
[HttpGet]
[Route(LuskiFunctions.Info.Routes.Default.Base + "/myprofiles")]
public IActionResult GetMyProfiles()
{
try
{
if (!this.CanTokenRequest(out long ID, out long SID, out IActionResult? toc) && toc != null) return toc;
Dictionary<long, ProfileSTC> Vals = new();
if (Tables.ProfileControllers.CreateCommand()
.WithFilter(ProfileControllers.User, ID)
.WithCrossTableCheck(ProfileControllers.Profile, Tables.Profiles, Classes.TableDef.Profiles.ID)
.TryReadRows(out ProfileRow[]? PcRs))
{
foreach (ProfileRow PR in PcRs)
{
Vals.Add(PR.ID, new()
{
ID = PR.ID,
DisplayName = PR.DisplayName,
PictureType = PR.PictureType,
Controllers = Tables.ProfileControllers.ReadColumn(ProfileControllers.User, ProfileControllers.Profile.CreateParameter(PR.ID)),
RoleControllers = Tables.ProfileRoleControllers.ReadColumn(ProfileRoleControllers.Role, ProfileRoleControllers.Profile.CreateParameter(PR.ID)),
Channels = Tables.ChannelProfiles.ReadColumn(ChannelProfiles.Channel, ChannelProfiles.Profile.CreateParameter(PR.ID)),
Categories = Tables.CategoryProfiles.ReadColumn(CategoryProfiles.Category, CategoryProfiles.Profile.CreateParameter(PR.ID)),
});
}
}
if (Tables.RoleMembers.CreateCommand()
.WithFilter(RoleMembers.User, ID)
.WithCrossTableCheck(RoleMembers.Role, Tables.ProfileRoleControllers, ProfileRoleControllers.Role)
.WithCrossTableCheck(ProfileRoleControllers.Profile, Tables.Profiles, Classes.TableDef.Profiles.ID)
.TryReadRows(out ProfileRow[]? rows))
{
foreach (var PR in rows)
{
if (!Vals.ContainsKey(PR.ID))
{
Vals.Add(PR.ID, new()
{
ID = PR.ID,
DisplayName = PR.DisplayName,
PictureType = PR.PictureType,
Controllers = Tables.ProfileControllers.ReadColumn(ProfileControllers.User, ProfileControllers.Profile.CreateParameter(PR.ID)),
RoleControllers = Tables.ProfileRoleControllers.ReadColumn(ProfileRoleControllers.Role, ProfileRoleControllers.Profile.CreateParameter(PR.ID)),
Channels = Tables.ChannelProfiles.ReadColumn(ChannelProfiles.Channel, ChannelProfiles.Profile.CreateParameter(PR.ID)),
Categories = Tables.CategoryProfiles.ReadColumn(CategoryProfiles.Category, CategoryProfiles.Profile.CreateParameter(PR.ID)),
});
}
}
}
return this.ResponseToResult(new ProfileListSTC()
{
Profiles = Vals.Values.ToList()
});
}
catch (Exception e)
{
return this.ShowError(e);
}
}
}