refactor: use resource identifiers
Build Test / test (pull_request) Successful in 26s
JS Unit Tests / test (pull_request) Failing after 29s
PHP Unit Tests / test (pull_request) Successful in 56s

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-05-14 22:34:18 -04:00
parent 69d4b2f42c
commit c7ef2c5495
13 changed files with 425 additions and 621 deletions
+87 -133
View File
@@ -4,13 +4,16 @@
import { ref, computed, readonly } from 'vue'
import { defineStore } from 'pinia'
import { collectionService, entityService } from '../services'
import {
type ServiceIdentifier,
type CollectionIdentifier,
type ListFilter,
type ListSort,
collectionService,
} from '../services'
import { CollectionObject, CollectionPropertiesObject } from '../models/collection'
import type { SourceSelector, ListFilter, ListSort, CollectionIdentifier, CollectionMoveResponse } from '../types'
export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
const ROOT_IDENTIFIER = '__root__'
const SERVICE_INDEX_IDENTIFIER = '__service__'
// State
const _collections = ref<Record<string, CollectionObject>>({})
@@ -67,14 +70,13 @@ export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
*
* @returns Collection object or null
*/
function collection(provider: string, service: string | number, identifier: string | number, retrieve: boolean = false): CollectionObject | null {
const key = identifierKey(provider, service, identifier)
if (retrieve === true && !_collections.value[key]) {
console.debug(`[Mail Manager][Store] - Force fetching collection "${key}"`)
fetch(provider, service, identifier)
function collection(target: CollectionIdentifier, retrieve: boolean = false): CollectionObject | null {
if (retrieve === true && !_collections.value[target]) {
console.debug(`[Mail Manager][Store] - Force fetching collection "${target}"`)
fetch([target])
}
return _collections.value[key] || null
return _collections.value[target] || null
}
/**
@@ -87,18 +89,14 @@ export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
* @returns Array of collection objects
*/
function collectionsForService(provider: string, service: string | number, retrieve: boolean = false): CollectionObject[] {
const serviceIdentifier = `${provider}:${service}` as ServiceIdentifier
const serviceCollections = collectionObjectsForKeys(
_collectionsByServiceIndex.value[identifierKey(provider, service, SERVICE_INDEX_IDENTIFIER)] ?? [],
_collectionsByServiceIndex.value[serviceIdentifier] ?? [],
)
if (retrieve === true && serviceCollections.length === 0) {
console.debug(`[Mail Manager][Store] - Force fetching collections for service "${provider}:${service}"`)
const sources: SourceSelector = {
[provider]: {
[String(service)]: true
}
}
list(sources)
console.debug(`[Mail Manager][Store] - Force fetching collections for service "${serviceIdentifier}"`)
list([serviceIdentifier])
}
return serviceCollections
@@ -114,33 +112,23 @@ export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
*
* @returns Array of direct child collection objects
*/
function collectionsInCollection(provider: string, service: string | number, collectionId: string | number | null, retrieve: boolean = false): CollectionObject[] {
function collectionsInCollection(provider: string, service: string | number, collection?: CollectionIdentifier | null, retrieve: boolean = false): CollectionObject[] {
const collectionIdentifier = collection ?? `${provider}:${service}` as CollectionIdentifier
const nestedCollections = collectionObjectsForKeys(
_collectionsByParentIndex.value[identifierKey(provider, service, collectionId)] ?? [],
_collectionsByParentIndex.value[collectionIdentifier] ?? [],
)
if (retrieve === true && nestedCollections.length === 0) {
console.debug(`[Mail Manager][Store] - Force fetching collections in collection "${provider}:${service}:${collectionId}"`)
const sources: SourceSelector = {
[provider]: {
[String(service)]: true
}
}
list(sources)
console.debug(`[Mail Manager][Store] - Force fetching collections in collection "${collectionIdentifier}"`)
list([collectionIdentifier])
}
return nestedCollections
}
function hasChildrenInCollection(provider: string, service: string | number, collectionId: string | number | null): boolean {
return (_collectionsByParentIndex.value[identifierKey(provider, service, collectionId)]?.length ?? 0) > 0
}
/**
* Create unique key for a collection
*/
function identifierKey(provider: string, service: string | number | null, identifier: string | number | null): string {
return `${provider}:${String(service ?? ROOT_IDENTIFIER)}:${String(identifier ?? ROOT_IDENTIFIER)}`
function hasChildrenInCollection(provider: string, service: string | number, collection: CollectionIdentifier | null): boolean {
const collectionIdentifier = collection ?? `${provider}:${service}` as CollectionIdentifier
return (_collectionsByParentIndex.value[collectionIdentifier]?.length ?? 0) > 0
}
function collectionObjectsForKeys(collectionKeys: string[]): CollectionObject[] {
@@ -149,6 +137,16 @@ export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
.filter((collection): collection is CollectionObject => collection !== undefined)
}
function indexCollection(collection: CollectionObject) {
addIndexEntry(_collectionsByServiceIndex.value, String(collection.service), String(collection.identifier))
addIndexEntry(_collectionsByParentIndex.value, String(collection.collection ?? collection.service), String(collection.identifier))
}
function deindexCollection(collection: CollectionObject) {
removeIndexEntry(_collectionsByServiceIndex.value, String(collection.service), String(collection.identifier))
removeIndexEntry(_collectionsByParentIndex.value, String(collection.collection ?? collection.service), String(collection.identifier))
}
function addIndexEntry(index: Record<string, string[]>, indexKey: string, collectionKey: string) {
const existing = index[indexKey] ?? []
@@ -176,24 +174,6 @@ export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
index[indexKey] = filtered
}
function indexCollection(collection: CollectionObject) {
const collectionKey = identifierKey(collection.provider, collection.service, collection.identifier)
const serviceIndexKey = identifierKey(collection.provider, collection.service, SERVICE_INDEX_IDENTIFIER)
const parentIndexKey = identifierKey(collection.provider, collection.service, collection.collection)
addIndexEntry(_collectionsByServiceIndex.value, serviceIndexKey, collectionKey)
addIndexEntry(_collectionsByParentIndex.value, parentIndexKey, collectionKey)
}
function deindexCollection(collection: CollectionObject) {
const collectionKey = identifierKey(collection.provider, collection.service, collection.identifier)
const serviceIndexKey = identifierKey(collection.provider, collection.service, SERVICE_INDEX_IDENTIFIER)
const parentIndexKey = identifierKey(collection.provider, collection.service, collection.collection)
removeIndexEntry(_collectionsByServiceIndex.value, serviceIndexKey, collectionKey)
removeIndexEntry(_collectionsByParentIndex.value, parentIndexKey, collectionKey)
}
// Actions
/**
@@ -205,7 +185,7 @@ export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
*
* @returns Promise with collection object list keyed by provider, service, and collection identifier
*/
async function list(sources?: SourceSelector, filter?: ListFilter, sort?: ListSort): Promise<Record<string, CollectionObject>> {
async function list(sources?: ServiceIdentifier[] | CollectionIdentifier[], filter?: ListFilter, sort?: ListSort): Promise<Record<string, CollectionObject>> {
transceiving.value = true
try {
const response = await collectionService.list({ sources, filter, sort })
@@ -215,14 +195,11 @@ export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
Object.entries(response).forEach(([_providerId, providerServices]) => {
Object.entries(providerServices).forEach(([_serviceId, serviceCollections]) => {
Object.entries(serviceCollections).forEach(([_collectionId, collectionObj]) => {
const key = identifierKey(collectionObj.provider, collectionObj.service, collectionObj.identifier)
const previousCollection = _collections.value[key]
if (previousCollection) {
deindexCollection(previousCollection)
if (_collections.value[collectionObj.identifier]) {
deindexCollection(_collections.value[collectionObj.identifier])
}
collections[key] = collectionObj
collections[collectionObj.identifier] = collectionObj
})
})
})
@@ -252,26 +229,25 @@ export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
*
* @returns Promise with collection object
*/
async function fetch(provider: string, service: string | number, identifier: string | number): Promise<CollectionObject> {
async function fetch(targets: CollectionIdentifier[]): Promise<Record<string, CollectionObject>> {
transceiving.value = true
try {
const response = await collectionService.fetch({ provider, service, collection: identifier })
const response = await collectionService.fetch({ targets })
// Merge fetched collection into state
const key = identifierKey(response.provider, response.service, response.identifier)
const previousCollection = _collections.value[key]
Object.values(response).forEach(collectionObj => {
if (_collections.value[collectionObj.identifier]) {
deindexCollection(_collections.value[collectionObj.identifier])
}
if (previousCollection) {
deindexCollection(previousCollection)
}
_collections.value[collectionObj.identifier] = collectionObj
indexCollection(collectionObj)
})
_collections.value[key] = response
indexCollection(response)
console.debug('[Mail Manager][Store] - Successfully fetched collection:', key)
console.debug('[Mail Manager][Store] - Successfully fetched collections:', Object.keys(response).join(', '))
return response
} catch (error: any) {
console.error('[Mail Manager][Store] - Failed to fetch collection:', error)
console.error('[Mail Manager][Store] - Failed to fetch collections:', error)
throw error
} finally {
transceiving.value = false
@@ -285,12 +261,12 @@ export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
*
* @returns Promise with collection availability status
*/
async function extant(sources: SourceSelector) {
async function extant(targets: CollectionIdentifier[]): Promise<Record<string, Record<string, Record<string, boolean>>>> {
transceiving.value = true
try {
const response = await collectionService.extant({ sources })
const response = await collectionService.extant({ targets })
console.debug('[Mail Manager][Store] - Successfully checked', sources ? Object.keys(sources).length : 0, 'collections')
console.debug('[Mail Manager][Store] - Successfully checked', targets ? targets.length : 0, 'collections')
return response
} catch (error: any) {
console.error('[Mail Manager][Store] - Failed to check collections:', error)
@@ -310,22 +286,21 @@ export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
*
* @returns Promise with created collection object
*/
async function create(provider: string, service: string | number, collection: string | number | null, data: CollectionPropertiesObject): Promise<CollectionObject> {
async function create(provider: string, service: string | number, properties: CollectionPropertiesObject, target?: CollectionIdentifier): Promise<CollectionObject> {
transceiving.value = true
try {
const response = await collectionService.create({
provider,
service,
collection,
properties: data
const response = await collectionService.create({
provider,
service,
target,
properties: properties.toJson()
})
// Merge created collection into state
const key = identifierKey(response.provider, response.service, response.identifier)
_collections.value[key] = response
_collections.value[response.identifier] = response
indexCollection(response)
console.debug('[Mail Manager][Store] - Successfully created collection:', key)
console.debug('[Mail Manager][Store] - Successfully created collection:', response.identifier)
return response
} catch (error: any) {
console.error('[Mail Manager][Store] - Failed to create collection:', error)
@@ -336,37 +311,29 @@ export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
}
/**
* Update an existing collection with given provider, service, identifier, and data
* Update an existing collection with given target and properties
*
* @param provider - provider identifier for the collection to update
* @param service - service identifier for the collection to update
* @param identifier - collection identifier for the collection to update
* @param data - collection properties for update
* @param target - collection identifier for the collection to update
* @param properties - collection properties for update
*
* @returns Promise with updated collection object
*/
async function update(provider: string, service: string | number, identifier: string | number, data: CollectionPropertiesObject): Promise<CollectionObject> {
async function update(target: CollectionIdentifier, properties: CollectionPropertiesObject): Promise<CollectionObject> {
transceiving.value = true
try {
const response = await collectionService.update({
provider,
service,
identifier,
properties: data
target,
properties: properties.toJson()
})
// Merge updated collection into state
const key = identifierKey(response.provider, response.service, response.identifier)
const previousCollection = _collections.value[key]
if (previousCollection) {
deindexCollection(previousCollection)
if (_collections.value[response.identifier]) {
deindexCollection(_collections.value[response.identifier])
}
_collections.value[key] = response
_collections.value[response.identifier] = response
indexCollection(response)
console.debug('[Mail Manager][Store] - Successfully updated collection:', key)
console.debug('[Mail Manager][Store] - Successfully updated collection:', response.identifier)
return response
} catch (error: any) {
console.error('[Mail Manager][Store] - Failed to update collection:', error)
@@ -377,45 +344,38 @@ export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
}
/**
* Delete a collection by provider, service, and identifier
* Delete a collection by identifier, with optional force delete if collection is not empty.
*
* @param provider - provider identifier for the collection to delete
* @param service - service identifier for the collection to delete
* @param identifier - collection identifier for the collection to delete
* @param target - collection identifier for the collection to delete
* @param force - optional flag to force delete if collection is not empty
*
* @returns Promise with deletion result
*/
async function remove(provider: string, service: string | number, identifier: string | number): Promise<CollectionObject | boolean> {
async function remove(target: CollectionIdentifier, force?: boolean): Promise<CollectionObject | boolean> {
transceiving.value = true
try {
const response = await collectionService.delete({ provider, service, identifier })
const response = await collectionService.delete({ target, options: { force } })
if (response !== true && !(response instanceof CollectionObject)) {
console.warn('[Mail Manager][Store] - Delete failed. Received unexpected response from delete operation:', response)
return false
}
const key = identifierKey(provider, service, identifier)
const previousCollection = _collections.value[key]
if (previousCollection) {
deindexCollection(previousCollection)
if (_collections.value[target]) {
deindexCollection(_collections.value[target])
}
delete _collections.value[key]
delete _collections.value[target]
if (response instanceof CollectionObject) {
const movedCollection = response
const movedKey = identifierKey(movedCollection.provider, movedCollection.service, movedCollection.identifier)
_collections.value[response.identifier] = response
indexCollection(response)
_collections.value[movedKey] = movedCollection
indexCollection(movedCollection)
console.debug('[Mail Manager][Store] - Successfully moved collection to trash', key, '->', movedKey)
console.debug('[Mail Manager][Store] - Successfully moved collection to trash', target, '->', response.identifier)
return response
}
console.debug('[Mail Manager][Store] - Successfully deleted collection:', key)
console.debug('[Mail Manager][Store] - Successfully deleted collection:', target)
return response
} catch (error: any) {
console.error('[Mail Manager][Store] - Failed to delete collection:', error)
@@ -446,21 +406,15 @@ export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
throw new Error('Failed to move collection: unexpected response from move operation')
}
const sourceCollection = _collections.value[source]
if (sourceCollection) {
deindexCollection(sourceCollection)
if (_collections.value[source]) {
deindexCollection(_collections.value[source])
}
delete _collections.value[source]
const movedCollection = response
const movedKey = identifierKey(movedCollection.provider, movedCollection.service, movedCollection.identifier)
_collections.value[response.identifier] = response
indexCollection(response)
_collections.value[movedKey] = movedCollection
indexCollection(movedCollection)
console.debug('[Mail Manager][Store] - Successfully moved collection:', source, ' to ', movedKey)
console.debug('[Mail Manager][Store] - Successfully moved collection:', source, ' to ', response.identifier)
return response
} catch (error: any) {
console.error('[Mail Manager][Store] - Failed to move collection:', error)