Cache roomflags, autojoin/autocreate based on them

Fixes #3
This commit is contained in:
Jaidyn Ann 2021-06-13 02:34:11 -05:00
parent 1124e3cf2b
commit b2489a5551
4 changed files with 58 additions and 8 deletions

View File

@ -103,6 +103,7 @@ Conversation::ImMessage(BMessage* msg)
int32 disabledFlags;
if (msg->FindInt32("room_disallowed_flags", &disabledFlags) == B_OK)
fDisallowedFlags = disabledFlags;
_CacheRoomFlags();
break;
}
case IM_ROOM_PARTICIPANT_JOINED:
@ -205,6 +206,7 @@ void
Conversation::SetProtocolLooper(ProtocolLooper* looper)
{
fLooper = looper;
_LoadRoomFlags();
}
@ -384,6 +386,30 @@ Conversation::_GetChatLogs(BMessage* msg)
}
void
Conversation::_CacheRoomFlags()
{
_EnsureCachePath();
BFile cacheFile(fCachePath.Path(), B_READ_WRITE | B_CREATE_FILE);
if (cacheFile.InitCheck() != B_OK)
return;
cacheFile.WriteAttr("Caya:flags", B_INT32_TYPE, 0, &fRoomFlags, sizeof(int32));
}
void
Conversation::_LoadRoomFlags()
{
_EnsureCachePath();
BFile cacheFile(fCachePath.Path(), B_READ_ONLY);
if (cacheFile.InitCheck() != B_OK)
return;
cacheFile.ReadAttr("Caya:flags", B_INT32_TYPE, 0, &fRoomFlags, sizeof(int32));
}
void
Conversation::_EnsureCachePath()
{

View File

@ -68,10 +68,15 @@ public:
void SetRole(BString id, Role* role);
Role* GetRole(BString id);
int32 GetFlags(int32 flags) { return fRoomFlags; }
private:
void _LogChatMessage(BMessage* msg);
status_t _GetChatLogs(BMessage* msg);
void _CacheRoomFlags();
void _LoadRoomFlags();
void _EnsureCachePath();
User* _EnsureUser(BMessage* msg);

View File

@ -30,6 +30,7 @@
#include "InviteDialogue.h"
#include "ProtocolLooper.h"
#include "ProtocolManager.h"
#include "RoomFlags.h"
#include "RosterItem.h"
#include "Server.h"
@ -489,8 +490,25 @@ Server::ImMessage(BMessage* msg)
while (dir.GetNextEntry(&entry, true) == B_OK)
if (entry.GetName(fileName) == B_OK) {
int32 flags;
BFile file(&entry, B_READ_ONLY);
if (file.InitCheck() != B_OK)
continue;
if (file.ReadAttr("Caya:flags", B_INT32_TYPE, 0, &flags,
sizeof(int32)) < 0)
continue;
if (!(flags & ROOM_AUTOJOIN) && !(flags & ROOM_AUTOCREATE))
continue;
BMessage join(IM_MESSAGE);
join.AddInt32("im_what", IM_JOIN_ROOM);
int32 im_what = IM_JOIN_ROOM;
if (flags & ROOM_AUTOCREATE) {
im_what = IM_CREATE_CHAT;
join.AddString("user_id", fileName);
}
join.AddInt32("im_what", im_what);
join.AddString("chat_id", fileName);
looper->PostMessage(&join);
}

View File

@ -136,14 +136,15 @@ JabberHandler::Process(BMessage* msg)
}
case IM_CREATE_CHAT: {
const char* invite_id = msg->FindString("user_id");
const char* user_id = msg->FindString("user_id");
// TODO: Contact validation, make sure permssion is granted
if (!invite_id)
if (!user_id)
return B_ERROR;
_ChatCreatedMsg(invite_id);
_EnsureUserChat(user_id);
_ChatCreatedMsg(user_id);
break;
}
@ -213,14 +214,14 @@ JabberHandler::Process(BMessage* msg)
gloox::MUCRoom* room = fRooms.ValueFor(chat_id);
if (room != NULL)
room->getRoomInfo();
else if (fUserChats.HasString(chat_id) == true)
if (fUserChats.HasString(chat_id) == true)
{
BMessage metadata(IM_MESSAGE);
metadata.AddInt32("im_what", IM_ROOM_METADATA);
metadata.AddString("chat_id", chat_id);
metadata.AddInt32("room_default_flags",
0 | ROOM_AUTOCREATE | ROOM_LOG_LOCALLY | ROOM_POPULATE_LOGS);
metadata.AddInt32("room_disallowed_flags", 0 | ROOM_AUTOJOIN);
metadata.AddInt32("room_default_flags", 0 | ROOM_LOG_LOCALLY | ROOM_POPULATE_LOGS);
metadata.AddInt32("room_disallowed_flags", 0 | ROOM_AUTOJOIN | ROOM_AUTOCREATE);
_SendMessage(&metadata);
}
break;
}