Add npData and myFriends support

This commit is contained in:
jvyden 2021-11-08 14:49:39 -05:00
commit 06c8b5fc44
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
4 changed files with 124 additions and 0 deletions

View file

@ -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">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb"&gt;&lt;ExtraRule Prefix="" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateConstants/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</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>

View 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));
}
}
}

View 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();
}
}

View 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; }
}
}