Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Fetch all conversation ordered by timestamp #175

Closed
WilliamYWY opened this issue Dec 8, 2023 · 1 comment
Closed

Feature request: Fetch all conversation ordered by timestamp #175

WilliamYWY opened this issue Dec 8, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@WilliamYWY
Copy link
Contributor

Is your feature request related to a problem?

Is there a way to retrieve conversations that are ordered based on the time of their last received message?
Or include the timestamp of the last received message in the object?
Thanks!

Describe the solution to the problem

Provide the timestamp of the last received message received by each conversation in the object.

Describe the uses cases for the feature

List the conversation based on the latest activity

Additional details

No response

@WilliamYWY WilliamYWY added the enhancement New feature or request label Dec 8, 2023
@humanagent humanagent self-assigned this Dec 8, 2023
@humanagent
Copy link

In XMTP, there isn't a direct way to retrieve conversations ordered by the time of their last received message or include the timestamp of the last received message in the conversation object. This is due to XMTP's commitment to privacy.

This means that an app must fetch all conversations up front, which might seem inefficient, especially for accounts with thousands of conversations. However, to mitigate this, apps built with XMTP should adopt a local-first cache architecture. This approach significantly speeds up subsequent app loads, turning the initial delay into a one-time occurrence and enhancing the overall user experience.

For now, because there is no server-side filtering you would need to fetch all the conversations and then go through the messages sorting by timestamp

// Fetch all conversations
const conversations = await client.conversations.list();

// Fetch the most recent message for each conversation
const conversationsWithLastMessage = await Promise.all(
  conversations.map(async (conversation) => {
    const messages = await conversation.messages();
    return {
      ...conversation,
      lastMessage: messages[0],
    };
  })
);

// Sort conversations by the timestamp of the most recent message
const sortedConversations = conversationsWithLastMessage.sort(
  (a, b) => b.lastMessage.sent - a.lastMessage.sent
);

We are currently working on making all of our SDKs local cache first!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants