Browse Source

feat(history): Remove group history on quit when selected

The group itself is a peer, as well as all the members of the group.
Members of the group have associated aliases, but the group does not.
Chat messages reference the chat_id of the group, with senders
referencing the member peer.

Deleting all history belonging to the chat_id already generically cleans
up group history. Users who only have history from that group need to
have their aliases and peer entries removed.

We don't store what users or aliases were in which group, so just remove
any alias that has no associated chat message with it, and peer that
doesn't have an alias (friend with history) and doesn't have a chat
message associated with it as a chat_id (group).

The group itself is removed from the peer table directly, and the group
never has an alias entry.
reviewable/pr6561/r36
Anthony Bilinski 4 years ago
parent
commit
6bc4dc2a2a
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
  1. 2
      src/model/about/aboutfriend.cpp
  2. 33
      src/persistence/history.cpp
  3. 2
      src/persistence/history.h

2
src/model/about/aboutfriend.cpp

@ -127,7 +127,7 @@ bool AboutFriend::clearHistory() @@ -127,7 +127,7 @@ bool AboutFriend::clearHistory()
const ToxPk pk = f->getPublicKey();
History* const history = Nexus::getProfile()->getHistory();
if (history) {
history->removeFriendHistory(pk);
history->removeChatHistory(pk);
return true;
}

33
src/persistence/history.cpp

@ -264,7 +264,7 @@ void History::eraseHistory() @@ -264,7 +264,7 @@ void History::eraseHistory()
* @brief Erases the chat history of one chat.
* @param chatId Chat ID to erase.
*/
void History::removeFriendHistory(const ToxPk& friendPk)
void History::removeChatHistory(const ChatId& chatId)
{
if (!isValid()) {
return;
@ -276,7 +276,7 @@ void History::removeFriendHistory(const ToxPk& friendPk) @@ -276,7 +276,7 @@ void History::removeFriendHistory(const ToxPk& friendPk)
" SELECT faux_offline_pending.id FROM faux_offline_pending "
" LEFT JOIN history ON faux_offline_pending.id = history.id "
" WHERE chat_id=");
addChatIdSubQuery(queryString, boundParams, friendPk);
addChatIdSubQuery(queryString, boundParams, chatId);
queryString += QStringLiteral(
"); "
"DELETE FROM broken_messages "
@ -284,42 +284,45 @@ void History::removeFriendHistory(const ToxPk& friendPk) @@ -284,42 +284,45 @@ void History::removeFriendHistory(const ToxPk& friendPk)
" SELECT broken_messages.id FROM broken_messages "
" LEFT JOIN history ON broken_messages.id = history.id "
" WHERE chat_id=");
addChatIdSubQuery(queryString, boundParams, friendPk);
addChatIdSubQuery(queryString, boundParams, chatId);
queryString += QStringLiteral(
"); "
"DELETE FROM text_messages "
"WHERE id IN ("
" SELECT id from history "
" WHERE message_type = 'T' AND chat_id=");
addChatIdSubQuery(queryString, boundParams, friendPk);
addChatIdSubQuery(queryString, boundParams, chatId);
queryString += QStringLiteral(
");"
"DELETE FROM file_transfers "
"WHERE id IN ( "
" SELECT id from history "
" WHERE message_type = 'F' AND chat_id=");
addChatIdSubQuery(queryString, boundParams, friendPk);
addChatIdSubQuery(queryString, boundParams, chatId);
queryString += QStringLiteral(
");"
"DELETE FROM system_messages "
"WHERE id IN ( "
" SELECT id from history "
" WHERE message_type = 'S' AND chat_id=");
addChatIdSubQuery(queryString, boundParams, friendPk);
addChatIdSubQuery(queryString, boundParams, chatId);
queryString += QStringLiteral(");"
"DELETE FROM history WHERE chat_id=");
addChatIdSubQuery(queryString, boundParams, friendPk);
addChatIdSubQuery(queryString, boundParams, chatId);
queryString += QStringLiteral("; "
"DELETE FROM chats WHERE id=");
addChatIdSubQuery(queryString, boundParams, friendPk);
queryString += QStringLiteral("; "
"DELETE FROM aliases WHERE owner=");
addAuthorIdSubQuery(queryString, boundParams, friendPk);
queryString += QStringLiteral("; "
"DELETE FROM authors WHERE id=");
addAuthorIdSubQuery(queryString, boundParams, friendPk);
addChatIdSubQuery(queryString, boundParams, chatId);
queryString += QStringLiteral("; "
"VACUUM;");
"DELETE FROM aliases WHERE id NOT IN ( "
" SELECT DISTINCT sender_alias FROM "
" text_messages JOIN file_transfers);");
queryString += QStringLiteral(
"DELETE FROM authors WHERE id NOT IN ( "
" SELECT DISTINCT owner FROM aliases);");
queryString += QStringLiteral("VACUUM;");
RawDatabase::Query query = {queryString, boundParams};
if (!db->execNow(query)) {

2
src/persistence/history.h

@ -219,7 +219,7 @@ public: @@ -219,7 +219,7 @@ public:
bool historyExists(const ChatId& chatId);
void eraseHistory();
void removeFriendHistory(const ToxPk& friendPk);
void removeChatHistory(const ChatId& chatId);
void addNewMessage(const ChatId& chatId, const QString& message, const ToxPk& sender,
const QDateTime& time, bool isDelivered, ExtensionSet extensions,
QString dispName, const std::function<void(RowId)>& insertIdCallback = {});

Loading…
Cancel
Save