mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-26 23:49:02 -07:00
Radicade Public Code
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace RadicadeServer.RecNet.Relationships
|
||||
{
|
||||
public class RealRelationship
|
||||
{
|
||||
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public long RelationshipId { get; set; }
|
||||
public long AccountId { get; set; }
|
||||
public long PlayerId { get; set; }
|
||||
public RelationshipType RelationshipType { get; set; }
|
||||
public ReciprocalStatus Muted { get; set; }
|
||||
public ReciprocalStatus Ignored { get; set; }
|
||||
public ReciprocalStatus Favorited { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace RadicadeServer.RecNet.Relationships
|
||||
{
|
||||
public enum ReciprocalStatus
|
||||
{
|
||||
None,
|
||||
Local,
|
||||
Remote,
|
||||
Mutual
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace RadicadeServer.RecNet.Relationships
|
||||
{
|
||||
public class Relationship
|
||||
{
|
||||
[JsonPropertyName("Id")]
|
||||
public long Id { get; set; }
|
||||
|
||||
[JsonPropertyName("PlayerID")]
|
||||
public long PlayerID { get; set; }
|
||||
|
||||
[JsonPropertyName("RelationshipType")]
|
||||
public RelationshipType RelationshipType { get; set; }
|
||||
|
||||
[JsonPropertyName("Favorited")]
|
||||
public ReciprocalStatus Favorited { get; set; }
|
||||
|
||||
[JsonPropertyName("Muted")]
|
||||
public ReciprocalStatus Muted { get; set; }
|
||||
|
||||
[JsonPropertyName("Ignored")]
|
||||
public ReciprocalStatus Ignored { get; set; }
|
||||
|
||||
public Relationship() { }
|
||||
|
||||
public Relationship(RealRelationship realRelationship)
|
||||
{
|
||||
Id = realRelationship.RelationshipId;
|
||||
PlayerID = realRelationship.PlayerId;
|
||||
RelationshipType = realRelationship.RelationshipType;
|
||||
Muted = realRelationship.Muted;
|
||||
Ignored = realRelationship.Ignored;
|
||||
Favorited = realRelationship.Favorited;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace RadicadeServer.RecNet.Relationships
|
||||
{
|
||||
public enum RelationshipType
|
||||
{
|
||||
None,
|
||||
FriendRequestSent,
|
||||
FriendRequestReceived,
|
||||
Friend
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Runtime.CompilerServices;
|
||||
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
|
||||
{
|
||||
public static class RelationshipsShared
|
||||
{
|
||||
internal static IDbContextFactory<RecDBContext> DbFactory = null!;
|
||||
|
||||
internal static async Task<RealRelationship> GetOrCreate(RecDBContext db, long accountId, long playerId)
|
||||
{
|
||||
var rel = await db.Relationships.FirstOrDefaultAsync(x => x.AccountId == accountId && x.PlayerId == playerId);
|
||||
if (rel == null)
|
||||
{
|
||||
rel = new RealRelationship { AccountId = accountId, PlayerId = playerId };
|
||||
await db.Relationships.AddAsync(rel);
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
return rel;
|
||||
}
|
||||
|
||||
internal static void SetReciprocalStatus(
|
||||
RealRelationship mine, RealRelationship theirs,
|
||||
Func<RealRelationship, ReciprocalStatus> getter,
|
||||
Action<RealRelationship, ReciprocalStatus> setter,
|
||||
bool enabling)
|
||||
{
|
||||
if (enabling)
|
||||
{
|
||||
var theirStatus = getter(theirs);
|
||||
bool theyHaveIt = theirStatus == ReciprocalStatus.Local || theirStatus == ReciprocalStatus.Mutual;
|
||||
setter(mine, theyHaveIt ? ReciprocalStatus.Mutual : ReciprocalStatus.Local);
|
||||
setter(theirs, theyHaveIt ? ReciprocalStatus.Mutual : ReciprocalStatus.Remote);
|
||||
}
|
||||
else
|
||||
{
|
||||
var theirStatus = getter(theirs);
|
||||
setter(mine, ReciprocalStatus.None);
|
||||
setter(theirs, theirStatus == ReciprocalStatus.Mutual ? ReciprocalStatus.Remote : ReciprocalStatus.None);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Text.Json.Nodes;
|
||||
using RadicadeServer.API.Notifications;
|
||||
using RadicadeServer.DB;
|
||||
using RadicadeServer.Extensions;
|
||||
using RadicadeServer.RecNet.Enums.Messages;
|
||||
using RadicadeServer.RecNet.Notifications;
|
||||
using RadicadeServer.RecNet.Moderation;
|
||||
using RadicadeServer.RecNet.Relationships;
|
||||
|
||||
namespace RadicadeServer.API.Relationships.V1;
|
||||
|
||||
[Route("api/relationships/v1")]
|
||||
public class RelationshipsV1(RecDBContext db) : Controller
|
||||
{
|
||||
|
||||
[HttpPost("bulkignoreplatformusers")]
|
||||
public IActionResult BulkIgnorePlatformUsers()
|
||||
{
|
||||
return Ok(Array.Empty<object>());
|
||||
}
|
||||
|
||||
[HttpPost("~/api/relationships/sendfriendintroductions"), Authorize]
|
||||
public async Task<IActionResult> SendFriendIntroductions()
|
||||
{
|
||||
var userId = User.GetUserId();
|
||||
if (userId == null)
|
||||
return Unauthorized();
|
||||
|
||||
var (toPlayerIds, aboutPlayerId) = await ReadIntroductionFieldsAsync();
|
||||
aboutPlayerId = aboutPlayerId > 0 ? aboutPlayerId : userId.Value;
|
||||
|
||||
_ = toPlayerIds;
|
||||
return Ok(new OkResponse { Success = true });
|
||||
}
|
||||
|
||||
private async Task<(List<long> ToPlayerIds, long AboutPlayerId)> ReadIntroductionFieldsAsync()
|
||||
{
|
||||
var toPlayerIds = new List<long>();
|
||||
long aboutPlayerId = 0;
|
||||
|
||||
void AddIds(JsonNode? node)
|
||||
{
|
||||
if (node is JsonValue value && long.TryParse(value.ToString(), out var single))
|
||||
{
|
||||
toPlayerIds.Add(single);
|
||||
return;
|
||||
}
|
||||
if (node is not JsonArray array) return;
|
||||
foreach (var entry in array)
|
||||
if (long.TryParse(entry?.ToString(), out var id))
|
||||
toPlayerIds.Add(id);
|
||||
}
|
||||
|
||||
void ReadObject(JsonObject body)
|
||||
{
|
||||
foreach (var pair in body)
|
||||
{
|
||||
if (pair.Key.Equals("ToPlayerIds", StringComparison.OrdinalIgnoreCase) ||
|
||||
pair.Key.Equals("PlayerIds", StringComparison.OrdinalIgnoreCase) ||
|
||||
pair.Key.Equals("PlayerId", StringComparison.OrdinalIgnoreCase) ||
|
||||
pair.Key.Equals("Id", StringComparison.OrdinalIgnoreCase))
|
||||
AddIds(pair.Value);
|
||||
else if (pair.Key.Equals("AboutPlayerId", StringComparison.OrdinalIgnoreCase) ||
|
||||
pair.Key.Equals("AccountId", StringComparison.OrdinalIgnoreCase))
|
||||
long.TryParse(pair.Value?.ToString(), out aboutPlayerId);
|
||||
}
|
||||
}
|
||||
|
||||
var request = Request;
|
||||
if (request != null)
|
||||
{
|
||||
foreach (var key in new[] { "toPlayerIds", "ToPlayerIds", "playerIds", "PlayerIds", "playerId", "PlayerId", "id", "Id" })
|
||||
if (request.Query.TryGetValue(key, out var query))
|
||||
foreach (var raw in query)
|
||||
if (long.TryParse(raw, out var queryId))
|
||||
toPlayerIds.Add(queryId);
|
||||
|
||||
foreach (var key in new[] { "aboutPlayerId", "AboutPlayerId", "accountId", "AccountId" })
|
||||
if (aboutPlayerId <= 0 && request.Query.TryGetValue(key, out var queryAbout) &&
|
||||
long.TryParse(queryAbout.FirstOrDefault(), out var parsedAbout))
|
||||
aboutPlayerId = parsedAbout;
|
||||
|
||||
if (request.ContentLength is > 0 && request.Body != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
request.EnableBuffering();
|
||||
var node = await JsonNode.ParseAsync(request.Body);
|
||||
if (node is JsonObject bodyObject) ReadObject(bodyObject);
|
||||
else AddIds(node);
|
||||
request.Body.Position = 0;
|
||||
}
|
||||
catch
|
||||
{
|
||||
try { request.Body.Position = 0; } catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (toPlayerIds, aboutPlayerId);
|
||||
}
|
||||
|
||||
[HttpGet("relationshipwith/{playerId:long}"), Authorize]
|
||||
public async Task<IActionResult> RelationshipWith(long playerId)
|
||||
{
|
||||
var userId = User.GetUserId();
|
||||
if (userId == null)
|
||||
return Unauthorized();
|
||||
|
||||
var row = await db.Relationships.AsNoTracking()
|
||||
.FirstOrDefaultAsync(x => x.AccountId == userId && x.PlayerId == playerId);
|
||||
if (row != null)
|
||||
return Ok(new Relationship(row));
|
||||
|
||||
return Ok(new Relationship(new RealRelationship { AccountId = (long)userId, PlayerId = playerId }));
|
||||
}
|
||||
|
||||
[HttpGet("mutualfriends"), Authorize]
|
||||
[HttpGet("~/api/relationships/mutualfriends"), Authorize]
|
||||
public async Task<List<object>> MutualFriends([FromQuery] long id)
|
||||
{
|
||||
var userId = User.GetUserId();
|
||||
if (userId == null || id <= 0 || id == userId)
|
||||
return new List<object>();
|
||||
|
||||
var mine = await db.Relationships.AsNoTracking()
|
||||
.Where(x => x.AccountId == userId && x.RelationshipType == RelationshipType.Friend)
|
||||
.Select(x => x.PlayerId)
|
||||
.ToListAsync();
|
||||
var theirs = await db.Relationships.AsNoTracking()
|
||||
.Where(x => x.AccountId == id && x.RelationshipType == RelationshipType.Friend)
|
||||
.Select(x => x.PlayerId)
|
||||
.ToListAsync();
|
||||
|
||||
var mutual = mine.Intersect(theirs).OrderBy(x => x).Take(50).ToList();
|
||||
if (mutual.Count == 0)
|
||||
return new List<object>();
|
||||
|
||||
var accounts = await db.Profiles.AsNoTracking()
|
||||
.Where(p => mutual.Contains(p.accountId))
|
||||
.ToDictionaryAsync(p => p.accountId);
|
||||
|
||||
return mutual
|
||||
.Where(accounts.ContainsKey)
|
||||
.Select(accountId => (object)new
|
||||
{
|
||||
AccountId = accountId,
|
||||
Username = accounts[accountId].username,
|
||||
DisplayName = accounts[accountId].displayName,
|
||||
ProfileImage = accounts[accountId].profileImage ?? ""
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
|
||||
[HttpGet("favorite"), Authorize]
|
||||
[HttpPost("favorite"), Authorize]
|
||||
[HttpGet("favorite/{id:long}"), Authorize]
|
||||
[HttpPost("favorite/{id:long}"), Authorize]
|
||||
public async Task<Relationship> Favorite()
|
||||
=> await ToggleFlag(await ReadTargetPlayerIdAsync(), r => r.Favorited, (r, v) => r.Favorited = v, true);
|
||||
|
||||
[HttpGet("unfavorite"), Authorize]
|
||||
[HttpPost("unfavorite"), Authorize]
|
||||
[HttpGet("unfavorite/{id:long}"), Authorize]
|
||||
[HttpPost("unfavorite/{id:long}"), Authorize]
|
||||
public async Task<Relationship> Unfavorite()
|
||||
=> await ToggleFlag(await ReadTargetPlayerIdAsync(), r => r.Favorited, (r, v) => r.Favorited = v, false);
|
||||
|
||||
private static readonly string[] TargetIdKeys =
|
||||
{
|
||||
"id", "Id", "playerId", "PlayerId", "targetPlayerId", "TargetPlayerId",
|
||||
"accountId", "AccountId", "otherPlayerId", "OtherPlayerId"
|
||||
};
|
||||
|
||||
private async Task<long> ReadTargetPlayerIdAsync()
|
||||
{
|
||||
var routeData = RouteData;
|
||||
if (routeData != null &&
|
||||
routeData.Values.TryGetValue("id", out var route) &&
|
||||
long.TryParse(route?.ToString(), out var routeId) && routeId > 0)
|
||||
return routeId;
|
||||
|
||||
var request = Request;
|
||||
if (request == null) return 0;
|
||||
|
||||
foreach (var key in TargetIdKeys)
|
||||
if (request.Query.TryGetValue(key, out var query) &&
|
||||
long.TryParse(query.FirstOrDefault(), out var queryId) && queryId > 0)
|
||||
return queryId;
|
||||
|
||||
if (request.ContentLength is > 0 && request.Body != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
request.EnableBuffering();
|
||||
if (await JsonNode.ParseAsync(request.Body) is JsonObject body)
|
||||
foreach (var pair in body)
|
||||
if (TargetIdKeys.Contains(pair.Key, StringComparer.OrdinalIgnoreCase) &&
|
||||
long.TryParse(pair.Value?.ToString(), out var bodyId) && bodyId > 0)
|
||||
{
|
||||
request.Body.Position = 0;
|
||||
return bodyId;
|
||||
}
|
||||
|
||||
request.Body.Position = 0;
|
||||
}
|
||||
catch
|
||||
{
|
||||
try { request.Body.Position = 0; } catch { }
|
||||
}
|
||||
}
|
||||
|
||||
if (request.HasFormContentType)
|
||||
{
|
||||
var form = await request.ReadFormAsync();
|
||||
foreach (var key in TargetIdKeys)
|
||||
if (form.TryGetValue(key, out var formValue) &&
|
||||
long.TryParse(formValue.FirstOrDefault(), out var formId) && formId > 0)
|
||||
return formId;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
[HttpPost("mute"), Authorize]
|
||||
[HttpPost("mute/{id:long}"), Authorize]
|
||||
[HttpGet("mute"), Authorize]
|
||||
[HttpGet("mute/{id:long}"), Authorize]
|
||||
public async Task<IActionResult> Mute()
|
||||
=> await ToggleFlagActionResult(await ReadTargetPlayerIdAsync(), r => r.Muted, (r, v) => r.Muted = v, true);
|
||||
|
||||
[HttpPost("unmute"), Authorize]
|
||||
[HttpPost("unmute/{id:long}"), Authorize]
|
||||
[HttpGet("unmute"), Authorize]
|
||||
[HttpGet("unmute/{id:long}"), Authorize]
|
||||
public async Task<IActionResult> Unmute()
|
||||
=> await ToggleFlagActionResult(await ReadTargetPlayerIdAsync(), r => r.Muted, (r, v) => r.Muted = v, false);
|
||||
|
||||
[HttpPost("ignore"), Authorize]
|
||||
[HttpPost("ignore/{id:long}"), Authorize]
|
||||
[HttpGet("ignore"), Authorize]
|
||||
[HttpGet("ignore/{id:long}"), Authorize]
|
||||
public async Task<IActionResult> Ignore()
|
||||
=> await ToggleFlagActionResult(await ReadTargetPlayerIdAsync(), r => r.Ignored, (r, v) => r.Ignored = v, true);
|
||||
|
||||
[HttpPost("unignore"), Authorize]
|
||||
[HttpPost("unignore/{id:long}"), Authorize]
|
||||
[HttpGet("unignore"), Authorize]
|
||||
[HttpGet("unignore/{id:long}"), Authorize]
|
||||
public async Task<IActionResult> Unignore()
|
||||
=> await ToggleFlagActionResult(await ReadTargetPlayerIdAsync(), r => r.Ignored, (r, v) => r.Ignored = v, false);
|
||||
|
||||
private async Task<Relationship> ToggleFlag(long id, Func<RealRelationship, ReciprocalStatus> getter, Action<RealRelationship, ReciprocalStatus> setter, bool enabling)
|
||||
{
|
||||
var userId = (long)User.GetUserId()!;
|
||||
|
||||
var mine = await RelationshipsShared.GetOrCreate(db, userId, id);
|
||||
var theirs = await RelationshipsShared.GetOrCreate(db, id, userId);
|
||||
|
||||
RelationshipsShared.SetReciprocalStatus(mine, theirs, getter, setter, enabling);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
await Notify.SendNotify(userId, new Notification { Id = Notifs.RelationshipChanged, Msg = new Relationship(mine) });
|
||||
await Notify.SendNotify(id, new Notification { Id = Notifs.RelationshipChanged, Msg = new Relationship(theirs) });
|
||||
|
||||
return new Relationship(mine);
|
||||
}
|
||||
|
||||
private async Task<IActionResult> ToggleFlagActionResult(long id, Func<RealRelationship, ReciprocalStatus> getter, Action<RealRelationship, ReciprocalStatus> setter, bool enabling)
|
||||
{
|
||||
if (id <= 0)
|
||||
return BadRequest(new { error = "invalid player id" });
|
||||
return Ok(await ToggleFlag(id, getter, setter, enabling));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user