summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2023-03-23 21:52:25 +0300
committerGeorge Hazan <ghazan@miranda.im>2023-03-23 21:52:25 +0300
commit0e4a45eff4dd8ab29f8dd79e419d33a89a808939 (patch)
treed1c82826735c217a46f8d0cec1f87390d6a396f5
parenta1284729b31119dc50d95f73e2ffe7fed4a20ac8 (diff)
group chats unserialization
-rw-r--r--protocols/Telegram/src/groupchat.cpp48
-rw-r--r--src/mir_app/src/chat.h2
-rw-r--r--src/mir_app/src/chat_manager.cpp2
-rw-r--r--src/mir_app/src/chat_svc.cpp12
-rw-r--r--src/mir_app/src/chat_tools.cpp55
5 files changed, 95 insertions, 24 deletions
diff --git a/protocols/Telegram/src/groupchat.cpp b/protocols/Telegram/src/groupchat.cpp
index c0ea6165e8..5b13fe4fe3 100644
--- a/protocols/Telegram/src/groupchat.cpp
+++ b/protocols/Telegram/src/groupchat.cpp
@@ -28,32 +28,40 @@ void CTelegramProto::InitGroupChat(TG_USER *pUser, const TD::chat *pChat, bool b
if (bUpdateMembers) {
si = Chat_NewSession(GCW_CHATROOM, m_szModuleName, wszId, Utf2T(pChat->title_.c_str()), pUser);
- Chat_AddGroup(si, TranslateT("Creator"));
- Chat_AddGroup(si, TranslateT("Admin"));
- Chat_AddGroup(si, TranslateT("Participant"));
+ if (!si->pStatuses) {
+ Chat_AddGroup(si, TranslateT("Creator"));
+ Chat_AddGroup(si, TranslateT("Admin"));
+ Chat_AddGroup(si, TranslateT("Participant"));
- // push async query to fetch users
- SendQuery(new TD::getBasicGroupFullInfo(pUser->id), &CTelegramProto::StartGroupChat, pUser);
+ // push async query to fetch users
+ SendQuery(new TD::getBasicGroupFullInfo(pUser->id), &CTelegramProto::StartGroupChat, pUser);
+ }
+ else {
+ Chat_Control(si, m_bHideGroupchats ? WINDOW_HIDDEN : SESSION_INITDONE);
+ Chat_Control(si, SESSION_ONLINE);
+ }
}
else {
si = Chat_NewSession(GCW_CHANNEL, m_szModuleName, wszId, Utf2T(pChat->title_.c_str()), pUser);
- Chat_AddGroup(si, TranslateT("SuperAdmin"));
- Chat_AddGroup(si, TranslateT("Visitor"));
+ if (!si->pStatuses) {
+ Chat_AddGroup(si, TranslateT("SuperAdmin"));
+ Chat_AddGroup(si, TranslateT("Visitor"));
- ptrW wszUserId(getWStringA(DBKEY_ID)), wszNick(Contact::GetInfo(CNF_DISPLAY, 0, m_szModuleName));
-
- GCEVENT gce = { si, GC_EVENT_JOIN };
- gce.pszUID.w = wszUserId;
- gce.pszNick.w = wszNick;
- gce.bIsMe = true;
- gce.pszStatus.w = TranslateT("Visitor");
- Chat_Event(&gce);
+ ptrW wszUserId(getWStringA(DBKEY_ID)), wszNick(Contact::GetInfo(CNF_DISPLAY, 0, m_szModuleName));
- gce.bIsMe = false;
- gce.pszUID.w = L"---";
- gce.pszNick.w = TranslateT("Admin");
- gce.pszStatus.w = TranslateT("SuperAdmin");
- Chat_Event(&gce);
+ GCEVENT gce = { si, GC_EVENT_JOIN };
+ gce.pszUID.w = wszUserId;
+ gce.pszNick.w = wszNick;
+ gce.bIsMe = true;
+ gce.pszStatus.w = TranslateT("Visitor");
+ Chat_Event(&gce);
+
+ gce.bIsMe = false;
+ gce.pszUID.w = L"---";
+ gce.pszNick.w = TranslateT("Admin");
+ gce.pszStatus.w = TranslateT("SuperAdmin");
+ Chat_Event(&gce);
+ }
Chat_Control(si, m_bHideGroupchats ? WINDOW_HIDDEN : SESSION_INITDONE);
Chat_Control(si, SESSION_ONLINE);
diff --git a/src/mir_app/src/chat.h b/src/mir_app/src/chat.h
index c41c5344a8..04fa195b9c 100644
--- a/src/mir_app/src/chat.h
+++ b/src/mir_app/src/chat.h
@@ -84,6 +84,7 @@ SESSION_INFO* SM_FindSessionByIndex(const char *pszModule, int iItem);
STATUSINFO* TM_AddStatus(STATUSINFO **ppStatusList, const wchar_t *pszStatus, int *iCount);
uint16_t TM_StringToWord(STATUSINFO *pStatusList, const wchar_t *pszStatus);
+USERINFO* UM_AddUser(SESSION_INFO *si, const wchar_t *pszUID, const wchar_t *pszNick, uint16_t wStatus);
USERINFO* UM_FindUserFromIndex(SESSION_INFO *si, int index);
BOOL UM_RemoveAll(SESSION_INFO *si);
BOOL UM_SetStatusEx(SESSION_INFO *si, const wchar_t* pszText, int flags);
@@ -132,5 +133,6 @@ void Chat_RemoveContact(MCONTACT hContact);
CMStringW Chat_GetFolderName(SESSION_INFO *si = nullptr);
void Chat_Serialize(SESSION_INFO *si);
+bool Chat_Unserialize(SESSION_INFO *si);
#pragma comment(lib,"comctl32.lib")
diff --git a/src/mir_app/src/chat_manager.cpp b/src/mir_app/src/chat_manager.cpp
index a448c77be0..23984f3601 100644
--- a/src/mir_app/src/chat_manager.cpp
+++ b/src/mir_app/src/chat_manager.cpp
@@ -570,7 +570,7 @@ STATUSINFO* TM_AddStatus(STATUSINFO **ppStatusList, const wchar_t *pszStatus, in
return node;
}
- return FALSE;
+ return nullptr;
}
static STATUSINFO* TM_FindStatus(STATUSINFO *pStatusList, const wchar_t *pszStatus)
diff --git a/src/mir_app/src/chat_svc.cpp b/src/mir_app/src/chat_svc.cpp
index 9fd2d559ba..64132cdde5 100644
--- a/src/mir_app/src/chat_svc.cpp
+++ b/src/mir_app/src/chat_svc.cpp
@@ -255,10 +255,18 @@ MIR_APP_DLL(SESSION_INFO*) Chat_NewSession(
mir_snwprintf(szTemp, L"Server: %s", si->ptszName);
else
wcsncpy_s(szTemp, si->ptszName, _TRUNCATE);
+
si->hContact = AddRoom(pszModule, ptszID, szTemp, si->iType);
si->iPopupFlags = Chat::iPopupFlags;
si->iTrayFlags = Chat::iTrayIconFlags;
- db_set_s(si->hContact, si->pszModule, "Topic", "");
+
+ bool bRestored = false;
+ if (mi->bPersistent)
+ bRestored = Chat_Unserialize(si);
+
+ if (!bRestored)
+ db_set_s(si->hContact, si->pszModule, "Topic", "");
+
db_unset(si->hContact, "CList", "StatusMsg");
if (si->ptszStatusbarText)
db_set_ws(si->hContact, si->pszModule, "StatusBar", si->ptszStatusbarText);
@@ -423,7 +431,7 @@ static void AddUser(SESSION_INFO *si, GCEVENT &gce)
{
uint16_t status = TM_StringToWord(si->pStatuses, gce.pszStatus.w);
- USERINFO *ui = g_chatApi.UM_AddUser(si, gce.pszUID.w, gce.pszNick.w, status);
+ USERINFO *ui = UM_AddUser(si, gce.pszUID.w, gce.pszNick.w, status);
if (ui == nullptr)
return;
diff --git a/src/mir_app/src/chat_tools.cpp b/src/mir_app/src/chat_tools.cpp
index 744797f205..ee13b56e43 100644
--- a/src/mir_app/src/chat_tools.cpp
+++ b/src/mir_app/src/chat_tools.cpp
@@ -867,7 +867,8 @@ void Chat_Serialize(SESSION_INFO *si)
JSONNode pUserList(JSON_ARRAY); pUserList.set_name("users");
for (auto &it : si->arUsers) {
JSONNode user;
- user << JSONNode("id", T2Utf(it->pszUID).get()) << JSONNode("nick", T2Utf(it->pszNick).get()) << JSONNode("role", it->Status);
+ user << JSONNode("id", T2Utf(it->pszUID).get()) << JSONNode("nick", T2Utf(it->pszNick).get())
+ << JSONNode("role", it->Status) << JSONNode("isMe", it == si->pMe);
pUserList << user;
}
@@ -886,3 +887,55 @@ void Chat_Serialize(SESSION_INFO *si)
}
}
}
+
+bool Chat_Unserialize(SESSION_INFO *si)
+{
+ FILE *in = _wfopen(Chat_GetFolderName(si), L"r");
+ if (in == nullptr)
+ return false;
+
+ CMStringA szJson;
+ unsigned iFileLength = filelength(fileno(in));
+ szJson.Truncate(iFileLength);
+ fread(szJson.GetBuffer(), 1, iFileLength, in);
+ fclose(in);
+
+ JSONNode root = JSONNode::parse(szJson);
+ if (!root)
+ return false;
+
+ CMStringW str = root["name"].as_mstring();
+ if (!str.IsEmpty())
+ si->ptszName = str.Detach();
+
+ str = root["topic"].as_mstring();
+ if (!str.IsEmpty())
+ si->ptszTopic = str.Detach();
+
+ for (auto &it : root["roles"])
+ if (auto *pStatus = TM_AddStatus(&si->pStatuses, it["name"].as_mstring(), &si->iStatusCount))
+ si->iStatusCount++;
+
+ for (auto &it : root["users"]) {
+ int iStatus = it["role"].as_int();
+ USERINFO *ui = UM_AddUser(si, it["id"].as_mstring(), it["nick"].as_mstring(), iStatus);
+ if (ui == nullptr)
+ continue;
+
+ if (g_chatApi.OnAddUser)
+ g_chatApi.OnAddUser(si, ui);
+
+ if (it["isMe"].as_bool())
+ si->pMe = ui;
+ ui->Status = iStatus;
+ ui->Status |= si->pStatuses->iStatus;
+
+ if (g_chatApi.OnNewUser)
+ g_chatApi.OnNewUser(si, ui);
+ }
+
+ if (si->pDlg)
+ si->pDlg->UpdateNickList();
+
+ return true;
+}