Receive room invitations, allow user to accept or deny

A BAlert is used to inform the user of room invitations― InviteDialogue―
and to send their response directly to the protocol's looper.

Some new protocol messages were added (and older ones were moved around
to compenstate)― IM_ROOM_SEND_INVITE, IM_ROOM_INVITE_REFUSED,
IM_ROOM_INVITE_RECEIVED, IM_ROOM_INVITE_ACCEPT, and
IM_ROOM_INVITE_REFUSE.
This commit is contained in:
Jaidyn Ann 2021-06-08 15:32:04 -05:00
parent bc2c211458
commit 37512144a1
5 changed files with 290 additions and 131 deletions

View File

@ -10,13 +10,13 @@
* What-codes for messages. * What-codes for messages.
*/ */
enum message_what_codes { enum message_what_codes {
//! All client <> protocol communication uses this what-code //! All client <> protocol communication uses this what-code
IM_MESSAGE = 'IMme', IM_MESSAGE = 'IMme',
//! Used for very important (blocking) error messages //! Used for very important (blocking) error messages
IM_ERROR = 'IMer', IM_ERROR = 'IMer',
//! Returned after a request has succeded //! Returned after a request has succeded
IM_ACTION_PERFORMED = 'IMap' IM_ACTION_PERFORMED = 'IMap'
}; };
@ -28,19 +28,19 @@ enum im_what_code {
* Messages that involves server-side contact list. * Messages that involves server-side contact list.
*/ */
//! Request a server-side contact list from protocol //! Request a server-side contact list from protocol →Protocol
IM_GET_CONTACT_LIST = 1, IM_GET_CONTACT_LIST = 1,
//! Server-side contact list received //! Server-side contact list received →Caya
// Requires: Stringlist "user_id" // Requires: Stringlist "user_id"
IM_CONTACT_LIST = 2, IM_CONTACT_LIST = 2,
//! Contact(s) was added to the server-side list //! Contact(s) was added to the server-side list →Caya
// Requires: String "user_id" // Requires: String "user_id"
IM_CONTACT_LIST_ADD_CONTACT = 3, IM_CONTACT_LIST_ADD_CONTACT = 3,
//! Contact(s) removed from the server-side list //! Contact(s) removed from the server-side list →Caya
// Requires: String "user_id" // Requires: String "user_id"
IM_CONTACT_LIST_REMOVED_CONTACT = 4, IM_CONTACT_LIST_REMOVED_CONTACT = 4,
@ -48,31 +48,31 @@ enum im_what_code {
* Messages related to text chat. * Messages related to text chat.
*/ */
//! Send a chat message to the protocol //! Send a chat message →Protocol
// Requires: String "user_id", String "body" // Requires: String "user_id", String "body"
IM_SEND_MESSAGE = 20, IM_SEND_MESSAGE = 20,
//! Chat message has been sent //! Chat message has been sent →Caya
// Requires: String "chat_id", String "user_id", String "body" // Requires: String "chat_id", String "user_id", String "body"
// Accepts: String "subject" // Accepts: String "subject"
IM_MESSAGE_SENT = 21, IM_MESSAGE_SENT = 21,
//! Chat message received //! Chat message received →Caya
// Requires: String "chat_id", String "user_id", String "body" // Requires: String "chat_id", String "user_id", String "body"
// Accepts: String "subject" // Accepts: String "subject"
IM_MESSAGE_RECEIVED = 22, IM_MESSAGE_RECEIVED = 22,
//! Logs received //! Logs received →Caya
// Requires: String "chat_id", String "user_id", String "body" // Requires: String "chat_id", String "user_id", String "body"
// Accepts: String "subject" // Accepts: String "subject"
IM_LOGS_RECEIVED = 23, IM_LOGS_RECEIVED = 23,
//! User started typing //! User started typing →Caya
// Requires: String "chat_id", String "user_id" // Requires: String "chat_id", String "user_id"
IM_USER_STARTED_TYPING = 24, IM_USER_STARTED_TYPING = 24,
//! User stopped typing //! User stopped typing →Caya
// Requires: String "chat_id", String "user_id" // Requires: String "chat_id", String "user_id"
IM_USER_STOPPED_TYPING = 25, IM_USER_STOPPED_TYPING = 25,
@ -81,10 +81,10 @@ enum im_what_code {
* Messages related to contact changes. * Messages related to contact changes.
*/ */
//! Change contact's status //! Change contact's status →Protocol
IM_SET_NICKNAME = 40, IM_SET_NICKNAME = 40,
//! Contact's status has changed //! Contact's status has changed →Caya
IM_NICKNAME_SET = 41, IM_NICKNAME_SET = 41,
@ -92,28 +92,28 @@ enum im_what_code {
* Messages related to contact's information received from protocols. * Messages related to contact's information received from protocols.
*/ */
//! Received contact new status //! Received contact new status →Caya
// Requires: String "user_id", int32/CayaStatus "status" // Requires: String "user_id", int32/CayaStatus "status"
IM_STATUS_SET = 60, IM_STATUS_SET = 60,
//! Contact's avatar icon was changed //! User's avatar icon was changed →Caya
// Requires: String "user_id", Ref "ref" // Requires: String "user_id", Ref "ref"
IM_AVATAR_SET = 61, IM_AVATAR_SET = 61,
//! Get contact information //! Get contact information →Caya
IM_GET_CONTACT_INFO = 62, IM_GET_CONTACT_INFO = 62,
//! Received contact information //! Received contact information →Caya
// Requires: String "user_id" // Requires: String "user_id"
// Accepts: String "user_name", String "message", int32/CayaStatus "status" // Accepts: String "user_name", String "message", int32/CayaStatus "status"
IM_CONTACT_INFO = 63, IM_CONTACT_INFO = 63,
//! Get extended contact information //! Request contact information →Protocol
IM_GET_EXTENDED_CONTACT_INFO = 64, IM_GET_EXTENDED_CONTACT_INFO = 64,
//! Received extended contact information //! Received contact information →Caya
// Requires: String "user_id" // Requires: String "user_id"
// Accepts: String "user_name", String "full_name" // Accepts: String "user_name", String "full_name"
IM_EXTENDED_CONTACT_INFO = 65, IM_EXTENDED_CONTACT_INFO = 65,
@ -121,30 +121,30 @@ enum im_what_code {
* Messages that involve changing own information. * Messages that involve changing own information.
*/ */
//! Change own nickname //! Change own nickname →Protocol
// Requires: String "user_name" // Requires: String "user_name"
IM_SET_OWN_NICKNAME = 80, IM_SET_OWN_NICKNAME = 80,
//! Own nickname was changed //! Own nickname was changed →Caya
IM_OWN_NICKNAME_SET = 81, IM_OWN_NICKNAME_SET = 81,
//! Change own status //! Change own status →Protocol
// Requires: int32/CayaStatus "status" // Requires: int32/CayaStatus "status"
IM_SET_OWN_STATUS = 82, IM_SET_OWN_STATUS = 82,
// Own status was chagned // Own status was changed →Caya
// Requires: int32/CayaStatus "status" // Requires: int32/CayaStatus "status"
IM_OWN_STATUS_SET = 83, IM_OWN_STATUS_SET = 83,
//! Get own contact information //! Get own contact information
// Requires: String "user_id" // Requires: String "user_id"
IM_OWN_CONTACT_INFO = 84, IM_OWN_CONTACT_INFO = 84,
//! Change own avatar icon //! Change own avatar icon
IM_SET_OWN_AVATAR = 85, IM_SET_OWN_AVATAR = 85,
//! Own avatar icon was changed //! Own avatar icon was changed
// Requires: Ref "ref" // Requires: Ref "ref"
IM_OWN_AVATAR_SET = 86, IM_OWN_AVATAR_SET = 86,
@ -152,10 +152,10 @@ enum im_what_code {
* Contacts registration. * Contacts registration.
*/ */
//! Start listening to changes in these contact's statuses //! Start listening to changes in these contact's statuses
IM_REGISTER_CONTACTS = 100, IM_REGISTER_CONTACTS = 100,
//! Stop listening to status changes from these contacts //! Stop listening to status changes from these contacts
IM_UNREGISTER_CONTACTS = 101, IM_UNREGISTER_CONTACTS = 101,
@ -163,19 +163,19 @@ enum im_what_code {
* Authorization. * Authorization.
*/ */
//! Ask authorization to contact //! Ask authorization to contact
IM_ASK_AUTHORIZATION = 120, IM_ASK_AUTHORIZATION = 120,
//! Authorization response received from contact //! Authorization response received from contact
IM_AUTHORIZATION_RECEIVED = 121, IM_AUTHORIZATION_RECEIVED = 121,
//! Authorization request received from contact //! Authorization request received from contact
IM_AUTHORIZATION_REQUEST = 122, IM_AUTHORIZATION_REQUEST = 122,
//! Authorization response given to contact //! Authorization response given to contact
IM_AUTHORIZATION_RESPONSE = 123, IM_AUTHORIZATION_RESPONSE = 123,
//! Contact has been authorized //! Contact has been authorized
IM_CONTACT_AUTHORIZED = 124, IM_CONTACT_AUTHORIZED = 124,
@ -183,10 +183,10 @@ enum im_what_code {
* Miscellaneous. * Miscellaneous.
*/ */
//! Progress message received, could be login sequence, file transfer etc... //! Progress message received, could be login sequence, file transfer etc...
IM_PROGRESS = 140, IM_PROGRESS = 140,
//! Notifications //! Notifications
IM_NOTIFICATION = 141, IM_NOTIFICATION = 141,
@ -194,121 +194,145 @@ enum im_what_code {
* Room membership * Room membership
*/ */
//! Create an individual chat //! Create an individual chat
// Requires: String "user_id" // Requires: String "user_id" →Protocol
IM_CREATE_CHAT = 150, IM_CREATE_CHAT = 150,
//! Chat has been created //! Chat has been created →Caya
// Requires: String "chat_id", String "user_id" // Requires: String "chat_id", String "user_id"
IM_CHAT_CREATED = 151, IM_CHAT_CREATED = 151,
//! Join a room //! Join a room →Protocol
// Requires: String "chat_id" // Requires: String "chat_id"
IM_JOIN_ROOM = 152, IM_JOIN_ROOM = 152,
//! Confirm the room's been joined //! Confirm the room's been joined →Caya
// Requires: String "chat_id" // Requires: String "chat_id"
IM_ROOM_JOINED = 153, IM_ROOM_JOINED = 153,
//! User wants to leave the room //! User wants to leave the room →Protocol
// Requires: String "chat_id" // Requires: String "chat_id"
IM_LEAVE_ROOM = 154, IM_LEAVE_ROOM = 154,
//! User left the room //! User left the room →Caya
// Requires: String "chat_id" // Requires: String "chat_id"
IM_ROOM_LEFT = 155, IM_ROOM_LEFT = 155,
//! Quietly add a user(s) to the chat //! Quietly add user(s) to the chat →Caya
// Requires: String "chat_id", StringList "user_id" // Requires: String "chat_id", StringList "user_id"
// Accepts: StringList "user_name" // Accepts: StringList "user_name"
IM_ROOM_PARTICIPANTS = 156, IM_ROOM_PARTICIPANTS = 156,
//! User has newly and explicitly joined //! User has explicitly joined →Caya
// Requires: String "chat_id", String "user_id" // Requires: String "chat_id", String "user_id"
// Accepts: String "body" // Accepts: String "body"
IM_ROOM_PARTICIPANT_JOINED = 157, IM_ROOM_PARTICIPANT_JOINED = 157,
//! A user left the room //! A user left the room →Caya
// Requires: String "chat_id", String "user_id" // Requires: String "chat_id", String "user_id"
// Accepts: String "user_name", String "body" // Accepts: String "user_name", String "body"
IM_ROOM_PARTICIPANT_LEFT = 158, IM_ROOM_PARTICIPANT_LEFT = 158,
//! Invite a user to a room →Protocol
// You can tell it succeded with ROOM_PARTICIPANT_JOINED
// Requires: String "chat_id", String "user_id"
// Accepts: String "body"
IM_ROOM_SEND_INVITE = 159,
//! Invitee explicitly refused →Caya
// Requires: String "chat_id", String "user_id"
// Accepts: String "user_name", String "body"
IM_ROOM_INVITE_REFUSED = 160,
//! User was invited to a room →Caya
// Requires: String "chat_id"
// Accepts: String "user_id", String "chat_name", String "body"
IM_ROOM_INVITE_RECEIVED = 161,
//! User accepted an invite →Protocol
// Requires: String "chat_id"
IM_ROOM_INVITE_ACCEPT = 162,
//! User denies an invite →Protocol
// Requires: String "chat_id"
IM_ROOM_INVITE_REFUSE = 163,
/* /*
* Room metadata * Room metadata
*/ */
//! Set the room name //! Set the room name →Protocol
// Requires: String "chat_id", String "chat_name" // Requires: String "chat_id", String "chat_name"
IM_SET_ROOM_NAME = 160, IM_SET_ROOM_NAME = 170,
//! Room name //! Room name changed →Protocol
// Requires: String "chat_id", String "chat_name" // Requires: String "chat_id", String "chat_name"
IM_ROOM_NAME_SET = 161, IM_ROOM_NAME_SET = 171,
//! Set the room subject //! Set the room subject →Caya
// Requires: String "chat_id", String "subject" // Requires: String "chat_id", String "subject"
IM_SET_ROOM_SUBJECT = 162, IM_SET_ROOM_SUBJECT = 172,
//! Room has been set //! Subject has been set →Caya
// Requires: String "chat_id", String "subject" // Requires: String "chat_id", String "subject"
IM_ROOM_SUBJECT_SET = 163, IM_ROOM_SUBJECT_SET = 173,
/* /*
* Room moderation * Room moderation
*/ */
//! A user's role has been changed //! A user's role has been changed →Caya
// Requires: String "role_title", int32 "role_perms", int32 "role_priority" // Requires: String "role_title", int32 "role_perms", int32 "role_priority"
IM_ROOM_ROLECHANGED = 170, IM_ROOM_ROLECHANGED = 190,
//! Kick user //! Kick user →Protocol
// Requires: String "chat_id", String "user_id" // Requires: String "chat_id", String "user_id"
IM_ROOM_KICK_PARTICIPANT = 171, IM_ROOM_KICK_PARTICIPANT = 191,
//! A user was kicked //! A user was kicked →Caya
// Requires: String "chat_id", String "user_id" // Requires: String "chat_id", String "user_id"
// Accepts: String "user_name", String "body" // Accepts: String "user_name", String "body"
IM_ROOM_PARTICIPANT_KICKED = 172, IM_ROOM_PARTICIPANT_KICKED = 192,
//! Ban user //! Ban user →Protocol
// Requires: String "chat_id", String "user_id" // Requires: String "chat_id", String "user_id"
IM_ROOM_BAN_PARTICIPANT = 173, IM_ROOM_BAN_PARTICIPANT = 193,
//! A user was banned //! A user was banned →Caya
// Requires: String "chat_id", String "user_id" // Requires: String "chat_id", String "user_id"
// Accepts: String "user_name", String "body" // Accepts: String "user_name", String "body"
IM_ROOM_PARTICIPANT_BANNED = 174, IM_ROOM_PARTICIPANT_BANNED = 194,
//! Unban user //! Unban user →Protocol
IM_ROOM_UNBAN_PARTICIPANT = 175, IM_ROOM_UNBAN_PARTICIPANT = 195,
//! Mute user //! Mute user →Protocol
// Requires: String "chat_id", String "user_id" // Requires: String "chat_id", String "user_id"
IM_ROOM_MUTE_PARTICIPANT = 176, IM_ROOM_MUTE_PARTICIPANT = 196,
//! Unmute user //! Unmute user →Protocol
// Requires: String "chat_id", String "user_id" // Requires: String "chat_id", String "user_id"
IM_ROOM_UNMUTE_PARTICIPANT = 177, IM_ROOM_UNMUTE_PARTICIPANT = 197,
//! Deafen //! Deafen →Protocol
// Requires: String "chat_id", String "user_id" // Requires: String "chat_id", String "user_id"
IM_ROOM_DEAFEN_PARTICIPANT = 178, IM_ROOM_DEAFEN_PARTICIPANT = 198,
//! Allow to read messages //! Allow to read messages →Protocol
// Requires: String "chat_id", String "user_id" // Requires: String "chat_id", String "user_id"
IM_ROOM_UNDEAFEN_PARTICIPANT = 179, IM_ROOM_UNDEAFEN_PARTICIPANT = 199,
/* /*
* Special messages * Special messages
*/ */
//! Special message forwarded to protocol //! Special message forwarded to protocol
IM_SPECIAL_TO_PROTOCOL = 1000, IM_SPECIAL_TO_PROTOCOL = 1000,
//! Special message forwarded from protocol //! Special message forwarded from protocol
IM_SPECIAL_FROM_PROTOCOL = 1001 IM_SPECIAL_FROM_PROTOCOL = 1001
}; };

View File

@ -65,6 +65,7 @@ SRCS = \
application/views/ConversationItem.cpp \ application/views/ConversationItem.cpp \
application/views/ConversationListView.cpp \ application/views/ConversationListView.cpp \
application/views/ConversationView.cpp \ application/views/ConversationView.cpp \
application/views/InviteDialogue.cpp \
application/views/NicknameTextControl.cpp \ application/views/NicknameTextControl.cpp \
application/views/ReplicantStatusView.cpp \ application/views/ReplicantStatusView.cpp \
application/views/ReplicantMenuItem.cpp \ application/views/ReplicantMenuItem.cpp \

View File

@ -21,12 +21,13 @@
#include "Account.h" #include "Account.h"
#include "AccountManager.h" #include "AccountManager.h"
#include "ProtocolLooper.h"
#include "CayaMessages.h" #include "CayaMessages.h"
#include "CayaProtocol.h" #include "CayaProtocol.h"
#include "CayaPreferences.h" #include "CayaPreferences.h"
#include "CayaProtocolMessages.h" #include "CayaProtocolMessages.h"
#include "ImageCache.h" #include "ImageCache.h"
#include "InviteDialogue.h"
#include "ProtocolLooper.h"
#include "ProtocolManager.h" #include "ProtocolManager.h"
#include "RosterItem.h" #include "RosterItem.h"
#include "Server.h" #include "Server.h"
@ -337,13 +338,54 @@ Server::ImMessage(BMessage* msg)
break; break;
} }
case IM_ROOM_INVITE_RECEIVED:
{
msg->PrintToStream();
BString chat_id;
User* user = _EnsureUser(msg);
BString user_id = msg->FindString("user_id");
BString user_name = user_id;
BString chat_name = msg->FindString("chat_name");
BString body = msg->FindString("body");
ProtocolLooper* looper = _LooperFromMessage(msg);
if (msg->FindString("chat_id", &chat_id) != B_OK || looper == NULL)
result = B_SKIP_MESSAGE;
break;
}
if (chat_name.IsEmpty() == true)
chat_name = chat_id;
if (user != NULL)
user_name = user->GetName();
BString alertBody("You've been invited to %room%.");
if (user_id.IsEmpty() == false)
alertBody = "%user% has invited you to %room%.";
if (body.IsEmpty() == false)
alertBody << "\n\n\"%body%\"";
alertBody.ReplaceAll("%user%", user_name);
alertBody.ReplaceAll("%room%", chat_name);
alertBody.ReplaceAll("%body%", body);
BMessage* accept = new BMessage(IM_ROOM_INVITE_ACCEPT);
accept->AddString("chat_id", chat_id);
BMessage* reject = new BMessage(IM_ROOM_INVITE_REFUSE);
reject->AddString("chat_id", chat_id);
InviteDialogue* invite = new InviteDialogue(BMessenger(looper),
"Invitation received",
alertBody.String(), accept, reject);
invite->Go();
break;
}
case IM_USER_STARTED_TYPING: case IM_USER_STARTED_TYPING:
case IM_USER_STOPPED_TYPING: case IM_USER_STOPPED_TYPING:
{ {
// BString id = msg->FindString("chat_id"); // User* user = _EnsureUser();
// Conversation* item = _EnsureConversation(msg); // Conversation* chat = _EnsureConversation();
// item->ImMessage(msg);
result = B_SKIP_MESSAGE; result = B_SKIP_MESSAGE;
break; break;
} }

View File

@ -0,0 +1,59 @@
/*
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "InviteDialogue.h"
#include <Messenger.h>
InviteDialogue::InviteDialogue(BMessenger target, const char* title,
const char* body, BMessage* acceptMsg, BMessage* rejectMsg, BBitmap* icon)
:
BAlert(title, body, "Cancel", "Reject", "Accept", B_WIDTH_AS_USUAL,
B_OFFSET_SPACING),
fMessenger(target),
fAcceptMsg(acceptMsg),
fRejectMsg(rejectMsg)
{
if (icon != NULL)
SetIcon(icon);
}
void
InviteDialogue::MessageReceived(BMessage* msg)
{
int32 which;
if (msg->FindInt32("which", &which) != B_OK) {
BAlert::MessageReceived(msg);
return;
}
msg->PrintToStream();
switch (which)
{
case 0:
break;
case 1:
fMessenger.SendMessage(fRejectMsg);
break;
case 2:
fMessenger.SendMessage(fAcceptMsg);
break;
default:
return;
}
PostMessage(B_QUIT_REQUESTED);
}
status_t
InviteDialogue::Go()
{
return BAlert::Go(NULL);
}

View File

@ -0,0 +1,33 @@
/*
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#ifndef INVITE_DIALOGUE_H
#define INVITE_DIALOGUE_H
#include <Alert.h>
#include <Messenger.h>
class BBitmap;
// BAlert for quickly sending yes/no messages directly to a protocol looper
class InviteDialogue : public BAlert {
public:
InviteDialogue(BMessenger target, const char* title, const char* body,
BMessage* acceptMsg, BMessage* rejectMsg,
BBitmap* icon = NULL);
void MessageReceived(BMessage* msg);
status_t Go();
private:
BMessenger fMessenger;
BMessage* fAcceptMsg;
BMessage* fRejectMsg;
};
#endif // INVITE_DIALOGUE_H