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

room.getUnreadNotificationCount() always return undefined #997

Open
lxltmx opened this issue Jul 19, 2019 · 26 comments
Open

room.getUnreadNotificationCount() always return undefined #997

lxltmx opened this issue Jul 19, 2019 · 26 comments

Comments

@lxltmx
Copy link

lxltmx commented Jul 19, 2019

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

@t3chguy
Copy link
Member

t3chguy commented Jul 19, 2019

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

@LuLongSH
Copy link

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
Copy link
Member

t3chguy commented Sep 24, 2019

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

@LuLongSH
Copy link

LuLongSH commented Sep 24, 2019

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
Copy link

any suggestions ?

@y0n4
Copy link

y0n4 commented Nov 23, 2019

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
Copy link

tusharmahajan8359 commented Dec 7, 2022

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
Copy link

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
Copy link
Member

t3chguy commented Jun 19, 2023

@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
Copy link

@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
Copy link

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

@t3chguy
Copy link
Member

t3chguy commented Jun 19, 2023

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
Copy link

@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
Copy link
Member

t3chguy commented Jun 19, 2023

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
Copy link

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

@t3chguy
Copy link
Member

t3chguy commented Jun 19, 2023

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

@dhaval-t-simform
Copy link

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
Copy link

Hello @t3chguy

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

@t3chguy
Copy link
Member

t3chguy commented Oct 11, 2023

@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
Copy link

@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
Copy link
Member

t3chguy commented Oct 11, 2023

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
Copy link

dhaval-t-simform commented Oct 11, 2023

@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
Copy link
Member

t3chguy commented Oct 11, 2023

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
Copy link
Member

t3chguy commented Oct 11, 2023

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

@dhaval-t-simform
Copy link

dhaval-t-simform commented Oct 11, 2023

@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
Copy link
Member

t3chguy commented Oct 11, 2023

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

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

No branches or pull requests

6 participants