Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/common/src/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ struct ContactInfo
std::string name;
std::string phone;
bool isSelf = false;
bool isStarred = false;
};

struct ChatInfo
Expand All @@ -129,6 +130,7 @@ struct ChatInfo
bool isUnreadMention = false; // only required for tgchat
bool isMuted = false;
bool isPinned = false;
bool isArchived = false;
int64_t lastMessageTime = -1;
};

Expand Down
120 changes: 111 additions & 9 deletions lib/ncutil/src/messagecache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,29 @@ void MessageCache::AddProfile(const std::string& p_ProfileId, bool p_CheckSequen
"SET schema = ?;" << schemaVersion;
}

static const int64_t s_SchemaVersion = 6;
if (schemaVersion == 6)
{
LOG_INFO("update db schema 6 to 7");

*m_Dbs[p_ProfileId] << "ALTER TABLE chats2 ADD COLUMN isArchived INT;";

schemaVersion = 7;
*m_Dbs[p_ProfileId] << "UPDATE version "
"SET schema = ?;" << schemaVersion;
}

if (schemaVersion == 7)
{
LOG_INFO("update db schema 7 to 8");

*m_Dbs[p_ProfileId] << "ALTER TABLE contacts2 ADD COLUMN isStarred INT;";

schemaVersion = 8;
*m_Dbs[p_ProfileId] << "UPDATE version "
"SET schema = ?;" << schemaVersion;
}

static const int64_t s_SchemaVersion = 8;
if (schemaVersion > s_SchemaVersion)
{
LOG_WARNING("cache db schema %d from newer nchat version detected, if cache issues are encountered "
Expand Down Expand Up @@ -726,6 +748,30 @@ void MessageCache::UpdatePin(const std::string& p_ProfileId, const std::string&
EnqueueRequest(updatePinRequest);
}

void MessageCache::UpdateArchive(const std::string& p_ProfileId, const std::string& p_ChatId, bool p_IsArchived)
{
if (!m_CacheEnabled) return;

std::shared_ptr<UpdateArchiveRequest> updateArchiveRequest =
std::make_shared<UpdateArchiveRequest>();
updateArchiveRequest->profileId = p_ProfileId;
updateArchiveRequest->chatId = p_ChatId;
updateArchiveRequest->isArchived = p_IsArchived;
EnqueueRequest(updateArchiveRequest);
}

void MessageCache::UpdateStar(const std::string& p_ProfileId, const std::string& p_ContactId, bool p_IsStarred)
{
if (!m_CacheEnabled) return;

std::shared_ptr<UpdateStarRequest> updateStarRequest =
std::make_shared<UpdateStarRequest>();
updateStarRequest->profileId = p_ProfileId;
updateStarRequest->contactId = p_ContactId;
updateStarRequest->isStarred = p_IsStarred;
EnqueueRequest(updateStarRequest);
}

void MessageCache::Export(const std::string& p_ExportDir)
{
if (!m_CacheEnabled)
Expand Down Expand Up @@ -1082,10 +1128,10 @@ void MessageCache::PerformRequest(std::shared_ptr<Request> p_Request)
for (const auto& chatInfo : addChatsRequest->chatInfos)
{
*m_Dbs[profileId] << "INSERT INTO " + s_TableChats + " "
"(id, isMuted, isPinned, lastMessageTime) VALUES "
"(?, ?, ?, ?);" <<
"(id, isMuted, isPinned, lastMessageTime, isArchived) VALUES "
"(?, ?, ?, ?, ?);" <<
chatInfo.id << chatInfo.isMuted << chatInfo.isPinned <<
chatInfo.lastMessageTime;
chatInfo.lastMessageTime << chatInfo.isArchived;
}
*m_Dbs[profileId] << "COMMIT;";
}
Expand Down Expand Up @@ -1120,9 +1166,9 @@ void MessageCache::PerformRequest(std::shared_ptr<Request> p_Request)
for (const auto& contactInfo : addContactsRequest->contactInfos)
{
*m_Dbs[profileId] << "INSERT INTO " + s_TableContacts + " "
"(id, name, phone, isSelf) VALUES "
"(?,?,?,?);" <<
contactInfo.id << contactInfo.name << contactInfo.phone << contactInfo.isSelf;
"(id, name, phone, isSelf, isStarred) VALUES "
"(?,?,?,?,?);" <<
contactInfo.id << contactInfo.name << contactInfo.phone << contactInfo.isSelf << contactInfo.isStarred;
}
*m_Dbs[profileId] << "COMMIT;";
}
Expand Down Expand Up @@ -1168,6 +1214,7 @@ void MessageCache::PerformRequest(std::shared_ptr<Request> p_Request)
chatInfo.isUnread = !isOutgoing && !isRead;
chatInfo.isMuted = chatIdMuted[chatId];
chatInfo.isPinned = chatIdPinned[chatId];
chatInfo.isArchived = chatIdArchived[chatId];
chatInfo.lastMessageTime = chatInfo.isPinned ? chatIdLastMessageTime[chatId] : timeSent;
chatInfos.push_back(chatInfo);
}
Expand Down Expand Up @@ -1202,14 +1249,15 @@ void MessageCache::PerformRequest(std::shared_ptr<Request> p_Request)
try
{
// *INDENT-OFF*
*m_Dbs[profileId] << "SELECT id, name, phone, isSelf FROM " + s_TableContacts + ";" >>
[&](const std::string& id, const std::string& name, const std::string& phone, int32_t isSelf)
*m_Dbs[profileId] << "SELECT id, name, phone, isSelf, isStarred FROM " + s_TableContacts + ";" >>
[&](const std::string& id, const std::string& name, const std::string& phone, int32_t isSelf, int32_t isStarred)
{
ContactInfo contactInfo;
contactInfo.id = id;
contactInfo.name = name;
contactInfo.phone = phone;
contactInfo.isSelf = isSelf;
contactInfo.isStarred = isStarred;
contactInfos.push_back(contactInfo);
};
// *INDENT-ON*
Expand Down Expand Up @@ -1688,6 +1736,60 @@ void MessageCache::PerformRequest(std::shared_ptr<Request> p_Request)
}
break;

