Skip to content

Commit

Permalink
add a limit to list groups so you only get the last n items instead o…
Browse files Browse the repository at this point in the history
…f all
  • Loading branch information
nplasterer committed Oct 4, 2024
1 parent 4630be2 commit 4fd8dd1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -626,19 +626,22 @@ class XMTPModule : Module() {
}
}

AsyncFunction("listGroups") Coroutine { inboxId: String, groupParams: String?, sortOrder: String? ->
AsyncFunction("listGroups") Coroutine { inboxId: String, groupParams: String?, sortOrder: String?, limit: Int? ->
withContext(Dispatchers.IO) {
logV("listGroups")
val client = clients[inboxId] ?: throw XMTPException("No client")
val groupList = client.conversations.listGroups()
val params = GroupParamsWrapper.groupParamsFromJson(groupParams ?: "")
val order = getConversationSortOrder(sortOrder ?: "")
val sortedGroupList = if (order == ConversationOrder.LAST_MESSAGE) {
groupList.sortedByDescending { group ->
group.decryptedMessages(limit = 1).firstOrNull()?.sentAt
}
client.conversations.listGroups()
.sortedByDescending { group ->
group.decryptedMessages(limit = 1).firstOrNull()?.sentAt
}
.let { groups ->
if (limit != null && limit > 0) groups.take(limit) else groups
}
} else {
groupList
client.conversations.listGroups(limit = limit)
}
sortedGroupList.map { group ->
groups[group.cacheKey(inboxId)] = group
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ export async function listGroups<
>(
client: Client<ContentTypes>,
opts?: GroupOptions | undefined,
order?: ConversationOrder | undefined
order?: ConversationOrder | undefined,
limit?: number | undefined
): Promise<Group<ContentTypes>[]> {
const groupParams: GroupParams = {
members: opts?.members,
Expand All @@ -333,7 +334,8 @@ export async function listGroups<
await XMTPModule.listGroups(
client.inboxId,
JSON.stringify(groupParams),
order
order,
limit
)
).map((json: string) => {
const group = JSON.parse(json)
Expand Down
5 changes: 3 additions & 2 deletions src/lib/Conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ export default class Conversations<
*/
async listGroups(
opts?: GroupOptions | undefined,
order?: ConversationOrder | undefined
order?: ConversationOrder | undefined,
limit?: number | undefined
): Promise<Group<ContentTypes>[]> {
const result = await XMTPModule.listGroups(this.client, opts, order)
const result = await XMTPModule.listGroups(this.client, opts, order, limit)

for (const group of result) {
this.known[group.id] = true
Expand Down

0 comments on commit 4fd8dd1

Please sign in to comment.