matrix-org / matrix-js-sdk

Matrix Client-Server SDK for JavaScript
Apache License 2.0
1.61k stars 590 forks source link

room.getUnreadNotificationCount() always return undefined #997

Open lxltmx opened 5 years ago

lxltmx commented 5 years ago

I want to get unread chat messages count (total or one room),but haven't find the right function.

t3chguy commented 5 years ago

Share your code. Without seeing how you set up the state of the js-sdk its impossible to help you

LuLongSH commented 5 years ago

I'm trying to implement notifications when user has unread messages at rooms. setUnreadNotificationCount('total', 1) will add notification to room but in one session. After page reload it disappears and getUnreadNotificationCount() returns undefined.

What can be a reason ?

t3chguy commented 5 years ago

Share your code. It depends on the various stores and sync mechanisms you are using.

LuLongSH commented 5 years ago
this.client = matrixsdk.createClient({
      baseUrl: environment.chat_url,
      accessToken: info.access_token,
      userId: info.username
});
this.client.startClient({
      store: new matrixsdk.IndexedDBStore({ indexedDB: window.indexedDB }),
      initialSyncLimit: 16,
});
this.client.on('sync', (state, payload) => {
        console.log(`Sync State: ${state}`);
        if (state === 'SYNCING') {
          resolve(this.client);
        } else if (state === 'ERROR') {
          reject(payload.error);
        }
});
LuLongSH commented 5 years ago

any suggestions ?

y0n4 commented 4 years ago

I was also experiencing the same problem like @LuLongSH and I looked at the source code and realized that it returns undefined because it doesn't automatically record how many messages the receiver has missed. It's necessary to set the notifications first. Suppose I know based on the timeline history there were 3 more messages I missed after I sent a message, then I would set it like so, room.setUnreadNotification('total', 3)

Then when you set the number of missed messages you are able to get how many unread messages there are by calling room.getUnreadNotification() // will return 3. After I view the messages, I simply set it back to 0`

To know how many messages were missed, I looked at the room.timeline events and each event has its own unique event_id. And i can compare the last time I read an message from room._receipts.['m.read] event_id and just compare it on room.timeline

Mine could be a lil hacky, I hope this helps

tusharmahajan8359 commented 1 year ago

i tried to set unread msg count to zero after reading msgs ,but when i refresh that page and call room.getUnreadNotification , than it will remain same

dhaval-t-simform commented 1 year ago

Any updates on this @LuLongSH @lxltmx @y0n4. I want to know how to set the notification count to 0 after reading the message in the matrix accountData in some way so even on refresh it will show the actual notification count. If I am wrong pls guide me.

t3chguy commented 1 year ago

@dhaval-t-simform you should be sending a read receipt so zero it out on the server, rather than just clobbering the value locally

dhaval-t-simform commented 1 year ago

@t3chguy Thank you for responding. Yes, I got across read receipts and got it that I will have to set it on server. But I am not getting any kind of function for manipulating read receipts. I came across sendReadReceipt and setReadReceipt functions but still not able to figure out a way from it. Can you suggest some solution and a place as well where I can find proper documentation for this as well as all other matrix related stuff as matrix.org also does not seem to have proper docs for reference.

dhaval-t-simform commented 1 year ago

@t3chguy And even on what event type do I need to trigger setReadReceipt() and set ReceiptType.Read if it is a feasible solution?

t3chguy commented 1 year ago

But I am not getting any kind of function for manipulating read receipts. I came across sendReadReceipt and setReadReceipt functions but still not able to figure out a way from it.

http://matrix-org.github.io/matrix-js-sdk/stable/classes/MatrixClient.html#sendReadReceipt

Can you suggest some solution and a place as well where I can find proper documentation for this as well as all other matrix related stuff as matrix.org also does not seem to have proper docs for reference.

https://spec.matrix.org/v1.7/client-server-api/#receipts

And even on what event type do I need to trigger setReadReceipt() and set ReceiptType.Read if it is a feasible solution?

Depends on your push rules, the notification counts are counts of unread events which when evaluated in your push rules result in a notify action. You can do it for every timeline event to guarantee to cover it.

dhaval-t-simform commented 1 year ago

@t3chguy Thank you for responding. I did try but maybe not getting what I need. I'll just share my basic scenario if you can suggest a way out of it.

What I've observed is on reload or re-login its room.notificationCounts.total gets to the previous count which I want to update on server as you already mentioned.

Basically I just want to manage the unread notification count(where in I'm using getUnreadNotificationCount()) and on opening the respective chat I want to update the count to 0 or accordingly on the server as you said.

Any help will be very helpful.

t3chguy commented 1 year ago

You can't just update the server count to a specific number, you have to move the read receipt to a given event, so your server knows up to which point you have read and based on that it'll calculate your unreads. So if you want to update it to zero, pass in the ID of the very latest event that happened in the timeline of the room.

dhaval-t-simform commented 1 year ago

@t3chguy Got it. Still if I face some issue will reply in this thread. Thank you.

t3chguy commented 1 year ago

I suggest https://matrix.to/#/#matrix-dev:matrix.org - this is an issue tracker, not a support forum

dhaval-t-simform commented 1 year ago

Hello @t3chguy

The upper issue is resolved using but after reading the unread messages the count gets 0 but on refreshing the page on the UI it still shows the last unread message count while on second refresh it shows 0. So, does the issue seem to be from matrix server side or maybe there's something I'm still missing?

I've asked the same in channel as well but didn't get any solution.

dhaval-t-simform commented 1 year ago

Hello @t3chguy

Can we send read receipts in thread? As I am not able to fix unread notification count for threads.

t3chguy commented 1 year ago

@dhaval-t-simform yes, as per the Matrix spec, read receipts can be sent in a thread to events other than the thread root.

dhaval-t-simform commented 1 year ago

@t3chguy Is there any sdk method available for that? I tried sendReceipt and sendReadReceipt but it is throwing event.getId() is not a function as I am sharing the thread's id(which is from class Thread) as in eventId parameter and it is expecting event and event_id as of class MatrixEvent. Let me know if I am wrong else what to do in this scenario.

t3chguy commented 1 year ago

Indeed those methods, seems like you are passing the wrong object. You have to pass the MatrixEvent of the reply in the thread you wish to send an RR to. http://matrix-org.github.io/matrix-js-sdk/stable/classes/MatrixClient.html#sendReadReceipt

dhaval-t-simform commented 1 year ago

@t3chguy Thank you for immediate response. But the replies in my chat are of Thread type and not MatrixEvent type. Will it work properly if I send the root of thread which is actually of MatrixEvent type.

t3chguy commented 1 year ago

Will it work properly if I send the root of thread which is actually of MatrixEvent type.

No, as I said before

read receipts can be sent in a thread to events other than the thread root.

Threads are a container of multiple events, they have their own timeline, you read messages not entire rooms/threads.

t3chguy commented 1 year ago

I suggest #matrix-dev:matrix.org for any future help, this isn't a support forum.

dhaval-t-simform commented 1 year ago

@t3chguy I've tried there but didn't receive any response. Last one question if you can guide on. Is this solution feasible/possible directly calling api: {{baseUrl}}/rooms/:roomId/receipt/:receiptType/:eventId along with passing thread id in the request body? Or is there any possible sdk function for the same. Thank you for responding.

t3chguy commented 1 year ago

I suggest #matrix-dev:matrix.org for any future help, this isn't a support forum.