case UpdateArchiveRequestType:
{
std::unique_lock<std::mutex> lock(m_DbMutex);
std::shared_ptr<UpdateArchiveRequest> updateArchiveRequest =
std::static_pointer_cast<UpdateArchiveRequest>(p_Request);
const std::string& profileId = updateArchiveRequest->profileId;
if (!m_Dbs[profileId]) return;

const std::string& chatId = updateArchiveRequest->chatId;
bool isArchived = updateArchiveRequest->isArchived;

try
{
*m_Dbs[profileId] << "INSERT INTO " + s_TableChats + " "
"(id, isArchived) VALUES "
"(?, ?) ON CONFLICT(id) DO UPDATE SET isArchived=?;" <<
chatId << isArchived << isArchived;
}
catch (const sqlite::sqlite_exception& ex)
{
HANDLE_SQLITE_EXCEPTION(ex);
}

LOG_DEBUG("cache update archived %s %d", chatId.c_str(), isArchived);
}
break;

case UpdateStarRequestType:
{
std::unique_lock<std::mutex> lock(m_DbMutex);
std::shared_ptr<UpdateStarRequest> updateStarRequest =
std::static_pointer_cast<UpdateStarRequest>(p_Request);
const std::string& profileId = updateStarRequest->profileId;
if (!m_Dbs[profileId]) return;

const std::string& contactId = updateStarRequest->contactId;
bool isStarred = updateStarRequest->isStarred;

try
{
*m_Dbs[profileId] << "INSERT INTO " + s_TableContacts + " "
"(id, isStarred) VALUES "
"(?, ?) ON CONFLICT(id) DO UPDATE SET isStarred=?;" <<
contactId << isStarred << isStarred;
}
catch (const sqlite::sqlite_exception& ex)
{
HANDLE_SQLITE_EXCEPTION(ex);
}

LOG_DEBUG("cache update starred %s %d", contactId.c_str(), isStarred);
}
break;

