Initial commit
This commit is contained in:
191
src/models/collection.ts
Normal file
191
src/models/collection.ts
Normal file
@@ -0,0 +1,191 @@
|
||||
/**
|
||||
* Class model for Collection Interface
|
||||
*/
|
||||
|
||||
import type { CollectionInterface, CollectionPropertiesInterface } from "@/types/collection";
|
||||
|
||||
export class CollectionObject implements CollectionInterface {
|
||||
|
||||
_data!: CollectionInterface;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
provider: '',
|
||||
service: '',
|
||||
collection: null,
|
||||
identifier: '',
|
||||
signature: null,
|
||||
created: null,
|
||||
modified: null,
|
||||
properties: {
|
||||
'@type': 'mail.collection',
|
||||
version: 1,
|
||||
total: 0,
|
||||
unread: 0,
|
||||
label: '',
|
||||
role: null,
|
||||
rank: 0,
|
||||
subscribed: true,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
fromJson(data: CollectionInterface): CollectionObject {
|
||||
this._data = data;
|
||||
if (data.properties) {
|
||||
this._data.properties = new CollectionPropertiesObject().fromJson(data.properties as CollectionPropertiesInterface);
|
||||
} else {
|
||||
this._data.properties = new CollectionPropertiesObject();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): CollectionInterface {
|
||||
const json = { ...this._data };
|
||||
if (this._data.properties instanceof CollectionPropertiesObject) {
|
||||
json.properties = this._data.properties.toJson();
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
clone(): CollectionObject {
|
||||
const cloned = new CollectionObject();
|
||||
cloned._data = { ...this._data };
|
||||
cloned._data.properties = this.properties.clone();
|
||||
return cloned;
|
||||
}
|
||||
|
||||
/** Immutable Properties */
|
||||
|
||||
get provider(): string {
|
||||
return this._data.provider;
|
||||
}
|
||||
|
||||
get service(): string | number {
|
||||
return this._data.service;
|
||||
}
|
||||
|
||||
get collection(): string | number | null {
|
||||
return this._data.collection;
|
||||
}
|
||||
|
||||
get identifier(): string | number {
|
||||
return this._data.identifier;
|
||||
}
|
||||
|
||||
get signature(): string | null | undefined {
|
||||
return this._data.signature;
|
||||
}
|
||||
|
||||
get created(): string | null | undefined {
|
||||
return this._data.created;
|
||||
}
|
||||
|
||||
get modified(): string | null | undefined {
|
||||
return this._data.modified;
|
||||
}
|
||||
|
||||
get properties(): CollectionPropertiesObject {
|
||||
if (this._data.properties instanceof CollectionPropertiesObject) {
|
||||
return this._data.properties;
|
||||
}
|
||||
|
||||
if (this._data.properties) {
|
||||
const hydrated = new CollectionPropertiesObject().fromJson(this._data.properties as CollectionPropertiesInterface);
|
||||
this._data.properties = hydrated;
|
||||
return hydrated;
|
||||
}
|
||||
|
||||
return new CollectionPropertiesObject();
|
||||
}
|
||||
|
||||
set properties(value: CollectionPropertiesObject) {
|
||||
if (value instanceof CollectionPropertiesObject) {
|
||||
this._data.properties = value as any;
|
||||
} else {
|
||||
this._data.properties = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class CollectionPropertiesObject implements CollectionPropertiesInterface {
|
||||
|
||||
_data!: CollectionPropertiesInterface;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'mail.collection',
|
||||
version: 1,
|
||||
total: 0,
|
||||
unread: 0,
|
||||
label: '',
|
||||
role: null,
|
||||
rank: 0,
|
||||
subscribed: true,
|
||||
};
|
||||
}
|
||||
|
||||
fromJson(data: CollectionPropertiesInterface): CollectionPropertiesObject {
|
||||
this._data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): CollectionPropertiesInterface {
|
||||
return this._data;
|
||||
}
|
||||
|
||||
clone(): CollectionPropertiesObject {
|
||||
const cloned = new CollectionPropertiesObject();
|
||||
cloned._data = { ...this._data };
|
||||
return cloned;
|
||||
}
|
||||
|
||||
/** Immutable Properties */
|
||||
|
||||
get '@type'(): string {
|
||||
return this._data['@type'];
|
||||
}
|
||||
|
||||
get version(): number {
|
||||
return this._data.version;
|
||||
}
|
||||
|
||||
get role(): string | null | undefined {
|
||||
return this._data.role;
|
||||
}
|
||||
|
||||
get total(): number | undefined {
|
||||
return this._data.total;
|
||||
}
|
||||
|
||||
get unread(): number | undefined {
|
||||
return this._data.unread;
|
||||
}
|
||||
|
||||
/** Mutable Properties */
|
||||
|
||||
get label(): string {
|
||||
return this._data.label || '';
|
||||
}
|
||||
|
||||
set label(value: string) {
|
||||
this._data.label = value;
|
||||
}
|
||||
|
||||
get rank(): number | undefined {
|
||||
return this._data.rank;
|
||||
}
|
||||
|
||||
set rank(value: number) {
|
||||
this._data.rank = value;
|
||||
}
|
||||
|
||||
get subscribed(): boolean | undefined {
|
||||
return this._data.subscribed;
|
||||
}
|
||||
|
||||
set subscribed(value: boolean) {
|
||||
this._data.subscribed = value;
|
||||
}
|
||||
|
||||
}
|
||||
107
src/models/entity.ts
Normal file
107
src/models/entity.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
* Class model for Message/Entity Interface
|
||||
*/
|
||||
|
||||
import type { EntityInterface } from "@/types/entity";
|
||||
import type { MessageInterface, MessagePartInterface } from "@/types/message";
|
||||
import { MessageObject } from "./message";
|
||||
|
||||
export class EntityObject {
|
||||
|
||||
_data!: EntityInterface<MessageInterface>;
|
||||
_message!: MessageObject;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'mail.entity',
|
||||
provider: '',
|
||||
service: '',
|
||||
collection: '',
|
||||
identifier: '',
|
||||
signature: null,
|
||||
created: null,
|
||||
modified: null,
|
||||
properties: {
|
||||
'@type': 'mail.message',
|
||||
version: 1,
|
||||
urid: '',
|
||||
size: 0,
|
||||
receivedDate: undefined,
|
||||
date: undefined,
|
||||
subject: '',
|
||||
snippet: '',
|
||||
from: undefined,
|
||||
to: [],
|
||||
cc: [],
|
||||
bcc: [],
|
||||
replyTo: [],
|
||||
flags: {},
|
||||
body: undefined,
|
||||
attachments: [],
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fromJson(data: EntityInterface<MessageInterface>): EntityObject {
|
||||
this._data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): EntityInterface<MessageInterface> {
|
||||
return this._data;
|
||||
}
|
||||
|
||||
clone(): EntityObject {
|
||||
const cloned = new EntityObject();
|
||||
cloned._data = {
|
||||
...this._data,
|
||||
properties: { ...this._data.properties }
|
||||
};
|
||||
return cloned;
|
||||
}
|
||||
|
||||
/** Metadata Properties */
|
||||
|
||||
get provider(): string {
|
||||
return this._data.provider;
|
||||
}
|
||||
|
||||
get service(): string {
|
||||
return this._data.service;
|
||||
}
|
||||
|
||||
get collection(): string|number {
|
||||
return this._data.collection;
|
||||
}
|
||||
|
||||
get identifier(): string|number {
|
||||
return this._data.identifier;
|
||||
}
|
||||
|
||||
get signature(): string | null {
|
||||
return this._data.signature;
|
||||
}
|
||||
|
||||
get created(): string | null {
|
||||
return this._data.created;
|
||||
}
|
||||
|
||||
get modified(): string | null {
|
||||
return this._data.modified;
|
||||
}
|
||||
|
||||
/** Message Object Properties */
|
||||
|
||||
get properties(): MessageObject {
|
||||
if (!this._message) {
|
||||
this._message = new MessageObject(this._data.properties);
|
||||
}
|
||||
return this._message;
|
||||
}
|
||||
|
||||
// Alias for backward compatibility
|
||||
get object(): MessageObject {
|
||||
return this.properties;
|
||||
}
|
||||
|
||||
}
|
||||
196
src/models/identity.ts
Normal file
196
src/models/identity.ts
Normal file
@@ -0,0 +1,196 @@
|
||||
/**
|
||||
* Identity implementation classes for Mail Manager services
|
||||
*/
|
||||
|
||||
import type {
|
||||
ServiceIdentity,
|
||||
ServiceIdentityNone,
|
||||
ServiceIdentityBasic,
|
||||
ServiceIdentityToken,
|
||||
ServiceIdentityOAuth,
|
||||
ServiceIdentityCertificate
|
||||
} from '@/types/service';
|
||||
|
||||
/**
|
||||
* Base Identity class
|
||||
*/
|
||||
export abstract class Identity {
|
||||
abstract toJson(): ServiceIdentity;
|
||||
|
||||
static fromJson(data: ServiceIdentity): Identity {
|
||||
switch (data.type) {
|
||||
case 'NA':
|
||||
return IdentityNone.fromJson(data);
|
||||
case 'BA':
|
||||
return IdentityBasic.fromJson(data);
|
||||
case 'TA':
|
||||
return IdentityToken.fromJson(data);
|
||||
case 'OA':
|
||||
return IdentityOAuth.fromJson(data);
|
||||
case 'CC':
|
||||
return IdentityCertificate.fromJson(data);
|
||||
default:
|
||||
throw new Error(`Unknown identity type: ${(data as any).type}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* No authentication
|
||||
*/
|
||||
export class IdentityNone extends Identity {
|
||||
readonly type = 'NA' as const;
|
||||
|
||||
static fromJson(_data: ServiceIdentityNone): IdentityNone {
|
||||
return new IdentityNone();
|
||||
}
|
||||
|
||||
toJson(): ServiceIdentityNone {
|
||||
return {
|
||||
type: this.type
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic authentication (username/password)
|
||||
*/
|
||||
export class IdentityBasic extends Identity {
|
||||
readonly type = 'BA' as const;
|
||||
identity: string;
|
||||
secret: string;
|
||||
|
||||
constructor(identity: string = '', secret: string = '') {
|
||||
super();
|
||||
this.identity = identity;
|
||||
this.secret = secret;
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceIdentityBasic): IdentityBasic {
|
||||
return new IdentityBasic(data.identity, data.secret);
|
||||
}
|
||||
|
||||
toJson(): ServiceIdentityBasic {
|
||||
return {
|
||||
type: this.type,
|
||||
identity: this.identity,
|
||||
secret: this.secret
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Token authentication (API key, static token)
|
||||
*/
|
||||
export class IdentityToken extends Identity {
|
||||
readonly type = 'TA' as const;
|
||||
token: string;
|
||||
|
||||
constructor(token: string = '') {
|
||||
super();
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceIdentityToken): IdentityToken {
|
||||
return new IdentityToken(data.token);
|
||||
}
|
||||
|
||||
toJson(): ServiceIdentityToken {
|
||||
return {
|
||||
type: this.type,
|
||||
token: this.token
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* OAuth authentication
|
||||
*/
|
||||
export class IdentityOAuth extends Identity {
|
||||
readonly type = 'OA' as const;
|
||||
accessToken: string;
|
||||
accessScope?: string[];
|
||||
accessExpiry?: number;
|
||||
refreshToken?: string;
|
||||
refreshLocation?: string;
|
||||
|
||||
constructor(
|
||||
accessToken: string = '',
|
||||
accessScope?: string[],
|
||||
accessExpiry?: number,
|
||||
refreshToken?: string,
|
||||
refreshLocation?: string
|
||||
) {
|
||||
super();
|
||||
this.accessToken = accessToken;
|
||||
this.accessScope = accessScope;
|
||||
this.accessExpiry = accessExpiry;
|
||||
this.refreshToken = refreshToken;
|
||||
this.refreshLocation = refreshLocation;
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceIdentityOAuth): IdentityOAuth {
|
||||
return new IdentityOAuth(
|
||||
data.accessToken,
|
||||
data.accessScope,
|
||||
data.accessExpiry,
|
||||
data.refreshToken,
|
||||
data.refreshLocation
|
||||
);
|
||||
}
|
||||
|
||||
toJson(): ServiceIdentityOAuth {
|
||||
return {
|
||||
type: this.type,
|
||||
accessToken: this.accessToken,
|
||||
...(this.accessScope && { accessScope: this.accessScope }),
|
||||
...(this.accessExpiry && { accessExpiry: this.accessExpiry }),
|
||||
...(this.refreshToken && { refreshToken: this.refreshToken }),
|
||||
...(this.refreshLocation && { refreshLocation: this.refreshLocation })
|
||||
};
|
||||
}
|
||||
|
||||
isExpired(): boolean {
|
||||
if (!this.accessExpiry) return false;
|
||||
return Date.now() / 1000 >= this.accessExpiry;
|
||||
}
|
||||
|
||||
expiresIn(): number {
|
||||
if (!this.accessExpiry) return Infinity;
|
||||
return Math.max(0, this.accessExpiry - Date.now() / 1000);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Client certificate authentication (mTLS)
|
||||
*/
|
||||
export class IdentityCertificate extends Identity {
|
||||
readonly type = 'CC' as const;
|
||||
certificate: string;
|
||||
privateKey: string;
|
||||
passphrase?: string;
|
||||
|
||||
constructor(certificate: string = '', privateKey: string = '', passphrase?: string) {
|
||||
super();
|
||||
this.certificate = certificate;
|
||||
this.privateKey = privateKey;
|
||||
this.passphrase = passphrase;
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceIdentityCertificate): IdentityCertificate {
|
||||
return new IdentityCertificate(
|
||||
data.certificate,
|
||||
data.privateKey,
|
||||
data.passphrase
|
||||
);
|
||||
}
|
||||
|
||||
toJson(): ServiceIdentityCertificate {
|
||||
return {
|
||||
type: this.type,
|
||||
certificate: this.certificate,
|
||||
privateKey: this.privateKey,
|
||||
...(this.passphrase && { passphrase: this.passphrase })
|
||||
};
|
||||
}
|
||||
}
|
||||
27
src/models/index.ts
Normal file
27
src/models/index.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Central export point for all Mail Manager models
|
||||
*/
|
||||
|
||||
export { CollectionObject } from './collection';
|
||||
export { EntityObject } from './entity';
|
||||
export { ProviderObject } from './provider';
|
||||
export { ServiceObject } from './service';
|
||||
|
||||
// Identity models
|
||||
export {
|
||||
Identity,
|
||||
IdentityNone,
|
||||
IdentityBasic,
|
||||
IdentityToken,
|
||||
IdentityOAuth,
|
||||
IdentityCertificate
|
||||
} from './identity';
|
||||
|
||||
// Location models
|
||||
export {
|
||||
Location,
|
||||
LocationUri,
|
||||
LocationSocketSole,
|
||||
LocationSocketSplit,
|
||||
LocationFile
|
||||
} from './location';
|
||||
240
src/models/location.ts
Normal file
240
src/models/location.ts
Normal file
@@ -0,0 +1,240 @@
|
||||
/**
|
||||
* Location implementation classes for Mail Manager services
|
||||
*/
|
||||
|
||||
import type {
|
||||
ServiceLocation,
|
||||
ServiceLocationUri,
|
||||
ServiceLocationSocketSole,
|
||||
ServiceLocationSocketSplit,
|
||||
ServiceLocationFile
|
||||
} from '@/types/service';
|
||||
|
||||
/**
|
||||
* Base Location class
|
||||
*/
|
||||
export abstract class Location {
|
||||
abstract toJson(): ServiceLocation;
|
||||
|
||||
static fromJson(data: ServiceLocation): Location {
|
||||
switch (data.type) {
|
||||
case 'URI':
|
||||
return LocationUri.fromJson(data);
|
||||
case 'SOCKET_SOLE':
|
||||
return LocationSocketSole.fromJson(data);
|
||||
case 'SOCKET_SPLIT':
|
||||
return LocationSocketSplit.fromJson(data);
|
||||
case 'FILE':
|
||||
return LocationFile.fromJson(data);
|
||||
default:
|
||||
throw new Error(`Unknown location type: ${(data as any).type}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* URI-based service location for API and web services
|
||||
* Used by: JMAP, Gmail API, etc.
|
||||
*/
|
||||
export class LocationUri extends Location {
|
||||
readonly type = 'URI' as const;
|
||||
scheme: string;
|
||||
host: string;
|
||||
port: number;
|
||||
path?: string;
|
||||
verifyPeer: boolean;
|
||||
verifyHost: boolean;
|
||||
|
||||
constructor(
|
||||
scheme: string = 'https',
|
||||
host: string = '',
|
||||
port: number = 443,
|
||||
path?: string,
|
||||
verifyPeer: boolean = true,
|
||||
verifyHost: boolean = true
|
||||
) {
|
||||
super();
|
||||
this.scheme = scheme;
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.path = path;
|
||||
this.verifyPeer = verifyPeer;
|
||||
this.verifyHost = verifyHost;
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceLocationUri): LocationUri {
|
||||
return new LocationUri(
|
||||
data.scheme,
|
||||
data.host,
|
||||
data.port,
|
||||
data.path,
|
||||
data.verifyPeer ?? true,
|
||||
data.verifyHost ?? true
|
||||
);
|
||||
}
|
||||
|
||||
toJson(): ServiceLocationUri {
|
||||
return {
|
||||
type: this.type,
|
||||
scheme: this.scheme,
|
||||
host: this.host,
|
||||
port: this.port,
|
||||
...(this.path && { path: this.path }),
|
||||
...(this.verifyPeer !== undefined && { verifyPeer: this.verifyPeer }),
|
||||
...(this.verifyHost !== undefined && { verifyHost: this.verifyHost })
|
||||
};
|
||||
}
|
||||
|
||||
getUrl(): string {
|
||||
const path = this.path || '';
|
||||
return `${this.scheme}://${this.host}:${this.port}${path}`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Single socket-based service location
|
||||
* Used by: services using a single host/port combination
|
||||
*/
|
||||
export class LocationSocketSole extends Location {
|
||||
readonly type = 'SOCKET_SOLE' as const;
|
||||
host: string;
|
||||
port: number;
|
||||
encryption: 'none' | 'ssl' | 'tls' | 'starttls';
|
||||
verifyPeer: boolean;
|
||||
verifyHost: boolean;
|
||||
|
||||
constructor(
|
||||
host: string = '',
|
||||
port: number = 993,
|
||||
encryption: 'none' | 'ssl' | 'tls' | 'starttls' = 'ssl',
|
||||
verifyPeer: boolean = true,
|
||||
verifyHost: boolean = true
|
||||
) {
|
||||
super();
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.encryption = encryption;
|
||||
this.verifyPeer = verifyPeer;
|
||||
this.verifyHost = verifyHost;
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceLocationSocketSole): LocationSocketSole {
|
||||
return new LocationSocketSole(
|
||||
data.host,
|
||||
data.port,
|
||||
data.encryption,
|
||||
data.verifyPeer ?? true,
|
||||
data.verifyHost ?? true
|
||||
);
|
||||
}
|
||||
|
||||
toJson(): ServiceLocationSocketSole {
|
||||
return {
|
||||
type: this.type,
|
||||
host: this.host,
|
||||
port: this.port,
|
||||
encryption: this.encryption,
|
||||
...(this.verifyPeer !== undefined && { verifyPeer: this.verifyPeer }),
|
||||
...(this.verifyHost !== undefined && { verifyHost: this.verifyHost })
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Split socket-based service location
|
||||
* Used by: traditional IMAP/SMTP configurations
|
||||
*/
|
||||
export class LocationSocketSplit extends Location {
|
||||
readonly type = 'SOCKET_SPLIT' as const;
|
||||
inboundHost: string;
|
||||
inboundPort: number;
|
||||
inboundEncryption: 'none' | 'ssl' | 'tls' | 'starttls';
|
||||
outboundHost: string;
|
||||
outboundPort: number;
|
||||
outboundEncryption: 'none' | 'ssl' | 'tls' | 'starttls';
|
||||
inboundVerifyPeer: boolean;
|
||||
inboundVerifyHost: boolean;
|
||||
outboundVerifyPeer: boolean;
|
||||
outboundVerifyHost: boolean;
|
||||
|
||||
constructor(
|
||||
inboundHost: string = '',
|
||||
inboundPort: number = 993,
|
||||
inboundEncryption: 'none' | 'ssl' | 'tls' | 'starttls' = 'ssl',
|
||||
outboundHost: string = '',
|
||||
outboundPort: number = 465,
|
||||
outboundEncryption: 'none' | 'ssl' | 'tls' | 'starttls' = 'ssl',
|
||||
inboundVerifyPeer: boolean = true,
|
||||
inboundVerifyHost: boolean = true,
|
||||
outboundVerifyPeer: boolean = true,
|
||||
outboundVerifyHost: boolean = true
|
||||
) {
|
||||
super();
|
||||
this.inboundHost = inboundHost;
|
||||
this.inboundPort = inboundPort;
|
||||
this.inboundEncryption = inboundEncryption;
|
||||
this.outboundHost = outboundHost;
|
||||
this.outboundPort = outboundPort;
|
||||
this.outboundEncryption = outboundEncryption;
|
||||
this.inboundVerifyPeer = inboundVerifyPeer;
|
||||
this.inboundVerifyHost = inboundVerifyHost;
|
||||
this.outboundVerifyPeer = outboundVerifyPeer;
|
||||
this.outboundVerifyHost = outboundVerifyHost;
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceLocationSocketSplit): LocationSocketSplit {
|
||||
return new LocationSocketSplit(
|
||||
data.inboundHost,
|
||||
data.inboundPort,
|
||||
data.inboundEncryption,
|
||||
data.outboundHost,
|
||||
data.outboundPort,
|
||||
data.outboundEncryption,
|
||||
data.inboundVerifyPeer ?? true,
|
||||
data.inboundVerifyHost ?? true,
|
||||
data.outboundVerifyPeer ?? true,
|
||||
data.outboundVerifyHost ?? true
|
||||
);
|
||||
}
|
||||
|
||||
toJson(): ServiceLocationSocketSplit {
|
||||
return {
|
||||
type: this.type,
|
||||
inboundHost: this.inboundHost,
|
||||
inboundPort: this.inboundPort,
|
||||
inboundEncryption: this.inboundEncryption,
|
||||
outboundHost: this.outboundHost,
|
||||
outboundPort: this.outboundPort,
|
||||
outboundEncryption: this.outboundEncryption,
|
||||
...(this.inboundVerifyPeer !== undefined && { inboundVerifyPeer: this.inboundVerifyPeer }),
|
||||
...(this.inboundVerifyHost !== undefined && { inboundVerifyHost: this.inboundVerifyHost }),
|
||||
...(this.outboundVerifyPeer !== undefined && { outboundVerifyPeer: this.outboundVerifyPeer }),
|
||||
...(this.outboundVerifyHost !== undefined && { outboundVerifyHost: this.outboundVerifyHost })
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* File-based service location
|
||||
* Used by: local file system providers
|
||||
*/
|
||||
export class LocationFile extends Location {
|
||||
readonly type = 'FILE' as const;
|
||||
path: string;
|
||||
|
||||
constructor(path: string = '') {
|
||||
super();
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceLocationFile): LocationFile {
|
||||
return new LocationFile(data.path);
|
||||
}
|
||||
|
||||
toJson(): ServiceLocationFile {
|
||||
return {
|
||||
type: this.type,
|
||||
path: this.path
|
||||
};
|
||||
}
|
||||
}
|
||||
376
src/models/message.ts
Normal file
376
src/models/message.ts
Normal file
@@ -0,0 +1,376 @@
|
||||
/**
|
||||
* Message and MessagePart model classes
|
||||
*/
|
||||
|
||||
import type { MessageInterface, MessagePartInterface } from "@/types/message";
|
||||
|
||||
/**
|
||||
* MessagePart class for working with message body parts
|
||||
*/
|
||||
export class MessagePartObject {
|
||||
|
||||
_data: MessagePartInterface;
|
||||
|
||||
constructor(data?: Partial<MessagePartInterface>) {
|
||||
this._data = {
|
||||
partId: data?.partId ?? null,
|
||||
blobId: data?.blobId ?? null,
|
||||
size: data?.size ?? null,
|
||||
name: data?.name ?? null,
|
||||
type: data?.type ?? undefined,
|
||||
charset: data?.charset ?? null,
|
||||
disposition: data?.disposition ?? null,
|
||||
cid: data?.cid ?? null,
|
||||
language: data?.language ?? null,
|
||||
location: data?.location ?? null,
|
||||
content: data?.content ?? undefined,
|
||||
subParts: data?.subParts ?? undefined,
|
||||
};
|
||||
}
|
||||
|
||||
fromJson(data: MessagePartInterface): MessagePartObject {
|
||||
this._data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): MessagePartInterface {
|
||||
return this._data;
|
||||
}
|
||||
|
||||
clone(): MessagePartObject {
|
||||
return new MessagePartObject(JSON.parse(JSON.stringify(this._data)));
|
||||
}
|
||||
|
||||
/** Properties */
|
||||
|
||||
get partId(): string | null | undefined {
|
||||
return this._data.partId;
|
||||
}
|
||||
|
||||
get blobId(): string | null | undefined {
|
||||
return this._data.blobId;
|
||||
}
|
||||
|
||||
get size(): number | null | undefined {
|
||||
return this._data.size;
|
||||
}
|
||||
|
||||
get name(): string | null | undefined {
|
||||
return this._data.name;
|
||||
}
|
||||
|
||||
get type(): string | undefined {
|
||||
return this._data.type;
|
||||
}
|
||||
|
||||
get charset(): string | null | undefined {
|
||||
return this._data.charset;
|
||||
}
|
||||
|
||||
get disposition(): string | null | undefined {
|
||||
return this._data.disposition;
|
||||
}
|
||||
|
||||
get cid(): string | null | undefined {
|
||||
return this._data.cid;
|
||||
}
|
||||
|
||||
get language(): string | null | undefined {
|
||||
return this._data.language;
|
||||
}
|
||||
|
||||
get location(): string | null | undefined {
|
||||
return this._data.location;
|
||||
}
|
||||
|
||||
get content(): string | undefined {
|
||||
return this._data.content;
|
||||
}
|
||||
|
||||
get subParts(): MessagePartInterface[] | undefined {
|
||||
return this._data.subParts;
|
||||
}
|
||||
|
||||
/** Helper methods */
|
||||
|
||||
hasContent(): boolean {
|
||||
return !!this._data.content;
|
||||
}
|
||||
|
||||
hasSubParts(): boolean {
|
||||
return !!this._data.subParts && this._data.subParts.length > 0;
|
||||
}
|
||||
|
||||
isMultipart(): boolean {
|
||||
return this._data.type?.startsWith('multipart/') ?? false;
|
||||
}
|
||||
|
||||
isText(): boolean {
|
||||
return this._data.type === 'text/plain';
|
||||
}
|
||||
|
||||
isHtml(): boolean {
|
||||
return this._data.type === 'text/html';
|
||||
}
|
||||
|
||||
isAttachment(): boolean {
|
||||
return this._data.disposition === 'attachment';
|
||||
}
|
||||
|
||||
isInline(): boolean {
|
||||
return this._data.disposition === 'inline';
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a part by partId (recursive search)
|
||||
*/
|
||||
findPartById(partId: string): MessagePartInterface | null {
|
||||
if (this._data.partId === partId) {
|
||||
return this._data;
|
||||
}
|
||||
|
||||
if (this._data.subParts) {
|
||||
for (const subPart of this._data.subParts) {
|
||||
const part = new MessagePartObject(subPart);
|
||||
const found = part.findPartById(partId);
|
||||
if (found) {
|
||||
return found;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all parts of a specific type (recursive search)
|
||||
*/
|
||||
findPartsByType(type: string): MessagePartInterface[] {
|
||||
const parts: MessagePartInterface[] = [];
|
||||
|
||||
if (this._data.type === type) {
|
||||
parts.push(this._data);
|
||||
}
|
||||
|
||||
if (this._data.subParts) {
|
||||
for (const subPart of this._data.subParts) {
|
||||
const part = new MessagePartObject(subPart);
|
||||
parts.push(...part.findPartsByType(type));
|
||||
}
|
||||
}
|
||||
|
||||
return parts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract text content from body structure
|
||||
*/
|
||||
extractTextContent(): string | null {
|
||||
if (this._data.type === 'text/plain' && this._data.content) {
|
||||
return this._data.content;
|
||||
}
|
||||
|
||||
if (this._data.subParts) {
|
||||
for (const subPart of this._data.subParts) {
|
||||
const part = new MessagePartObject(subPart);
|
||||
const content = part.extractTextContent();
|
||||
if (content) {
|
||||
return content;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract HTML content from body structure
|
||||
*/
|
||||
extractHtmlContent(): string | null {
|
||||
if (this._data.type === 'text/html' && this._data.content) {
|
||||
return this._data.content;
|
||||
}
|
||||
|
||||
if (this._data.subParts) {
|
||||
for (const subPart of this._data.subParts) {
|
||||
const part = new MessagePartObject(subPart);
|
||||
const content = part.extractHtmlContent();
|
||||
if (content) {
|
||||
return content;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Message class for working with message objects
|
||||
*/
|
||||
export class MessageObject {
|
||||
|
||||
_data: MessageInterface;
|
||||
_body: MessagePartObject | null = null;
|
||||
|
||||
constructor(data?: Partial<MessageInterface>) {
|
||||
this._data = {
|
||||
urid: data?.urid ?? undefined,
|
||||
size: data?.size ?? undefined,
|
||||
receivedDate: data?.receivedDate ?? undefined,
|
||||
date: data?.date ?? undefined,
|
||||
subject: data?.subject ?? undefined,
|
||||
snippet: data?.snippet ?? undefined,
|
||||
from: data?.from ?? undefined,
|
||||
to: data?.to ?? [],
|
||||
cc: data?.cc ?? [],
|
||||
bcc: data?.bcc ?? [],
|
||||
replyTo: data?.replyTo ?? [],
|
||||
flags: data?.flags ?? {},
|
||||
body: data?.body ?? undefined,
|
||||
attachments: data?.attachments ?? [],
|
||||
};
|
||||
}
|
||||
|
||||
fromJson(data: MessageInterface): MessageObject {
|
||||
this._data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): MessageInterface {
|
||||
return this._data;
|
||||
}
|
||||
|
||||
clone(): MessageObject {
|
||||
return new MessageObject(JSON.parse(JSON.stringify(this._data)));
|
||||
}
|
||||
|
||||
/** Properties */
|
||||
|
||||
get urid(): string | undefined {
|
||||
return this._data.urid;
|
||||
}
|
||||
|
||||
get size(): number | undefined {
|
||||
return this._data.size;
|
||||
}
|
||||
|
||||
get receivedDate(): string | undefined {
|
||||
return this._data.receivedDate;
|
||||
}
|
||||
|
||||
get date(): string | undefined {
|
||||
return this._data.date;
|
||||
}
|
||||
|
||||
get subject(): string | undefined {
|
||||
return this._data.subject;
|
||||
}
|
||||
|
||||
get snippet(): string | undefined {
|
||||
return this._data.snippet;
|
||||
}
|
||||
|
||||
get from(): { address: string; label?: string } | undefined {
|
||||
return this._data.from;
|
||||
}
|
||||
|
||||
get to(): Array<{ address: string; label?: string }> | undefined {
|
||||
return this._data.to;
|
||||
}
|
||||
|
||||
get cc(): Array<{ address: string; label?: string }> | undefined {
|
||||
return this._data.cc;
|
||||
}
|
||||
|
||||
get bcc(): Array<{ address: string; label?: string }> | undefined {
|
||||
return this._data.bcc;
|
||||
}
|
||||
|
||||
get replyTo(): Array<{ address: string; label?: string }> | undefined {
|
||||
return this._data.replyTo;
|
||||
}
|
||||
|
||||
get flags(): { read?: boolean; flagged?: boolean; answered?: boolean; draft?: boolean } | undefined {
|
||||
return this._data.flags;
|
||||
}
|
||||
|
||||
get body(): MessagePartInterface | undefined {
|
||||
return this._data.body;
|
||||
}
|
||||
|
||||
get attachments(): MessageInterface['attachments'] {
|
||||
return this._data.attachments;
|
||||
}
|
||||
|
||||
/** Helper methods */
|
||||
|
||||
get isRead(): boolean {
|
||||
return this._data.flags?.read ?? false;
|
||||
}
|
||||
|
||||
get isFlagged(): boolean {
|
||||
return this._data.flags?.flagged ?? false;
|
||||
}
|
||||
|
||||
get isAnswered(): boolean {
|
||||
return this._data.flags?.answered ?? false;
|
||||
}
|
||||
|
||||
get isDraft(): boolean {
|
||||
return this._data.flags?.draft ?? false;
|
||||
}
|
||||
|
||||
get hasAttachments(): boolean {
|
||||
return (this._data.attachments?.length ?? 0) > 0;
|
||||
}
|
||||
|
||||
hasRecipients(): boolean {
|
||||
return (this._data.to?.length ?? 0) > 0
|
||||
|| (this._data.cc?.length ?? 0) > 0
|
||||
|| (this._data.bcc?.length ?? 0) > 0;
|
||||
}
|
||||
|
||||
/** Body content helpers */
|
||||
|
||||
getBody(): MessagePartObject | null {
|
||||
if (!this._body && this._data.body) {
|
||||
this._body = new MessagePartObject(this._data.body);
|
||||
}
|
||||
return this._body;
|
||||
}
|
||||
|
||||
hasContent(): boolean {
|
||||
return !!this.getTextContent() || !!this.getHtmlContent();
|
||||
}
|
||||
|
||||
hasTextContent(): boolean {
|
||||
return !!this.getTextContent();
|
||||
}
|
||||
|
||||
getTextContent(): string | null {
|
||||
const bodyPart = this.getBody();
|
||||
return bodyPart ? bodyPart.extractTextContent() : null;
|
||||
}
|
||||
|
||||
hasHtmlContent(): boolean {
|
||||
return !!this.getHtmlContent();
|
||||
}
|
||||
|
||||
getHtmlContent(): string | null {
|
||||
const bodyPart = this.getBody();
|
||||
return bodyPart ? bodyPart.extractHtmlContent() : null;
|
||||
}
|
||||
|
||||
findPartById(partId: string): MessagePartInterface | null {
|
||||
const bodyPart = this.getBody();
|
||||
return bodyPart ? bodyPart.findPartById(partId) : null;
|
||||
}
|
||||
|
||||
findPartsByType(type: string): MessagePartInterface[] {
|
||||
const bodyPart = this.getBody();
|
||||
return bodyPart ? bodyPart.findPartsByType(type) : [];
|
||||
}
|
||||
|
||||
}
|
||||
62
src/models/provider.ts
Normal file
62
src/models/provider.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Class model for Provider Interface
|
||||
*/
|
||||
|
||||
import type {
|
||||
ProviderInterface,
|
||||
ProviderCapabilitiesInterface
|
||||
} from "@/types/provider";
|
||||
|
||||
export class ProviderObject implements ProviderInterface {
|
||||
|
||||
_data!: ProviderInterface;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'mail.provider',
|
||||
identifier: '',
|
||||
label: '',
|
||||
capabilities: {},
|
||||
};
|
||||
}
|
||||
|
||||
fromJson(data: ProviderInterface): ProviderObject {
|
||||
this._data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): ProviderInterface {
|
||||
return this._data;
|
||||
}
|
||||
|
||||
capable(capability: keyof ProviderCapabilitiesInterface): boolean {
|
||||
const value = this._data.capabilities?.[capability];
|
||||
return value !== undefined && value !== false;
|
||||
}
|
||||
|
||||
capability(capability: keyof ProviderCapabilitiesInterface): any | null {
|
||||
if (this._data.capabilities) {
|
||||
return this._data.capabilities[capability];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Immutable Properties */
|
||||
|
||||
get '@type'(): string {
|
||||
return this._data['@type'];
|
||||
}
|
||||
|
||||
get identifier(): string {
|
||||
return this._data.identifier;
|
||||
}
|
||||
|
||||
get label(): string {
|
||||
return this._data.label;
|
||||
}
|
||||
|
||||
get capabilities(): ProviderCapabilitiesInterface {
|
||||
return this._data.capabilities;
|
||||
}
|
||||
|
||||
}
|
||||
136
src/models/service.ts
Normal file
136
src/models/service.ts
Normal file
@@ -0,0 +1,136 @@
|
||||
/**
|
||||
* Class model for Service Interface
|
||||
*/
|
||||
|
||||
import type {
|
||||
ServiceInterface,
|
||||
ServiceCapabilitiesInterface,
|
||||
ServiceIdentity,
|
||||
ServiceLocation
|
||||
} from "@/types/service";
|
||||
import { Identity } from './identity';
|
||||
import { Location } from './location';
|
||||
|
||||
export class ServiceObject implements ServiceInterface {
|
||||
|
||||
_data!: ServiceInterface;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'mail:service',
|
||||
provider: '',
|
||||
identifier: null,
|
||||
label: null,
|
||||
enabled: false,
|
||||
capabilities: {}
|
||||
};
|
||||
}
|
||||
|
||||
fromJson(data: ServiceInterface): ServiceObject {
|
||||
this._data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): ServiceInterface {
|
||||
return this._data;
|
||||
}
|
||||
|
||||
capable(capability: keyof ServiceCapabilitiesInterface): boolean {
|
||||
const value = this._data.capabilities?.[capability];
|
||||
return value !== undefined && value !== false;
|
||||
}
|
||||
|
||||
capability(capability: keyof ServiceCapabilitiesInterface): any | null {
|
||||
if (this._data.capabilities) {
|
||||
return this._data.capabilities[capability];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Immutable Properties */
|
||||
|
||||
get '@type'(): string {
|
||||
return this._data['@type'];
|
||||
}
|
||||
|
||||
get provider(): string {
|
||||
return this._data.provider;
|
||||
}
|
||||
|
||||
get identifier(): string | number | null {
|
||||
return this._data.identifier;
|
||||
}
|
||||
|
||||
get capabilities(): ServiceCapabilitiesInterface | undefined {
|
||||
return this._data.capabilities;
|
||||
}
|
||||
|
||||
get primaryAddress(): string | null {
|
||||
return this._data.primaryAddress ?? null;
|
||||
}
|
||||
|
||||
get secondaryAddresses(): string[] {
|
||||
return this._data.secondaryAddresses ?? [];
|
||||
}
|
||||
|
||||
/** Mutable Properties */
|
||||
|
||||
get label(): string | null {
|
||||
return this._data.label;
|
||||
}
|
||||
|
||||
set label(value: string | null) {
|
||||
this._data.label = value;
|
||||
}
|
||||
|
||||
get enabled(): boolean {
|
||||
return this._data.enabled;
|
||||
}
|
||||
|
||||
set enabled(value: boolean) {
|
||||
this._data.enabled = value;
|
||||
}
|
||||
|
||||
get location(): ServiceLocation | null {
|
||||
return this._data.location ?? null;
|
||||
}
|
||||
|
||||
set location(value: ServiceLocation | null) {
|
||||
this._data.location = value;
|
||||
}
|
||||
|
||||
get identity(): ServiceIdentity | null {
|
||||
return this._data.identity ?? null;
|
||||
}
|
||||
|
||||
set identity(value: ServiceIdentity | null) {
|
||||
this._data.identity = value;
|
||||
}
|
||||
|
||||
get auxiliary(): Record<string, any> {
|
||||
return this._data.auxiliary ?? {};
|
||||
}
|
||||
|
||||
set auxiliary(value: Record<string, any>) {
|
||||
this._data.auxiliary = value;
|
||||
}
|
||||
|
||||
/** Helper Methods */
|
||||
|
||||
/**
|
||||
* Get identity as a class instance for easier manipulation
|
||||
*/
|
||||
getIdentity(): Identity | null {
|
||||
if (!this._data.identity) return null;
|
||||
return Identity.fromJson(this._data.identity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get location as a class instance for easier manipulation
|
||||
*/
|
||||
getLocation(): Location | null {
|
||||
if (!this._data.location) return null;
|
||||
return Location.fromJson(this._data.location);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user