Skip to content

Commit

Permalink
Merge branch 'task/cht-1178-fix-open-preview' into 'develop'
Browse files Browse the repository at this point in the history
CHT-1178. Fix chatlink preview process

See merge request megachat/MEGAchat!1851
  • Loading branch information
jgandres committed Mar 1, 2024
2 parents 3fc1761 + 69722c4 commit 6fe3591
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
29 changes: 20 additions & 9 deletions src/chatClient.cpp
Expand Up @@ -2845,29 +2845,40 @@ chatd::Priv PeerChatRoom::getSdkRoomPeerPriv(const mega::MegaTextChat &chat)
return (chatd::Priv) peers->getPeerPrivilege(0);
}

bool ChatRoom::syncOwnPriv(chatd::Priv priv)
bool ChatRoom::syncOwnPriv(chatd::Priv newPriv)
{
if (mOwnPriv == priv)
if (mOwnPriv == newPriv)
{
return false;
}

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

if (!validPrivInPreviewMode)
{
KR_LOG_ERROR("syncOwnPriv: invalid current privilege in preview mode for chat: ",
karere::Id(chatid()).toString().c_str());
assert(false);
}

if (priv >= chatd::PRIV_RO)
if (newPriv >= chatd::PRIV_RO)
{
//Join
KR_LOG_DEBUG("syncOwnPriv: invalidating ph as we are not in preview mode for chat: ",
karere::Id(chatid()).toString().c_str());

// Join
mChat->setPublicHandle(Id::inval());

//Remove preview mode flag from DB
// Remove preview mode flag from DB
parent.mKarereClient.db.query("update chats set mode = '1' where chatid = ?", mChatid);
}
}

mOwnPriv = priv;
mOwnPriv = newPriv;
parent.mKarereClient.db.query("update chats set own_priv = ? where chatid = ?", mOwnPriv, mChatid);
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/chatClient.h
Expand Up @@ -103,7 +103,7 @@ class ChatRoom: public chatd::Listener, public DeleteTrackable
std::shared_ptr<std::string> unifiedKey = nullptr, int isUnifiedKeyEncrypted = false, const karere::Id& = karere::Id::inval() ); //We can't do the join in the ctor, as chatd may fire callbcks synchronously from join(), and the derived class will not be constructed at that point.
void notifyExcludedFromChat();
void notifyRejoinedChat();
bool syncOwnPriv(chatd::Priv priv);
bool syncOwnPriv(chatd::Priv newPriv);
bool syncArchive(bool aIsArchived);
void onMessageTimestamp(uint32_t ts);
ApiPromise requestGrantAccess(mega::MegaNode *node, mega::MegaHandle userHandle);
Expand Down
32 changes: 21 additions & 11 deletions src/megachatapi_impl.cpp
Expand Up @@ -1135,41 +1135,51 @@ int MegaChatApiImpl::performRequest_loadPreview(MegaChatRequestPrivate *request)

if (room) // chatroom already exists
{
if (hasChanged)
if (room->previewMode() &&
hasChanged) // update existing chat in preview mode
{
// if mcphurl information is different respect GroupChatRoom in ram
// we need to remove preview and recreate again, this is simplier than update
// groupchatroom field by field
API_LOG_DEBUG("Chat (%s) has changed", karere::Id(chatId).toString().c_str());
API_LOG_DEBUG("performRequest_loadPreview: Chat (%s) has changed",
karere::Id(chatId).toString().c_str());
mClient->chats->removeRoomPreview(chatId);
mClient->createPublicChatRoom(chatId, ph.val, shard, decryptedTitle, key, url, ts, meeting, opts, smList);
room = dynamic_cast<GroupChatRoom *> (findChatRoom(chatId));
if (!room)
{
API_LOG_DEBUG("Cannot re-create chat (%s) preview", karere::Id(chatId).toString().c_str());
API_LOG_DEBUG("performRequest_loadPreview: Cannot re-create chat (%s) preview", karere::Id(chatId).toString().c_str());
MegaChatErrorPrivate* megaChatError = new MegaChatErrorPrivate(MegaChatError::ERROR_UNKNOWN);
fireOnChatRequestFinish(request, megaChatError);
return;
}
}
else if (!room->isActive() && room->previewMode()) // re-enable preview
else if (room->previewMode() && !room->isActive() ) // re-enable preview
{
API_LOG_DEBUG("Re-enable chat (%s) preview", karere::Id(chatId).toString().c_str());
API_LOG_DEBUG(
"performRequest_loadPreview: Re-enable chat (%s) preview",
karere::Id(chatId).toString().c_str());
room->enablePreview(ph);
}
else
{
API_LOG_ERROR(
"performRequest_loadPreview: Chat (%s) already exists",
karere::Id(chatId).toString().c_str());
}

// update sched meetings if necessary and notify app
API_LOG_ERROR("Chatid (%s) already exists", karere::Id(chatId).toString().c_str());
room->updateSchedMeetingsWithList(smList);
MegaChatErrorPrivate* megaChatError = new MegaChatErrorPrivate(MegaChatError::ERROR_EXIST);
fireOnChatRequestFinish(request, megaChatError);
return;
}

// create public chat room from info received from API
mClient->createPublicChatRoom(chatId, ph.val, shard, decryptedTitle, key, url, ts, meeting, opts, smList);
MegaChatErrorPrivate* megaChatError = new MegaChatErrorPrivate(MegaChatError::ERROR_OK);
fireOnChatRequestFinish(request, megaChatError);
else // create public chat room from info received from API
{
mClient->createPublicChatRoom(chatId, ph.val, shard, decryptedTitle, key, url, ts, meeting, opts, smList);
MegaChatErrorPrivate* megaChatError = new MegaChatErrorPrivate(MegaChatError::ERROR_OK);
fireOnChatRequestFinish(request, megaChatError);
}
}
})
.fail([request, this](const ::promise::Error& err)
Expand Down

0 comments on commit 6fe3591

Please sign in to comment.