Use 'User' over 'Contact' where appropriate

This commit is contained in:
Jaidyn Ann 2021-05-31 11:56:45 -05:00
parent 63d8bbef26
commit 0581bf1df9
6 changed files with 77 additions and 39 deletions

View File

@ -141,7 +141,7 @@ Conversation::Users()
} }
Contact* User*
Conversation::UserById(BString id) Conversation::UserById(BString id)
{ {
bool found = false; bool found = false;
@ -260,14 +260,14 @@ Conversation::_EnsureLogPath()
} }
Contact* User*
Conversation::_EnsureUser(BMessage* msg) Conversation::_EnsureUser(BMessage* msg)
{ {
BString id = msg->FindString("user_id"); BString id = msg->FindString("user_id");
if (id.IsEmpty() == true) return NULL; if (id.IsEmpty() == true) return NULL;
Contact* user = UserById(id); User* user = UserById(id);
Contact* serverUser = _GetServer()->ContactById(id); User* serverUser = _GetServer()->UserById(id);
if (user == NULL && serverUser != NULL) { if (user == NULL && serverUser != NULL) {
fUsers.AddItem(id, serverUser); fUsers.AddItem(id, serverUser);
@ -275,10 +275,10 @@ Conversation::_EnsureUser(BMessage* msg)
GetView()->UpdateUserList(fUsers); GetView()->UpdateUserList(fUsers);
} }
else if (user == NULL) { else if (user == NULL) {
user = new Contact(id, _GetServer()->Looper()); user = new User(id, _GetServer()->Looper());
user->SetProtocolLooper(fLooper); user->SetProtocolLooper(fLooper);
_GetServer()->AddContact(user); _GetServer()->AddUser(user);
fUsers.AddItem(id, user); fUsers.AddItem(id, user);
GetView()->UpdateUserList(fUsers); GetView()->UpdateUserList(fUsers);
} }

View File

@ -12,17 +12,16 @@
#include <libsupport/KeyMap.h> #include <libsupport/KeyMap.h>
#include "Observer.h" #include "Observer.h"
#include "Server.h"
#include "User.h" #include "User.h"
class ChatWindow;
class Contact;
class ConversationItem; class ConversationItem;
class ConversationView; class ConversationView;
class ProtocolLooper; class ProtocolLooper;
class Server; class Server;
typedef KeyMap<BString, Contact*> UserMap; typedef KeyMap<BString, User*> UserMap;
class Conversation : public Observer { class Conversation : public Observer {
@ -53,7 +52,7 @@ public:
BString GetName() const; BString GetName() const;
UserMap Users(); UserMap Users();
Contact* UserById(BString id); User* UserById(BString id);
void AddUser(User* user); void AddUser(User* user);
private: private:
@ -61,7 +60,7 @@ private:
BStringList _GetChatLogs(); BStringList _GetChatLogs();
void _EnsureLogPath(); void _EnsureLogPath();
Contact* _EnsureUser(BMessage* msg); User* _EnsureUser(BMessage* msg);
Server* _GetServer(); Server* _GetServer();
BMessenger fMessenger; BMessenger fMessenger;

View File

@ -188,6 +188,38 @@ Server::AddContact(Contact* 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 ChatMap
Server::Conversations() const Server::Conversations() const
{ {
@ -305,17 +337,17 @@ Server::ImMessage(BMessage* msg)
} }
case IM_AVATAR_SET: case IM_AVATAR_SET:
{ {
Contact* contact = _EnsureContact(msg); User* user = _EnsureUser(msg);
if (!contact) if (!user)
break; break;
entry_ref ref; entry_ref ref;
if (msg->FindRef("ref", &ref) == B_OK) { if (msg->FindRef("ref", &ref) == B_OK) {
BBitmap* bitmap = BTranslationUtils::GetBitmap(&ref); BBitmap* bitmap = BTranslationUtils::GetBitmap(&ref);
contact->SetNotifyAvatarBitmap(bitmap); user->SetNotifyAvatarBitmap(bitmap);
} else } else
contact->SetNotifyAvatarBitmap(NULL); user->SetNotifyAvatarBitmap(NULL);
break; break;
} }
case IM_CREATE_CHAT: case IM_CREATE_CHAT:
@ -330,7 +362,7 @@ Server::ImMessage(BMessage* msg)
case IM_CHAT_CREATED: case IM_CHAT_CREATED:
{ {
Conversation* chat = _EnsureConversation(msg); Conversation* chat = _EnsureConversation(msg);
User* user = _EnsureContact(msg); User* user = _EnsureUser(msg);
if (chat != NULL && user != NULL) { if (chat != NULL && user != NULL) {
chat->AddUser(user); chat->AddUser(user);
@ -464,29 +496,11 @@ Server::_LooperFromMessage(BMessage* message)
} }
Contact*
Server::_GetContact(BMessage* message)
{
if (!message)
return NULL;
BString id = message->FindString("user_id");
Contact* item = NULL;
if (id.IsEmpty() == false) {
bool found = false;
item = fRosterMap.ValueFor(id, &found);
}
return item;
}
Contact* Contact*
Server::_EnsureContact(BMessage* message) Server::_EnsureContact(BMessage* message)
{ {
Contact* contact = _GetContact(message);
BString id = message->FindString("user_id"); BString id = message->FindString("user_id");
Contact* contact = ContactById(id);
if (contact == NULL && id.IsEmpty() == false) { if (contact == NULL && id.IsEmpty() == false) {
contact = new Contact(id, Looper()); contact = new Contact(id, Looper());
@ -498,6 +512,22 @@ Server::_EnsureContact(BMessage* message)
} }
User*
Server::_EnsureUser(BMessage* message)
{
BString id = message->FindString("user_id");
User* user = UserById(id);
if (user == NULL && id.IsEmpty() == false) {
user = new User(id, Looper());
user->SetProtocolLooper(_LooperFromMessage(message));
fUserMap.AddItem(id, user);
}
return user;
}
Conversation* Conversation*
Server::_EnsureConversation(BMessage* message) Server::_EnsureConversation(BMessage* message)
{ {

View File

@ -14,6 +14,7 @@
#include "CayaConstants.h" #include "CayaConstants.h"
#include "Contact.h" #include "Contact.h"
#include "Conversation.h" #include "Conversation.h"
#include "User.h"
class CayaProtocol; class CayaProtocol;
class RosterItem; class RosterItem;
@ -21,6 +22,7 @@ class ProtocolLooper;
typedef KeyMap<BString, Contact*> RosterMap; typedef KeyMap<BString, Contact*> RosterMap;
typedef KeyMap<BString, User*> UserMap;
typedef KeyMap<BString, Conversation*> ChatMap; typedef KeyMap<BString, Conversation*> ChatMap;
typedef KeyMap<bigtime_t, ProtocolLooper*> ProtocolLoopers; typedef KeyMap<bigtime_t, ProtocolLooper*> ProtocolLoopers;
@ -47,6 +49,10 @@ public:
Contact* ContactById(BString id); Contact* ContactById(BString id);
void AddContact(Contact* contact); void AddContact(Contact* contact);
UserMap Users() const;
User* UserById(BString id);
void AddUser(User* user);
ChatMap Conversations() const; ChatMap Conversations() const;
Conversation* ConversationById(BString id); Conversation* ConversationById(BString id);
void AddConversation(Conversation* chat); void AddConversation(Conversation* chat);
@ -56,12 +62,15 @@ public:
private: private:
ProtocolLooper* _LooperFromMessage(BMessage* message); ProtocolLooper* _LooperFromMessage(BMessage* message);
Contact* _GetContact(BMessage* message);
Contact* _EnsureContact(BMessage* message); Contact* _EnsureContact(BMessage* message);
User* _EnsureUser(BMessage* message);
Conversation* _EnsureConversation(BMessage* message); Conversation* _EnsureConversation(BMessage* message);
void _ReplicantStatusNotify(CayaStatus status); void _ReplicantStatusNotify(CayaStatus status);
RosterMap fRosterMap; RosterMap fRosterMap;
UserMap fUserMap;
ChatMap fChatMap; ChatMap fChatMap;
ProtocolLoopers fLoopers; ProtocolLoopers fLoopers;
Contact* fMySelf; Contact* fMySelf;

View File

@ -21,9 +21,9 @@
#include "CayaPreferences.h" #include "CayaPreferences.h"
#include "CayaProtocolMessages.h" #include "CayaProtocolMessages.h"
#include "CayaRenderView.h" #include "CayaRenderView.h"
#include "Contact.h"
#include "Conversation.h" #include "Conversation.h"
#include "NotifyMessage.h" #include "NotifyMessage.h"
#include "User.h"
#include "UserItem.h" #include "UserItem.h"
#include "UserListView.h" #include "UserListView.h"

View File

@ -16,7 +16,7 @@ class BTextView;
class BitmapView; class BitmapView;
class CayaRenderView; class CayaRenderView;
class Contact; class User;
class UserListView; class UserListView;
@ -54,7 +54,7 @@ private:
void _AppendMessage(BMessage* msg); void _AppendMessage(BMessage* msg);
Conversation* fConversation; Conversation* fConversation;
Contact* fContact; User* fContact;
int32 fMessageCount; int32 fMessageCount;
BObjectList<BMessage> fMessageQueue; BObjectList<BMessage> fMessageQueue;