refactor: use resource identifiers
Some checks failed
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

View File

@@ -4,6 +4,7 @@
import type { CollectionInterface, CollectionModelInterface, CollectionPropertiesInterface, CollectionPropertiesModelInterface } from "@/types/collection";
import { clonePlain } from './clone-plain';
import type { CollectionIdentifier, ServiceIdentifier } from "@/services";
export class CollectionObject implements CollectionModelInterface {
@@ -49,16 +50,16 @@ export class CollectionObject implements CollectionModelInterface {
return this._data.provider;
}
get service(): string | number {
return this._data.service;
get service(): ServiceIdentifier {
return this._data.service as ServiceIdentifier;
}
get collection(): string | number | null {
return this._data.collection;
get collection(): CollectionIdentifier | null {
return this._data.collection as CollectionIdentifier | null;
}
get identifier(): string | number {
return this._data.identifier;
get identifier(): CollectionIdentifier {
return this._data.identifier as CollectionIdentifier;
}
get signature(): string | null | undefined {

View File

@@ -6,6 +6,7 @@ import type { EntityInterface, EntityModelInterface } from "@/types/entity";
import type { MessageInterface } from "@/types/message";
import { MessageObject } from "./message";
import { clonePlain } from './clone-plain';
import type { CollectionIdentifier, EntityIdentifier, ServiceIdentifier } from "@/services";
export class EntityObject implements EntityModelInterface {
@@ -18,8 +19,8 @@ export class EntityObject implements EntityModelInterface {
version: 1,
provider: '',
service: '',
collection: '',
identifier: '',
collection: null,
identifier: null,
signature: null,
created: null,
modified: null,
@@ -54,16 +55,16 @@ export class EntityObject implements EntityModelInterface {
return this._data.provider;
}
get service(): string {
return this._data.service;
get service(): ServiceIdentifier {
return this._data.service as ServiceIdentifier;
}
get collection(): string|number {
return this._data.collection;
get collection(): CollectionIdentifier {
return this._data.collection as CollectionIdentifier;
}
get identifier(): string|number {
return this._data.identifier;
get identifier(): EntityIdentifier {
return this._data.identifier as EntityIdentifier;
}
get signature(): string | null {

View File

@@ -48,38 +48,46 @@ export class MessageObject implements MessageModelInterface {
/** Properties */
get size(): number {
return this._data.size ?? 0;
}
get headers(): Record<string, string> {
return clonePlain(this._data.headers ?? {});
}
get urid(): string | null{
return this._data.urid ?? null;
}
get size(): number {
return this._data.size ?? 0;
get inReplyTo(): string | null {
return this._data.inReplyTo ?? null;
}
get receivedDate(): string | null {
return this._data.receivedDate ?? null;
get references(): string | null {
return this._data.references ?? null;
}
get sentDate(): string | null {
return this._data.sentDate ?? null;
get received(): string | null {
return this._data.received ?? null;
}
get date(): string | null {
return this._data.date ?? null;
get sent(): string | null {
return this._data.sent ?? null;
}
get subject(): string | null {
return this._data.subject ?? null;
}
get snippet(): string | null {
return this._data.snippet ?? null;
get sender(): MessageAddressObject | null {
return this._data.sender ? new MessageAddressObject(this._data.sender) : null;
}
get from(): MessageAddressObject | null {
return this._data.from ? new MessageAddressObject(this._data.from) : null;
}
get replyTo(): Array<MessageAddressObject> | null {
return this._data.replyTo ? this._data.replyTo.map(addr => new MessageAddressObject(addr)) : null;
}
get to(): Array<MessageAddressObject> | null {
return this._data.to ? this._data.to.map(addr => new MessageAddressObject(addr)) : null;
}
@@ -92,12 +100,8 @@ export class MessageObject implements MessageModelInterface {
return this._data.bcc ? this._data.bcc.map(addr => new MessageAddressObject(addr)) : null;
}
get replyTo(): Array<MessageAddressObject> | null {
return this._data.replyTo ? this._data.replyTo.map(addr => new MessageAddressObject(addr)) : null;
}
get flags(): { read?: boolean; flagged?: boolean; answered?: boolean; draft?: boolean } | {} {
return clonePlain(this._data.flags ?? {});
get subject(): string | null {
return this._data.subject ?? null;
}
get body(): MessagePartObject | null {
@@ -115,6 +119,10 @@ export class MessageObject implements MessageModelInterface {
return this._data.attachments ? this._data.attachments.map(att => new MessagePartObject(att)) : [];
}
get flags(): { read?: boolean; flagged?: boolean; answered?: boolean; draft?: boolean } | {} {
return clonePlain(this._data.flags ?? {});
}
/** Helper methods */
get isRead(): boolean {

View File

@@ -72,9 +72,16 @@ export const collectionService = {
*
* @returns Promise with collection object
*/
async fetch(request: CollectionFetchRequest): Promise<CollectionObject> {
async fetch(request: CollectionFetchRequest): Promise<Record<string, CollectionObject>> {
const response = await transceivePost<CollectionFetchRequest, CollectionFetchResponse>('collection.fetch', request);
return createCollectionObject(response);
// Convert response to CollectionObject instances
const list: Record<string, CollectionObject> = {};
Object.entries(response).forEach(([identifier, entity]) => {
list[entity.identifier] = createCollectionObject(entity);
});
return list;
},
/**
@@ -128,8 +135,8 @@ export const collectionService = {
async delete(request: CollectionDeleteRequest): Promise<boolean | CollectionObject> {
const response = await transceivePost<CollectionDeleteRequest, CollectionDeleteResponse>('collection.delete', request);
if (response.outcome === 'moved' && response.data) {
return createCollectionObject(response.data);
if (response.disposition === 'moved' && response.mutation) {
return createCollectionObject(response.mutation);
}
return true;

View File

@@ -87,8 +87,8 @@ export const entityService = {
// Convert response to EntityObject instances
const list: Record<string, EntityObject> = {};
Object.entries(response).forEach(([identifier, entityData]) => {
list[identifier] = createEntityObject(entityData);
Object.entries(response).forEach(([identifier, entity]) => {
list[entity.identifier] = createEntityObject(entity);
});
return list;
@@ -184,10 +184,7 @@ export const entityService = {
*
* @returns Promise resolving to { total } when the stream completes
*/
async stream(
request: EntityStreamRequest,
onEntity: (entity: EntityObject) => void
): Promise<{ total: number }> {
async stream(request: EntityStreamRequest, onEntity: (entity: EntityObject) => void): Promise<{ total: number }> {
return await transceiveStream<EntityStreamRequest, EntityStreamResponse>(
'entity.stream',
request,

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)

View File

@@ -5,10 +5,8 @@
import { ref, computed, readonly } from 'vue'
import { defineStore } from 'pinia'
import { entityService } from '../services'
import { EntityObject } from '../models'
import { EntityObject, MessageObject } from '../models'
import type {
EntityDeleteResponse,
EntityMoveResponse,
EntityStreamRequest,
EntityTransmitRequest,
EntityTransmitResponse,
@@ -19,8 +17,8 @@ import type {
ListFilter,
ListRange,
ListSort,
SourceSelector,
} from '../types/common'
import type { MessageInterface } from '@/types/message'
export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
// State
@@ -53,43 +51,13 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
*
* @returns Entity object or null
*/
function entity(provider: string, service: string | number, collection: string | number, identifier: string | number, retrieve: boolean = false): EntityObject | null {
const key = identifierKey(provider, service, collection, identifier)
if (retrieve === true && !_entities.value[key]) {
console.debug(`[Mail Manager][Store] - Force fetching entity "${key}"`)
fetch(provider, service, collection, [identifier])
function entity(target: EntityIdentifier, retrieve: boolean = false): EntityObject | null {
if (retrieve === true && !_entities.value[target]) {
console.debug(`[Mail Manager][Store] - Force fetching entity "${target}"`)
fetch([target])
}
return _entities.value[key] || null
}
/**
* Resolve an entity from cache by full entity identifier.
*/
function entityByIdentifier(identifier: EntityIdentifier, retrieve: boolean = false): EntityObject | null {
if (retrieve === true && !_entities.value[identifier]) {
console.debug(`[Mail Manager][Store] - Force fetching entity "${identifier}"`)
const { provider, service, collection, identifier: id } = parseEntityIdentifier(identifier)
fetch(provider, service, collection, [id])
}
return _entities.value[identifier] || null
}
/**
* Resolve multiple entities from cache by full entity identifiers.
*/
function entitiesByIdentifiers(identifiers: EntityIdentifier[], retrieve: boolean = false): Record<EntityIdentifier, EntityObject> {
const resolved: Record<EntityIdentifier, EntityObject> = {} as Record<EntityIdentifier, EntityObject>
Array.from(new Set(identifiers)).forEach(identifier => {
const entity = entityByIdentifier(identifier, retrieve)
if (entity) {
resolved[identifier] = entity
}
})
return resolved
return _entities.value[target] || null
}
/**
@@ -102,52 +70,19 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
*
* @returns Array of entity objects
*/
function entitiesForCollection(provider: string, service: string | number, collection: string | number, retrieve: boolean = false): EntityObject[] {
const collectionKeyPrefix = `${provider}:${service}:${collection}:`
function entitiesForCollection(target: CollectionIdentifier, retrieve: boolean = false): EntityObject[] {
const collectionEntities = Object.entries(_entities.value)
.filter(([key]) => key.startsWith(collectionKeyPrefix))
.filter(([key]) => key.startsWith(target))
.map(([_, entity]) => entity)
if (retrieve === true && collectionEntities.length === 0) {
console.debug(`[Mail Manager][Store] - Force fetching entities for collection "${provider}:${service}:${collection}"`)
const sources: SourceSelector = {
[provider]: {
[String(service)]: {
[String(collection)]: true
}
}
}
list(sources)
console.debug(`[Mail Manager][Store] - Force fetching entities for collection "${target}"`)
list([target])
}
return collectionEntities
}
/**
* Create unique key for an entity
*/
function identifierKey(provider: string, service: string | number, collection: string | number, identifier: string | number): string {
return `${provider}:${service}:${collection}:${identifier}`
}
/**
* Parse a full entity identifier into its components.
*/
function parseEntityIdentifier(identifier: EntityIdentifier): {
provider: string
service: string
collection: string
identifier: string
} {
const [provider, service, collection, entity] = identifier.split(':', 4)
return {
provider,
service,
collection,
identifier: entity,
}
}
// Actions
/**
@@ -160,19 +95,18 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
*
* @returns Promise with entity object list keyed by identifier
*/
async function list(sources?: SourceSelector, filter?: ListFilter, sort?: ListSort, range?: ListRange): Promise<Record<string, EntityObject>> {
async function list(sources: CollectionIdentifier[], filter?: ListFilter, sort?: ListSort, range?: ListRange): Promise<Record<string, EntityObject>> {
transceiving.value = true
try {
const added: Record<string, EntityObject> = {}
const entities: Record<string, EntityObject> = {}
await entityService.stream({ sources, filter, sort, range }, (entity: EntityObject) => {
const key = identifierKey(entity.provider, entity.service, entity.collection, entity.identifier)
_entities.value[key] = entity
added[key] = entity
_entities.value[entity.identifier] = entity
entities[entity.identifier] = entity
})
console.debug('[Mail Manager][Store] - Successfully retrieved', Object.keys(added).length, 'entities')
return added
console.debug('[Mail Manager][Store] - Successfully retrieved', Object.keys(entities).length, 'entities')
return entities
} catch (error: any) {
console.error('[Mail Manager][Store] - Failed to retrieve entities:', error)
throw error
@@ -191,17 +125,16 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
*
* @returns Promise with entity objects keyed by identifier
*/
async function fetch(provider: string, service: string | number, collection: string | number, identifiers: (string | number)[]): Promise<Record<string, EntityObject>> {
async function fetch(targets: EntityIdentifier[]): Promise<Record<string, EntityObject>> {
transceiving.value = true
try {
const response = await entityService.fetch({ provider, service, collection, identifiers })
const response = await entityService.fetch({ targets })
// Merge fetched entities into state
const entities: Record<string, EntityObject> = {}
Object.entries(response).forEach(([identifier, entityData]) => {
const key = identifierKey(provider, service, collection, identifier)
entities[key] = entityData
_entities.value[key] = entityData
Object.entries(response).forEach(([identifier, entity]) => {
entities[identifier] = entity
_entities.value[identifier] = entity
})
console.debug('[Mail Manager][Store] - Successfully fetched', Object.keys(entities).length, 'entities')
@@ -215,16 +148,16 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
}
/**
* Retrieve entity availability status for a given source selector
* Retrieve entity availability status for a given set of entity identifiers
*
* @param sources - source selector to check availability for
* @param targets - array of entity identifiers to check availability for
*
* @returns Promise with entity availability status
*/
async function extant(sources: SourceSelector) {
async function extant(targets: EntityIdentifier[]) {
transceiving.value = true
try {
const response = await entityService.extant({ sources })
const response = await entityService.extant({ targets })
console.debug('[Mail Manager][Store] - Successfully checked entity availability')
return response
} catch (error: any) {
@@ -245,7 +178,7 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
* Note: Delta returns only identifiers, not full entities.
* Caller should fetch full entities for additions/modifications separately.
*/
async function delta(sources: SourceSelector) {
async function delta(sources: CollectionIdentifier[]) {
transceiving.value = true
try {
const response = await entityService.delta({ sources })
@@ -266,13 +199,9 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
// Process deletions (remove from store)
if (collectionData.deletions && collectionData.deletions.length > 0) {
collectionData.deletions.forEach((identifier) => {
const key = identifierKey(provider, service, collection, identifier)
delete _entities.value[key]
delete _entities.value[identifier]
})
}
// Note: additions and modifications contain only identifiers
// The caller should fetch full entities using the fetch() method
})
})
})
@@ -288,25 +217,25 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
}
/**
* Create a new entity with given provider, service, collection, and data
* Create a new entity with given collection identifier and properties
*
* @param provider - provider identifier for the new entity
* @param service - service identifier for the new entity
* @param collection - collection identifier for the new entity
* @param data - entity properties for creation
* @param target - collection identifier for the new entity
* @param properties - entity properties for creation
*
* @returns Promise with created entity object
*/
async function create(provider: string, service: string | number, collection: string | number, data: any): Promise<EntityObject> {
async function create(target: CollectionIdentifier, properties: MessageInterface | MessageObject): Promise<EntityObject> {
transceiving.value = true
try {
const response = await entityService.create({ provider, service, collection, properties: data })
if (properties instanceof MessageObject) {
properties = properties.toJson()
}
const response = await entityService.create({ target, properties })
// Add created entity to state
const key = identifierKey(response.provider, response.service, response.collection, response.identifier)
_entities.value[key] = response
_entities.value[response.identifier] = response
console.debug('[Mail Manager][Store] - Successfully created entity:', key)
console.debug('[Mail Manager][Store] - Successfully created entity:', response.identifier)
return response
} catch (error: any) {
console.error('[Mail Manager][Store] - Failed to create entity:', error)
@@ -327,16 +256,18 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
*
* @returns Promise with updated entity object
*/
async function update(provider: string, service: string | number, collection: string | number, identifier: string | number, data: any): Promise<EntityObject> {
async function update(target: EntityIdentifier, properties: MessageInterface | MessageObject): Promise<EntityObject> {
transceiving.value = true
try {
const response = await entityService.update({ provider, service, collection, identifier, properties: data })
if (properties instanceof MessageObject) {
properties = properties.toJson()
}
const response = await entityService.update({ target, properties })
// Update entity in state
const key = identifierKey(response.provider, response.service, response.collection, response.identifier)
_entities.value[key] = response
_entities.value[response.identifier] = response
console.debug('[Mail Manager][Store] - Successfully updated entity:', key)
console.debug('[Mail Manager][Store] - Successfully updated entity:', response.identifier)
return response
} catch (error: any) {
console.error('[Mail Manager][Store] - Failed to update entity:', error)
@@ -355,7 +286,7 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
*
* @returns Promise with deletion results keyed by source identifier
*/
async function remove(sources: EntityIdentifier[]): Promise<EntityDeleteResponse> {
async function remove(sources: EntityIdentifier[]): Promise<{successes: EntityIdentifier[], failures: EntityIdentifier[]}> {
transceiving.value = true
try {
const response = await entityService.delete({ sources })
@@ -363,42 +294,39 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
const failures: EntityIdentifier[] = []
Object.entries(response).forEach(([sourceIdentifier, result]) => {
const originalIdentifier = sourceIdentifier as EntityIdentifier
if (!result.disposition || result.disposition === 'error') {
console.warn(`[Mail Manager][Store] - Entity move on "${sourceIdentifier}" returned an error: ${result.error})`)
failures.push(sourceIdentifier)
console.warn(`[Mail Manager][Store] - Entity move on "${originalIdentifier}" returned an error: ${result.error})`)
failures.push(originalIdentifier)
return
}
if (!result.disposition || (result.disposition !== 'moved' && result.disposition !== 'deleted')) {
console.warn(`[Mail Manager][Store] - Entity move on "${sourceIdentifier}" returned invalid disposition: ${result.disposition})`)
failures.push(sourceIdentifier)
console.warn(`[Mail Manager][Store] - Entity move on "${originalIdentifier}" returned invalid disposition: ${result.disposition})`)
failures.push(originalIdentifier)
return
}
const cachedEntity = _entities.value[sourceIdentifier]
const cachedEntity = _entities.value[originalIdentifier]
if (!cachedEntity) {
return
}
if (result.disposition === 'moved') {
const mutation = parseEntityIdentifier(result.mutation?.identifier || sourceIdentifier)
const movedEntity = cachedEntity.clone().fromJson({
...cachedEntity.toJson(),
provider: mutation.provider,
service: mutation.service,
collection: mutation.collection,
identifier: mutation.identifier,
collection: result.destination,
identifier: result.mutation,
})
const key = identifierKey(mutation.provider, mutation.service, mutation.collection, mutation.identifier)
_entities.value[key] = movedEntity
_entities.value[result.mutation] = movedEntity
}
delete _entities.value[sourceIdentifier]
successes.push(sourceIdentifier)
delete _entities.value[originalIdentifier]
successes.push(originalIdentifier)
})
console.debug('[Mail Manager][Store] - Successfully deleted', Object.keys(response).length, 'entities')
return [successes, failures]
console.debug('[Mail Manager][Store] - Successfully deleted', successes.length, 'entities')
return { successes, failures }
} catch (error: any) {
console.error('[Mail Manager][Store] - Failed to delete entities:', error)
throw error
@@ -418,7 +346,7 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
*
* @returns Promise with move results keyed by source identifier
*/
async function move(target: CollectionIdentifier, sources: EntityIdentifier[]): Promise<EntityIdentifier[]> {
async function move(target: CollectionIdentifier, sources: EntityIdentifier[]): Promise<{successes: EntityIdentifier[], failures: EntityIdentifier[]}> {
transceiving.value = true
try {
const response = await entityService.move({ target, sources })
@@ -426,40 +354,37 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
const failures: EntityIdentifier[] = []
Object.entries(response).forEach(([sourceIdentifier, result]) => {
const originalIdentifier = sourceIdentifier as EntityIdentifier
if (!result.disposition || result.disposition === 'error') {
console.warn(`[Mail Manager][Store] - Entity move on "${sourceIdentifier}" returned an error: ${result.error})`)
failures.push(sourceIdentifier)
console.warn(`[Mail Manager][Store] - Entity move on "${originalIdentifier}" returned an error: ${result.error})`)
failures.push(originalIdentifier)
return
}
if (!result.disposition || result.disposition !== 'moved') {
console.warn(`[Mail Manager][Store] - Entity move on "${sourceIdentifier}" returned invalid disposition: ${result.disposition})`)
failures.push(sourceIdentifier)
console.warn(`[Mail Manager][Store] - Entity move on "${originalIdentifier}" returned invalid disposition: ${result.disposition})`)
failures.push(originalIdentifier)
return
}
const cachedEntity = _entities.value[sourceIdentifier]
const cachedEntity = _entities.value[originalIdentifier]
if (!cachedEntity) {
return
}
const mutation = parseEntityIdentifier(result.mutation?.identifier || sourceIdentifier)
const movedEntity = cachedEntity.clone().fromJson({
...cachedEntity.toJson(),
provider: mutation.provider,
service: mutation.service,
collection: mutation.collection,
identifier: mutation.identifier,
collection: result.destination,
identifier: result.mutation,
})
const movedKey = identifierKey(mutation.provider, mutation.service, mutation.collection, mutation.identifier)
_entities.value[movedKey] = movedEntity
_entities.value[result.mutation] = movedEntity
delete _entities.value[sourceIdentifier]
successes.push(sourceIdentifier)
delete _entities.value[originalIdentifier]
successes.push(originalIdentifier)
})
console.debug('[Mail Manager][Store] - Successfully moved', Object.keys(response).length, 'entities')
return [successes, failures]
console.debug('[Mail Manager][Store] - Successfully moved', successes.length, 'entities')
return { successes, failures }
} catch (error: any) {
console.error('[Mail Manager][Store] - Failed to move entities:', error)
throw error
@@ -502,18 +427,12 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
*
* @returns Promise resolving to { total } when the stream completes
*/
async function stream(
sources?: SourceSelector,
filter?: ListFilter,
sort?: ListSort,
range?: ListRange
): Promise<{ total: number }> {
async function stream(sources?: CollectionIdentifier[], filter?: ListFilter, sort?: ListSort, range?: ListRange): Promise<{ total: number }> {
transceiving.value = true
try {
const request: EntityStreamRequest = { sources, filter, sort, range }
const result = await entityService.stream(request, (entity: EntityObject) => {
const key = identifierKey(entity.provider, entity.service, entity.collection, entity.identifier)
_entities.value[key] = entity
_entities.value[entity.identifier] = entity
})
console.debug('[Mail Manager][Store] - Successfully streamed', result.total, 'entities')
return result
@@ -534,9 +453,7 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
has,
entities,
entitiesForCollection,
entitiesByIdentifiers,
entity,
entityByIdentifier,
list,
fetch,
extant,

View File

@@ -1,7 +1,7 @@
/**
* Collection type definitions
*/
import type { CollectionIdentifier, ListFilter, ListSort, SourceSelector } from './common';
import type { CollectionIdentifier, ListFilter, ListSort, ServiceIdentifier, SourceSelector } from './common';
/**
* Collection information
@@ -11,8 +11,8 @@ export interface CollectionInterface<T = CollectionPropertiesInterface> {
version: number;
provider: string;
service: string | number;
collection: string | number | null;
identifier: string | number;
collection: CollectionIdentifier | null;
identifier: CollectionIdentifier;
signature?: string | null;
created?: string | null;
modified?: string | null;
@@ -47,7 +47,7 @@ export interface CollectionPropertiesModelInterface extends Omit<CollectionPrope
* Collection list
*/
export interface CollectionListRequest {
sources?: SourceSelector;
sources?: ServiceIdentifier[] | CollectionIdentifier[];
filter?: ListFilter;
sort?: ListSort;
}
@@ -64,18 +64,18 @@ export interface CollectionListResponse {
* Collection fetch
*/
export interface CollectionFetchRequest {
provider: string;
service: string | number;
collection: string | number;
targets: CollectionIdentifier[];
}
export interface CollectionFetchResponse extends CollectionInterface {}
export interface CollectionFetchResponse {
[identifier: CollectionIdentifier]: CollectionInterface;
}
/**
* Collection extant
*/
export interface CollectionExtantRequest {
sources: SourceSelector;
targets: CollectionIdentifier[];
}
export interface CollectionExtantResponse {
@@ -92,7 +92,7 @@ export interface CollectionExtantResponse {
export interface CollectionCreateRequest {
provider: string;
service: string | number;
collection?: string | number | null; // Parent Collection Identifier
target?: CollectionIdentifier; // Optional parent target for the new collection
properties: CollectionMutableProperties;
}
@@ -102,9 +102,7 @@ export interface CollectionCreateResponse extends CollectionInterface {}
* Collection modify
*/
export interface CollectionUpdateRequest {
provider: string;
service: string | number;
identifier: string | number;
target: CollectionIdentifier;
properties: CollectionMutableProperties;
}
@@ -114,17 +112,15 @@ export interface CollectionUpdateResponse extends CollectionInterface {}
* Collection delete
*/
export interface CollectionDeleteRequest {
provider: string;
service: string | number;
identifier: string | number;
target: CollectionIdentifier;
options?: {
force?: boolean; // Whether to force delete even if collection is not empty
};
}
export interface CollectionDeleteResponse {
outcome: 'deleted' | 'moved';
data?: CollectionInterface | null; // If moved, the new location of the collection
disposition: 'deleted' | 'moved';
mutation?: CollectionInterface | null; // If moved, the new location of the collection
}
/**

View File

@@ -115,8 +115,8 @@ export type EntitySelector = (string | number)[];
export type ProviderIdentifier = `${string}`;
export type ServiceIdentifier = `${string}:${string}`;
export type CollectionIdentifier = `${string}:${string}:${string}`;
export type EntityIdentifier = `${string}:${string}:${string}:${string}`;
export type CollectionIdentifier = `${string}:${string}:${string | number}`;
export type EntityIdentifier = `${string}:${string}:${string}:${string | number}`;
/**
* Filter comparison for list operations

View File

@@ -4,7 +4,6 @@
import type {
CollectionIdentifier,
EntityIdentifier,
SourceSelector,
ListFilter,
ListRange,
ListSort,
@@ -19,8 +18,8 @@ export interface EntityInterface<T = MessageInterface> {
version: number;
provider: string;
service: string;
collection: string | number;
identifier: string | number;
collection: CollectionIdentifier;
identifier: EntityIdentifier;
signature: string | null;
created: string | null;
modified: string | null;
@@ -33,7 +32,7 @@ export interface EntityModelInterface extends Omit<EntityInterface<MessageModelI
* Entity list
*/
export interface EntityListRequest {
sources?: SourceSelector;
sources?: CollectionIdentifier[];
filter?: ListFilter;
sort?: ListSort;
range?: ListRange;
@@ -53,10 +52,7 @@ export interface EntityListResponse {
* Entity fetch
*/
export interface EntityFetchRequest {
provider: string;
service: string | number;
collection: string | number;
identifiers: (string | number)[];
targets: EntityIdentifier[];
}
export interface EntityFetchResponse {
@@ -67,7 +63,7 @@ export interface EntityFetchResponse {
* Entity extant
*/
export interface EntityExtantRequest {
sources: SourceSelector;
targets: EntityIdentifier[];
}
export interface EntityExtantResponse {
@@ -84,7 +80,7 @@ export interface EntityExtantResponse {
* Entity delta
*/
export interface EntityDeltaRequest {
sources: SourceSelector;
sources: CollectionIdentifier[];
}
export interface EntityDeltaResponse {
@@ -104,9 +100,7 @@ export interface EntityDeltaResponse {
* Entity create
*/
export interface EntityCreateRequest<T = MessageInterface> {
provider: string;
service: string | number;
collection: string | number;
target: CollectionIdentifier;
properties: T;
}
@@ -116,10 +110,7 @@ export interface EntityCreateResponse<T = MessageInterface> extends EntityInterf
* Entity update
*/
export interface EntityUpdateRequest<T = MessageInterface> {
provider: string;
service: string | number;
collection: string | number;
identifier: string | number;
target: EntityIdentifier;
properties: T;
}
@@ -193,7 +184,7 @@ export interface EntityTransmitResponse {
* Entity stream
*/
export interface EntityStreamRequest {
sources?: SourceSelector;
sources?: CollectionIdentifier[];
filter?: ListFilter;
sort?: ListSort;
range?: ListRange;

View File

@@ -10,21 +10,23 @@ export interface MessageModelInterface extends Omit<{
export interface MessageInterface {
'@type': string;
urid?: string | null;
size?: number | null;
date?: string | null;
receivedDate?: string | null;
sentDate?: string | null;
subject?: string | null;
snippet?: string | null;
headers?: Record<string, string> | null;
urid?: string | null;
inReplyTo?: string | null;
references?: string | null;
received?: string | null;
sent?: string | null;
sender?: MessageAddressInterface | null;
from?: MessageAddressInterface | null;
replyTo?: Array<MessageAddressInterface> | null;
to?: Array<MessageAddressInterface> | null;
cc?: Array<MessageAddressInterface> | null;
bcc?: Array<MessageAddressInterface> | null;
replyTo?: Array<MessageAddressInterface> | null;
flags?: MessageFlagsInterface | null;
subject?: string | null;
body?: MessagePartInterface | null;
attachments?: Array<MessagePartInterface> | [];
flags?: MessageFlagsInterface | null;
}
export interface MessageAddressInterface {