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

This commit is contained in:
Audric Ackermann 2023-03-10 11:14:23 +11:00
parent 21d8151b8b
commit 2a5dc5f2a5
6 changed files with 74 additions and 33 deletions

View file

@ -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<ConversationAttributes> {
public updateLastMessage: () => any;
@ -350,6 +354,8 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
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<ConversationAttributes> {
}
}
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<ConversationAttributes> {
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<ConversationAttributes> {
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];
console.warn(
`should be saved to wrapper ${id}: contacts:${shouldBeSavedToContactsWrapper}; usergroups:${shouldBeSavedToUserGroupsWrapper}, volatile:${shouldBeSavedToConvoInfoVolatileWrapper}`
);
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;
if (shouldBeSavedToContactsWrapper) {
await SessionUtilContact.insertContactFromDBIntoWrapperAndRefresh(convo.id);
} else if (shouldBeSavedToUserGroupsWrapper) {
await SessionUtilUserGroups.insertGroupsFromDBIntoWrapperAndRefresh(convo.id);
case 'ConvoInfoVolatileConfig':
if (SessionUtilConvoInfoVolatile.isConvoToStoreInWrapper(convo)) {
await SessionUtilConvoInfoVolatile.insertConvoFromDBIntoWrapperAndRefresh(convo.id);
}
break;
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())

View file

@ -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,
});

View file

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

View file

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

View file

@ -217,6 +217,14 @@ function getAllLegacyGroups(): Array<ConvoInfoVolatileLegacyGroup> {
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,
};

View file

@ -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,
};