chore: standardize protocol

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-02-14 11:45:34 -05:00
parent 169b7b4c91
commit fefa0a0384
18 changed files with 3090 additions and 1239 deletions

View File

@@ -1,261 +1,369 @@
/**
* Entities Store
*/
import { ref, computed, readonly } from 'vue'
import { defineStore } from 'pinia'
import { entityService } from '../services'
import type { MessageObject, EntityWrapper, MessageSendRequest } from '../types'
import { EntityObject } from '../models'
import type { EntityTransmitRequest, EntityTransmitResponse } from '../types/entity'
import type { SourceSelector, ListFilter, ListSort, ListRange } from '../types/common'
export const useEntitiesStore = defineStore('mail-entities', {
state: () => ({
messages: {} as Record<string, Record<string, Record<string, Record<string, EntityWrapper<MessageObject>>>>>,
signatures: {} as Record<string, Record<string, Record<string, string>>>, // Track delta signatures
loading: false,
error: null as string | null,
}),
export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
// State
const _entities = ref<Record<string, EntityObject>>({})
const transceiving = ref(false)
actions: {
async loadMessages(sources?: any, filter?: any, sort?: any, range?: any) {
this.loading = true
this.error = null
try {
const response = await entityService.list({ sources, filter, sort, range })
// Entities come as objects keyed by identifier
Object.entries(response).forEach(([provider, providerData]) => {
Object.entries(providerData).forEach(([service, serviceData]) => {
Object.entries(serviceData).forEach(([collection, entities]) => {
if (!this.messages[provider]) {
this.messages[provider] = {}
}
if (!this.messages[provider][service]) {
this.messages[provider][service] = {}
}
if (!this.messages[provider][service][collection]) {
this.messages[provider][service][collection] = {}
}
// Entities are already keyed by identifier
this.messages[provider][service][collection] = entities as Record<string, EntityWrapper<MessageObject>>
})
})
})
} catch (error: any) {
this.error = error.message
throw error
} finally {
this.loading = false
}
},
/**
* Get count of entities in store
*/
const count = computed(() => Object.keys(_entities.value).length)
async getMessages(
provider: string,
service: string | number,
collection: string | number,
identifiers: (string | number)[],
properties?: string[]
) {
this.loading = true
this.error = null
try {
const response = await entityService.fetch({
provider,
service,
collection,
identifiers,
properties
})
// Update in store
if (!this.messages[provider]) {
this.messages[provider] = {}
/**
* Check if any entities are present in store
*/
const has = computed(() => count.value > 0)
/**
* Get all entities present in store
*/
const entities = computed(() => Object.values(_entities.value))
/**
* Get a specific entity from store, with optional retrieval
*
* @param provider - provider identifier
* @param service - service identifier
* @param collection - collection identifier
* @param identifier - entity identifier
* @param retrieve - Retrieve behavior: true = fetch if missing or refresh, false = cache only
*
* @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])
}
return _entities.value[key] || null
}
/**
* Get all entities for a specific collection
*
* @param provider - provider identifier
* @param service - service identifier
* @param collection - collection identifier
* @param retrieve - Retrieve behavior: true = fetch if missing or refresh, false = cache only
*
* @returns Array of entity objects
*/
function entitiesForCollection(provider: string, service: string | number, collection: string | number, retrieve: boolean = false): EntityObject[] {
const collectionKeyPrefix = `${provider}:${service}:${collection}:`
const collectionEntities = Object.entries(_entities.value)
.filter(([key]) => key.startsWith(collectionKeyPrefix))
.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
}
}
if (!this.messages[provider][String(service)]) {
this.messages[provider][String(service)] = {}
}
if (!this.messages[provider][String(service)][String(collection)]) {
this.messages[provider][String(service)][String(collection)] = {}
}
// Index fetched entities by identifier
response.entities.forEach((entity: EntityWrapper<MessageObject>) => {
this.messages[provider][String(service)][String(collection)][entity.identifier] = entity
})
return response
} catch (error: any) {
this.error = error.message
throw error
} finally {
this.loading = false
}
},
list(sources)
}
return collectionEntities
}
async searchMessages(
provider: string,
service: string | number,
query: string,
collections?: (string | number)[],
filter?: any,
sort?: any,
range?: any
) {
this.loading = true
this.error = null
try {
const response = await entityService.search({
provider,
service,
query,
collections,
filter,
sort,
range
})
return response
} catch (error: any) {
this.error = error.message
throw error
} finally {
this.loading = false
}
},
/**
* 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}`
}
async sendMessage(request: MessageSendRequest) {
this.loading = true
this.error = null
try {
const response = await entityService.send(request)
return response
} catch (error: any) {
this.error = error.message
throw error
} finally {
this.loading = false
}
},
// Actions
/**
* Retrieve all or specific entities, optionally filtered by source selector
*
* @param sources - optional source selector
* @param filter - optional list filter
* @param sort - optional list sort
* @param range - optional list range
*
* @returns Promise with entity object list keyed by identifier
*/
async function list(sources?: SourceSelector, filter?: ListFilter, sort?: ListSort, range?: ListRange): Promise<Record<string, EntityObject>> {
transceiving.value = true
try {
const response = await entityService.list({ sources, filter, sort, range })
async getDelta(sources: any) {
this.loading = true
this.error = null
try {
// Sources are already in correct format: { provider: { service: { collection: signature } } }
const response = await entityService.delta({ sources })
// Process delta and update store
Object.entries(response).forEach(([provider, providerData]) => {
Object.entries(providerData).forEach(([service, serviceData]) => {
Object.entries(serviceData).forEach(([collection, collectionData]) => {
// Skip if no changes (server returns false or string signature)
if (collectionData === false || typeof collectionData === 'string') {
return
}
if (!this.messages[provider]) {
this.messages[provider] = {}
}
if (!this.messages[provider][service]) {
this.messages[provider][service] = {}
}
if (!this.messages[provider][service][collection]) {
this.messages[provider][service][collection] = {}
}
const collectionMessages = this.messages[provider][service][collection]
// Update signature if provided
if (typeof collectionData === 'object' && collectionData.signature) {
if (!this.signatures[provider]) {
this.signatures[provider] = {}
}
if (!this.signatures[provider][service]) {
this.signatures[provider][service] = {}
}
this.signatures[provider][service][collection] = collectionData.signature
console.log(`[Store] Updated signature for ${provider}/${service}/${collection}: "${collectionData.signature}"`)
}
// Process additions (from delta response format)
if (collectionData.additions) {
// Note: additions are just identifiers, need to fetch full entities separately
// This is handled by the sync composable
}
// Process modifications
if (collectionData.modifications) {
// Note: modifications are just identifiers, need to fetch full entities separately
}
// Remove deleted messages
if (collectionData.deletions) {
collectionData.deletions.forEach((id: string | number) => {
delete collectionMessages[String(id)]
})
}
// Legacy support: Also handle created/modified/deleted format
if (collectionData.created) {
collectionData.created.forEach((entity: EntityWrapper<MessageObject>) => {
collectionMessages[entity.identifier] = entity
})
}
if (collectionData.modified) {
collectionData.modified.forEach((entity: EntityWrapper<MessageObject>) => {
collectionMessages[entity.identifier] = entity
})
}
if (collectionData.deleted) {
collectionData.deleted.forEach((id: string | number) => {
delete collectionMessages[String(id)]
})
}
})
})
})
return response
} catch (error: any) {
this.error = error.message
throw error
} finally {
this.loading = false
}
},
},
getters: {
messageList: (state) => {
const list: EntityWrapper<MessageObject>[] = []
Object.values(state.messages).forEach(providerMessages => {
Object.values(providerMessages).forEach(serviceMessages => {
Object.values(serviceMessages).forEach(collectionMessages => {
Object.values(collectionMessages).forEach(message => {
list.push(message)
// Flatten nested structure: provider:service:collection:entity -> "provider:service:collection:entity": object
const entities: Record<string, EntityObject> = {}
Object.entries(response).forEach(([providerId, providerServices]) => {
Object.entries(providerServices).forEach(([serviceId, serviceCollections]) => {
Object.entries(serviceCollections).forEach(([collectionId, collectionEntities]) => {
Object.entries(collectionEntities).forEach(([entityId, entityData]) => {
const key = identifierKey(providerId, serviceId, collectionId, entityId)
entities[key] = entityData
})
})
})
})
return list
},
messageCount: (state) => {
let count = 0
Object.values(state.messages).forEach(providerMessages => {
Object.values(providerMessages).forEach(serviceMessages => {
Object.values(serviceMessages).forEach(collectionMessages => {
count += Object.keys(collectionMessages).length
// Merge retrieved entities into state
_entities.value = { ..._entities.value, ...entities }
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
} finally {
transceiving.value = false
}
}
/**
* Retrieve specific entities by provider, service, collection, and identifiers
*
* @param provider - provider identifier
* @param service - service identifier
* @param collection - collection identifier
* @param identifiers - array of entity identifiers to fetch
*
* @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>> {
transceiving.value = true
try {
const response = await entityService.fetch({ provider, service, collection, identifiers })
// 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
})
console.debug('[Mail Manager][Store] - Successfully fetched', Object.keys(entities).length, 'entities')
return entities
} catch (error: any) {
console.error('[Mail Manager][Store] - Failed to fetch entities:', error)
throw error
} finally {
transceiving.value = false
}
}
/**
* Retrieve entity availability status for a given source selector
*
* @param sources - source selector to check availability for
*
* @returns Promise with entity availability status
*/
async function extant(sources: SourceSelector) {
transceiving.value = true
try {
const response = await entityService.extant({ sources })
console.debug('[Mail Manager][Store] - Successfully checked entity availability')
return response
} catch (error: any) {
console.error('[Mail Manager][Store] - Failed to check entity availability:', error)
throw error
} finally {
transceiving.value = false
}
}
/**
* Create a new entity with given provider, service, collection, and data
*
* @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
*
* @returns Promise with created entity object
*/
async function create(provider: string, service: string | number, collection: string | number, data: any): Promise<EntityObject> {
transceiving.value = true
try {
const response = await entityService.create({ provider, service, collection, properties: data })
// Add created entity to state
const key = identifierKey(response.provider, response.service, response.collection, response.identifier)
_entities.value[key] = response
console.debug('[Mail Manager][Store] - Successfully created entity:', key)
return response
} catch (error: any) {
console.error('[Mail Manager][Store] - Failed to create entity:', error)
throw error
} finally {
transceiving.value = false
}
}
/**
* Update an existing entity with given provider, service, collection, identifier, and data
*
* @param provider - provider identifier for the entity to update
* @param service - service identifier for the entity to update
* @param collection - collection identifier for the entity to update
* @param identifier - entity identifier for the entity to update
* @param data - entity properties for update
*
* @returns Promise with updated entity object
*/
async function update(provider: string, service: string | number, collection: string | number, identifier: string | number, data: any): Promise<EntityObject> {
transceiving.value = true
try {
const response = await entityService.update({ provider, service, collection, identifier, properties: data })
// Update entity in state
const key = identifierKey(response.provider, response.service, response.collection, response.identifier)
_entities.value[key] = response
console.debug('[Mail Manager][Store] - Successfully updated entity:', key)
return response
} catch (error: any) {
console.error('[Mail Manager][Store] - Failed to update entity:', error)
throw error
} finally {
transceiving.value = false
}
}
/**
* Delete an entity by provider, service, collection, and identifier
*
* @param provider - provider identifier for the entity to delete
* @param service - service identifier for the entity to delete
* @param collection - collection identifier for the entity to delete
* @param identifier - entity identifier for the entity to delete
*
* @returns Promise with deletion result
*/
async function remove(provider: string, service: string | number, collection: string | number, identifier: string | number): Promise<any> {
transceiving.value = true
try {
const response = await entityService.delete({ provider, service, collection, identifier })
// Remove entity from state
const key = identifierKey(provider, service, collection, identifier)
delete _entities.value[key]
console.debug('[Mail Manager][Store] - Successfully deleted entity:', key)
return response
} catch (error: any) {
console.error('[Mail Manager][Store] - Failed to delete entity:', error)
throw error
} finally {
transceiving.value = false
}
}
/**
* Retrieve delta changes for entities
*
* @param sources - source selector for delta check
*
* @returns Promise with delta changes (additions, modifications, deletions)
*
* Note: Delta returns only identifiers, not full entities.
* Caller should fetch full entities for additions/modifications separately.
*/
async function delta(sources: SourceSelector) {
transceiving.value = true
try {
const response = await entityService.delta({ sources })
// Process delta and update store
Object.entries(response).forEach(([provider, providerData]) => {
// Skip if no changes for provider
if (providerData === false) return
Object.entries(providerData).forEach(([service, serviceData]) => {
// Skip if no changes for service
if (serviceData === false) return
Object.entries(serviceData).forEach(([collection, collectionData]) => {
// Skip if no changes for collection
if (collectionData === false) return
// 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]
})
}
// Note: additions and modifications contain only identifiers
// The caller should fetch full entities using the fetch() method
})
})
})
return count
},
hasMessages: (state) => {
return Object.values(state.messages).some(providerMessages =>
Object.values(providerMessages).some(serviceMessages =>
Object.values(serviceMessages).some(collectionMessages =>
Object.keys(collectionMessages).length > 0
)
)
)
},
},
console.debug('[Mail Manager][Store] - Successfully processed delta changes')
return response
} catch (error: any) {
console.error('[Mail Manager][Store] - Failed to process delta:', error)
throw error
} finally {
transceiving.value = false
}
}
/**
* Send/transmit an entity
*
* @param request - transmit request parameters
*
* @returns Promise with transmission result
*/
async function transmit(request: EntityTransmitRequest): Promise<EntityTransmitResponse> {
transceiving.value = true
try {
const response = await entityService.transmit(request)
console.debug('[Mail Manager][Store] - Successfully transmitted entity')
return response
} catch (error: any) {
console.error('[Mail Manager][Store] - Failed to transmit entity:', error)
throw error
} finally {
transceiving.value = false
}
}
// Return public API
return {
// State (readonly)
transceiving: readonly(transceiving),
// Getters
count,
has,
entities,
entitiesForCollection,
// Actions
entity,
list,
fetch,
extant,
create,
update,
delete: remove,
delta,
transmit,
}
})