Message/mention notifications

If the user's in a one-on-one chat, notify on every message― otherwise,
only notify when the user's name/ID is mentioned by another user.

Notifications should be managed by roomflags, but that's for another
time.
This commit is contained in:
Jaidyn Ann 2021-07-15 18:01:58 -05:00
parent 0c8af4dd86
commit e1ec602be8

View File

@ -14,6 +14,7 @@
#include <ListView.h> #include <ListView.h>
#include <Notification.h> #include <Notification.h>
#include <ScrollView.h> #include <ScrollView.h>
#include <StringFormat.h>
#include <StringList.h> #include <StringList.h>
#include <StringView.h> #include <StringView.h>
@ -131,34 +132,45 @@ ConversationView::ImMessage(BMessage* msg)
} }
case IM_MESSAGE_RECEIVED: case IM_MESSAGE_RECEIVED:
{ {
BString id = msg->FindString("user_id"); BString text = msg->FindString("body");
User* sender = fConversation->UserById(id); Contact* contact = fConversation->GetOwnContact();
bool mentioned = ((text.IFindFirst(contact->GetName()) != B_ERROR)
|| (text.IFindFirst(contact->GetName()) != B_ERROR));
// Send a notification, if it's appropriate // Send a notification, if it's appropriate
if ((Window() == NULL || Window()->IsActive() == false) BWindow* win = Window();
&& (!AppPreferences::Item()->NotifyNewMessage) if ((win == NULL || !win->IsFront() || win->IsMinimized())
&& sender != NULL) && AppPreferences::Item()->NotifyNewMessage
&& (fConversation->Users().CountItems() <= 2 || mentioned))
{ {
fMessageCount++; fMessageCount++;
BString notify_message;
notify_message << "You've got "; BString notifyTitle = "New mention";
notify_message << fMessageCount; BString notifyText = "You've been summoned from %source%.";
if (fMessageCount==1)
notify_message << " new message from "; if (mentioned == false) {
else notifyTitle.SetTo("New message");
notify_message << " new messages from "; notifyText.SetTo("");
notify_message << sender->GetName();
BStringFormat pmFormat("{0, plural,"
"=1{You've got a new message from %source%.}"
"other{You've got # new messages from %source%.}}");
pmFormat.Format(notifyText, fMessageCount);
}
notifyText.ReplaceAll("%source%", fConversation->GetName());
BBitmap* icon = fConversation->IconBitmap();
if (icon == NULL)
icon = fConversation->ProtocolBitmap();
BNotification notification(B_INFORMATION_NOTIFICATION); BNotification notification(B_INFORMATION_NOTIFICATION);
notification.SetGroup(BString(APP_NAME)); notification.SetGroup(BString(APP_NAME));
notification.SetTitle(BString("New message")); notification.SetTitle(notifyTitle);
notification.SetIcon(sender->AvatarBitmap()); notification.SetIcon(icon);
notification.SetContent(notify_message); notification.SetContent(notifyText);
notification.SetMessageID(sender->GetName()); notification.SetMessageID(fConversation->GetId());
notification.Send(); notification.Send();
// Check if the user want the notification
if (!AppPreferences::Item()->NotifyNewMessage)
break;
} }
_AppendOrEnqueueMessage(msg); _AppendOrEnqueueMessage(msg);