using System; using System.Collections.Generic; using System.Linq; using System.Text.Json.Serialization.Metadata; using System.Threading; using System.Threading.Tasks; using Luski.net.Enums; using Luski.net.Interfaces; using Luski.net.JsonTypes.BaseTypes; using Luski.net.JsonTypes.HTTP; using Luski.net.Structures.Main; using Luski.net.Structures.Public; namespace Luski.net; public partial class MainServer : Server { public MainSocketAppUser User { get; internal set; } = default!; public async Task GetChannel(long Channel, CancellationToken CancellationToken) where TChannel : MainSocketChannel, new() { TChannel Return = new(); switch (Return) { case MainSocketDMChannel: Return = (await GetChannel(Channel, MainSocketDMChannelContext.Default.MainSocketDMChannel, CancellationToken) as TChannel)!; break; case MainSocketGroupChannel: Return = (await GetChannel(Channel, MainSocketGroupChannelContext.Default.MainSocketGroupChannel, CancellationToken) as TChannel)!; break; case MainSocketTextChannel: Return = (await GetChannel(Channel, MainSocketTextChannelContext.Default.MainSocketTextChannel, CancellationToken) as TChannel)!; break; case MainSocketChannel: Return = (await GetChannel(Channel, MainSocketChannelContext.Default.MainSocketChannel, CancellationToken) as TChannel)!; break; case null: throw new NullReferenceException(nameof(TChannel)); default: throw new Exception("Unknown channel type"); } return Return; } internal async Task GetChannel(long id, JsonTypeInfo Json, CancellationToken CancellationToken) where TChannel : MainSocketChannel, new() { TChannel request; if (chans.Count > 0 && chans.Any(s => s.Id == id)) { return chans.Where(s => s is TChannel && s.Id == id).Cast().FirstOrDefault()!; } while (true) { if (CanRequest) { request = await GetFromServer($"SocketChannel/Get/{id}", Json, CancellationToken); break; } } if (request is null) throw new Exception("Something was wrong with the server responce"); if (request.Error is null) { if (chans.Count > 0 && chans.Any(s => s.Id == request.Id)) { foreach (MainSocketChannel? p in chans.Where(s => s.Id == request.Id)) { chans.Remove(p); } } chans.Add(request); return request; } throw request.Error switch { ErrorCode.InvalidToken => new Exception("Your current token is no longer valid"), ErrorCode.Forbidden => new Exception("The server rejected your request"), ErrorCode.ServerError => new Exception("Error from server: " + request.ErrorMessage), ErrorCode.InvalidURL or ErrorCode.MissingHeader => new Exception(request.ErrorMessage), _ => new Exception($"Unknown data: '{request.ErrorMessage}'"), }; } public Task GetUser(long UserID, CancellationToken CancellationToken) where Tuser : MainSocketUserBase, new() { Tuser user = new(); switch (user) { case MainSocketAppUser: user = (GetUser(UserID, MainSocketAppUserContext.Default.MainSocketAppUser, CancellationToken).Result as Tuser)!; break; case MainSocketUserBase: user = (GetUser(UserID, MainSocketUserBaseContext.Default.MainSocketUserBase, CancellationToken).Result as Tuser)!; break; case null: throw new NullReferenceException(nameof(Tuser)); default: throw new Exception("Unknown channel type"); } return Task.FromResult(user); } public async Task GetMessage(long id, CancellationToken CancellationToken) { MainSocketMessage message; while (true) { if (CanRequest) { message = await GetFromServer("socketmessage", MainSocketMessageContext.Default.MainSocketMessage, CancellationToken, new System.Collections.Generic.KeyValuePair("msg_id", id.ToString())); break; } } if (message is not null) return message; throw new Exception("Server did not return a message"); } /// /// Sends the server a request to update the of you account /// /// The you want to set your status to /// public async Task UpdateStatus(UserStatus Status, CancellationToken CancellationToken) { IncomingHTTP? data = await SendServer("SocketUserProfile/Status", new Status() { UserStatus = Status }, StatusContext.Default.Status, IncomingHTTPContext.Default.IncomingHTTP, CancellationToken); if (data.Error is not null && ((int)data.StatusCode < 200 || (int)data.StatusCode > 299)) { if (data?.ErrorMessage is not null) throw new Exception(data.ErrorMessage); if (data?.Error is not null) throw new Exception(((int)data.Error).ToString()); else throw new Exception("Something went worng"); } User.Status = Status; return Task.CompletedTask; } internal async Task GetUser(long UserId, JsonTypeInfo Json, CancellationToken CancellationToken) where Tuser : MainSocketUserBase, new() { Tuser user; if (poeople.Count > 0 && poeople.Any(s => s.Id == UserId)) { Tuser temp = poeople.Where(s => s is Tuser && s.Id == UserId).Cast().FirstOrDefault()!; return temp; } while (true) { if (CanRequest) { user = await GetFromServer("socketuser", Json, CancellationToken, new KeyValuePair("id", UserId.ToString())); break; } } if (user is null) throw new Exception("Server did not return a user"); if (poeople.Count > 0 && poeople.Any(s => s.Id == UserId)) { foreach (IUser? p in poeople.Where(s => s.Id == UserId)) { poeople.Remove(p); } } poeople.Add(user); return user; } }