mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-25 23:18:56 -07:00
153 lines
5.7 KiB
C#
153 lines
5.7 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.V2;
|
|
|
|
[Route("api/relationships/v2")]
|
|
public class RelationshipsV2(RecDBContext db) : Controller
|
|
{
|
|
[HttpGet("get"), Authorize]
|
|
public async Task<List<Relationship>> GetRelationships()
|
|
{
|
|
var userId = User.GetUserId();
|
|
|
|
var rows = await db.Relationships.AsNoTracking()
|
|
.Where(x => x.AccountId == userId)
|
|
.ToListAsync();
|
|
|
|
return rows.Select(relationship => new Relationship(relationship)).ToList();
|
|
}
|
|
|
|
[HttpGet("friends"), Authorize]
|
|
public async Task<List<Relationship>> GetFriends()
|
|
{
|
|
var userId = User.GetUserId();
|
|
|
|
var rows = await db.Relationships.AsNoTracking()
|
|
.Where(x => x.AccountId == userId && x.RelationshipType == RelationshipType.Friend)
|
|
.OrderBy(x => x.RelationshipId)
|
|
.ToListAsync();
|
|
|
|
return rows.Select(relationship => new Relationship(relationship)).ToList();
|
|
}
|
|
|
|
[HttpGet("sendfriendrequest"), Authorize]
|
|
[HttpPost("sendfriendrequest"), 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)
|
|
return Ok(new Relationship(mine));
|
|
|
|
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));
|
|
}
|
|
|
|
[HttpGet("acceptfriendrequest"), Authorize]
|
|
[HttpPost("acceptfriendrequest"), Authorize]
|
|
public async Task<IActionResult> AcceptFriendRequest(long id)
|
|
{
|
|
var userId = User.GetUserId();
|
|
if (userId == null || id <= 0 || id == userId)
|
|
return BadRequest(new { error = "invalid player id" });
|
|
|
|
var mine = await db.Relationships
|
|
.FirstOrDefaultAsync(x => x.AccountId == userId && x.PlayerId == id);
|
|
var theirs = await db.Relationships
|
|
.FirstOrDefaultAsync(x => x.AccountId == id && x.PlayerId == userId);
|
|
|
|
if (mine == null || theirs == null ||
|
|
mine.RelationshipType != RelationshipType.FriendRequestReceived ||
|
|
theirs.RelationshipType != RelationshipType.FriendRequestSent)
|
|
{
|
|
return Ok(mine != null ? new Relationship(mine) : new Relationship());
|
|
}
|
|
|
|
mine.RelationshipType = RelationshipType.Friend;
|
|
theirs.RelationshipType = RelationshipType.Friend;
|
|
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.FriendRequestAccepted });
|
|
|
|
return Ok(new Relationship(mine));
|
|
}
|
|
|
|
[HttpGet("addfriend"), Authorize]
|
|
[HttpPost("addfriend"), Authorize]
|
|
public async Task<IActionResult> AddFriend(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.FriendRequestReceived || theirs.RelationshipType == RelationshipType.FriendRequestReceived)
|
|
{
|
|
mine.RelationshipType = RelationshipType.Friend;
|
|
theirs.RelationshipType = RelationshipType.Friend;
|
|
}
|
|
else 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));
|
|
}
|
|
|
|
[HttpGet("removefriend"), Authorize]
|
|
[HttpPost("removefriend"), 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));
|
|
}
|
|
}
|