fix incorrect model usage when reading from the cache storage

We change the usages of resolveServerTypeReference to its client
counterpart in all cache functions except for put, as using the server
type reference only makes sense when putting parsed entities in the
cache storage.

Co-authored-by: jomapp <17314077+jomapp@users.noreply.github.com>
This commit is contained in:
abp 2025-04-29 10:18:18 +02:00
parent f8a61909ea
commit f398d8ef6f
No known key found for this signature in database
GPG key ID: 791D4EC38A7AA7C2
2 changed files with 168 additions and 168 deletions

View file

@ -19,7 +19,7 @@ import {
TypeRef,
} from "@tutao/tutanota-utils"
import { isDesktop, isOfflineStorageAvailable, isTest } from "../../common/Env.js"
import { globalClientModelInfo, resolveServerTypeReference } from "../../common/EntityFunctions.js"
import { globalClientModelInfo, resolveClientTypeReference, resolveServerTypeReference } from "../../common/EntityFunctions.js"
import { DateProvider } from "../../common/DateProvider.js"
import { TokenOrNestedTokens } from "cborg/interface"
import { CalendarEventTypeRef, MailTypeRef } from "../../entities/tutanota/TypeRefs.js"
@ -171,7 +171,7 @@ export class OfflineStorage implements CacheStorage, ExposedCacheStorage {
async deleteIfExists(typeRef: TypeRef<SomeEntity>, listId: Id | null, elementId: Id): Promise<void> {
const type = getTypeId(typeRef)
const typeModel: TypeModel = await resolveServerTypeReference(typeRef)
const typeModel: TypeModel = await resolveClientTypeReference(typeRef)
const encodedElementId = ensureBase64Ext(typeModel, elementId)
let formattedQuery
switch (typeModel.type) {
@ -204,7 +204,7 @@ export class OfflineStorage implements CacheStorage, ExposedCacheStorage {
async deleteAllOfType(typeRef: TypeRef<SomeEntity>): Promise<void> {
const type = getTypeId(typeRef)
let typeModel: TypeModel
typeModel = await resolveServerTypeReference(typeRef)
typeModel = await resolveClientTypeReference(typeRef)
let formattedQuery
switch (typeModel.type) {
case TypeId.Element:
@ -248,7 +248,7 @@ export class OfflineStorage implements CacheStorage, ExposedCacheStorage {
async get<T extends SomeEntity>(typeRef: TypeRef<T>, listId: Id | null, elementId: Id): Promise<T | null> {
const type = getTypeId(typeRef)
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
const encodedElementId = ensureBase64Ext(typeModel, elementId)
let formattedQuery
switch (typeModel.type) {
@ -281,7 +281,7 @@ export class OfflineStorage implements CacheStorage, ExposedCacheStorage {
async provideMultiple<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, elementIds: Id[]): Promise<Array<T>> {
if (elementIds.length === 0) return []
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
const encodedElementIds = elementIds.map((elementId) => ensureBase64Ext(typeModel, elementId))
const type = getTypeId(typeRef)
@ -302,7 +302,7 @@ export class OfflineStorage implements CacheStorage, ExposedCacheStorage {
async getIdsInRange<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id): Promise<Array<Id>> {
const type = getTypeId(typeRef)
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
const range = await this.getRange(typeRef, listId)
if (range == null) {
throw new Error(`no range exists for ${type} and list ${listId}`)
@ -324,7 +324,7 @@ export class OfflineStorage implements CacheStorage, ExposedCacheStorage {
async getRangeForList<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id): Promise<Range | null> {
let range = await this.getRange(typeRef, listId)
if (range == null) return range
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
return {
lower: customIdToBase64Url(typeModel, range.lower),
upper: customIdToBase64Url(typeModel, range.upper),
@ -332,7 +332,7 @@ export class OfflineStorage implements CacheStorage, ExposedCacheStorage {
}
async isElementIdInCacheRange<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, elementId: Id): Promise<boolean> {
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
const encodedElementId = ensureBase64Ext(typeModel, elementId)
const range = await this.getRange(typeRef, listId)
@ -340,7 +340,7 @@ export class OfflineStorage implements CacheStorage, ExposedCacheStorage {
}
async provideFromRange<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, start: Id, count: number, reverse: boolean): Promise<T[]> {
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
const encodedStartId = ensureBase64Ext(typeModel, start)
const type = getTypeId(typeRef)
let formattedQuery
@ -412,7 +412,7 @@ export class OfflineStorage implements CacheStorage, ExposedCacheStorage {
}
async setLowerRangeForList<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, lowerId: Id): Promise<void> {
lowerId = ensureBase64Ext(await resolveServerTypeReference(typeRef), lowerId)
lowerId = ensureBase64Ext(await resolveClientTypeReference(typeRef), lowerId)
const type = getTypeId(typeRef)
const { query, params } = sql`UPDATE ranges
SET lower = ${lowerId}
@ -422,7 +422,7 @@ export class OfflineStorage implements CacheStorage, ExposedCacheStorage {
}
async setUpperRangeForList<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, upperId: Id): Promise<void> {
upperId = ensureBase64Ext(await resolveServerTypeReference(typeRef), upperId)
upperId = ensureBase64Ext(await resolveClientTypeReference(typeRef), upperId)
const type = getTypeId(typeRef)
const { query, params } = sql`UPDATE ranges
SET upper = ${upperId}
@ -432,7 +432,7 @@ export class OfflineStorage implements CacheStorage, ExposedCacheStorage {
}
async setNewRangeForList<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, lower: Id, upper: Id): Promise<void> {
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
lower = ensureBase64Ext(typeModel, lower)
upper = ensureBase64Ext(typeModel, upper)
@ -689,7 +689,7 @@ export class OfflineStorage implements CacheStorage, ExposedCacheStorage {
async deleteIn(typeRef: TypeRef<unknown>, listId: Id | null, elementIds: Id[]): Promise<void> {
if (elementIds.length === 0) return
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
const encodedElementIds = elementIds.map((elementIds) => ensureBase64Ext(typeModel, elementIds))
switch (typeModel.type) {
case TypeId.Element:
@ -744,7 +744,7 @@ export class OfflineStorage implements CacheStorage, ExposedCacheStorage {
}
async updateRangeForList<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, rawCutoffId: Id): Promise<void> {
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
const isCustomId = isCustomIdType(typeModel)
const encodedCutoffId = ensureBase64Ext(typeModel, rawCutoffId)
@ -806,7 +806,7 @@ export class OfflineStorage implements CacheStorage, ExposedCacheStorage {
return null
}
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
return (await this.fixupTypeRefs(typeModel, deserialized)) as T
}
@ -826,7 +826,7 @@ export class OfflineStorage implements CacheStorage, ExposedCacheStorage {
const associationName = associationModel.name
if (associationModel.type === AssociationType.Aggregation) {
const aggregateTypeRef = new TypeRef(associationModel.dependency ?? typeModel.app, associationModel.refTypeId)
const aggregateTypeModel = await resolveServerTypeReference(aggregateTypeRef)
const aggregateTypeModel = await resolveClientTypeReference(aggregateTypeRef)
switch (associationModel.cardinality) {
case Cardinality.One:
case Cardinality.ZeroOrOne: {

View file

@ -4,7 +4,7 @@ import { firstBiggerThanSecond } from "../../common/utils/EntityUtils.js"
import { CacheStorage, expandId, LastUpdateTime } from "./DefaultEntityRestCache.js"
import { assertNotNull, clone, getFromMap, getTypeId, remove, TypeRef } from "@tutao/tutanota-utils"
import { CustomCacheHandlerMap } from "./CustomCacheHandler.js"
import { resolveServerTypeReference } from "../../common/EntityFunctions.js"
import { resolveClientTypeReference, resolveServerTypeReference } from "../../common/EntityFunctions.js"
import { Type as TypeId } from "../../common/EntityConstants.js"
import { ProgrammingError } from "../../common/error/ProgrammingError.js"
import { customIdToBase64Url, ensureBase64Ext } from "../offline/OfflineStorage.js"
@ -63,7 +63,7 @@ export class EphemeralCacheStorage implements CacheStorage {
async get<T extends SomeEntity>(typeRef: TypeRef<T>, listId: Id | null, elementId: Id): Promise<T | null> {
// We downcast because we can't prove that map has correct entity on the type level
const type = getTypeId(typeRef)
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
elementId = ensureBase64Ext(typeModel, elementId)
switch (typeModel.type) {
case TypeId.Element:
@ -79,7 +79,7 @@ export class EphemeralCacheStorage implements CacheStorage {
async deleteIfExists<T>(typeRef: TypeRef<T>, listId: Id | null, elementId: Id): Promise<void> {
const type = getTypeId(typeRef)
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
elementId = ensureBase64Ext(typeModel, elementId)
switch (typeModel.type) {
case TypeId.Element:
@ -106,7 +106,7 @@ export class EphemeralCacheStorage implements CacheStorage {
}
async isElementIdInCacheRange<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, elementId: Id): Promise<boolean> {
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
elementId = ensureBase64Ext(typeModel, elementId)
const cache = this.lists.get(getTypeId(typeRef))?.get(listId)
@ -175,7 +175,7 @@ export class EphemeralCacheStorage implements CacheStorage {
// if the element already exists in the cache, overwrite it
// add new element to existing list if necessary
cache.elements.set(elementId, entity)
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
if (await this.isElementIdInCacheRange(typeRef, listId, customIdToBase64Url(typeModel, elementId))) {
this.insertIntoRange(cache.allRange, elementId)
}
@ -198,7 +198,7 @@ export class EphemeralCacheStorage implements CacheStorage {
}
async provideFromRange<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, startElementId: Id, count: number, reverse: boolean): Promise<T[]> {
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
startElementId = ensureBase64Ext(typeModel, startElementId)
const listCache = this.lists.get(getTypeId(typeRef))?.get(listId)
@ -241,7 +241,7 @@ export class EphemeralCacheStorage implements CacheStorage {
async provideMultiple<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, elementIds: Id[]): Promise<Array<T>> {
const listCache = this.lists.get(getTypeId(typeRef))?.get(listId)
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
elementIds = elementIds.map((el) => ensureBase64Ext(typeModel, el))
if (listCache == null) {
@ -267,7 +267,7 @@ export class EphemeralCacheStorage implements CacheStorage {
return null
}
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
return {
lower: customIdToBase64Url(typeModel, listCache.lowerRangeId),
upper: customIdToBase64Url(typeModel, listCache.upperRangeId),
@ -275,7 +275,7 @@ export class EphemeralCacheStorage implements CacheStorage {
}
async setUpperRangeForList<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, upperId: Id): Promise<void> {
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
upperId = ensureBase64Ext(typeModel, upperId)
const listCache = this.lists.get(getTypeId(typeRef))?.get(listId)
if (listCache == null) {
@ -285,7 +285,7 @@ export class EphemeralCacheStorage implements CacheStorage {
}
async setLowerRangeForList<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, lowerId: Id): Promise<void> {
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
lowerId = ensureBase64Ext(typeModel, lowerId)
const listCache = this.lists.get(getTypeId(typeRef))?.get(listId)
if (listCache == null) {
@ -302,7 +302,7 @@ export class EphemeralCacheStorage implements CacheStorage {
* @param upper
*/
async setNewRangeForList<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, lower: Id, upper: Id): Promise<void> {
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
lower = ensureBase64Ext(typeModel, lower)
upper = ensureBase64Ext(typeModel, upper)
@ -323,7 +323,7 @@ export class EphemeralCacheStorage implements CacheStorage {
}
async getIdsInRange<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id): Promise<Array<Id>> {
const typeModel = await resolveServerTypeReference(typeRef)
const typeModel = await resolveClientTypeReference(typeRef)
return (
this.lists
.get(getTypeId(typeRef))