mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-08 12:58:41 +00:00
Add npData and myFriends support
This commit is contained in:
parent
f0a3170531
commit
06c8b5fc44
4 changed files with 124 additions and 0 deletions
|
@ -72,6 +72,7 @@
|
|||
<s:String x:Key="/Default/CodeStyle/CSharpVarKeywordUsage/ForOtherTypes/@EntryValue">UseExplicitType</s:String>
|
||||
<s:String x:Key="/Default/CodeStyle/CSharpVarKeywordUsage/ForSimpleTypes/@EntryValue">UseExplicitType</s:String>
|
||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=MM/@EntryIndexedValue">MM</s:String>
|
||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=NP/@EntryIndexedValue">NP</s:String>
|
||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Method/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb"><ExtraRule Prefix="" Suffix="" Style="aaBb" /></Policy></s:String>
|
||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateConstants/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /></s:String>
|
||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /></s:String>
|
||||
|
@ -97,6 +98,7 @@
|
|||
<s:Boolean x:Key="/Default/UserDictionary/Words/=luckydip/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=mmpick/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=mmpicks/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=npdata/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=NPEA/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=NPHG/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=PCAS/@EntryIndexedValue">True</s:Boolean>
|
||||
|
|
92
ProjectLighthouse/Controllers/FriendsController.cs
Normal file
92
ProjectLighthouse/Controllers/FriendsController.cs
Normal file
|
@ -0,0 +1,92 @@
|
|||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using LBPUnion.ProjectLighthouse.Types.Profiles;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("LITTLEBIGPLANETPS3_XML/")]
|
||||
public class FriendsController : ControllerBase
|
||||
{
|
||||
private readonly Database database;
|
||||
|
||||
public FriendsController(Database database)
|
||||
{
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
[HttpPost("npdata")]
|
||||
public async Task<IActionResult> NPData()
|
||||
{
|
||||
User? user = await this.database.UserFromRequest(this.Request);
|
||||
if (user == null) return this.StatusCode(403, "");
|
||||
|
||||
this.Request.Body.Position = 0;
|
||||
string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync();
|
||||
|
||||
XmlSerializer serializer = new(typeof(NPData));
|
||||
NPData? npData = (NPData?)serializer.Deserialize(new StringReader(bodyString));
|
||||
if (npData == null) return this.BadRequest();
|
||||
|
||||
List<int> friends = new();
|
||||
foreach (string friendName in npData.Friends)
|
||||
{
|
||||
User? friend = await this.database.Users.FirstOrDefaultAsync(u => u.Username == friendName);
|
||||
if (friend == null) continue;
|
||||
|
||||
friends.Add(friend.UserId);
|
||||
}
|
||||
|
||||
List<int> blockedUsers = new();
|
||||
foreach (string blockedUserName in npData.BlockedUsers)
|
||||
{
|
||||
User? blockedUser = await this.database.Users.FirstOrDefaultAsync(u => u.Username == blockedUserName);
|
||||
if (blockedUser == null) continue;
|
||||
|
||||
blockedUsers.Add(blockedUser.UserId);
|
||||
}
|
||||
|
||||
if (FriendHelper.FriendIdsByUserId.ContainsKey(user.UserId))
|
||||
{
|
||||
FriendHelper.FriendIdsByUserId.Remove(user.UserId);
|
||||
FriendHelper.BlockedIdsByUserId.Remove(user.UserId);
|
||||
}
|
||||
|
||||
FriendHelper.FriendIdsByUserId.Add(user.UserId, friends.ToArray());
|
||||
FriendHelper.BlockedIdsByUserId.Add(user.UserId, blockedUsers.ToArray());
|
||||
|
||||
return this.Ok();
|
||||
}
|
||||
|
||||
[HttpGet("myFriends")]
|
||||
public async Task<IActionResult> MyFriends()
|
||||
{
|
||||
User? user = await this.database.UserFromRequest(this.Request);
|
||||
if (user == null) return this.StatusCode(403, "");
|
||||
|
||||
if (!FriendHelper.FriendIdsByUserId.TryGetValue(user.UserId, out int[]? friendIds) || friendIds == null)
|
||||
{
|
||||
return this.NotFound();
|
||||
}
|
||||
|
||||
string friends = "";
|
||||
foreach (int friendId in friendIds)
|
||||
{
|
||||
User? friend = await this.database.Users.Include(u => u.Location).FirstOrDefaultAsync(u => u.UserId == friendId);
|
||||
if (friend == null) continue;
|
||||
|
||||
friends += friend.Serialize();
|
||||
}
|
||||
|
||||
return this.Ok(LbpSerializer.StringElement("myFriends", friends));
|
||||
}
|
||||
}
|
||||
}
|
12
ProjectLighthouse/Helpers/FriendHelper.cs
Normal file
12
ProjectLighthouse/Helpers/FriendHelper.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Helpers
|
||||
{
|
||||
[NotMapped]
|
||||
public static class FriendHelper
|
||||
{
|
||||
public static Dictionary<int, int[]> FriendIdsByUserId = new();
|
||||
public static Dictionary<int, int[]> BlockedIdsByUserId = new();
|
||||
}
|
||||
}
|
18
ProjectLighthouse/Types/Profiles/NPData.cs
Normal file
18
ProjectLighthouse/Types/Profiles/NPData.cs
Normal file
|
@ -0,0 +1,18 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Types.Profiles
|
||||
{
|
||||
[XmlRoot("npdata")]
|
||||
[XmlType("npdata")]
|
||||
public class NPData
|
||||
{
|
||||
[XmlArray("friends")]
|
||||
[XmlArrayItem("npHandle")]
|
||||
public List<string> Friends { get; set; }
|
||||
|
||||
[XmlArray("blocked")]
|
||||
[XmlArrayItem("npHandle")]
|
||||
public List<string> BlockedUsers { get; set; }
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue