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 Channels { get { if (_Channels is null || ChannelIdList is not null) { if (ChannelIdList.Length != 0) { _Channels = new List(); 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(); } return _Channels.AsReadOnly(); } } [JsonIgnore] public IReadOnlyList 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(id, CancellationToken.None).Result; _FriendRequests.Add(frq); } } else _FriendRequests = new(); } return _FriendRequests.AsReadOnly(); } } [JsonIgnore] public IReadOnlyList Friends { get { if (_Friends is null || FriendIdList is not null) { if (ChannelIdList.Length != 0) { _Friends = new List(); foreach (long person in FriendIdList) { _Friends.Add(Server.GetUser(person, CancellationToken.None).Result); } } else _Friends = new List(); } 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 _Channels = default!; [JsonIgnore] private List _Friends = default!; [JsonIgnore] private List _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 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 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 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 { }