chore: standardize protocol
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
+312
-152
@@ -1,169 +1,329 @@
|
||||
/**
|
||||
* Collections Store
|
||||
*/
|
||||
|
||||
import { ref, computed, readonly } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { collectionService } from '../services'
|
||||
import type { CollectionInterface, CollectionCreateRequest } from '../types'
|
||||
import { CollectionObject, CollectionPropertiesObject } from '../models/collection'
|
||||
import { CollectionObject } from '../models/collection'
|
||||
import type { SourceSelector, ListFilter, ListSort, CollectionMutableProperties } from '../types'
|
||||
|
||||
export const useCollectionsStore = defineStore('mail-collections', {
|
||||
state: () => ({
|
||||
collections: {} as Record<string, Record<string, Record<string, CollectionObject>>>,
|
||||
loading: false,
|
||||
error: null as string | null,
|
||||
}),
|
||||
export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
|
||||
// State
|
||||
const _collections = ref<Record<string, CollectionObject>>({})
|
||||
const transceiving = ref(false)
|
||||
|
||||
actions: {
|
||||
async loadCollections(sources?: any) {
|
||||
this.loading = true
|
||||
this.error = null
|
||||
try {
|
||||
const response = await collectionService.list({ sources })
|
||||
|
||||
// Response is already in nested object format: provider -> service -> collection
|
||||
// Transform to CollectionObject instances
|
||||
const transformed: Record<string, Record<string, Record<string, CollectionObject>>> = {}
|
||||
|
||||
for (const [providerId, providerData] of Object.entries(response)) {
|
||||
transformed[providerId] = {}
|
||||
|
||||
for (const [serviceId, collections] of Object.entries(providerData as any)) {
|
||||
transformed[providerId][serviceId] = {}
|
||||
|
||||
// Collections come as an object keyed by identifier
|
||||
for (const [collectionId, collection] of Object.entries(collections as any)) {
|
||||
// Create CollectionObject instance with provider and service set
|
||||
const collectionData = {
|
||||
...collection,
|
||||
provider: providerId,
|
||||
service: serviceId,
|
||||
} as CollectionInterface
|
||||
|
||||
transformed[providerId][serviceId][collectionId] = new CollectionObject().fromJson(collectionData)
|
||||
}
|
||||
/**
|
||||
* Get count of collections in store
|
||||
*/
|
||||
const count = computed(() => Object.keys(_collections.value).length)
|
||||
|
||||
/**
|
||||
* Check if any collections are present in store
|
||||
*/
|
||||
const has = computed(() => count.value > 0)
|
||||
|
||||
/**
|
||||
* Get all collections present in store
|
||||
*/
|
||||
const collections = computed(() => Object.values(_collections.value))
|
||||
|
||||
/**
|
||||
* Get all collections present in store grouped by service
|
||||
*/
|
||||
const collectionsByService = computed(() => {
|
||||
const groups: Record<string, CollectionObject[]> = {}
|
||||
|
||||
Object.values(_collections.value).forEach((collection) => {
|
||||
const serviceKey = `${collection.provider}:${collection.service}`
|
||||
if (!groups[serviceKey]) {
|
||||
groups[serviceKey] = []
|
||||
}
|
||||
groups[serviceKey].push(collection)
|
||||
})
|
||||
|
||||
return groups
|
||||
})
|
||||
|
||||
/**
|
||||
* Get a specific collection from store, with optional retrieval
|
||||
*
|
||||
* @param provider - provider identifier
|
||||
* @param service - service identifier
|
||||
* @param identifier - collection identifier
|
||||
* @param retrieve - Retrieve behavior: true = fetch if missing or refresh, false = cache only
|
||||
*
|
||||
* @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)
|
||||
}
|
||||
|
||||
return _collections.value[key] || null
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all collections for a specific service
|
||||
*
|
||||
* @param provider - provider identifier
|
||||
* @param service - service identifier
|
||||
* @param retrieve - Retrieve behavior: true = fetch if missing or refresh, false = cache only
|
||||
*
|
||||
* @returns Array of collection objects
|
||||
*/
|
||||
function collectionsForService(provider: string, service: string | number, retrieve: boolean = false): CollectionObject[] {
|
||||
const serviceKeyPrefix = `${provider}:${service}:`
|
||||
const serviceCollections = Object.entries(_collections.value)
|
||||
.filter(([key]) => key.startsWith(serviceKeyPrefix))
|
||||
.map(([_, collection]) => collection)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
return serviceCollections
|
||||
}
|
||||
|
||||
function collectionsInCollection(provider: string, service: string | number, collectionId: string | number, retrieve: boolean = false): CollectionObject[] {
|
||||
const collectionKeyPrefix = `${provider}:${service}:${collectionId}:`
|
||||
const nestedCollections = Object.entries(_collections.value)
|
||||
.filter(([key]) => key.startsWith(collectionKeyPrefix))
|
||||
.map(([_, collection]) => collection)
|
||||
|
||||
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)]: {
|
||||
[String(collectionId)]: true
|
||||
}
|
||||
}
|
||||
|
||||
this.collections = transformed
|
||||
} catch (error: any) {
|
||||
this.error = error.message
|
||||
throw error
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
list(sources)
|
||||
}
|
||||
|
||||
return nestedCollections
|
||||
}
|
||||
|
||||
async getCollection(provider: string, service: string | number, collectionId: string | number) {
|
||||
this.loading = true
|
||||
this.error = null
|
||||
try {
|
||||
const response = await collectionService.fetch({
|
||||
provider,
|
||||
service,
|
||||
collection: collectionId
|
||||
})
|
||||
|
||||
// Create CollectionObject instance
|
||||
const collectionObject = new CollectionObject().fromJson(response)
|
||||
|
||||
// Update in store
|
||||
if (!this.collections[provider]) {
|
||||
this.collections[provider] = {}
|
||||
}
|
||||
if (!this.collections[provider][String(service)]) {
|
||||
this.collections[provider][String(service)] = {}
|
||||
}
|
||||
this.collections[provider][String(service)][String(collectionId)] = collectionObject
|
||||
|
||||
return collectionObject
|
||||
} catch (error: any) {
|
||||
this.error = error.message
|
||||
throw error
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Create unique key for a collection
|
||||
*/
|
||||
function identifierKey(provider: string, service: string | number | null, identifier: string | number | null): string {
|
||||
return `${provider}:${service ?? ''}:${identifier ?? ''}`
|
||||
}
|
||||
|
||||
async createCollection(params: {
|
||||
provider: string
|
||||
service: string | number
|
||||
collection?: string | number | null
|
||||
properties: CollectionPropertiesObject
|
||||
}): Promise<CollectionObject> {
|
||||
this.loading = true
|
||||
this.error = null
|
||||
|
||||
try {
|
||||
// Prepare request data from CollectionPropertiesObject
|
||||
const requestData: CollectionCreateRequest = {
|
||||
provider: params.provider,
|
||||
service: params.service,
|
||||
collection: params.collection ?? null,
|
||||
properties: {
|
||||
'@type': 'mail.collection',
|
||||
label: params.properties.label,
|
||||
role: params.properties.role ?? null,
|
||||
rank: params.properties.rank ?? 0,
|
||||
subscribed: params.properties.subscribed ?? true,
|
||||
},
|
||||
}
|
||||
|
||||
// Call service to create collection
|
||||
const response = await collectionService.create(requestData)
|
||||
|
||||
// Create CollectionObject instance
|
||||
const collectionObject = new CollectionObject().fromJson(response)
|
||||
|
||||
// Update store with new collection
|
||||
const provider = response.provider
|
||||
const service = String(response.service)
|
||||
const identifier = String(response.identifier)
|
||||
|
||||
if (!this.collections[provider]) {
|
||||
this.collections[provider] = {}
|
||||
}
|
||||
if (!this.collections[provider][service]) {
|
||||
this.collections[provider][service] = {}
|
||||
}
|
||||
|
||||
this.collections[provider][service][identifier] = collectionObject
|
||||
|
||||
return collectionObject
|
||||
} catch (error: any) {
|
||||
this.error = error.message
|
||||
throw error
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
},
|
||||
// Actions
|
||||
|
||||
/**
|
||||
* Retrieve all or specific collections, optionally filtered by source selector
|
||||
*
|
||||
* @param sources - optional source selector
|
||||
* @param filter - optional list filter
|
||||
* @param sort - optional list sort
|
||||
*
|
||||
* @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>> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await collectionService.list({ sources, filter, sort })
|
||||
|
||||
getters: {
|
||||
collectionList: (state) => {
|
||||
const list: CollectionObject[] = []
|
||||
Object.values(state.collections).forEach(providerCollections => {
|
||||
Object.values(providerCollections).forEach(serviceCollections => {
|
||||
Object.values(serviceCollections).forEach(collection => {
|
||||
list.push(collection)
|
||||
// Flatten nested structure: provider:service:collection -> "provider:service:collection": object
|
||||
const collections: Record<string, CollectionObject> = {}
|
||||
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)
|
||||
collections[key] = collectionObj
|
||||
})
|
||||
})
|
||||
})
|
||||
return list
|
||||
},
|
||||
|
||||
collectionCount: (state) => {
|
||||
let count = 0
|
||||
Object.values(state.collections).forEach(providerCollections => {
|
||||
Object.values(providerCollections).forEach(serviceCollections => {
|
||||
count += Object.keys(serviceCollections).length
|
||||
})
|
||||
// Merge retrieved collections into state
|
||||
_collections.value = { ..._collections.value, ...collections }
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully retrieved', Object.keys(collections).length, 'collections')
|
||||
return collections
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to retrieve collections:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a specific collection by provider, service, and identifier
|
||||
*
|
||||
* @param provider - provider identifier
|
||||
* @param service - service identifier
|
||||
* @param identifier - collection identifier
|
||||
*
|
||||
* @returns Promise with collection object
|
||||
*/
|
||||
async function fetch(provider: string, service: string | number, identifier: string | number): Promise<CollectionObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await collectionService.fetch({ provider, service, collection: identifier })
|
||||
|
||||
// Merge fetched collection into state
|
||||
const key = identifierKey(response.provider, response.service, response.identifier)
|
||||
_collections.value[key] = response
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully fetched collection:', key)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to fetch collection:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve collection availability status for a given source selector
|
||||
*
|
||||
* @param sources - source selector to check availability for
|
||||
*
|
||||
* @returns Promise with collection availability status
|
||||
*/
|
||||
async function extant(sources: SourceSelector) {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await collectionService.extant({ sources })
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully checked', sources ? Object.keys(sources).length : 0, 'collections')
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to check collections:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new collection with given provider, service, and data
|
||||
*
|
||||
* @param provider - provider identifier for the new collection
|
||||
* @param service - service identifier for the new collection
|
||||
* @param collection - optional parent collection identifier
|
||||
* @param data - collection properties for creation
|
||||
*
|
||||
* @returns Promise with created collection object
|
||||
*/
|
||||
async function create(provider: string, service: string | number, collection: string | number | null, data: CollectionMutableProperties): Promise<CollectionObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await collectionService.create({
|
||||
provider,
|
||||
service,
|
||||
collection,
|
||||
properties: data
|
||||
})
|
||||
return count
|
||||
},
|
||||
|
||||
// Merge created collection into state
|
||||
const key = identifierKey(response.provider, response.service, response.identifier)
|
||||
_collections.value[key] = response
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully created collection:', key)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to create collection:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
hasCollections: (state) => {
|
||||
return Object.values(state.collections).some(providerCollections =>
|
||||
Object.values(providerCollections).some(serviceCollections =>
|
||||
Object.keys(serviceCollections).length > 0
|
||||
)
|
||||
)
|
||||
},
|
||||
},
|
||||
/**
|
||||
* Update an existing collection with given provider, service, identifier, and data
|
||||
*
|
||||
* @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
|
||||
*
|
||||
* @returns Promise with updated collection object
|
||||
*/
|
||||
async function update(provider: string, service: string | number, identifier: string | number, data: CollectionMutableProperties): Promise<CollectionObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await collectionService.update({
|
||||
provider,
|
||||
service,
|
||||
identifier,
|
||||
properties: data
|
||||
})
|
||||
|
||||
// Merge updated collection into state
|
||||
const key = identifierKey(response.provider, response.service, response.identifier)
|
||||
_collections.value[key] = response
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully updated collection:', key)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to update collection:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a collection by provider, service, and identifier
|
||||
*
|
||||
* @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
|
||||
*
|
||||
* @returns Promise with deletion result
|
||||
*/
|
||||
async function remove(provider: string, service: string | number, identifier: string | number): Promise<any> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
await collectionService.delete({ provider, service, identifier })
|
||||
|
||||
// Remove deleted collection from state
|
||||
const key = identifierKey(provider, service, identifier)
|
||||
delete _collections.value[key]
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully deleted collection:', key)
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to delete collection:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// Return public API
|
||||
return {
|
||||
// State (readonly)
|
||||
transceiving: readonly(transceiving),
|
||||
// Getters
|
||||
count,
|
||||
has,
|
||||
collections,
|
||||
collectionsByService,
|
||||
collectionsForService,
|
||||
collectionsInCollection,
|
||||
// Actions
|
||||
collection,
|
||||
list,
|
||||
fetch,
|
||||
extant,
|
||||
create,
|
||||
update,
|
||||
delete: remove,
|
||||
}
|
||||
})
|
||||
|
||||
+349
-241
@@ -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,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -12,59 +12,60 @@ export const useProvidersStore = defineStore('mailProvidersStore', () => {
|
||||
// State
|
||||
const _providers = ref<Record<string, ProviderObject>>({})
|
||||
const transceiving = ref(false)
|
||||
const error = ref<string | null>(null)
|
||||
|
||||
// Getters
|
||||
/**
|
||||
* Get count of providers in store
|
||||
*/
|
||||
const count = computed(() => Object.keys(_providers.value).length)
|
||||
|
||||
/**
|
||||
* Check if any providers are present in store
|
||||
*/
|
||||
const has = computed(() => count.value > 0)
|
||||
|
||||
/**
|
||||
* Get providers as an array
|
||||
* @returns Array of provider objects
|
||||
* Get all providers present in store
|
||||
*/
|
||||
const providers = computed(() => Object.values(_providers.value))
|
||||
|
||||
/**
|
||||
* Get a specific provider by identifier from cache
|
||||
* Get a specific provider from store, with optional retrieval
|
||||
*
|
||||
* @param identifier - Provider identifier
|
||||
* @param retrieve - Retrieve behavior: true = fetch if missing or refresh, false = cache only
|
||||
*
|
||||
* @returns Provider object or null
|
||||
*/
|
||||
function provider(identifier: string): ProviderObject | null {
|
||||
function provider(identifier: string, retrieve: boolean = false): ProviderObject | null {
|
||||
if (retrieve === true && !_providers.value[identifier]) {
|
||||
console.debug(`[Mail Manager][Store] - Force fetching provider "${identifier}"`)
|
||||
fetch(identifier)
|
||||
}
|
||||
|
||||
return _providers.value[identifier] || null
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
/**
|
||||
* Retrieve all or specific providers
|
||||
* Retrieve all or specific providers, optionally filtered by source selector
|
||||
*
|
||||
* @param request - list request parameters
|
||||
*
|
||||
* @returns Promise with provider object list keyed by provider identifier
|
||||
*/
|
||||
async function list(sources?: SourceSelector): Promise<Record<string, ProviderObject>> {
|
||||
transceiving.value = true
|
||||
error.value = null
|
||||
try {
|
||||
const response = await providerService.list({ sources })
|
||||
const providers = await providerService.list({ sources })
|
||||
|
||||
console.debug('[Mail Manager](Store) - Successfully retrieved', Object.keys(response).length, 'providers')
|
||||
// Merge retrieved providers into state
|
||||
_providers.value = { ..._providers.value, ...providers }
|
||||
|
||||
_providers.value = response
|
||||
return response
|
||||
} catch (err: any) {
|
||||
console.error('[Mail Manager](Store) - Failed to retrieve providers:', err)
|
||||
error.value = err.message
|
||||
throw err
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a specific provider
|
||||
*/
|
||||
async function fetch(identifier: string): Promise<ProviderObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
return await providerService.fetch({ identifier })
|
||||
console.debug('[Mail Manager][Store] - Successfully retrieved', Object.keys(providers).length, 'providers')
|
||||
return providers
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager](Store) - Failed to fetch provider:', error)
|
||||
console.error('[Mail Manager][Store] - Failed to retrieve providers:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
@@ -72,19 +73,53 @@ export const useProvidersStore = defineStore('mailProvidersStore', () => {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check which providers exist/are available
|
||||
* Retrieve a specific provider by identifier
|
||||
*
|
||||
* @param identifier - provider identifier
|
||||
*
|
||||
* @returns Promise with provider object
|
||||
*/
|
||||
async function fetch(identifier: string): Promise<ProviderObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const provider = await providerService.fetch({ identifier })
|
||||
|
||||
// Merge fetched provider into state
|
||||
_providers.value[provider.identifier] = provider
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully fetched provider:', provider.identifier)
|
||||
return provider
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to fetch provider:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve provider availability status for a given source selector
|
||||
*
|
||||
* @param sources - source selector to check availability for
|
||||
*
|
||||
* @returns Promise with provider availability status
|
||||
*/
|
||||
async function extant(sources: SourceSelector) {
|
||||
transceiving.value = true
|
||||
error.value = null
|
||||
try {
|
||||
const response = await providerService.extant({ sources })
|
||||
console.debug('[Mail Manager](Store) - Successfully checked', sources ? Object.keys(sources).length : 0, 'providers')
|
||||
|
||||
Object.entries(response).forEach(([providerId, providerStatus]) => {
|
||||
if (providerStatus === false) {
|
||||
delete _providers.value[providerId]
|
||||
}
|
||||
})
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully checked', sources ? Object.keys(sources).length : 0, 'providers')
|
||||
return response
|
||||
} catch (err: any) {
|
||||
console.error('[Mail Manager](Store) - Failed to check providers:', err)
|
||||
error.value = err.message
|
||||
throw err
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to check providers:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
@@ -94,7 +129,6 @@ export const useProvidersStore = defineStore('mailProvidersStore', () => {
|
||||
return {
|
||||
// State
|
||||
transceiving: readonly(transceiving),
|
||||
error: readonly(error),
|
||||
// computed
|
||||
count,
|
||||
has,
|
||||
|
||||
+217
-79
@@ -10,27 +10,31 @@ import type {
|
||||
ServiceLocation,
|
||||
SourceSelector,
|
||||
ServiceIdentity,
|
||||
ServiceInterface,
|
||||
} from '../types'
|
||||
|
||||
export const useServicesStore = defineStore('mailServicesStore', () => {
|
||||
// State
|
||||
const _services = ref<Record<string, ServiceObject>>({})
|
||||
const transceiving = ref(false)
|
||||
const lastTestResult = ref<any>(null)
|
||||
|
||||
// Getters
|
||||
/**
|
||||
* Get count of services in store
|
||||
*/
|
||||
const count = computed(() => Object.keys(_services.value).length)
|
||||
|
||||
/**
|
||||
* Check if any services are present in store
|
||||
*/
|
||||
const has = computed(() => count.value > 0)
|
||||
|
||||
/**
|
||||
* Get services as an array
|
||||
* @returns Array of service objects
|
||||
* Get all services present in store
|
||||
*/
|
||||
const services = computed(() => Object.values(_services.value))
|
||||
|
||||
/**
|
||||
* Get services grouped by provider
|
||||
* @returns Services grouped by provider ID
|
||||
* Get all services present in store grouped by provider
|
||||
*/
|
||||
const servicesByProvider = computed(() => {
|
||||
const groups: Record<string, ServiceObject[]> = {}
|
||||
@@ -45,9 +49,60 @@ export const useServicesStore = defineStore('mailServicesStore', () => {
|
||||
return groups
|
||||
})
|
||||
|
||||
// Actions
|
||||
/**
|
||||
* Retrieve for all or specific services
|
||||
* Get a specific service from store, with optional retrieval
|
||||
*
|
||||
* @param provider - provider identifier
|
||||
* @param identifier - service identifier
|
||||
* @param retrieve - Retrieve behavior: true = fetch if missing or refresh, false = cache only
|
||||
*
|
||||
* @returns Service object or null
|
||||
*/
|
||||
function service(provider: string, identifier: string | number, retrieve: boolean = false): ServiceObject | null {
|
||||
const key = identifierKey(provider, identifier)
|
||||
if (retrieve === true && !_services.value[key]) {
|
||||
console.debug(`[Mail Manager][Store] - Force fetching service "${key}"`)
|
||||
fetch(provider, identifier)
|
||||
}
|
||||
|
||||
return _services.value[key] || null
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a service from store matching a given address, with optional retrieval
|
||||
*
|
||||
* @param address - email address to match against primary and secondary addresses
|
||||
* @param retrieve - Retrieve behavior: true = fetch if missing, false = cache only
|
||||
*
|
||||
* @returns Service object or null
|
||||
*/
|
||||
function serviceForAddress(address: string, retrieve: boolean = false): ServiceObject | null {
|
||||
const service = Object.values(_services.value).find(s => s.primaryAddress === address || s.secondaryAddresses?.includes(address))
|
||||
|
||||
if (retrieve === true && !service) {
|
||||
console.debug(`[Mail Manager][Store] - No service found for address "${address}", discovery may be needed`)
|
||||
|
||||
// TODO: Implement retrieving service by address
|
||||
}
|
||||
|
||||
return service || null
|
||||
}
|
||||
|
||||
/**
|
||||
* Unique key for a service
|
||||
*/
|
||||
function identifierKey(provider: string, identifier: string | number | null): string {
|
||||
return `${provider}:${identifier ?? ''}`
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
/**
|
||||
* Retrieve all or specific services, optionally filtered by source selector
|
||||
*
|
||||
* @param sources - optional source selector
|
||||
*
|
||||
* @returns Promise with service object list keyed by provider and service identifier
|
||||
*/
|
||||
async function list(sources?: SourceSelector): Promise<Record<string, ServiceObject>> {
|
||||
transceiving.value = true
|
||||
@@ -55,20 +110,21 @@ export const useServicesStore = defineStore('mailServicesStore', () => {
|
||||
const response = await serviceService.list({ sources })
|
||||
|
||||
// Flatten nested structure: provider-id: { service-id: object } -> "provider-id:service-id": object
|
||||
const flattened: Record<string, ServiceObject> = {}
|
||||
const services: Record<string, ServiceObject> = {}
|
||||
Object.entries(response).forEach(([_providerId, providerServices]) => {
|
||||
Object.entries(providerServices).forEach(([_serviceId, serviceObj]) => {
|
||||
const key = `${serviceObj.provider}:${serviceObj.identifier}`
|
||||
flattened[key] = serviceObj
|
||||
const key = identifierKey(serviceObj.provider, serviceObj.identifier)
|
||||
services[key] = serviceObj
|
||||
})
|
||||
})
|
||||
|
||||
console.debug('[Mail Manager](Store) - Successfully retrieved', Object.keys(flattened).length, 'services')
|
||||
// Merge retrieved services into state
|
||||
_services.value = { ..._services.value, ...services }
|
||||
|
||||
_services.value = flattened
|
||||
return flattened
|
||||
console.debug('[Mail Manager][Store] - Successfully retrieved', Object.keys(services).length, 'services')
|
||||
return services
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager](Store) - Failed to retrieve services:', error)
|
||||
console.error('[Mail Manager][Store] - Failed to retrieve services:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
@@ -76,14 +132,26 @@ export const useServicesStore = defineStore('mailServicesStore', () => {
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a specific service
|
||||
* Retrieve a specific service by provider and identifier
|
||||
*
|
||||
* @param provider - provider identifier
|
||||
* @param identifier - service identifier
|
||||
*
|
||||
* @returns Promise with service object
|
||||
*/
|
||||
async function fetch(provider: string, identifier: string | number): Promise<ServiceObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
return await serviceService.fetch({ provider, identifier })
|
||||
const service = await serviceService.fetch({ provider, identifier })
|
||||
|
||||
// Merge fetched service into state
|
||||
const key = identifierKey(service.provider, service.identifier)
|
||||
_services.value[key] = service
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully fetched service:', key)
|
||||
return service
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager](Store) - Failed to fetch service:', error)
|
||||
console.error('[Mail Manager][Store] - Failed to fetch service:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
@@ -91,9 +159,117 @@ export const useServicesStore = defineStore('mailServicesStore', () => {
|
||||
}
|
||||
|
||||
/**
|
||||
* Discover service configuration
|
||||
* Retrieve service availability status for a given source selector
|
||||
*
|
||||
* @returns Array of discovered services sorted by provider
|
||||
* @param sources - source selector to check availability for
|
||||
*
|
||||
* @returns Promise with service availability status
|
||||
*/
|
||||
async function extant(sources: SourceSelector) {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await serviceService.extant({ sources })
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully checked', sources ? Object.keys(sources).length : 0, 'services')
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to check services:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new service with given provider and data
|
||||
*
|
||||
* @param provider - provider identifier for the new service
|
||||
* @param data - partial service data for creation
|
||||
*
|
||||
* @returns Promise with created service object
|
||||
*/
|
||||
async function create(provider: string, data: Partial<ServiceInterface>): Promise<ServiceObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const service = await serviceService.create({ provider, data })
|
||||
|
||||
// Merge created service into state
|
||||
const key = identifierKey(service.provider, service.identifier)
|
||||
_services.value[key] = service
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully created service:', key)
|
||||
return service
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to create service:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing service with given provider, identifier, and data
|
||||
*
|
||||
* @param provider - provider identifier for the service to update
|
||||
* @param identifier - service identifier for the service to update
|
||||
* @param data - partial service data for update
|
||||
*
|
||||
* @returns Promise with updated service object
|
||||
*/
|
||||
async function update(provider: string, identifier: string | number, data: Partial<ServiceInterface>): Promise<ServiceObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const service = await serviceService.update({ provider, identifier, data })
|
||||
|
||||
// Merge updated service into state
|
||||
const key = identifierKey(service.provider, service.identifier)
|
||||
_services.value[key] = service
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully updated service:', key)
|
||||
return service
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to update service:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a service by provider and identifier
|
||||
*
|
||||
* @param provider - provider identifier for the service to delete
|
||||
* @param identifier - service identifier for the service to delete
|
||||
*
|
||||
* @returns Promise with deletion result
|
||||
*/
|
||||
async function remove(provider: string, identifier: string | number): Promise<any> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
await serviceService.delete({ provider, identifier })
|
||||
|
||||
// Remove deleted service from state
|
||||
const key = identifierKey(provider, identifier)
|
||||
delete _services.value[key]
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully deleted service:', key)
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to delete service:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Discover services based on provided parameters
|
||||
*
|
||||
* @param identity - optional service identity for discovery
|
||||
* @param secret - optional secret for discovery
|
||||
* @param location - optional location for discovery
|
||||
* @param provider - optional provider identifier for discovery
|
||||
*
|
||||
* @returns Promise with list of discovered service objects
|
||||
*/
|
||||
async function discover(
|
||||
identity: string,
|
||||
@@ -105,16 +281,27 @@ export const useServicesStore = defineStore('mailServicesStore', () => {
|
||||
|
||||
try {
|
||||
const services = await serviceService.discover({identity, secret, location, provider})
|
||||
console.debug('[Mail Manager](Store) - Successfully discovered', services.length, 'services')
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully discovered', services.length, 'services')
|
||||
return services
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager](Store) - Failed to discover service:', error)
|
||||
console.error('[Mail Manager][Store] - Failed to discover service:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test service connectivity and configuration
|
||||
*
|
||||
* @param provider - provider identifier for the service to test
|
||||
* @param identifier - optional service identifier for testing existing service
|
||||
* @param location - optional service location for testing new configuration
|
||||
* @param identity - optional service identity for testing new configuration
|
||||
*
|
||||
* @return Promise with test results
|
||||
*/
|
||||
async function test(
|
||||
provider: string,
|
||||
identifier?: string | number | null,
|
||||
@@ -124,62 +311,11 @@ export const useServicesStore = defineStore('mailServicesStore', () => {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await serviceService.test({ provider, identifier, location, identity })
|
||||
lastTestResult.value = response
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully tested service:', provider, identifier || location)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager](Store) - Failed to test service:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function create(provider: string, data: any) {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const serviceObj = await serviceService.create({ provider, data })
|
||||
|
||||
// Add to store with composite key
|
||||
const key = `${serviceObj.provider}:${serviceObj.identifier}`
|
||||
_services.value[key] = serviceObj
|
||||
|
||||
return serviceObj
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager](Store) - Failed to create service:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function update(provider: string, identifier: string | number, data: any) {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const serviceObj = await serviceService.update({ provider, identifier, data })
|
||||
|
||||
// Update in store with composite key
|
||||
const key = `${serviceObj.provider}:${serviceObj.identifier}`
|
||||
_services.value[key] = serviceObj
|
||||
|
||||
return serviceObj
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager](Store) - Failed to update service:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(provider: string, identifier: string | number) {
|
||||
transceiving.value = true
|
||||
try {
|
||||
await serviceService.delete({ provider, identifier })
|
||||
|
||||
// Remove from store using composite key
|
||||
const key = `${provider}:${identifier}`
|
||||
delete _services.value[key]
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager](Store) - Failed to delete service:', error)
|
||||
console.error('[Mail Manager][Store] - Failed to test service:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
@@ -190,7 +326,6 @@ export const useServicesStore = defineStore('mailServicesStore', () => {
|
||||
return {
|
||||
// State (readonly)
|
||||
transceiving: readonly(transceiving),
|
||||
lastTestResult: readonly(lastTestResult),
|
||||
// Getters
|
||||
count,
|
||||
has,
|
||||
@@ -198,12 +333,15 @@ export const useServicesStore = defineStore('mailServicesStore', () => {
|
||||
servicesByProvider,
|
||||
|
||||
// Actions
|
||||
service,
|
||||
serviceForAddress,
|
||||
list,
|
||||
fetch,
|
||||
discover,
|
||||
test,
|
||||
extant,
|
||||
create,
|
||||
update,
|
||||
delete: remove,
|
||||
discover,
|
||||
test,
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user