about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEmma [it/its]@Rory& <root@rory.gay>2024-02-09 16:14:07 +0100
committerEmma [it/its]@Rory& <root@rory.gay>2024-02-09 16:14:18 +0100
commit621130574be06fd9bccdbf585088aaf1d7e862df (patch)
tree36686a2bd0648225631de980833f61382e0f9471
parentAdd submodules for example bots (diff)
downloadLibMatrix-621130574be06fd9bccdbf585088aaf1d7e862df.tar.xz
Allow specifying chunk size for GetManyMessages
-rw-r--r--LibMatrix.EventTypes/Common/MjolnirShortcodeEventContent.cs4
-rw-r--r--LibMatrix/RoomTypes/GenericRoom.cs43
2 files changed, 30 insertions, 17 deletions
diff --git a/LibMatrix.EventTypes/Common/MjolnirShortcodeEventContent.cs b/LibMatrix.EventTypes/Common/MjolnirShortcodeEventContent.cs
index 7924a33..a4974e3 100644
--- a/LibMatrix.EventTypes/Common/MjolnirShortcodeEventContent.cs
+++ b/LibMatrix.EventTypes/Common/MjolnirShortcodeEventContent.cs
@@ -2,8 +2,10 @@ using System.Text.Json.Serialization;
 
 namespace LibMatrix.EventTypes.Common;
 
-[MatrixEvent(EventName = "org.matrix.mjolnir.shortcode")]
+[MatrixEvent(EventName = EventId)]
 public class MjolnirShortcodeEventContent : TimelineEventContent {
+    public const string EventId = "org.matrix.mjolnir.shortcode";
+
     [JsonPropertyName("shortcode")]
     public string? Shortcode { get; set; }
 }
diff --git a/LibMatrix/RoomTypes/GenericRoom.cs b/LibMatrix/RoomTypes/GenericRoom.cs
index 38ced58..ec14ca9 100644
--- a/LibMatrix/RoomTypes/GenericRoom.cs
+++ b/LibMatrix/RoomTypes/GenericRoom.cs
@@ -112,7 +112,7 @@ public class GenericRoom {
     /// Same as <see cref="GetMessagesAsync"/>, except keeps fetching more responses until the beginning of the room is found, or the target message limit is reached
     /// </summary>
     public async IAsyncEnumerable<MessagesResponse> GetManyMessagesAsync(string from = "", int limit = 100, string dir = "b", string filter = "", bool includeState = true,
-        bool fixForward = false) {
+        bool fixForward = false, int chunkSize = 100) {
         if (dir == "f" && fixForward) {
             var concat = new List<MessagesResponse>();
             while (true) {
@@ -144,7 +144,7 @@ public class GenericRoom {
         }
         else {
             while (limit > 0) {
-                var resp = await GetMessagesAsync(from, limit, dir, filter);
+                var resp = await GetMessagesAsync(from, Math.Min(chunkSize, limit), dir, filter);
 
                 if (!includeState)
                     resp.State.Clear();
@@ -280,21 +280,32 @@ public class GenericRoom {
             return await GetNameAsync();
         }
         catch {
-            try {
-                var members = GetMembersEnumerableAsync();
-                var memberList = new List<string>();
-                int memberCount = 0;
-                await foreach (var member in members)
-                    memberList.Add(member.RawContent?["displayname"]?.GetValue<string>() ?? "");
-                memberCount = memberList.Count;
-                memberList.RemoveAll(string.IsNullOrWhiteSpace);
-                memberList = memberList.OrderBy(x => x).ToList();
-                if (memberList.Count > maxMemberNames)
-                    return string.Join(", ", memberList.Take(maxMemberNames)) + " and " + (memberCount - maxMemberNames) + " others.";
-                return string.Join(", ", memberList);
+            try
+            {
+                var alias = await GetCanonicalAliasAsync();
+                if (alias?.Alias is not null) return alias.Alias;
+                throw new Exception("No name or alias");
             }
-            catch {
-                return RoomId;
+            catch
+            {
+                try
+                {
+                    var members = GetMembersEnumerableAsync();
+                    var memberList = new List<string>();
+                    int memberCount = 0;
+                    await foreach (var member in members)
+                        memberList.Add(member.RawContent?["displayname"]?.GetValue<string>() ?? "");
+                    memberCount = memberList.Count;
+                    memberList.RemoveAll(string.IsNullOrWhiteSpace);
+                    memberList = memberList.OrderBy(x => x).ToList();
+                    if (memberList.Count > maxMemberNames)
+                        return string.Join(", ", memberList.Take(maxMemberNames)) + " and " + (memberCount - maxMemberNames) + " others.";
+                    return string.Join(", ", memberList);
+                }
+                catch
+                {
+                    return RoomId;
+                }
             }
         }
     }