Reordered methods in CajetanApiService

This commit is contained in:
Casper Sparre 2026-02-21 00:31:19 +01:00
parent 043b3ec0b0
commit e698ea2313

View File

@ -27,6 +27,37 @@ public class CajetanApiService(IAuthService authService, IConfigService configSe
return GetAllSubscriptions(endpoint, includeRestricted, "expired"); return GetAllSubscriptions(endpoint, includeRestricted, "expired");
} }
public new async Task<MessageEntities.MessageCollection> GetMessages(string endpoint, string folder, IStatusReporter statusReporter)
{
(bool couldExtract, long userId) = ExtractUserId(endpoint);
_eventHandler.OnMessage("Getting Unread Chats", "grey");
HashSet<long> usersWithUnread = couldExtract ? await GetUsersWithUnreadMessagesAsync() : [];
MessageEntities.MessageCollection messages = await base.GetMessages(endpoint, folder, statusReporter);
if (usersWithUnread.Contains(userId))
{
_eventHandler.OnMessage("Restoring unread state", "grey");
await MarkAsUnreadAsync($"/chats/{userId}/mark-as-read");
}
return messages;
static (bool couldExtract, long userId) ExtractUserId(string endpoint)
{
string withoutChatsAndMessages = endpoint
.Replace("chats", "", StringComparison.OrdinalIgnoreCase)
.Replace("messages", "", StringComparison.OrdinalIgnoreCase);
string trimmed = withoutChatsAndMessages.Trim(' ', '/', '\\');
if (long.TryParse(trimmed, out long userId))
return (true, userId);
return (false, default);
}
}
public new async Task<Dictionary<string, long>?> GetListUsers(string endpoint) public new async Task<Dictionary<string, long>?> GetListUsers(string endpoint)
{ {
if (!HasSignedRequestAuth()) if (!HasSignedRequestAuth())
@ -122,29 +153,6 @@ public class CajetanApiService(IAuthService authService, IConfigService configSe
return null; return null;
} }
public async Task SortBlockedAsync(string endpoint, string order = "recent", string direction = "desc")
{
Log.Debug($"Calling SortBlocked - {endpoint}");
try
{
var reqBody = new { order, direction };
var result = new { success = false, canAddFriends = false };
string? body = await BuildHeaderAndExecuteRequests([], endpoint, GetHttpClient(), HttpMethod.Post, reqBody);
if (!string.IsNullOrWhiteSpace(body))
result = JsonConvert.DeserializeAnonymousType(body, result);
if (result?.success != true)
_eventHandler.OnMessage($"Failed to sort blocked (order: {order}, direction; {direction})! Endpoint: {endpoint}", "yellow");
}
catch (Exception ex)
{
ExceptionLoggerHelper.LogException(ex);
}
}
public async Task<Dictionary<string, long>> GetUsersWithProgressAsync(string typeDisplay, string endpoint, string? typeParam, bool offsetByCount) public async Task<Dictionary<string, long>> GetUsersWithProgressAsync(string typeDisplay, string endpoint, string? typeParam, bool offsetByCount)
{ {
Dictionary<string, long> usersOfType = await _eventHandler.WithStatusAsync( Dictionary<string, long> usersOfType = await _eventHandler.WithStatusAsync(
@ -230,37 +238,6 @@ public class CajetanApiService(IAuthService authService, IConfigService configSe
} }
} }
public new async Task<MessageEntities.MessageCollection> GetMessages(string endpoint, string folder, IStatusReporter statusReporter)
{
(bool couldExtract, long userId) = ExtractUserId(endpoint);
_eventHandler.OnMessage("Getting Unread Chats", "grey");
HashSet<long> usersWithUnread = couldExtract ? await GetUsersWithUnreadMessagesAsync() : [];
MessageEntities.MessageCollection messages = await base.GetMessages(endpoint, folder, statusReporter);
if (usersWithUnread.Contains(userId))
{
_eventHandler.OnMessage("Restoring unread state", "grey");
await MarkAsUnreadAsync($"/chats/{userId}/mark-as-read");
}
return messages;
static (bool couldExtract, long userId) ExtractUserId(string endpoint)
{
string withoutChatsAndMessages = endpoint
.Replace("chats", "", StringComparison.OrdinalIgnoreCase)
.Replace("messages", "", StringComparison.OrdinalIgnoreCase);
string trimmed = withoutChatsAndMessages.Trim(' ', '/', '\\');
if (long.TryParse(trimmed, out long userId))
return (true, userId);
return (false, default);
}
}
public async Task<HashSet<long>> GetUsersWithUnreadMessagesAsync() public async Task<HashSet<long>> GetUsersWithUnreadMessagesAsync()
{ {
MessageDtos.ChatsDto unreadChats = await GetChatsAsync("/chats", onlyUnread: true); MessageDtos.ChatsDto unreadChats = await GetChatsAsync("/chats", onlyUnread: true);
@ -302,6 +279,29 @@ public class CajetanApiService(IAuthService authService, IConfigService configSe
} }
} }
public async Task SortBlockedAsync(string endpoint, string order = "recent", string direction = "desc")
{
Log.Debug($"Calling SortBlocked - {endpoint}");
try
{
var reqBody = new { order, direction };
var result = new { success = false, canAddFriends = false };
string? body = await BuildHeaderAndExecuteRequests([], endpoint, GetHttpClient(), HttpMethod.Post, reqBody);
if (!string.IsNullOrWhiteSpace(body))
result = JsonConvert.DeserializeAnonymousType(body, result);
if (result?.success != true)
_eventHandler.OnMessage($"Failed to sort blocked (order: {order}, direction; {direction})! Endpoint: {endpoint}", "yellow");
}
catch (Exception ex)
{
ExceptionLoggerHelper.LogException(ex);
}
}
private async Task<Dictionary<string, long>?> GetAllSubscriptions(string endpoint, bool includeRestricted, string type) private async Task<Dictionary<string, long>?> GetAllSubscriptions(string endpoint, bool includeRestricted, string type)
{ {
if (!HasSignedRequestAuth()) if (!HasSignedRequestAuth())