Skip to content

Commit

Permalink
Make api internal
Browse files Browse the repository at this point in the history
  • Loading branch information
exp111 committed Nov 28, 2023
1 parent cccefba commit d13087e
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Turbulence.Core/ViewModels/ChannelListViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public async void Receive(ServerSelectedMsg m)
// Calculate permissions //TODO: move into client
var everyoneRole = m.Server.Roles.First(r => r.Id == m.Server.Id);
// TODO: Get rid of API call
var myGuildMember = await Api.GetCurrentUserGuildMember(_client.HttpClient, m.Server.Id);
var myGuildMember = await _client.GetCurrentUserGuildMember(m.Server.Id);
var myRoles = m.Server.Roles.Where(r => myGuildMember.Roles.Contains(r.Id)).ToList();
// Parse base permission for everyone
var perms = BigInteger.Parse(everyoneRole.Permissions);
Expand Down Expand Up @@ -129,7 +129,7 @@ public async void Receive(ServerSelectedMsg m)

public void Receive(DMsSelectedMsg message)
{
//reverse sort by last message id
// reverse sort by last message id
message.Channels.Sort((a, b) => b.LastMessageId == null ? -1 : b.LastMessageId.CompareTo(a.LastMessageId));
Channels.ReplaceAll(message.Channels);
}
Expand Down
6 changes: 3 additions & 3 deletions Turbulence.Core/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ public void Receive(ServerSelectedMsg message)
public void Receive(SearchClosedMsg message) => SearchOpen = false;

public async void Receive(SendMessageMsg message) =>
await Api.CreateAndSendMessage(_client.HttpClient, SelectedChannel!, message.Message, message.Reply, message.ShouldPing);
await _client.SendMessage(SelectedChannel!, message.Message, message.Reply, message.ShouldPing);

public async void Receive(EditMessageMsg message) =>
await Api.EditMessage(_client.HttpClient, message.Message, message.Original);
await _client.EditMessage(message.Message, message.Original);

public async void Receive(DeleteMessageMsg message) =>
await Api.DeleteMessage(_client.HttpClient, message.Message);
await _client.DeleteMessage(message.Message);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Turbulence.Core/ViewModels/MessagesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task RequestMoreMessages(bool older = true)
{
var last = CurrentMessages.Last();
var messages = await _client.GetMessagesAfter(last.ChannelId, last.Id);
// TODO: insert at the end
CurrentMessages.AddRange(messages);
}
_loadingMessages = false;
}
Expand Down
4 changes: 2 additions & 2 deletions Turbulence.Desktop/Converters/ChannelNameConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public class ChannelNameConverter : IValueConverter
{
if (value is not Channel channel)
return null;


// design exception so we dont api call
if (Design.IsDesignMode)
{
return channel.Name;
}

//TODO: fetch names from client (cache)
return Task.Run(() => _client.GetChannelName(channel)).Result;
}

Expand Down
2 changes: 1 addition & 1 deletion Turbulence.Discord/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Turbulence.Discord;

public static class Api
internal static class Api
{
public const string Version = "9";
private const string RootAdress = "https://discord.com/api";
Expand Down
21 changes: 18 additions & 3 deletions Turbulence.Discord/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class Client : IPlatformClient
public event EventHandler<Event<bool>>? OnConnectionStatusChanged;

public bool Connected => WebSocket.State == WebSocketState.Open;
public HttpClient HttpClient { get; } = new();
private HttpClient HttpClient { get; } = new();
private static readonly HttpClient CdnClient = new();

private readonly ICache _cache = Ioc.Default.GetService<ICache>()!;
Expand Down Expand Up @@ -433,9 +433,24 @@ public async Task<List<Message>> GetMessagesAfter(Snowflake channelId, Snowflake
return await Api.GetChannelMessages(HttpClient, channelId, after: messageId);
}

public async Task<Message> SendMessage(string content, Channel channelId)
public async Task<Message> SendMessage(Channel channel, string content, Message? reply = null, bool shouldPing = false)
{
return await Api.CreateAndSendMessage(HttpClient, channelId, content);
return await Api.CreateAndSendMessage(HttpClient, channel, content, reply, shouldPing);
}

public async Task<Message> EditMessage(string input, Message original)
{
return await Api.EditMessage(HttpClient, input, original);
}

public async Task DeleteMessage(Message message)
{
await Api.DeleteMessage(HttpClient, message);
}

public async Task<GuildMember> GetCurrentUserGuildMember(Snowflake guildId)
{
return await Api.GetCurrentUserGuildMember(HttpClient, guildId);
}

public async Task<Guild> GetGuild(Snowflake guildId)
Expand Down
6 changes: 4 additions & 2 deletions Turbulence.Discord/IPlatformClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public interface IPlatformClient
public Task<List<Message>> GetMessagesAfter(Snowflake channelId, Snowflake messageId);
public Task<Message[]> GetPinnedMessages(Snowflake channelId);
public Task<SearchResult> SearchMessages(SearchRequest request);
public Task<Message> SendMessage(Channel channel, string content, Message? reply = null, bool shouldPing = false);
public Task<Message> EditMessage(string input, Message original);
public Task DeleteMessage(Message message);
public Task<GuildMember> GetCurrentUserGuildMember(Snowflake guildId);
public Task<Guild> GetGuild(Snowflake guildId);
public event EventHandler<Event<bool>>? OnConnectionStatusChanged;
public event EventHandler<Event<Ready>>? Ready;
Expand All @@ -36,7 +40,5 @@ public interface IPlatformClient

public bool SendGatewayMessage<T>(GatewayOpcode opcode, T data);

public HttpClient HttpClient { get; }

#endregion
}
2 changes: 1 addition & 1 deletion Turbulence.TGUI/Views/ServerListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public IEnumerable<ServerTreeNode> GetChildren(ServerTreeNode node)
{
// Failed to find guild, probably because the guild was from a gateway event. Get it from the API directly
// TODO: Can we do this another way?
var channel = Task.Run(() => Api.GetChannel(_client.HttpClient, categoryNode.Channel.Id)).GetAwaiter().GetResult();
var channel = Task.Run(() => _client.GetChannel(categoryNode.Channel.Id)).GetAwaiter().GetResult();

if (_vm.Servers.FirstOrDefault(g => g.Id == channel.GuildId) is not { } retryGuild)
throw new Exception("Can't find guild associated with channel");
Expand Down

0 comments on commit d13087e

Please sign in to comment.