default:
{
LOG_WARNING("cache unknown request type %d", p_Request->GetRequestType());
Expand Down
24 changes: 23 additions & 1 deletion lib/ncutil/src/messagecache.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class MessageCache
UpdateMessageReactionsRequestType,
UpdateMuteRequestType,
UpdatePinRequestType,
UpdateArchiveRequestType,
UpdateStarRequestType,
};

class Request
Expand Down Expand Up @@ -194,6 +196,24 @@ class MessageCache
int64_t timePinned = -1;
};

class UpdateArchiveRequest : public Request
{
public:
virtual RequestType GetRequestType() const { return UpdateArchiveRequestType; }
std::string profileId;
std::string chatId;
bool isArchived = false;
};

class UpdateStarRequest : public Request
{
public:
virtual RequestType GetRequestType() const { return UpdateStarRequestType; }
std::string profileId;
std::string contactId;
bool isStarred = false;
};

public:
static void Init();
static void Cleanup();
Expand Down Expand Up @@ -232,6 +252,8 @@ class MessageCache
static void UpdateMute(const std::string& p_ProfileId, const std::string& p_ChatId, bool p_IsMuted);
static void UpdatePin(const std::string& p_ProfileId, const std::string& p_ChatId, bool p_IsPinned,
int64_t p_TimePinned);
static void UpdateArchive(const std::string& p_ProfileId, const std::string& p_ChatId, bool p_IsArchived);
static void UpdateStar(const std::string& p_ProfileId, const std::string& p_ContactId, bool p_IsStarred);
static void Export(const std::string& p_ExportDir);

private:
Expand Down Expand Up @@ -263,4 +285,4 @@ class MessageCache
static std::string m_HistoryDir;
static bool m_CacheEnabled;
static bool m_CacheReadOnly;
};
};
1 change: 1 addition & 0 deletions src/uiconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void UiConfig::Init()
{ "file_picker_command", "" },
{ "file_picker_persist_dir", "1" },
{ "help_enabled", "1" },
{ "hide_archived_chats", "0" },
{ "home_fetch_all", "0" },
{ "linefeed_on_enter", "1" },
{ "link_open_command", "" },
Expand Down
3 changes: 3 additions & 0 deletions src/uicontactlistdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ void UiContactListDialog::UpdateList()
std::sort(localContactListItemVec.begin(), localContactListItemVec.end(),
[&](const UiContactListItem& lhs, const UiContactListItem& rhs) -> bool
{
if (lhs.isStarred > rhs.isStarred) return true;
if (lhs.isStarred < rhs.isStarred) return false;

return lhs.name < rhs.name;
});

Expand Down
1 change: 1 addition & 0 deletions src/uicontactlistdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct UiContactListItem
std::string profileId;
std::string contactId;
std::string name;
bool isStarred = false;
};

class UiContactListDialog : public UiListDialog
Expand Down
5 changes: 5 additions & 0 deletions src/uihelpview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ void UiHelpView::Draw()
AppendHelpItem("find", "Find", helpItems);
AppendHelpItem("find_next", "FindNext", helpItems);
AppendHelpItem("select_contact", "AddrBook", helpItems);
AppendHelpItem("star_contact", "StarCont", helpItems);
AppendHelpItem("spell", "ExtSpell", helpItems);
AppendHelpItem("auto_compose", "AutoComp", helpItems);
AppendHelpItem("decrease_list_width", "DecListW", helpItems);
Expand All @@ -95,6 +96,7 @@ void UiHelpView::Draw()
AppendHelpItem("toggle_list", "TgList", helpItems);
AppendHelpItem("toggle_top", "TgTop", helpItems);
AppendHelpItem("toggle_help", "TgHelp", helpItems);
AppendHelpItem("toggle_archive", "TgArch", helpItems);

