Add own user to conversations' user lists

Really, only one line changed in _EnsureConversation()― the rest is
re-organizing the file.
This commit is contained in:
Jaidyn Ann 2021-05-31 13:04:58 -05:00
parent 0581bf1df9
commit 91d5b4e0dc
2 changed files with 143 additions and 144 deletions

View File

@ -56,20 +56,6 @@ Server::Quit()
}
void
Server::AddProtocolLooper(bigtime_t instanceId, CayaProtocol* cayap)
{
ProtocolLooper* looper = new ProtocolLooper(cayap);
fLoopers.AddItem(instanceId, looper);
}
void
Server::RemoveProtocolLooper(bigtime_t instanceId)
{
}
void
Server::LoginAll()
{
@ -84,41 +70,6 @@ Server::LoginAll()
}
void
Server::SendProtocolMessage(BMessage* msg)
{
// Skip null messages
if (!msg)
return;
// Check if message contains the instance field
bigtime_t id;
if (msg->FindInt64("instance", &id) == B_OK) {
bool found = false;
ProtocolLooper* looper
= fLoopers.ValueFor(id, &found);
if (found)
looper->PostMessage(msg);
}
}
void
Server::SendAllProtocolMessage(BMessage* msg)
{
// Skip null messages
if (!msg)
return;
// Send message to all protocols
for (uint32 i = 0; i < fLoopers.CountItems(); i++) {
ProtocolLooper* looper = fLoopers.ValueAt(i);
looper->PostMessage(msg);
}
}
filter_result
Server::Filter(BMessage* message, BHandler **target)
{
@ -166,82 +117,6 @@ Server::Filter(BMessage* message, BHandler **target)
}
RosterMap
Server::Contacts() const
{
return fRosterMap;
}
Contact*
Server::ContactById(BString id)
{
bool found = false;
return fRosterMap.ValueFor(id, &found);
}
void
Server::AddContact(Contact* contact)
{
fRosterMap.AddItem(contact->GetId(), contact);
}
UserMap
Server::Users() const
{
UserMap users = fUserMap;
for (int i = 0; i < fRosterMap.CountItems(); i++) {
User* user = (User*)fRosterMap.ValueAt(i);
users.AddItem(user->GetId(), user);
}
return users;
}
User*
Server::UserById(BString id)
{
bool found = false;
User* user = ContactById(id);
if (user == NULL)
user = fUserMap.ValueFor(id, &found);
return user;
}
void
Server::AddUser(User* user)
{
fUserMap.AddItem(user->GetId(), user);
}
ChatMap
Server::Conversations() const
{
return fChatMap;
}
Conversation*
Server::ConversationById(BString id)
{
bool found = false;
return fChatMap.ValueFor(id, &found);
}
void
Server::AddConversation(Conversation* chat)
{
fChatMap.AddItem(chat->GetId(), chat);
}
filter_result
Server::ImMessage(BMessage* msg)
{
@ -253,17 +128,8 @@ Server::ImMessage(BMessage* msg)
{
int i = 0;
BString id;
while (msg->FindString("user_id", i++, &id) == B_OK) {
bool found = false;
Contact* item = fRosterMap.ValueFor(id, &found);
if (found)
continue;
item = new Contact(id.String(), Looper());
item->SetProtocolLooper(_LooperFromMessage(msg));
fRosterMap.AddItem(id, item);
}
while (msg->FindString("user_id", i++, &id) == B_OK)
_EnsureContact(msg);
result = B_SKIP_MESSAGE;
break;
}
@ -300,6 +166,14 @@ Server::ImMessage(BMessage* msg)
}
break;
}
case IM_OWN_CONTACT_INFO:
{
Contact* contact = _EnsureContact(msg);
if (contact != NULL) {
fMySelf = contact->GetId();
}
break;
}
case IM_CONTACT_INFO:
{
Contact* contact = _EnsureContact(msg);
@ -469,7 +343,133 @@ Server::ImMessage(BMessage* msg)
}
void
Server::AddProtocolLooper(bigtime_t instanceId, CayaProtocol* cayap)
{
ProtocolLooper* looper = new ProtocolLooper(cayap);
fLoopers.AddItem(instanceId, looper);
}
void
Server::RemoveProtocolLooper(bigtime_t instanceId)
{
}
void
Server::SendProtocolMessage(BMessage* msg)
{
// Skip null messages
if (!msg)
return;
// Check if message contains the instance field
bigtime_t id;
if (msg->FindInt64("instance", &id) == B_OK) {
bool found = false;
ProtocolLooper* looper
= fLoopers.ValueFor(id, &found);
if (found)
looper->PostMessage(msg);
}
}
void
Server::SendAllProtocolMessage(BMessage* msg)
{
// Skip null messages
if (!msg)
return;
// Send message to all protocols
for (uint32 i = 0; i < fLoopers.CountItems(); i++) {
ProtocolLooper* looper = fLoopers.ValueAt(i);
looper->PostMessage(msg);
}
}
RosterMap
Server::Contacts() const
{
return fRosterMap;
}
Contact*
Server::ContactById(BString id)
{
bool found = false;
return fRosterMap.ValueFor(id, &found);
}
void
Server::AddContact(Contact* contact)
{
fRosterMap.AddItem(contact->GetId(), contact);
}
UserMap
Server::Users() const
{
UserMap users = fUserMap;
for (int i = 0; i < fRosterMap.CountItems(); i++) {
User* user = (User*)fRosterMap.ValueAt(i);
users.AddItem(user->GetId(), user);
}
return users;
}
User*
Server::UserById(BString id)
{
bool found = false;
User* user = ContactById(id);
if (user == NULL)
user = fUserMap.ValueFor(id, &found);
return user;
}
void
Server::AddUser(User* user)
{
fUserMap.AddItem(user->GetId(), user);
}
ChatMap
Server::Conversations() const
{
return fChatMap;
}
Conversation*
Server::ConversationById(BString id)
{
bool found = false;
return fChatMap.ValueFor(id, &found);
}
void
Server::AddConversation(Conversation* chat)
{
fChatMap.AddItem(chat->GetId(), chat);
}
BString
Server::GetOwnContact()
{
return fMySelf;
@ -505,7 +505,7 @@ Server::_EnsureContact(BMessage* message)
if (contact == NULL && id.IsEmpty() == false) {
contact = new Contact(id, Looper());
contact->SetProtocolLooper(_LooperFromMessage(message));
fRosterMap.AddItem(id, contact);
AddContact(contact);
}
return contact;
@ -521,7 +521,7 @@ Server::_EnsureUser(BMessage* message)
if (user == NULL && id.IsEmpty() == false) {
user = new User(id, Looper());
user->SetProtocolLooper(_LooperFromMessage(message));
fUserMap.AddItem(id, user);
AddUser(user);
}
return user;
@ -544,6 +544,7 @@ Server::_EnsureConversation(BMessage* message)
if (!found) {
item = new Conversation(chat_id, Looper());
item->SetProtocolLooper(_LooperFromMessage(message));
item->AddUser(ContactById(fMySelf));
fChatMap.AddItem(chat_id, item);
}
}

View File

@ -30,18 +30,16 @@ typedef KeyMap<bigtime_t, ProtocolLooper*> ProtocolLoopers;
class Server: public BMessageFilter {
public:
Server();
void Quit();
void LoginAll();
virtual filter_result Filter(BMessage* message, BHandler** target);
filter_result ImMessage(BMessage* msg);
void Quit();
void AddProtocolLooper(bigtime_t instanceId,
CayaProtocol* cayap);
void RemoveProtocolLooper(bigtime_t instanceId);
void LoginAll();
void SendProtocolMessage(BMessage* msg);
void SendAllProtocolMessage(BMessage* msg);
@ -58,7 +56,7 @@ public:
void AddConversation(Conversation* chat);
// TODO: there should be a contact for each account.
Contact* GetOwnContact();
BString GetOwnContact();
private:
ProtocolLooper* _LooperFromMessage(BMessage* message);
@ -73,7 +71,7 @@ private:
UserMap fUserMap;
ChatMap fChatMap;
ProtocolLoopers fLoopers;
Contact* fMySelf;
BString fMySelf;
};