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

View File

@ -12,17 +12,16 @@
#include <libsupport/KeyMap.h>
#include "Observer.h"
#include "Server.h"
#include "User.h"
class ChatWindow;
class Contact;
class ConversationItem;
class ConversationView;
class ProtocolLooper;
class Server;
typedef KeyMap<BString, Contact*> UserMap;
typedef KeyMap<BString, User*> UserMap;
class Conversation : public Observer {
@ -53,7 +52,7 @@ public:
BString GetName() const;
UserMap Users();
Contact* UserById(BString id);
User* UserById(BString id);
void AddUser(User* user);
private:
@ -61,7 +60,7 @@ private:
BStringList _GetChatLogs();
void _EnsureLogPath();
Contact* _EnsureUser(BMessage* msg);
User* _EnsureUser(BMessage* msg);
Server* _GetServer();
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
Server::Conversations() const
{
@ -305,17 +337,17 @@ Server::ImMessage(BMessage* msg)
}
case IM_AVATAR_SET:
{
Contact* contact = _EnsureContact(msg);
if (!contact)
User* user = _EnsureUser(msg);
if (!user)
break;
entry_ref ref;
if (msg->FindRef("ref", &ref) == B_OK) {
BBitmap* bitmap = BTranslationUtils::GetBitmap(&ref);
contact->SetNotifyAvatarBitmap(bitmap);
user->SetNotifyAvatarBitmap(bitmap);
} else
contact->SetNotifyAvatarBitmap(NULL);
user->SetNotifyAvatarBitmap(NULL);
break;
}
case IM_CREATE_CHAT:
@ -330,7 +362,7 @@ Server::ImMessage(BMessage* msg)
case IM_CHAT_CREATED:
{
Conversation* chat = _EnsureConversation(msg);
User* user = _EnsureContact(msg);
User* user = _EnsureUser(msg);
if (chat != NULL && user != NULL) {
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*
Server::_EnsureContact(BMessage* message)
{
Contact* contact = _GetContact(message);
BString id = message->FindString("user_id");
Contact* contact = ContactById(id);
if (contact == NULL && id.IsEmpty() == false) {
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*
Server::_EnsureConversation(BMessage* message)
{

View File

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

View File

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

View File

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