Skip to content

Commit

Permalink
Merge branch 'fix/CHT-824-limit-number-messages-loadhistory' into 'de…
Browse files Browse the repository at this point in the history
…velop'

CHT-824. Limit to 256 number of messages that can be requested per block

Closes CHT-824

See merge request megachat/MEGAchat!1523
  • Loading branch information
alber2510 committed Feb 15, 2023
2 parents 5ef8e79 + 699ed8c commit 5326765
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/megaclc/megaclc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ std::mutex g_reviewPublicChatOutFileLogsMutex;
std::unique_ptr<std::ofstream> g_reviewPublicChatOutFileLogs;
class ReviewPublicChat_GetUserEmail_Listener;

static const int MAX_NUMBER_MESSAGES = 300;
static const int MAX_NUMBER_MESSAGES = 100; // chatd doesn't allow more than 256

struct ConsoleLock
{
Expand Down
2 changes: 2 additions & 0 deletions src/chatdMsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ enum Opcode
* of OLDMSGs, followed by a HISTDONE. Note that count is always negative.
* Send: <chatid> <count>
*
* @note count is limited to 256
*
* This command is sent at the following situations:
* 1. After a JOIN command to load last messages, when no local history.
* 2. When the app requests to load more old messages and there's no more history
Expand Down
7 changes: 6 additions & 1 deletion src/megachatapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ typedef int MegaChatIndex; // int32_t
const MegaChatHandle MEGACHAT_INVALID_HANDLE = ~(MegaChatHandle)0;
const MegaChatIndex MEGACHAT_INVALID_INDEX = 0x7fffffff;
const MegaChatTimeStamp MEGACHAT_INVALID_TIMESTAMP = 0;
const int MAX_MESSAGES_PER_BLOCK = 256;
const int MIN_MESSAGES_PER_BLOCK = 1;

class MegaChatApi;
class MegaChatApiImpl;
Expand Down Expand Up @@ -4834,12 +4836,15 @@ class MegaChatApi
* is local and there's no more history locally available, the number of messages could be
* lower too (and the next call to MegaChatApi::loadMessages will fetch messages from server).
*
* @note \c count has a maximun value of 256. If user requests more than 256 messages,
* only 256 messages will returned if exits
*
* When there are no more history available from the reported source of messages
* (local / remote), or when the requested \c count has been already loaded,
* the callback MegaChatRoomListener::onMessageLoaded will be called with a NULL message.
*
* @param chatid MegaChatHandle that identifies the chat room
* @param count The number of requested messages to load.
* @param count The number of requested messages to load (Range 1 - 256)
*
* @return Return the source of the messages that is going to be fetched. The possible values are:
* - MegaChatApi::SOURCE_ERROR = -1: history has to be fetched from server, but we are not logged in yet
Expand Down
7 changes: 7 additions & 0 deletions src/megachatapi_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4799,6 +4799,13 @@ void MegaChatApiImpl::closeChatPreview(MegaChatHandle chatid)
int MegaChatApiImpl::loadMessages(MegaChatHandle chatid, int count)
{
int ret = MegaChatApi::SOURCE_NONE;

if (count > MAX_MESSAGES_PER_BLOCK)
{
API_LOG_WARNING("count value is higher than chatd allows");
}

count = std::clamp(count, MIN_MESSAGES_PER_BLOCK, MAX_MESSAGES_PER_BLOCK);
sdkMutex.lock();

ChatRoom *chatroom = findChatRoom(chatid);
Expand Down

0 comments on commit 5326765

Please sign in to comment.