Files
2026-10-16 23:33:13 -07:00

63 lines
2.4 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using RadicadeServer.API.Notifications;
using RadicadeServer.DB;
using RadicadeServer.Extensions;
using RadicadeServer.RecNet.Enums.Messages;
using RadicadeServer.RecNet.Messages;
using RadicadeServer.RecNet.Notifications;
using RadicadeServer.RecNet.Relationships;
namespace RadicadeServer.API.Relationships.V3;
[Route("api/relationships/v3")]
public class RelationshipsV3(RecDBContext db) : Controller
{
[HttpPost("{id:long}"), Authorize]
public async Task<IActionResult> SendFriendRequest(long id)
{
var userId = User.GetUserId();
if (userId == null || id <= 0 || id == userId)
return BadRequest(new { error = "invalid player id" });
var mine = await RelationshipsShared.GetOrCreate(db, (long)userId, id);
var theirs = await RelationshipsShared.GetOrCreate(db, id, (long)userId);
if (mine.RelationshipType == RelationshipType.None &&
theirs.RelationshipType == RelationshipType.None)
{
mine.RelationshipType = RelationshipType.FriendRequestSent;
theirs.RelationshipType = RelationshipType.FriendRequestReceived;
await db.SaveChangesAsync();
await Notify.SendNotify((long)userId, new Notification { Id = Notifs.RelationshipChanged, Msg = new Relationship(mine) });
await Notify.SendNotify(id, new Notification { Id = Notifs.RelationshipChanged, Msg = new Relationship(theirs) });
await Notify.SendMessage(id, new Message { FromPlayerId = (long)userId, Type = MessageType.FriendInvite });
}
return Ok(new Relationship(mine));
}
[HttpDelete("{id:long}"), Authorize]
[HttpPost("{id:long}/remove"), Authorize]
public async Task<IActionResult> RemoveFriend(long id)
{
var userId = User.GetUserId();
if (userId == null || id <= 0 || id == userId)
return BadRequest(new { error = "invalid player id" });
var mine = await RelationshipsShared.GetOrCreate(db, (long)userId, id);
var theirs = await RelationshipsShared.GetOrCreate(db, id, (long)userId);
mine.RelationshipType = RelationshipType.None;
theirs.RelationshipType = RelationshipType.None;
await db.SaveChangesAsync();
await Notify.SendNotify((long)userId, new Notification { Id = Notifs.RelationshipChanged, Msg = new Relationship(mine) });
await Notify.SendNotify(id, new Notification { Id = Notifs.RelationshipChanged, Msg = new Relationship(theirs) });
return Ok(new Relationship(mine));
}
}