diff --git a/ts/models/conversation.ts b/ts/models/conversation.ts index f8d23f994..dbf529bb9 100644 --- a/ts/models/conversation.ts +++ b/ts/models/conversation.ts @@ -104,6 +104,10 @@ import { import { SessionUtilUserGroups } from '../session/utils/libsession/libsession_utils_user_groups'; import { Registration } from '../util/registration'; import { SessionUtilConvoInfoVolatile } from '../session/utils/libsession/libsession_utils_convo_info_volatile'; +import { assertUnreachable } from '../types/sqlSharedTypes'; + +import { LibSessionUtil } from '../session/utils/libsession/libsession_utils'; +import { SessionUtilUserProfile } from '../session/utils/libsession/libsession_utils_user_profile'; export class ConversationModel extends Backbone.Model { public updateLastMessage: () => any; @@ -350,6 +354,8 @@ export class ConversationModel extends Backbone.Model { const foundContact = SessionUtilContact.getMappedValue(this.id); const foundCommunity = SessionUtilUserGroups.getCommunityMappedValueByConvoId(this.id); + const foundLegacyGroup = SessionUtilUserGroups.getLegacyGroupMappedValueByConvoId(this.id); + const foundVolatileInfo = SessionUtilConvoInfoVolatile.getFromAny(this.id); if (foundContact) { if (foundContact.name) { @@ -408,11 +414,11 @@ export class ConversationModel extends Backbone.Model { } } - if (foundCommunity) { - if (foundCommunity.priority > 0) { - toRet.isPinned = true; // TODO priority also handles sorting - } - } + // if (foundCommunity) { + // if (foundCommunity.priority > 0) { + // toRet.isPinned = true; // TODO priority also handles sorting + // } + // } if (unreadCount) { toRet.unreadCount = unreadCount; @@ -445,10 +451,6 @@ export class ConversationModel extends Backbone.Model { toRet.members = uniq(members); } - if (zombies && zombies.length) { - toRet.zombies = uniq(zombies); - } - if (expireTimer) { toRet.expireTimer = expireTimer; } @@ -460,6 +462,10 @@ export class ConversationModel extends Backbone.Model { toRet.currentNotificationSetting = currentNotificationSetting; } + if (zombies && zombies.length) { + toRet.zombies = uniq(zombies); + } + if (this.isOpenGroupV2()) { const room = OpenGroupData.getV2OpenGroupRoom(this.id); if (room && isArray(room.capabilities) && room.capabilities.length) { @@ -2232,27 +2238,40 @@ export async function commitConversationAndRefreshWrapper(id: string) { await Data.saveConversation(convo.attributes); - const shouldBeSavedToContactsWrapper = SessionUtilContact.isContactToStoreInContactsWrapper( - convo - ); - const shouldBeSavedToUserGroupsWrapper = SessionUtilUserGroups.isUserGroupToStoreInWrapper(convo); - const shouldBeSavedToConvoInfoVolatileWrapper = SessionUtilConvoInfoVolatile.isConvoToStoreInWrapper( - convo - ); + for (let index = 0; index < LibSessionUtil.requiredUserVariants.length; index++) { + const variant = LibSessionUtil.requiredUserVariants[index]; + + switch (variant) { + case 'UserConfig': + if (SessionUtilUserProfile.isUserProfileToStoreInContactsWrapper(convo.id)) { + await SessionUtilUserProfile.insertUserProfileIntoWrapper(convo.id); + } + break; + case 'ContactsConfig': + if (SessionUtilContact.isContactToStoreInContactsWrapper(convo)) { + await SessionUtilContact.insertContactFromDBIntoWrapperAndRefresh(convo.id); + } + break; + case 'UserGroupsConfig': + if (SessionUtilUserGroups.isUserGroupToStoreInWrapper(convo)) { + await SessionUtilUserGroups.insertGroupsFromDBIntoWrapperAndRefresh(convo.id); + } + break; - console.warn( - `should be saved to wrapper ${id}: contacts:${shouldBeSavedToContactsWrapper}; usergroups:${shouldBeSavedToUserGroupsWrapper}, volatile:${shouldBeSavedToConvoInfoVolatileWrapper}` - ); + case 'ConvoInfoVolatileConfig': + if (SessionUtilConvoInfoVolatile.isConvoToStoreInWrapper(convo)) { + await SessionUtilConvoInfoVolatile.insertConvoFromDBIntoWrapperAndRefresh(convo.id); + } + break; - if (shouldBeSavedToContactsWrapper) { - await SessionUtilContact.insertContactFromDBIntoWrapperAndRefresh(convo.id); - } else if (shouldBeSavedToUserGroupsWrapper) { - await SessionUtilUserGroups.insertGroupsFromDBIntoWrapperAndRefresh(convo.id); + default: + assertUnreachable( + variant, + `commitConversationAndRefreshWrapper unhandled case "${variant}"` + ); + } } - if (shouldBeSavedToConvoInfoVolatileWrapper) { - await SessionUtilConvoInfoVolatile.insertConvoFromDBIntoWrapperAndRefresh(convo.id); - } if (Registration.isDone()) { // save the new dump if needed to the DB asap // this call throttled so we do not run this too often (and not for every .commit()) diff --git a/ts/node/sql.ts b/ts/node/sql.ts index 4a1318cb4..c10424188 100644 --- a/ts/node/sql.ts +++ b/ts/node/sql.ts @@ -1032,7 +1032,7 @@ function getUnreadByConversation(conversationId: string) { ORDER BY received_at DESC;` ) .all({ - unread: 1, + unread: toSqliteBoolean(true), conversationId, }); @@ -1055,7 +1055,7 @@ function markAllAsReadByConversationNoExpiration( conversationId = $conversationId;` ) .all({ - unread: 1, + unread: toSqliteBoolean(true), conversationId, }); toReturn = compact(messagesUnreadBefore.map(row => jsonToObject(row.json).sent_at)); @@ -1069,7 +1069,7 @@ function markAllAsReadByConversationNoExpiration( conversationId = $conversationId;` ) .run({ - unread: 1, + unread: toSqliteBoolean(true), conversationId, }); @@ -1084,7 +1084,7 @@ function getUnreadCountByConversation(conversationId: string) { conversationId = $conversationId;` ) .get({ - unread: 1, + unread: toSqliteBoolean(true), conversationId, }); diff --git a/ts/session/utils/job_runners/jobs/ConfigurationSyncDumpJob.ts b/ts/session/utils/job_runners/jobs/ConfigurationSyncDumpJob.ts index fceac3ef4..240b84a3f 100644 --- a/ts/session/utils/job_runners/jobs/ConfigurationSyncDumpJob.ts +++ b/ts/session/utils/job_runners/jobs/ConfigurationSyncDumpJob.ts @@ -99,7 +99,7 @@ class ConfigurationSyncDumpJob extends PersistedJob const variant = LibSessionUtil.requiredUserVariants[index]; switch (variant) { case 'UserConfig': - await LibSessionUtil.insertUserProfileIntoWrapper(); + await LibSessionUtil.insertUserProfileIntoWrapper(us); break; case 'ContactsConfig': await LibSessionUtil.insertAllContactsIntoContactsWrapper(); diff --git a/ts/session/utils/libsession/libsession_utils_convo_info_volatile.ts b/ts/session/utils/libsession/libsession_utils_convo_info_volatile.ts index 738eeed9c..a0cb1b7b8 100644 --- a/ts/session/utils/libsession/libsession_utils_convo_info_volatile.ts +++ b/ts/session/utils/libsession/libsession_utils_convo_info_volatile.ts @@ -217,6 +217,14 @@ function getAllLegacyGroups(): Array { return [...mappedLegacyGroupWrapperValues.values()]; } +function getFromAny(convoId: string) { + return ( + mapped1o1WrapperValues.get(convoId) || + mappedLegacyGroupWrapperValues.get(convoId) || + mappedCommunityWrapperValues.get(convoId) + ); +} + /** * This function can be used where there are things to do for all the types handled by this wrapper. * You can do a loop on all the types handled by this wrapper and have a switch using assertUnreachable to get errors when not every case is handled. @@ -250,4 +258,5 @@ export const SessionUtilConvoInfoVolatile = { getAllCommunities, getCommunityMappedValueByConvoId, // removeCommunityFromWrapper, + getFromAny, }; diff --git a/ts/session/utils/libsession/libsession_utils_user_profile.ts b/ts/session/utils/libsession/libsession_utils_user_profile.ts index d24e7ffea..87cb3b489 100644 --- a/ts/session/utils/libsession/libsession_utils_user_profile.ts +++ b/ts/session/utils/libsession/libsession_utils_user_profile.ts @@ -4,7 +4,10 @@ import { UserConfigWrapperActions } from '../../../webworker/workers/browser/lib import { getConversationController } from '../../conversations'; import { fromHexToArray } from '../String'; -async function insertUserProfileIntoWrapper() { +async function insertUserProfileIntoWrapper(convoId: string) { + if (!isUserProfileToStoreInContactsWrapper(convoId)) { + return; + } const us = UserUtils.getOurPubKeyStrFromCache(); const ourConvo = getConversationController().get(us); @@ -24,6 +27,16 @@ async function insertUserProfileIntoWrapper() { } } +function isUserProfileToStoreInContactsWrapper(convoId: string) { + try { + const us = UserUtils.getOurPubKeyStrFromCache(); + return convoId === us; + } catch (e) { + return false; + } +} + export const SessionUtilUserProfile = { insertUserProfileIntoWrapper, + isUserProfileToStoreInContactsWrapper, };