Luski.Net/Luski.net/Structures/Main/MainSocketAppUser.cs
JacobTech d8fbf281d0 Server Separation.
I moved the two server types to their own classes to prevent API calls to a server, not of that type.
2023-07-08 09:06:13 -04:00

197 lines
6.4 KiB
C#
Executable File

using System;
using Luski.net.Interfaces;
using Luski.net.JsonTypes.BaseTypes;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
using System.Threading.Tasks;
using JacobTechEncryption;
using Luski.net.Enums;
using Luski.net.Enums.Main;
using Luski.net.JsonTypes;
namespace Luski.net.Structures.Main;
public class MainSocketAppUser : MainSocketUserBase, IAppUser
{
[JsonPropertyName("selected_channel")]
[JsonInclude]
public long SelectedChannel { get; internal set; } = default!;
[JsonPropertyName("username")]
[JsonInclude]
public string Username { get; internal set; } = default!;
[JsonPropertyName("flags")]
[JsonInclude]
public UserFlag Flags { get; internal set; } = default!;
public MainServer Server { get; internal set; } = default!;
[JsonIgnore]
public IReadOnlyList<MainSocketChannel> Channels
{
get
{
if (_Channels is null || ChannelIdList is not null)
{
if (ChannelIdList.Length != 0)
{
_Channels = new List<MainSocketChannel>();
foreach (long channel in ChannelIdList)
{
MainSocketChannel s = Server.GetChannel(channel,
MainSocketChannelContext.Default.MainSocketChannel, CancellationToken.None).Result;
Server.chans.Remove(s);
switch (s.Type)
{
case ChannelType.GROUP:
_Channels.Add(Server.GetChannel(channel,
MainSocketGroupChannelContext.Default.MainSocketGroupChannel, CancellationToken.None).Result);
break;
case ChannelType.DM:
_Channels.Add(Server.GetChannel(channel,
MainSocketDMChannelContext.Default.MainSocketDMChannel, CancellationToken.None).Result);
break;
}
}
}
else _Channels = new List<MainSocketChannel>();
}
return _Channels.AsReadOnly();
}
}
[JsonIgnore]
public IReadOnlyList<MainSocketRemoteUser> FriendRequests
{
get
{
if (_FriendRequests is null || FriendRequestsRaw is not null)
{
_FriendRequests = new();
if (ChannelIdList.Length != 0 && FriendRequestsRaw is not null)
{
foreach (FR person in FriendRequestsRaw)
{
//_Friends.Add(SocketRemoteUser.GetUser(person));
long id = person.user_id == Id ? person.from : person.user_id;
MainSocketRemoteUser frq = Server.GetUser<MainSocketRemoteUser>(id, CancellationToken.None).Result;
_FriendRequests.Add(frq);
}
}
else _FriendRequests = new();
}
return _FriendRequests.AsReadOnly();
}
}
[JsonIgnore]
public IReadOnlyList<MainSocketRemoteUser> Friends
{
get
{
if (_Friends is null || FriendIdList is not null)
{
if (ChannelIdList.Length != 0)
{
_Friends = new List<MainSocketRemoteUser>();
foreach (long person in FriendIdList)
{
_Friends.Add(Server.GetUser<MainSocketRemoteUser>(person, CancellationToken.None).Result);
}
}
else _Friends = new List<MainSocketRemoteUser>();
}
return _Friends.AsReadOnly();
}
}
[JsonPropertyName("channels")]
[JsonInclude]
public long[] ChannelIdList { get; internal set; } = default!;
[JsonPropertyName("friends")]
[JsonInclude]
public long[] FriendIdList { get; internal set; } = default!;
[JsonPropertyName("friend_requests")]
[JsonInclude]
public FR[] FriendRequestsRaw { get; internal set; } = default!;
[JsonIgnore]
private List<MainSocketChannel> _Channels = default!;
[JsonIgnore]
private List<MainSocketRemoteUser> _Friends = default!;
[JsonIgnore]
private List<MainSocketRemoteUser> _FriendRequests = default!;
public class FR
{
public long from { get; set; } = default!;
public long user_id { get; set; } = default!;
}
internal void AddFriend(MainSocketRemoteUser User)
{
if (Server.poeople.Any(s => s.Id == User.Id))
{
IEnumerable<IUser> b = Server.poeople.Where(s => s.Id == User.Id);
foreach (IUser item in b)
{
Server.poeople.Remove(item);
}
Server.poeople.Add(User);
}
else
{
Server.poeople.Add(User);
}
_Friends.Add(User);
}
internal void RemoveFriendRequest(MainSocketRemoteUser User)
{
if (Server.poeople.Any(s => s.Id == User.Id))
{
IEnumerable<IUser> b = Server.poeople.Where(s => s.Id == User.Id);
foreach (IUser item in b)
{
Server.poeople.Remove(item);
}
}
Server.poeople.Add(User);
foreach (MainSocketRemoteUser user in _FriendRequests)
{
if (User.Id == user.Id)
{
_FriendRequests.Remove(User);
}
}
}
internal void AddFriendRequest(MainSocketRemoteUser User)
{
if (Server.poeople.Any(s => s.Id == User.Id))
{
IEnumerable<IUser> b = Server.poeople.Where(s => s.Id == User.Id);
foreach (IUser item in b)
{
Server.poeople.Remove(item);
}
Server.poeople.Add(User);
}
else
{
Server.poeople.Add(User);
}
_FriendRequests.Add(User);
}
}
[JsonSerializable(typeof(MainSocketAppUser))]
[JsonSourceGenerationOptions(
GenerationMode = JsonSourceGenerationMode.Default,
PropertyNamingPolicy = JsonKnownNamingPolicy.Unspecified,
WriteIndented = false,
DefaultIgnoreCondition = JsonIgnoreCondition.Never)]
internal partial class MainSocketAppUserContext : JsonSerializerContext
{
}