From a19a6a33c7772e4209eabe980b50b8696765fa5e Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Fri, 4 Feb 2022 14:32:06 +1100 Subject: [PATCH] migrate already set nickname convo to name + profileName index --- app/sql.js | 47 +++++++++++++++++-- .../conversation-list-item/UserItem.tsx | 2 +- ts/hooks/useParamSelector.ts | 4 +- ts/session/group/closed-group.ts | 4 +- 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/app/sql.js b/app/sql.js index 2d9438c13..1e84d5424 100644 --- a/app/sql.js +++ b/app/sql.js @@ -842,6 +842,7 @@ const LOKI_SCHEMA_VERSIONS = [ updateToLokiSchemaVersion17, updateToLokiSchemaVersion18, updateToLokiSchemaVersion19, + updateToLokiSchemaVersion20, ]; function updateToLokiSchemaVersion1(currentVersion, db) { @@ -1337,6 +1338,43 @@ function updateToLokiSchemaVersion19(currentVersion, db) { console.log(`updateToLokiSchemaVersion${targetVersion}: success!`); } +function updateToLokiSchemaVersion20(currentVersion, db) { + const targetVersion = 20; + if (currentVersion >= targetVersion) { + return; + } + + console.log(`updateToLokiSchemaVersion${targetVersion}: starting...`); + db.transaction(() => { + // looking for all private conversations, with a nickname set + const rowsToUpdate = db + .prepare( + `SELECT * FROM ${CONVERSATIONS_TABLE} WHERE type = 'private' AND (name IS NULL or name = '') AND json_extract(json, '$.nickname') <> '';` + ) + .all(); + (rowsToUpdate || []).forEach(r => { + const obj = jsonToObject(r.json); + + // obj.profile.displayName is the display as this user set it. + if ( + obj && + obj.nickname && + obj.nickname.length && + obj.profile && + obj.profile.displayName && + obj.profile.displayName.length + ) { + // this one has a nickname set, but name is unset, set it to the displayName in the lokiProfile if it's exisitng + obj.name = obj.profile.displayName; + updateConversation(obj, db); + } + }); + + writeLokiSchemaVersion(targetVersion, db); + })(); + console.log(`updateToLokiSchemaVersion${targetVersion}: success!`); +} + function writeLokiSchemaVersion(newVersion, db) { db.prepare( `INSERT INTO loki_schema( @@ -1665,10 +1703,10 @@ function getConversationCount() { return row['count(*)']; } -function saveConversation(data) { +function saveConversation(data, instance) { const { id, active_at, type, members, name, profileName } = data; - globalInstance + (globalInstance || instance) .prepare( `INSERT INTO ${CONVERSATIONS_TABLE} ( id, @@ -1702,7 +1740,7 @@ function saveConversation(data) { }); } -function updateConversation(data) { +function updateConversation(data, instance) { const { id, // eslint-disable-next-line camelcase @@ -1713,7 +1751,8 @@ function updateConversation(data) { profileName, } = data; - globalInstance + (globalInstance || instance) + .prepare( `UPDATE ${CONVERSATIONS_TABLE} SET json = $json, diff --git a/ts/components/leftpane/conversation-list-item/UserItem.tsx b/ts/components/leftpane/conversation-list-item/UserItem.tsx index a550ac534..22ee22564 100644 --- a/ts/components/leftpane/conversation-list-item/UserItem.tsx +++ b/ts/components/leftpane/conversation-list-item/UserItem.tsx @@ -26,7 +26,7 @@ export const UserItem = () => { const displayedPubkey = username ? shortenedPubkey : conversationId; const displayName = isMe ? window.i18n('noteToSelf') - : isSearchResultsMode && hasNickname + : isSearchResultsMode && hasNickname && realName ? `${realName} (${username})` : username; diff --git a/ts/hooks/useParamSelector.ts b/ts/hooks/useParamSelector.ts index e36f4a2c6..3cff7c8a4 100644 --- a/ts/hooks/useParamSelector.ts +++ b/ts/hooks/useParamSelector.ts @@ -32,11 +32,11 @@ export function useConversationUsernameOrShorten(convoId?: string) { } /** - * Returns either the nickname, profileName, or the shorten pubkey + * Returns the name if that conversation. + * This is the group name, or the realName of a user for a private conversation with a recent nickname set */ export function useConversationRealName(convoId?: string) { const convoProps = useConversationPropsById(convoId); - return convoProps?.isPrivate ? convoProps?.name : undefined; } diff --git a/ts/session/group/closed-group.ts b/ts/session/group/closed-group.ts index 3c5b0e943..e432d8ad8 100644 --- a/ts/session/group/closed-group.ts +++ b/ts/session/group/closed-group.ts @@ -343,7 +343,7 @@ async function sendNewName(convo: ConversationModel, name: string, messageId: st // Send the update to the group const nameChangeMessage = new ClosedGroupNameChangeMessage({ timestamp: Date.now(), - groupId: groupId as string, + groupId, identifier: messageId, name, }); @@ -413,7 +413,7 @@ export async function sendRemovedMembers( } const ourNumber = UserUtils.getOurPubKeyFromCache(); const admins = convo.get('groupAdmins') || []; - const groupId = convo.get('id') as string; + const groupId = convo.get('id'); const isCurrentUserAdmin = admins.includes(ourNumber.key); const isUserLeaving = removedMembers.includes(ourNumber.key);