return helpItems;
}();
Expand All @@ -117,6 +119,7 @@ void UiHelpView::Draw()
AppendHelpItem("react", "AddReact", helpItems);
AppendHelpItem("open_msg", "ExtView", helpItems);
AppendHelpItem("forward_msg", "FwdMsg", helpItems);
AppendHelpItem("star_chat", "Star", helpItems);

helpItems.insert(std::end(helpItems), std::begin(mainPostHelpItems), std::end(mainPostHelpItems));
return helpItems;
Expand All @@ -129,6 +132,8 @@ void UiHelpView::Draw()

AppendHelpItem("up", "SelectMsg", helpItems);
AppendHelpItem("delete_chat", "DelChat", helpItems);
AppendHelpItem("archive_chat", "Archive", helpItems);
AppendHelpItem("star_chat", "Star", helpItems);

helpItems.insert(std::end(helpItems), std::begin(mainPostHelpItems), std::end(mainPostHelpItems));
return helpItems;
Expand Down
12 changes: 8 additions & 4 deletions src/uikeyconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ void UiKeyConfig::InitKeyCodes(bool p_MapKeys)
{ "KEY_SPREVIOUS", KEY_SPREVIOUS },
{ "KEY_SPRINT", KEY_SPRINT },
{ "KEY_SREDO", KEY_SREDO },
{ "KEY_SREPLACE", KEY_SREPLACE },
{ "KEY_SREPLACE", KEY_REPLACE },
{ "KEY_SRIGHT", KEY_SRIGHT },
{ "KEY_SRSUME", KEY_SRSUME },
{ "KEY_SSAVE", KEY_SSAVE },
Expand Down Expand Up @@ -218,8 +218,8 @@ void UiKeyConfig::Init(bool p_MapKeys)
#endif
{ "edit_msg", "KEY_CTRLZ" },
{ "backward_kill_word", "\\33\\177" }, // alt/opt-backspace
{ "cut", "\\33\\170" }, // alt/opt-x
{ "copy", "\\33\\143" }, // alt/opt-c
{ "cut", "\\33\\167" }, // alt/opt-w
{ "copy", "\\33\\171" }, // alt/opt-y
{ "paste", "\\33\\166" }, // alt/opt-v
{ "ext_call", "\\33\\164" }, // alt/opt-t
{ "ext_edit", "\\33\\145" }, // alt/opt-e
Expand All @@ -232,20 +232,24 @@ void UiKeyConfig::Init(bool p_MapKeys)
{ "toggle_help", "KEY_CTRLG" },
{ "toggle_list", "KEY_CTRLL" },
{ "toggle_top", "KEY_CTRLP" },
{ "toggle_archive", "\\33\\141" },
{ "next_chat", "KEY_TAB" },
{ "prev_chat", "KEY_BTAB" },
{ "unread_chat", "KEY_CTRLF" },
{ "send_msg", "KEY_CTRLX" },
{ "delete_msg", "KEY_CTRLD" },
{ "delete_chat", "\\33\\144" }, // alt/opt-d
{ "archive_chat", "\\33\\170" }, // alt/opt-x
{ "open", "KEY_CTRLV" },
{ "open_link", "KEY_CTRLW" },
{ "open_msg", "\\33\\167" }, // alt/opt-w
{ "save", "KEY_CTRLR" },
{ "transfer", "KEY_CTRLT" },
{ "select_emoji", "KEY_CTRLS" },
{ "select_contact", "\\33\\156" }, // alt/opt-n
{ "star_contact", "\\33\\143" }, // alt/opt-c
{ "forward_msg", "\\33\\162" }, // alt/opt-r
{ "star_chat", "\\33\\163" },
{ "goto_chat", "KEY_CTRLN" },
{ "other_commands_help", "KEY_CTRLO" },
{ "decrease_list_width", "\\33\\54" }, // alt/opt-,
Expand Down Expand Up @@ -422,4 +426,4 @@ void UiKeyConfig::DetectConflicts()
const std::string keyFunctions = StrUtil::Join(keyMapping, ", ");
LOG_WARNING("key \"%s\" has duplicate mappings: %s", keyCode.c_str(), keyFunctions.c_str());
}
}
}
Loading
Loading