Count of total unread messages - flutter

In my flutter & firebase app, I use cloud functions to send notifications for any new messages. The problem I face is how I show count of total unread messages in the chatroom list tile as shown in red circles in this famous chat app.
My chatroom page looks almost like this picture except for the unread message counter at the right side of each tile. What logic do I have to use?
Please give me any advice to implement showing count of unread messages at the right side of each chatroom tile.
Here is my chatroom collection in firestore.

You can simply add 3 more fields in the document:-
1stpersonMessageCount
2ndPersonMessageCount
MessageCount
So now what you need to do is that in your chatting screen you simply need to increment the message count of the person who is sending the message everytime he send the message:-
chatRoomReference.updateData({
"messageCount":FieldValue.increment(1),
"${currentUser}messageCount":FieldValue.increment(1),
});
and for message seen purpose you can simply update the messagecount of reciever by
chatRoomReference.updateData({
"${currentUser}messageCount":snapshot.documents.length,
});
and on the page where you want to see the count of new messages you simply subtract the value of messageCount and cureentUserMessageCount.
In both the code sections you can see that currentUser is there bt don't get confused when the sender is sending the message then currentUser's value would be the sender's name or his id and when someone is reading then his id or name. Ex- John is sending then current User will be John and when mohan is reading the message then it would be mohan. Hence you will have 3 fields i.e. JohnMessageCount, MohanMessageCount and total messages i.e. messageCount.

Related

How to send message to all agents in population?

I need to send message to all students in population from monthly event.
But (I guess) it`s not possible to use sendToAll()
method in this case, because event is declared inside of another enviroment.
I know how to send message to specific agent through using students.random() as a second argument in send()
method, but is it possible to send message to all agents?
This loops across all students in the population and sends them a message:
for (Student currentStudent : students) {
send("Hey", currentStudent);
}

How can I instantly get the result of my produced event in kafka and kafka-streams?

I am simplifying my problem with the following scenario:
3 friends share a loyalty card. The card has two restrictions
can be used max 10 times (does not matter which uses the card, ie
friend_a can use it 10 times.
the max money in the card is 200. So with 1 "event" of value = 200 the card is "completed".
I am using a kafka producer that sends events in the kafka cluster like this
{ "name": "friend_1", "value": 10 }
{ "name": "friend_3", "value": 20 }
the events are posted to a topic that is connected with a kafka stream that groups by key and doing aggregation to sum the money spent. That seems to work, however I am facing a "concurrency issue"
Let's imaging the card is used 9 times, so only 1 time remains to be used and the total money spent is 190, that means there are 10 units left to spend.
So friend_2 wants to buy something that costs 11 units (which should not be allowed) and friend_3 wants to buy something that costs 9 units which should be allowed. Friend_3 will modify the state using the card for the 10th time. All other future attempts should not modify something.
So it seems reasonable for the card user to know if the event he sent modified the max used number and the total count. How can I do it in kafka? Using the streams aggregation I can always increase the values, but how can do I know if my action "modified the state" of the card?
UPDATE: the card user should immediately get a negative feedback if the transaction validates a rule.
From how I understand your question there are a few options.
One option is to derive a new stream after the aggregation that you filter() for data that would modify 'the state' of the card, like filtering all events that have > 200 units spent or > 10 uses. This stream can then be used to inform the card user(s) that the card has been spent, e.g. by sending an email. This is approach can be implemented solely with the DSL.
When more flexibility or tighter control is needed, another option is to use the Processor API (which you can integrate with the DSL so most of your code could keep using the DSL), where you implement the aggregation step yourself (working with state stores that you attach to a Transformer or Processor). During the aggregation, you can implement the logic that checks whether the incoming event is valid (in your example: friend_3 with 9 units is valid, friend_2 with 11 units is not). If it is valid, the aggregation increases the card's counters (units and uses), and that's it. If it is invalid, the event is discarded and will not modify the counters, the Transformer/Processor can emit a new event to another stream that tells the card user(s) that something didn't work. You can similarly implement the functionality to inform users that a card has been fully used, that a card is no longer usable, or any other 'state change' of that card.
Also, depending on what you want to do, take a look at the interactive queries feature of Kafka Streams. Sometimes other applications may want to do quick point lookups (queries) of the latest state of something, like the card's state, which can be done with interactive queries via e.g. a REST API.
Hope this helps!

Count the number of messages in an IMAP folder using Gmail

What is the best way to get the total number of messages in a folder on a gmail account? I can get the number of unseen messages like this request/response:
d STATUS "py-lists/pygtk" (UNSEEN)
* STATUS "py-lists/pygtk" (UNSEEN 10963)
But when I want to get the total number of messages:
e STATUS "py-lists/pygtk" (MESSAGES)
* STATUS "py-lists/pygtk" (MESSAGES 0)
How on earth can the number of unseen messages (10963) be larger than the total number of messages (0)? Am I misunderstanding imap or have I stumbled upon a gmail bug? If so, what can I use as a workaround? Selecting the folder with f SELECT "py-lists/pygtk" works, but isn't great as the select operation can take a very long time to complete.

Aggregator pattern in Mirth

I am receiving a large number of correlated HL7 messages in Mirth. They contain an ID which is always the same for all correlated messages and they always come within a minute. Multiple batches can be received at the same time. It's hard to say when the batch ends, but when there are no more messages for a minute, it's safe to assume that the batch has finished.
How could I implement an aggregator pattern in Mirth that would keep reading and completing correlated messages and send the completed message after it didn't receive any new messages with the same ID within a defined time interval?
You may drop all incoming message to a folder and store the message ID in a Global Map. Once new messages start to arrive with the message ID different than the one stored in the map (meaning that the next sequence is started), trigger another channel either by sending the message ID it needs to look for or in some other way. After that replace the message ID in the Global Map with the message ID of a new sequence.
If that sounds too complicated, you may do the same, but the second channel will constantly scan the folder (File Reader) and grab only files having the same message ID and older than a minute from a current time (which is in my mind is too vague qualifier).
I've implemented this by saving all messages in a folder using an ID inside the message (that identifies the sequence) as the file name. The file gets updated with each new message. Several sequences live together in the same folder.
The next channel is using a simple file reader that only fetches the files that are a minute or more old.

Questions on Topic filters in azure service bus

Couldn't find any articles answering this specific question so here goes.
Say you have a topic called companyorders and you have 3 filters/subscriptions, companyA, companyB and allcompanies.
Messages sent to the topic for companyA get passed to sub companyA and allcompanies etc. Then messages start coming in for a companyC that hasn't got a specific sub setup so they are sent to only allcompanies sub.
When companyC starts up their client app and it creates the companyC sub(I don't see a way of setting up a sub with a specific filter in the portal) how or can I pull messages from the allcompanies sub for companyC that where previously missed because the sub was not setup beforehand?
Hope that makes sense.
Thanks
Paul
Seems that the subscription/filter needs to be setup before messages are sent to the topic. I tested this by creating a topic and a subscription. I then posted messages to the topic with property DriverID. I passed in a DriverID = 1. This message ended up in the subscription setup earlier as this subscription has the 'MatchAll' filter by default.
I then created another subscription with a filter of DriverID = 1. When I posted a message to the topic and set the property DriverId = 1 it got sent to the 2 subscriptions as expected. Messages posted before this subscription was setup that had DriverID = 1 were not automatically moved to the new subscription that matches the filter.
Paul