feat: add more compilation errors if forgetting to handle a wrapper type

pull/2620/head
Audric Ackermann 2 years ago
parent 21d8151b8b
commit 2a5dc5f2a5

@ -104,6 +104,10 @@ import {
import { SessionUtilUserGroups } from '../session/utils/libsession/libsession_utils_user_groups'; import { SessionUtilUserGroups } from '../session/utils/libsession/libsession_utils_user_groups';
import { Registration } from '../util/registration'; import { Registration } from '../util/registration';
import { SessionUtilConvoInfoVolatile } from '../session/utils/libsession/libsession_utils_convo_info_volatile'; 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<ConversationAttributes> { export class ConversationModel extends Backbone.Model<ConversationAttributes> {
public updateLastMessage: () => any; public updateLastMessage: () => any;
@ -350,6 +354,8 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
const foundContact = SessionUtilContact.getMappedValue(this.id); const foundContact = SessionUtilContact.getMappedValue(this.id);
const foundCommunity = SessionUtilUserGroups.getCommunityMappedValueByConvoId(this.id); const foundCommunity = SessionUtilUserGroups.getCommunityMappedValueByConvoId(this.id);
const foundLegacyGroup = SessionUtilUserGroups.getLegacyGroupMappedValueByConvoId(this.id);
const foundVolatileInfo = SessionUtilConvoInfoVolatile.getFromAny(this.id);
if (foundContact) { if (foundContact) {
if (foundContact.name) { if (foundContact.name) {
@ -408,11 +414,11 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
} }
} }
if (foundCommunity) { // if (foundCommunity) {
if (foundCommunity.priority > 0) { // if (foundCommunity.priority > 0) {
toRet.isPinned = true; // TODO priority also handles sorting // toRet.isPinned = true; // TODO priority also handles sorting
} // }
} // }
if (unreadCount) { if (unreadCount) {
toRet.unreadCount = unreadCount; toRet.unreadCount = unreadCount;
@ -445,10 +451,6 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
toRet.members = uniq(members); toRet.members = uniq(members);
} }
if (zombies && zombies.length) {
toRet.zombies = uniq(zombies);
}
if (expireTimer) { if (expireTimer) {
toRet.expireTimer = expireTimer; toRet.expireTimer = expireTimer;
} }
@ -460,6 +462,10 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
toRet.currentNotificationSetting = currentNotificationSetting; toRet.currentNotificationSetting = currentNotificationSetting;
} }
if (zombies && zombies.length) {
toRet.zombies = uniq(zombies);
}
if (this.isOpenGroupV2()) { if (this.isOpenGroupV2()) {
const room = OpenGroupData.getV2OpenGroupRoom(this.id); const room = OpenGroupData.getV2OpenGroupRoom(this.id);
if (room && isArray(room.capabilities) && room.capabilities.length) { if (room && isArray(room.capabilities) && room.capabilities.length) {
@ -2232,27 +2238,40 @@ export async function commitConversationAndRefreshWrapper(id: string) {
await Data.saveConversation(convo.attributes); await Data.saveConversation(convo.attributes);
const shouldBeSavedToContactsWrapper = SessionUtilContact.isContactToStoreInContactsWrapper( for (let index = 0; index < LibSessionUtil.requiredUserVariants.length; index++) {
convo const variant = LibSessionUtil.requiredUserVariants[index];
);
const shouldBeSavedToUserGroupsWrapper = SessionUtilUserGroups.isUserGroupToStoreInWrapper(convo); switch (variant) {
const shouldBeSavedToConvoInfoVolatileWrapper = SessionUtilConvoInfoVolatile.isConvoToStoreInWrapper( case 'UserConfig':
convo 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( case 'ConvoInfoVolatileConfig':
`should be saved to wrapper ${id}: contacts:${shouldBeSavedToContactsWrapper}; usergroups:${shouldBeSavedToUserGroupsWrapper}, volatile:${shouldBeSavedToConvoInfoVolatileWrapper}` if (SessionUtilConvoInfoVolatile.isConvoToStoreInWrapper(convo)) {
); await SessionUtilConvoInfoVolatile.insertConvoFromDBIntoWrapperAndRefresh(convo.id);
}
break;
if (shouldBeSavedToContactsWrapper) { default:
await SessionUtilContact.insertContactFromDBIntoWrapperAndRefresh(convo.id); assertUnreachable(
} else if (shouldBeSavedToUserGroupsWrapper) { variant,
await SessionUtilUserGroups.insertGroupsFromDBIntoWrapperAndRefresh(convo.id); `commitConversationAndRefreshWrapper unhandled case "${variant}"`
);
}
} }
if (shouldBeSavedToConvoInfoVolatileWrapper) {
await SessionUtilConvoInfoVolatile.insertConvoFromDBIntoWrapperAndRefresh(convo.id);
}
if (Registration.isDone()) { if (Registration.isDone()) {
// save the new dump if needed to the DB asap // 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()) // this call throttled so we do not run this too often (and not for every .commit())

@ -1032,7 +1032,7 @@ function getUnreadByConversation(conversationId: string) {
ORDER BY received_at DESC;` ORDER BY received_at DESC;`
) )
.all({ .all({
unread: 1, unread: toSqliteBoolean(true),
conversationId, conversationId,
}); });
@ -1055,7 +1055,7 @@ function markAllAsReadByConversationNoExpiration(
conversationId = $conversationId;` conversationId = $conversationId;`
) )
.all({ .all({
unread: 1, unread: toSqliteBoolean(true),
conversationId, conversationId,
}); });
toReturn = compact(messagesUnreadBefore.map(row => jsonToObject(row.json).sent_at)); toReturn = compact(messagesUnreadBefore.map(row => jsonToObject(row.json).sent_at));
@ -1069,7 +1069,7 @@ function markAllAsReadByConversationNoExpiration(
conversationId = $conversationId;` conversationId = $conversationId;`
) )
.run({ .run({
unread: 1, unread: toSqliteBoolean(true),
conversationId, conversationId,
}); });
@ -1084,7 +1084,7 @@ function getUnreadCountByConversation(conversationId: string) {
conversationId = $conversationId;` conversationId = $conversationId;`
) )
.get({ .get({
unread: 1, unread: toSqliteBoolean(true),
conversationId, conversationId,
}); });

@ -99,7 +99,7 @@ class ConfigurationSyncDumpJob extends PersistedJob<ConfigurationSyncDumpPersist
const variant = LibSessionUtil.requiredUserVariants[index]; const variant = LibSessionUtil.requiredUserVariants[index];
switch (variant) { switch (variant) {
case 'UserConfig': case 'UserConfig':
await LibSessionUtil.insertUserProfileIntoWrapper(); await LibSessionUtil.insertUserProfileIntoWrapper(us);
break; break;
case 'ContactsConfig': case 'ContactsConfig':
await LibSessionUtil.insertAllContactsIntoContactsWrapper(); await LibSessionUtil.insertAllContactsIntoContactsWrapper();

@ -195,7 +195,7 @@ class ConfigurationSyncJob extends PersistedJob<ConfigurationSyncPersistedData>
const variant = LibSessionUtil.requiredUserVariants[index]; const variant = LibSessionUtil.requiredUserVariants[index];
switch (variant) { switch (variant) {
case 'UserConfig': case 'UserConfig':
await LibSessionUtil.insertUserProfileIntoWrapper(); await LibSessionUtil.insertUserProfileIntoWrapper(us);
break; break;
case 'ContactsConfig': case 'ContactsConfig':
await LibSessionUtil.insertAllContactsIntoContactsWrapper(); await LibSessionUtil.insertAllContactsIntoContactsWrapper();

@ -217,6 +217,14 @@ function getAllLegacyGroups(): Array<ConvoInfoVolatileLegacyGroup> {
return [...mappedLegacyGroupWrapperValues.values()]; 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. * 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. * 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, getAllCommunities,
getCommunityMappedValueByConvoId, getCommunityMappedValueByConvoId,
// removeCommunityFromWrapper, // removeCommunityFromWrapper,
getFromAny,
}; };

@ -4,7 +4,10 @@ import { UserConfigWrapperActions } from '../../../webworker/workers/browser/lib
import { getConversationController } from '../../conversations'; import { getConversationController } from '../../conversations';
import { fromHexToArray } from '../String'; import { fromHexToArray } from '../String';
async function insertUserProfileIntoWrapper() { async function insertUserProfileIntoWrapper(convoId: string) {
if (!isUserProfileToStoreInContactsWrapper(convoId)) {
return;
}
const us = UserUtils.getOurPubKeyStrFromCache(); const us = UserUtils.getOurPubKeyStrFromCache();
const ourConvo = getConversationController().get(us); 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 = { export const SessionUtilUserProfile = {
insertUserProfileIntoWrapper, insertUserProfileIntoWrapper,
isUserProfileToStoreInContactsWrapper,
}; };

Loading…
Cancel
Save