Skip to content

Commit

Permalink
Merge branch 'task/cht-898-refactor-chatd-priv' into 'develop'
Browse files Browse the repository at this point in the history
CHT-898: Refactor chatd::priv

See merge request megachat/MEGAchat!1804
  • Loading branch information
Jose Manuel Otero Mato committed Jan 23, 2024
2 parents 45f554f + d52f4c0 commit 7d674dc
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 55 deletions.
24 changes: 12 additions & 12 deletions src/chatClient.cpp
Expand Up @@ -862,7 +862,7 @@ void Client::createPublicChatRoom(uint64_t chatId, uint64_t ph, int shard, const
bool meeting, const ::mega::ChatOptions_t opts, const mega::MegaScheduledMeetingList* smList)
{
GroupChatRoom* room = new GroupChatRoom(*chats, chatId, static_cast<unsigned char>(shard),
chatd::Priv::PRIV_RDONLY, ts, false, decryptedTitle, ph,
chatd::Priv::PRIV_RO, ts, false, decryptedTitle, ph,
unifiedKey, meeting, opts, smList);

chats->emplace(chatId, room);
Expand Down Expand Up @@ -2854,10 +2854,10 @@ bool ChatRoom::syncOwnPriv(chatd::Priv priv)

if(previewMode())
{
assert(mOwnPriv == chatd::PRIV_RDONLY
|| mOwnPriv == chatd::PRIV_NOTPRESENT); // still in preview, but ph is invalid
assert(mOwnPriv == chatd::PRIV_RO
|| mOwnPriv == chatd::PRIV_RM); // still in preview, but ph is invalid

if (priv >= chatd::PRIV_RDONLY)
if (priv >= chatd::PRIV_RO)
{
//Join
mChat->setPublicHandle(Id::inval());
Expand Down Expand Up @@ -3254,7 +3254,7 @@ void GroupChatRoom::notifySchedMeetingOccurrencesUpdated(bool append)

void GroupChatRoom::setRemoved()
{
mOwnPriv = chatd::PRIV_NOTPRESENT;
mOwnPriv = chatd::PRIV_RM;
parent.mKarereClient.db.query("update chats set own_priv=? where chatid=?", mOwnPriv, mChatid);
notifyExcludedFromChat();
}
Expand Down Expand Up @@ -3648,7 +3648,7 @@ promise::Promise<void> GroupChatRoom::autojoinPublicChat(uint64_t ph)
})
.then([this, myHandle](ReqResult)
{
onUserJoin(parent.mKarereClient.myHandle(), chatd::PRIV_FULL);
onUserJoin(parent.mKarereClient.myHandle(), chatd::PRIV_STANDARD);
})
.fail([this](const ::promise::Error&)
{
Expand Down Expand Up @@ -3986,8 +3986,8 @@ void ChatRoom::notifyChatOptionsChanged(int option)

void GroupChatRoom::enablePreview(uint64_t ph)
{
// Current priv is PRIV_NOTPRESENT and need to be updated
mOwnPriv = chatd::PRIV_RDONLY;
// Current priv is PRIV_RM and need to be updated
mOwnPriv = chatd::PRIV_RO;
parent.mKarereClient.db.query("update chats set own_priv = ? where chatid = ?", mOwnPriv, mChatid);
if (mRoomGui)
{
Expand Down Expand Up @@ -4243,9 +4243,9 @@ bool GroupChatRoom::syncWithApi(const mega::MegaTextChat& chat)
bool ownPrivChanged = syncOwnPriv((chatd::Priv) chat.getOwnPrivilege());
if (ownPrivChanged)
{
if (oldPriv == chatd::PRIV_NOTPRESENT)
if (oldPriv == chatd::PRIV_RM)
{
if (mOwnPriv != chatd::PRIV_NOTPRESENT)
if (mOwnPriv != chatd::PRIV_RM)
{
// in case chat-link was invalidated during preview, the room was disabled
// now, we upgrade from (invalid) previewer to participant --> enable it back
Expand Down Expand Up @@ -4273,7 +4273,7 @@ bool GroupChatRoom::syncWithApi(const mega::MegaTextChat& chat)
notifyRejoinedChat();
}
}
else if (mOwnPriv == chatd::PRIV_NOTPRESENT)
else if (mOwnPriv == chatd::PRIV_RM)
{
//we were excluded
KR_LOG_DEBUG("Chatroom[%s]: API event: We were removed", ID_CSTR(mChatid));
Expand Down Expand Up @@ -5014,7 +5014,7 @@ promise::Promise<ChatRoom*> Contact::createChatRoom()
return Promise<ChatRoom*>(mChatRoom);
}
mega::MegaTextChatPeerListPrivate peers;
peers.addPeer(mUserid, chatd::PRIV_OPER);
peers.addPeer(mUserid, chatd::PRIV_MODERATOR);
return mClist.client.api.call(&mega::MegaApi::createChat, false, &peers, nullptr, mega::ChatOptions::kEmpty, nullptr)
.then([this](ReqResult result) -> Promise<ChatRoom*>
{
Expand Down
2 changes: 1 addition & 1 deletion src/chatClient.h
Expand Up @@ -172,7 +172,7 @@ class ChatRoom: public chatd::Listener, public DeleteTrackable
/** @brief Whether we are currently member of the chatroom (for group
* chats), or we are contacts with the peer (for 1on1 chats)
*/
bool isActive() const { return mIsGroup ? (mOwnPriv != chatd::PRIV_NOTPRESENT) : true; }
bool isActive() const { return mIsGroup ? (mOwnPriv != chatd::PRIV_RM) : true; }

/** @brief The online state reported by chatd for that chatroom */
chatd::ChatState chatdOnlineState() const { return mChat->onlineState(); }
Expand Down
20 changes: 10 additions & 10 deletions src/chatd.cpp
Expand Up @@ -1644,7 +1644,7 @@ void Chat::join()
//Reset handshake state, as we may be reconnecting
mServerFetchState = kHistNotFetching;
CHATID_LOG_DEBUG("Sending JOIN");
sendCommand(Command(OP_JOIN) + mChatId + mChatdClient.mMyHandle + (int8_t)PRIV_NOCHANGE);
sendCommand(Command(OP_JOIN) + mChatId + mChatdClient.mMyHandle + (int8_t)PRIV_UNKNOWN);
requestHistoryFromServer(-static_cast<int32_t>(initialHistoryFetchCount));
}

Expand All @@ -1661,7 +1661,7 @@ void Chat::handlejoin()
uint64_t ph = getPublicHandle();
Command comm (OP_HANDLEJOIN);
comm.append((const char*) &ph, Id::CHATLINKHANDLE);
sendCommand(comm + mChatdClient.mMyHandle + (uint8_t)PRIV_RDONLY);
sendCommand(comm + mChatdClient.mMyHandle + (uint8_t)PRIV_RO);
requestHistoryFromServer(-static_cast<int32_t>(initialHistoryFetchCount));
}

Expand All @@ -1686,7 +1686,7 @@ void Chat::onHandleJoinRejected()
CHATID_LOG_WARNING("HANDLEJOIN was rejected, setting chat offline and disabling it");
disable(true);

// public-handle is not valid anymore --> notify the app: privilege is now PRIV_NOTPRESENT
// public-handle is not valid anymore --> notify the app: privilege is now PRIV_RM
CALL_LISTENER(onUserLeave, Id::null());
}

Expand Down Expand Up @@ -1826,7 +1826,7 @@ HistSource Chat::getHistoryFromDbOrServer(unsigned count)
return kHistSourceNone;
}

if (previewMode() && mOwnPrivilege == PRIV_NOTPRESENT)
if (previewMode() && mOwnPrivilege == PRIV_RM)
{
CHATID_LOG_DEBUG("getHistoryFromDbOrServer: no more history available for invalid chat-link");
return kHistSourceNotLoggedIn;
Expand Down Expand Up @@ -2167,7 +2167,7 @@ void Connection::execCommand(const StaticBuffer& buf)
}

auto& chat = mChatdClient.chats(chatid);
if (priv == PRIV_NOTPRESENT)
if (priv == PRIV_RM)
chat.onUserLeave(userid);
else
chat.onUserJoin(userid, priv);
Expand Down Expand Up @@ -3500,7 +3500,7 @@ Message* Chat::msgSubmit(const char* msg, size_t msglen, unsigned char type, voi
return NULL;
}

if (mOwnPrivilege == PRIV_NOTPRESENT)
if (mOwnPrivilege == PRIV_RM)
{
CHATID_LOG_WARNING("msgSubmit: Denying sending message because we don't participate in the chat");
return NULL;
Expand Down Expand Up @@ -4004,7 +4004,7 @@ void Chat::setPublicHandle(uint64_t ph)

if (Id(ph).isValid())
{
mOwnPrivilege = PRIV_RDONLY;
mOwnPrivilege = PRIV_RO;
}
}

Expand Down Expand Up @@ -4340,7 +4340,7 @@ Message* Chat::msgRemoveFromSending(const Id& msgxid, const Id& msgid)

if (!msgid) // message was rejected by chatd
{
moveItemToManualSending(mSending.begin(), (mOwnPrivilege < PRIV_FULL)
moveItemToManualSending(mSending.begin(), (mOwnPrivilege < PRIV_STANDARD)
? kManualSendNoWriteAccess
: kManualSendGeneralReject); //deletes item
return nullptr;
Expand Down Expand Up @@ -5797,13 +5797,13 @@ void Chat::onUserLeave(const Id& userid)
return;
}

mOwnPrivilege = PRIV_NOTPRESENT;
mOwnPrivilege = PRIV_RM;
disable(isPreviewerIdRecv); // the ph is invalid -> do not keep trying to login into chatd anymore
onPreviewersUpdate(0);

if (isPreviewerIdRecv)
{
// notify that our own user permission (in preview mode) has been updated to PRIV_NOTPRESENT
// notify that our own user permission (in preview mode) has been updated to PRIV_RM
// probably chat-link has been invalidated, so chatd send us a JOIN command with priv -1
CHATID_LOG_DEBUG("our own user permission (in preview mode) has been updated to not present (-1)");
CALL_LISTENER(onUserLeave, userid);
Expand Down
2 changes: 1 addition & 1 deletion src/chatdICrypto.h
Expand Up @@ -84,7 +84,7 @@ class ICrypto

/**
* @brief A user has joined, or their privilege has changed
* @param privilege - the new privilege, if it is PRIV_NOTPRESENT, then the user
* @param privilege - the new privilege, if it is PRIV_RM, then the user
* left the chat
*/
virtual void onUserJoin(karere::Id /*userid*/){}
Expand Down
34 changes: 17 additions & 17 deletions src/chatdMsg.h
Expand Up @@ -573,11 +573,11 @@ enum Opcode
enum Priv: signed char
{
PRIV_INVALID = -10,
PRIV_NOCHANGE = -2,
PRIV_NOTPRESENT = -1,
PRIV_RDONLY = 0,
PRIV_FULL = 2,
PRIV_OPER = 3
PRIV_UNKNOWN = -2,
PRIV_RM = -1,
PRIV_RO = 0,
PRIV_STANDARD = 2,
PRIV_MODERATOR = 3
};

class Message: public Buffer
Expand Down Expand Up @@ -669,8 +669,8 @@ class Message: public Buffer
* \c kMsgPrivChange - the new privilege of the user whose handle is in \c target
* \c kMsgAlterParticipants - this is used as a flag to specify whether the user
* was:
* - removed - the value is \c PRIV_NOTPRESENT
* - added - the value of \c PRIV_NOCHANGE
* - removed - the value is \c PRIV_RM
* - added - the value of \c PRIV_UNKNOWN
*/
Priv privilege = PRIV_INVALID;
};
Expand Down Expand Up @@ -1659,18 +1659,18 @@ static inline const char* privToString(Priv priv)
{
switch (priv)
{
case PRIV_NOCHANGE:
case PRIV_UNKNOWN:
return "No change";
case PRIV_NOTPRESENT:
return "Not present";
case PRIV_RDONLY:
return "READONLY";
case PRIV_FULL:
return "READ_WRITE";
case PRIV_OPER:
return "OPERATOR";
case PRIV_RM:
return "removed";
case PRIV_RO:
return "read-only";
case PRIV_STANDARD:
return "standard";
case PRIV_MODERATOR:
return "moderator";
default:
return "(unknown privilege)";
return "unknown privilege";
}
}
#ifdef __GNUC__
Expand Down
22 changes: 11 additions & 11 deletions src/megachatapi_impl.cpp
Expand Up @@ -1215,7 +1215,7 @@ int MegaChatApiImpl::performRequest_setPrivateMode(MegaChatRequestPrivate *reque
return MegaChatError::ERROR_TOOMANY;
}

if (room->ownPriv() != chatd::PRIV_OPER)
if (room->ownPriv() != chatd::PRIV_MODERATOR)
{
return MegaChatError::ERROR_ACCESS;
}
Expand Down Expand Up @@ -1267,8 +1267,8 @@ int MegaChatApiImpl::performRequest_chatLinkHandle(MegaChatRequestPrivate *reque

// anyone can retrieve an existing link, but only operator can create/delete it
int ownPriv = room->ownPriv();
if ((ownPriv == Priv::PRIV_NOTPRESENT)
|| ((del || createifmissing) && ownPriv != Priv::PRIV_OPER))
if ((ownPriv == Priv::PRIV_RM)
|| ((del || createifmissing) && ownPriv != Priv::PRIV_MODERATOR))
{
return MegaChatError::ERROR_ACCESS;
}
Expand Down Expand Up @@ -1755,7 +1755,7 @@ int MegaChatApiImpl::performRequest_startChatCall(MegaChatRequestPrivate* reques
return MegaChatError::ERROR_ACCESS;
}
}
else if (chatroom->ownPriv() <= Priv::PRIV_RDONLY)
else if (chatroom->ownPriv() <= Priv::PRIV_RO)
{
API_LOG_ERROR("Start call - Refusing start a call withouth enough privileges");
return MegaChatError::ERROR_ACCESS;
Expand All @@ -1776,7 +1776,7 @@ int MegaChatApiImpl::performRequest_startChatCall(MegaChatRequestPrivate* reques
bool enableAudio = request->getParamType();
if (enableAudio
&& chatroom->isSpeakRequest()
&& chatroom->ownPriv() != Priv::PRIV_OPER)
&& chatroom->ownPriv() != Priv::PRIV_MODERATOR)
{
API_LOG_ERROR("Start call - can't start a call with audio enabled "
"if speak request is enabled for chatroom and we are non-host");
Expand Down Expand Up @@ -1915,7 +1915,7 @@ int MegaChatApiImpl::performRequest_answerChatCall(MegaChatRequestPrivate* reque
bool enableAudio = request->getParamType();
if (enableAudio
&& chatroom->isSpeakRequest()
&& chatroom->ownPriv() != Priv::PRIV_OPER)
&& chatroom->ownPriv() != Priv::PRIV_MODERATOR)
{
API_LOG_ERROR("Answer call - can't answer a call with audio enabled "
"if speak request is enabled for chatroom and we are non-host");
Expand Down Expand Up @@ -9213,7 +9213,7 @@ void MegaChatRoomHandler::onUserJoin(Id userid, Priv privilege)
mRoom->onUserJoin(userid, privilege);

// avoid to notify if own user doesn't participate or isn't online and it's a public chat (for large chat-links, for performance)
if (mRoom->publicChat() && (mRoom->chat().onlineState() != kChatStateOnline || mRoom->chat().getOwnprivilege() == chatd::Priv::PRIV_NOTPRESENT))
if (mRoom->publicChat() && (mRoom->chat().onlineState() != kChatStateOnline || mRoom->chat().getOwnprivilege() == chatd::Priv::PRIV_RM))
{
return;
}
Expand All @@ -9238,7 +9238,7 @@ void MegaChatRoomHandler::onUserLeave(Id userid)
// forward the event to the chatroom, so chatlist items also receive the notification
mRoom->onUserLeave(userid);

if (mRoom->publicChat() && mRoom->chat().getOwnprivilege() == chatd::Priv::PRIV_NOTPRESENT)
if (mRoom->publicChat() && mRoom->chat().getOwnprivilege() == chatd::Priv::PRIV_RM)
{
return;
}
Expand Down Expand Up @@ -10791,7 +10791,7 @@ void MegaChatGroupListItemHandler::onUserJoin(uint64_t userid, Priv priv)
bool ownChange = (userid == chatApi.getMyUserHandle());

// avoid to notify if own user doesn't participate or isn't online and it's a public chat (for large chat-links, for performance)
if (!ownChange && mRoom.publicChat() && (mRoom.chat().onlineState() != kChatStateOnline || mRoom.chat().getOwnprivilege() == chatd::Priv::PRIV_NOTPRESENT))
if (!ownChange && mRoom.publicChat() && (mRoom.chat().onlineState() != kChatStateOnline || mRoom.chat().getOwnprivilege() == chatd::Priv::PRIV_RM))
{
return;
}
Expand All @@ -10811,7 +10811,7 @@ void MegaChatGroupListItemHandler::onUserJoin(uint64_t userid, Priv priv)

void MegaChatGroupListItemHandler::onUserLeave(uint64_t )
{
if (mRoom.publicChat() && mRoom.chat().getOwnprivilege() == chatd::Priv::PRIV_NOTPRESENT)
if (mRoom.publicChat() && mRoom.chat().getOwnprivilege() == chatd::Priv::PRIV_RM)
{
return;
}
Expand Down Expand Up @@ -10943,7 +10943,7 @@ MegaChatMessagePrivate::MegaChatMessagePrivate(const Message &msg, Message::Stat
edited = msg.updated && msg.size();
deleted = msg.updated && !msg.size();
mCode = 0;
priv = PRIV_UNKNOWN;
priv = MegaChatPeerList::PRIV_UNKNOWN;
hAction = MEGACHAT_INVALID_HANDLE;

switch (type)
Expand Down
6 changes: 3 additions & 3 deletions src/strongvelope/strongvelope.cpp
Expand Up @@ -285,7 +285,7 @@ ParsedMessage::ParsedMessage(const Message& binaryMessage, ProtocolHandler& prot
assert(managementInfo);
if (managementInfo->target || managementInfo->privilege != PRIV_INVALID)
throw std::runtime_error("TLV_TYPE_INC_PARTICIPANT: Already parsed an incompatible TLV record");
managementInfo->privilege = chatd::PRIV_NOCHANGE;
managementInfo->privilege = chatd::PRIV_UNKNOWN;
managementInfo->target = record.read<uint64_t>();
}
else
Expand All @@ -301,7 +301,7 @@ ParsedMessage::ParsedMessage(const Message& binaryMessage, ProtocolHandler& prot
assert(managementInfo);
if (managementInfo->target || managementInfo->privilege != PRIV_INVALID)
throw std::runtime_error("TLV_TYPE_EXC_PARTICIPANT: Already parsed an incompatible TLV record");
managementInfo->privilege = chatd::PRIV_NOTPRESENT;
managementInfo->privilege = chatd::PRIV_RM;
managementInfo->target = record.read<uint64_t>();
}
else
Expand Down Expand Up @@ -1793,7 +1793,7 @@ std::string Message::managementInfoToString() const
{
auto& info = mgmtInfo();
ret.append("User ").append(userid.toString())
.append((info.privilege == chatd::PRIV_NOTPRESENT) ? " removed" : " added")
.append((info.privilege == chatd::PRIV_RM) ? " removed" : " added")
.append(" user ").append(info.target.toString());
return ret;
}
Expand Down

0 comments on commit 7d674dc

Please sign in to comment.