Kotlin+Room+Paging+LiveData for messaging app problem - kotlin

I have 2 tables, first 'group' and second 'messages' and load messages from message table by group id and Paging library, My problem is every time new messages arrived from server and save in messages table Room notify LiveData data changed in table message and if for example I was open MessagesActivity for group 1, if new message arrive for group 2, because that added to messages table, room notify data updated (Which i don't want that).
So please help me:
Select * FROM messages WHERE groupId = 1
I need Room notify LiveData just if new message arrive for group which currently user open that

Related

How to filter out previously seen and read activities while retrieving notifications?

In my app We have notification feed for users, with grouping formula
{{ verb }}_{{ time.strftime('%Y-%m-%d') }}
When User1 notifications are retrieved, it is having 1 notification for a “like” action. “Like” notification is having 3 activities, means three users have liked user1’s post. So Notification text displayed is:
3 users likes your post
User1 notification are marked read and seen, After that 2 more users like user1’s post. Now when user1 retrieve notifications, He will get 1 notification and there are 5 activities in the group. Actor count for group is 5.
So Notification text displayed is:
5 users likes your post
As User1 have already seen previous 3 activities and already read those notifications, those are get repeated again. Only two activities are new but User1 is still getting all the 5 activities. Because activities do not know they are seen/read before.
It is still counting older seen and read activities in the group.
Can we skip these previously seen and read activities while retrieving notifications ?
Read state is kept at the activity group level and it resets from read to unread each time the group is updated (when a new activity is added). So a straight answer to what you are asking would be no, it's not possible to only retrieve the new activities in a group.
From how you describe things, it sounds like you don't really care about grouping and only want to notify about new events. If that's the case you can try to group by {{ id }} so that each activity group will always have 1 activity.

Stripe: webhook events order

How should you handle the fact that events received via webhooks can be received in random order ?
For instance, given the following ordered event:
A: invoiceitem.created (with quantity of 1)
B: invoiceitem.updated (with quantity going from 1 to 3)
C: invoiceitem.updated (with quantity going from 3 to 2)
How do you make sure receiving C-A-B does not result in corrupted data (ie with a quantity of 2 instead of 3)?
You could reject the webhook if the previous_attributes in Event#data do not correspond to the current state, but then you are stuck if your local model was updated already, as you will never find yourself in the state expected by the webhook.
Or you can just use treat any webhook as a hint to retrieve and update an object. You just disregard the data sent by the webhook and always retrieve it.
Even if you receive events ordered as update/delete/create it should work, as update would in fact create the object, delete would delete it, and create would fail to retrieve the object and do nothing.
But it feels like a waste of resources to retrieve data each time when the webhook offers it as event data.
This question was asked before but the answers don't cover the above solutions.
Thanks
If your application is sensitive to changes like this that can occur close in time, you really should just use the event as a signal to retrieve the object, as #koopajah noted in their comment. That's the only way to ensure you have the latest state.

How to know how many unread group messages I have

I'm designing a database. This is what I have to represent:
A user can have 0..n friends.
A user can send 0..n messages to a friend.
A user can be member of 0..n groups.
A group can have 1..n members.
A user can send 0..n messages to a group.
To manage conversations between users, and group I have a table (Talk) with these columns:
TalkId (NOT NULL, PK)
Type (NOT NULL, values: UserTalk or GroupTalk)
StarterUserId (NOT NULL, the user that has started the talk).
RecepientUserId: (NULL, the user that has received the first message. NULL if it is a GroupTalk).
DateStarted: (NOT NULL, when the talk has been started).
GroupId: (NULL, the group that owns the talk. NULL if it is a UserTalk)
I also have a Message table to store all the message for each Talk. This Message table has a column Read to indicate that the recipient has read or not the message.
If user 1 sends a message to a user 2, first I check if there is a Talk row with:
((StarterUserI == 1 and RecepientUserId == 2) OR
(StarterUserI == 2 and RecepientUserId == 1))
If there isn't, I create a new row on it. Then, I insert the message in Message table with Message.TalkId pointing to the row that I have created.
My problem is that I don't know how to know how many unread message a user has for a group talk.
For a user talk is easy checking if Message.Read column is false.
To know if a user has unread messages on a group's talk, I can insert the same message for each group member, changing the recipient. For example:
I have a group, with three members. Member 1 send a message to a group. I have to insert a message to user 2, and the same message to user 3:
But, this can make grow Message table very fast.
I've thought to add new two columns to Talk table, the date for the last message sent to that talk, and the id of user that has sent that last message. If I have the date and the ID for the last message in a talk, I can check if there are new messages, but I can't know how many.
I have also a UserGroup table to store the users that are members of a group, and the users' groups. I can add a new column to this table to store how many messages a user has for a group talk. Every time another user send a message to that group, I'm going to insert a new row on Message table, and increase the value on UserGroup.Unread by one. But I think I'm going to mess the design.
How can I know how many unread message a user has for a Group Talk?
You can add a new table MessageStatus with the columns UserID, MessageID and Read where you add one row for each recipient of a message (UserTalk or GroupTalk). This avoids the redundancies you would introduce when duplicating rows in the Message table.
For convenience you could introduce an INSERT-trigger on Message to create the rows in MessageStatus.

Get the first message of every conversation

I have one table to store all the messages sent inside of a xmpp service. I'm looking to create a query to get all the conversations and the first message of it (like whatsapp in chat logs).
Here is my table.
FromPersonId and ToPersonId are ids for people. What I do is, for example I want to see all the conversations of the personId = 643
SELECT DISTINCT MA.FromPersonId, MA.ToPersonId, MAX(MA.SENTDATE) AS [Date], Body
FROM MessageArchive AS MA
WHERE MA.FromPersonId = #personId OR MA.ToPersonId = #personId
GROUP BY MA.FromPersonId, MA.ToPersonId, Body
ORDER BY [Date] DESC
Above is what I have. And the result is
As you see, the result is for the same conversation. But cannot distinguish that is the same conversation because are the same people but in different position.
How can I fix this?
You miss the 644 to 643 message, supposing it exists, What I recommend is to put a ROW ID autoincremental, this columns can give you exact information about what records come first and what records come after, besides, How do you identify that the message is the same ?
You are missing a 'conversation' table, with a conversationID field being a foreign key in your MessageArchive table, as a manifestation of the 'one-to-many' relation existing between the conversation entity and the message entity: one conversation holds at least one message, and each message relates to one, and only one, conversation.
With such a database model, you would be able to collect the 'top 1' message of each conversation.

SQL - enhance schema to count unread messages

I have a simple message schema where a "thread" ties 2 or more users to a stream of messages. Each message belongs to a single thread. It works just like SMS messages, or Facebook messages.
I only need to count how many threads have new messages for a given user. When a user opens a thread, I need to update the db (for that user only), indicating that they have looked at the thread. Given these 2 tables, how should I enhance my schema to store this "updated thread" data for each user in each thread?
MessageThreads:
threadID
lastUpdated
MessageThreadUsers:
threadFK
userFK
I'm thinking along the lines of adding this table (but is there a better way???)
UserThreads
userFK
threadFK
lastChecked
You should be able to just add a lastChecked column to your MessageThreadUsers table. Then you can compare that value to the lastUpdated field in MessageThreads to determine whether the user has new messages. Or something like below should give you the total number of new messages.
SELECT COUNT(mtu.*) FROM MessageThreadUsers mtu JOIN MessageThreads mt ON mt.threadID = mtu.threadFK WHERE mtu.lastChecked < mt.lastUpdated GROUP BY mtu.userFK
What about some kind of unread boolean value in MessageThreads? When a thread is updated, unread is set to true. When a user checks a thread, set it to false.
Just a thought.