chore: bunch of improvements
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -2,45 +2,48 @@
|
||||
* Class model for Collection Interface
|
||||
*/
|
||||
|
||||
import type { CollectionInterface, CollectionPropertiesInterface } from "@/types/collection";
|
||||
import type { CollectionInterface, CollectionModelInterface, CollectionPropertiesInterface, CollectionPropertiesModelInterface } from "@/types/collection";
|
||||
|
||||
export class CollectionObject implements CollectionInterface {
|
||||
export class CollectionObject implements CollectionModelInterface {
|
||||
|
||||
_data!: CollectionInterface;
|
||||
_data!: CollectionInterface<CollectionPropertiesInterface>;
|
||||
_properties!: CollectionPropertiesObject;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'mail:collection',
|
||||
version: 1,
|
||||
provider: '',
|
||||
service: '',
|
||||
collection: null,
|
||||
identifier: '',
|
||||
signature: null,
|
||||
created: null,
|
||||
modified: null,
|
||||
properties: new CollectionPropertiesObject(),
|
||||
properties: {'@type': 'mail:folder', label: ''},
|
||||
};
|
||||
}
|
||||
|
||||
fromJson(data: CollectionInterface): CollectionObject {
|
||||
this._data = data;
|
||||
if (data.properties) {
|
||||
this._data.properties = new CollectionPropertiesObject().fromJson(data.properties as CollectionPropertiesInterface);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): CollectionInterface {
|
||||
const json = { ...this._data };
|
||||
if (this._data.properties instanceof CollectionPropertiesObject) {
|
||||
json.properties = this._data.properties.toJson();
|
||||
const json = {
|
||||
...this._data
|
||||
};
|
||||
if (this._properties) {
|
||||
json.properties = this._properties.toJson();
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
clone(): CollectionObject {
|
||||
const cloned = new CollectionObject();
|
||||
cloned._data = { ...this._data };
|
||||
cloned._data.properties = this.properties.clone();
|
||||
cloned._data = {
|
||||
...this._data,
|
||||
};
|
||||
if (this._properties) {
|
||||
cloned._properties = this._properties.clone();
|
||||
}
|
||||
return cloned;
|
||||
}
|
||||
|
||||
@@ -75,36 +78,30 @@ export class CollectionObject implements CollectionInterface {
|
||||
}
|
||||
|
||||
get properties(): CollectionPropertiesObject {
|
||||
if (this._data.properties instanceof CollectionPropertiesObject) {
|
||||
return this._data.properties;
|
||||
if (this._properties) {
|
||||
return this._properties;
|
||||
}
|
||||
|
||||
if (this._data.properties) {
|
||||
const hydrated = new CollectionPropertiesObject().fromJson(this._data.properties as CollectionPropertiesInterface);
|
||||
this._data.properties = hydrated;
|
||||
return hydrated;
|
||||
else if (this._data.properties) {
|
||||
const properties = new CollectionPropertiesObject().fromJson(this._data.properties as CollectionPropertiesInterface);
|
||||
this._properties = properties;
|
||||
return properties;
|
||||
}
|
||||
|
||||
return new CollectionPropertiesObject();
|
||||
}
|
||||
|
||||
set properties(value: CollectionPropertiesObject) {
|
||||
if (value instanceof CollectionPropertiesObject) {
|
||||
this._data.properties = value as any;
|
||||
} else {
|
||||
this._data.properties = value;
|
||||
}
|
||||
this._properties = value;
|
||||
}
|
||||
}
|
||||
|
||||
export class CollectionPropertiesObject implements CollectionPropertiesInterface {
|
||||
export class CollectionPropertiesObject implements CollectionPropertiesModelInterface {
|
||||
|
||||
_data!: CollectionPropertiesInterface;
|
||||
private _data!: CollectionPropertiesInterface;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'mail:collection',
|
||||
version: 1,
|
||||
'@type': 'mail:folder',
|
||||
total: 0,
|
||||
unread: 0,
|
||||
role: null,
|
||||
@@ -131,14 +128,6 @@ export class CollectionPropertiesObject implements CollectionPropertiesInterface
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
@@ -2,18 +2,19 @@
|
||||
* Class model for Message/Entity Interface
|
||||
*/
|
||||
|
||||
import type { EntityInterface } from "@/types/entity";
|
||||
import type { MessageInterface, MessagePartInterface } from "@/types/message";
|
||||
import type { EntityInterface, EntityModelInterface } from "@/types/entity";
|
||||
import type { MessageInterface } from "@/types/message";
|
||||
import { MessageObject } from "./message";
|
||||
|
||||
export class EntityObject {
|
||||
export class EntityObject implements EntityModelInterface {
|
||||
|
||||
_data!: EntityInterface<MessageInterface>;
|
||||
_message!: MessageObject;
|
||||
private _data!: EntityInterface<MessageInterface>;
|
||||
private _properties!: MessageObject;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'mail.entity',
|
||||
'@type': 'mail:entity',
|
||||
version: 1,
|
||||
provider: '',
|
||||
service: '',
|
||||
collection: '',
|
||||
@@ -21,24 +22,7 @@ export class EntityObject {
|
||||
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: [],
|
||||
}
|
||||
properties: {'@type': 'mail:message'},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -53,10 +37,10 @@ export class EntityObject {
|
||||
|
||||
clone(): EntityObject {
|
||||
const cloned = new EntityObject();
|
||||
cloned._data = {
|
||||
...this._data,
|
||||
properties: { ...this._data.properties }
|
||||
cloned._data = {
|
||||
...this._data
|
||||
};
|
||||
cloned._properties = this.properties.clone();
|
||||
return cloned;
|
||||
}
|
||||
|
||||
@@ -93,15 +77,10 @@ export class EntityObject {
|
||||
/** Message Object Properties */
|
||||
|
||||
get properties(): MessageObject {
|
||||
if (!this._message) {
|
||||
this._message = new MessageObject(this._data.properties);
|
||||
if (!this._properties) {
|
||||
this._properties = new MessageObject().fromJson(this._data.properties as MessageInterface);
|
||||
}
|
||||
return this._message;
|
||||
}
|
||||
|
||||
// Alias for backward compatibility
|
||||
get object(): MessageObject {
|
||||
return this.properties;
|
||||
return this._properties;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,7 +16,12 @@ import type {
|
||||
*/
|
||||
export abstract class Identity {
|
||||
abstract toJson(): ServiceIdentity;
|
||||
|
||||
abstract clone(): Identity;
|
||||
|
||||
toJSON(): ServiceIdentity {
|
||||
return this.toJson();
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceIdentity): Identity {
|
||||
switch (data.type) {
|
||||
case 'NA':
|
||||
@@ -39,16 +44,30 @@ export abstract class Identity {
|
||||
* No authentication
|
||||
*/
|
||||
export class IdentityNone extends Identity {
|
||||
readonly type = 'NA' as const;
|
||||
|
||||
private _data: ServiceIdentityNone;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this._data = {
|
||||
type: 'NA'
|
||||
};
|
||||
}
|
||||
|
||||
get type(): 'NA' {
|
||||
return this._data.type;
|
||||
}
|
||||
|
||||
static fromJson(_data: ServiceIdentityNone): IdentityNone {
|
||||
return new IdentityNone();
|
||||
}
|
||||
|
||||
toJson(): ServiceIdentityNone {
|
||||
return {
|
||||
type: this.type
|
||||
};
|
||||
return { ...this._data };
|
||||
}
|
||||
|
||||
clone(): IdentityNone {
|
||||
return IdentityNone.fromJson(this.toJson());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,14 +75,36 @@ export class IdentityNone extends Identity {
|
||||
* Basic authentication (username/password)
|
||||
*/
|
||||
export class IdentityBasic extends Identity {
|
||||
readonly type = 'BA' as const;
|
||||
identity: string;
|
||||
secret: string;
|
||||
|
||||
private _data: ServiceIdentityBasic;
|
||||
|
||||
constructor(identity: string = '', secret: string = '') {
|
||||
super();
|
||||
this.identity = identity;
|
||||
this.secret = secret;
|
||||
this._data = {
|
||||
type: 'BA',
|
||||
identity,
|
||||
secret
|
||||
};
|
||||
}
|
||||
|
||||
get type(): 'BA' {
|
||||
return this._data.type;
|
||||
}
|
||||
|
||||
get identity(): string {
|
||||
return this._data.identity;
|
||||
}
|
||||
|
||||
set identity(value: string) {
|
||||
this._data.identity = value;
|
||||
}
|
||||
|
||||
get secret(): string {
|
||||
return this._data.secret;
|
||||
}
|
||||
|
||||
set secret(value: string) {
|
||||
this._data.secret = value;
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceIdentityBasic): IdentityBasic {
|
||||
@@ -71,11 +112,11 @@ export class IdentityBasic extends Identity {
|
||||
}
|
||||
|
||||
toJson(): ServiceIdentityBasic {
|
||||
return {
|
||||
type: this.type,
|
||||
identity: this.identity,
|
||||
secret: this.secret
|
||||
};
|
||||
return { ...this._data };
|
||||
}
|
||||
|
||||
clone(): IdentityBasic {
|
||||
return IdentityBasic.fromJson(this.toJson());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,12 +124,27 @@ export class IdentityBasic extends Identity {
|
||||
* Token authentication (API key, static token)
|
||||
*/
|
||||
export class IdentityToken extends Identity {
|
||||
readonly type = 'TA' as const;
|
||||
token: string;
|
||||
|
||||
private _data: ServiceIdentityToken;
|
||||
|
||||
constructor(token: string = '') {
|
||||
super();
|
||||
this.token = token;
|
||||
this._data = {
|
||||
type: 'TA',
|
||||
token
|
||||
};
|
||||
}
|
||||
|
||||
get type(): 'TA' {
|
||||
return this._data.type;
|
||||
}
|
||||
|
||||
get token(): string {
|
||||
return this._data.token;
|
||||
}
|
||||
|
||||
set token(value: string) {
|
||||
this._data.token = value;
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceIdentityToken): IdentityToken {
|
||||
@@ -96,10 +152,11 @@ export class IdentityToken extends Identity {
|
||||
}
|
||||
|
||||
toJson(): ServiceIdentityToken {
|
||||
return {
|
||||
type: this.type,
|
||||
token: this.token
|
||||
};
|
||||
return { ...this._data };
|
||||
}
|
||||
|
||||
clone(): IdentityToken {
|
||||
return IdentityToken.fromJson(this.toJson());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,12 +164,8 @@ export class IdentityToken extends Identity {
|
||||
* OAuth authentication
|
||||
*/
|
||||
export class IdentityOAuth extends Identity {
|
||||
readonly type = 'OA' as const;
|
||||
accessToken: string;
|
||||
accessScope?: string[];
|
||||
accessExpiry?: number;
|
||||
refreshToken?: string;
|
||||
refreshLocation?: string;
|
||||
|
||||
private _data: ServiceIdentityOAuth;
|
||||
|
||||
constructor(
|
||||
accessToken: string = '',
|
||||
@@ -122,11 +175,58 @@ export class IdentityOAuth extends Identity {
|
||||
refreshLocation?: string
|
||||
) {
|
||||
super();
|
||||
this.accessToken = accessToken;
|
||||
this.accessScope = accessScope;
|
||||
this.accessExpiry = accessExpiry;
|
||||
this.refreshToken = refreshToken;
|
||||
this.refreshLocation = refreshLocation;
|
||||
this._data = {
|
||||
type: 'OA',
|
||||
accessToken,
|
||||
accessScope,
|
||||
accessExpiry,
|
||||
refreshToken,
|
||||
refreshLocation
|
||||
};
|
||||
}
|
||||
|
||||
get type(): 'OA' {
|
||||
return this._data.type;
|
||||
}
|
||||
|
||||
get accessToken(): string {
|
||||
return this._data.accessToken;
|
||||
}
|
||||
|
||||
set accessToken(value: string) {
|
||||
this._data.accessToken = value;
|
||||
}
|
||||
|
||||
get accessScope(): string[] | undefined {
|
||||
return this._data.accessScope;
|
||||
}
|
||||
|
||||
set accessScope(value: string[] | undefined) {
|
||||
this._data.accessScope = value;
|
||||
}
|
||||
|
||||
get accessExpiry(): number | undefined {
|
||||
return this._data.accessExpiry;
|
||||
}
|
||||
|
||||
set accessExpiry(value: number | undefined) {
|
||||
this._data.accessExpiry = value;
|
||||
}
|
||||
|
||||
get refreshToken(): string | undefined {
|
||||
return this._data.refreshToken;
|
||||
}
|
||||
|
||||
set refreshToken(value: string | undefined) {
|
||||
this._data.refreshToken = value;
|
||||
}
|
||||
|
||||
get refreshLocation(): string | undefined {
|
||||
return this._data.refreshLocation;
|
||||
}
|
||||
|
||||
set refreshLocation(value: string | undefined) {
|
||||
this._data.refreshLocation = value;
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceIdentityOAuth): IdentityOAuth {
|
||||
@@ -143,13 +243,17 @@ export class IdentityOAuth extends Identity {
|
||||
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 })
|
||||
...(this.accessScope !== undefined && { accessScope: [...this.accessScope] }),
|
||||
...(this.accessExpiry !== undefined && { accessExpiry: this.accessExpiry }),
|
||||
...(this.refreshToken !== undefined && { refreshToken: this.refreshToken }),
|
||||
...(this.refreshLocation !== undefined && { refreshLocation: this.refreshLocation })
|
||||
};
|
||||
}
|
||||
|
||||
clone(): IdentityOAuth {
|
||||
return IdentityOAuth.fromJson(this.toJson());
|
||||
}
|
||||
|
||||
isExpired(): boolean {
|
||||
if (!this.accessExpiry) return false;
|
||||
return Date.now() / 1000 >= this.accessExpiry;
|
||||
@@ -165,16 +269,44 @@ export class IdentityOAuth extends Identity {
|
||||
* Client certificate authentication (mTLS)
|
||||
*/
|
||||
export class IdentityCertificate extends Identity {
|
||||
readonly type = 'CC' as const;
|
||||
certificate: string;
|
||||
privateKey: string;
|
||||
passphrase?: string;
|
||||
private _data: ServiceIdentityCertificate;
|
||||
|
||||
constructor(certificate: string = '', privateKey: string = '', passphrase?: string) {
|
||||
super();
|
||||
this.certificate = certificate;
|
||||
this.privateKey = privateKey;
|
||||
this.passphrase = passphrase;
|
||||
this._data = {
|
||||
type: 'CC',
|
||||
certificate,
|
||||
privateKey,
|
||||
passphrase
|
||||
};
|
||||
}
|
||||
|
||||
get type(): 'CC' {
|
||||
return this._data.type;
|
||||
}
|
||||
|
||||
get certificate(): string {
|
||||
return this._data.certificate;
|
||||
}
|
||||
|
||||
set certificate(value: string) {
|
||||
this._data.certificate = value;
|
||||
}
|
||||
|
||||
get privateKey(): string {
|
||||
return this._data.privateKey;
|
||||
}
|
||||
|
||||
set privateKey(value: string) {
|
||||
this._data.privateKey = value;
|
||||
}
|
||||
|
||||
get passphrase(): string | undefined {
|
||||
return this._data.passphrase;
|
||||
}
|
||||
|
||||
set passphrase(value: string | undefined) {
|
||||
this._data.passphrase = value;
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceIdentityCertificate): IdentityCertificate {
|
||||
@@ -190,7 +322,11 @@ export class IdentityCertificate extends Identity {
|
||||
type: this.type,
|
||||
certificate: this.certificate,
|
||||
privateKey: this.privateKey,
|
||||
...(this.passphrase && { passphrase: this.passphrase })
|
||||
...(this.passphrase !== undefined && { passphrase: this.passphrase })
|
||||
};
|
||||
}
|
||||
|
||||
clone(): IdentityCertificate {
|
||||
return IdentityCertificate.fromJson(this.toJson());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import type {
|
||||
*/
|
||||
export abstract class Location {
|
||||
abstract toJson(): ServiceLocation;
|
||||
abstract clone(): Location;
|
||||
|
||||
static fromJson(data: ServiceLocation): Location {
|
||||
switch (data.type) {
|
||||
@@ -89,6 +90,17 @@ export class LocationUri extends Location {
|
||||
const path = this.path || '';
|
||||
return `${this.scheme}://${this.host}:${this.port}${path}`;
|
||||
}
|
||||
|
||||
clone(): LocationUri {
|
||||
return new LocationUri(
|
||||
this.scheme,
|
||||
this.host,
|
||||
this.port,
|
||||
this.path,
|
||||
this.verifyPeer,
|
||||
this.verifyHost
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,6 +150,16 @@ export class LocationSocketSole extends Location {
|
||||
...(this.verifyHost !== undefined && { verifyHost: this.verifyHost })
|
||||
};
|
||||
}
|
||||
|
||||
clone(): LocationSocketSole {
|
||||
return new LocationSocketSole(
|
||||
this.host,
|
||||
this.port,
|
||||
this.encryption,
|
||||
this.verifyPeer,
|
||||
this.verifyHost
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -212,6 +234,21 @@ export class LocationSocketSplit extends Location {
|
||||
...(this.outboundVerifyHost !== undefined && { outboundVerifyHost: this.outboundVerifyHost })
|
||||
};
|
||||
}
|
||||
|
||||
clone(): LocationSocketSplit {
|
||||
return new LocationSocketSplit(
|
||||
this.inboundHost,
|
||||
this.inboundPort,
|
||||
this.inboundEncryption,
|
||||
this.outboundHost,
|
||||
this.outboundPort,
|
||||
this.outboundEncryption,
|
||||
this.inboundVerifyPeer,
|
||||
this.inboundVerifyHost,
|
||||
this.outboundVerifyPeer,
|
||||
this.outboundVerifyHost
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -237,4 +274,8 @@ export class LocationFile extends Location {
|
||||
path: this.path
|
||||
};
|
||||
}
|
||||
|
||||
clone(): LocationFile {
|
||||
return new LocationFile(this.path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,235 @@
|
||||
/**
|
||||
* Message and MessagePart model classes
|
||||
*/
|
||||
import type {
|
||||
MessageAddressInterface,
|
||||
MessageInterface,
|
||||
MessageModelInterface,
|
||||
MessagePartInterface,
|
||||
MessagePartModelInterface
|
||||
} from "@/types/message";
|
||||
|
||||
import type { MessageInterface, MessagePartInterface } from "@/types/message";
|
||||
/**
|
||||
* Message class for working with message objects
|
||||
*/
|
||||
export class MessageObject implements MessageModelInterface {
|
||||
|
||||
_data: MessageInterface;
|
||||
_body: MessagePartObject | null = null;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'mail:message',
|
||||
};
|
||||
this._body = null;
|
||||
}
|
||||
|
||||
fromJson(data: MessageInterface): MessageObject {
|
||||
this._data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): MessageInterface {
|
||||
const json = {
|
||||
...this._data
|
||||
};
|
||||
if (this._body) {
|
||||
json.body = this._body.toJson();
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
clone(): MessageObject {
|
||||
const cloned = new MessageObject();
|
||||
cloned._data = {
|
||||
...this._data,
|
||||
};
|
||||
if (this._body) {
|
||||
cloned._body = this._body.clone();
|
||||
}
|
||||
return cloned;
|
||||
}
|
||||
|
||||
/** Properties */
|
||||
|
||||
get urid(): string | null{
|
||||
return this._data.urid ?? null;
|
||||
}
|
||||
|
||||
get size(): number {
|
||||
return this._data.size ?? 0;
|
||||
}
|
||||
|
||||
get receivedDate(): string | null {
|
||||
return this._data.receivedDate ?? null;
|
||||
}
|
||||
|
||||
get sentDate(): string | null {
|
||||
return this._data.sentDate ?? null;
|
||||
}
|
||||
|
||||
get date(): string | null {
|
||||
return this._data.date ?? null;
|
||||
}
|
||||
|
||||
get subject(): string | null {
|
||||
return this._data.subject ?? null;
|
||||
}
|
||||
|
||||
get snippet(): string | null {
|
||||
return this._data.snippet ?? null;
|
||||
}
|
||||
|
||||
get from(): MessageAddressObject | null {
|
||||
return this._data.from ? new MessageAddressObject(this._data.from) : null;
|
||||
}
|
||||
|
||||
get to(): Array<MessageAddressObject> | null {
|
||||
return this._data.to ? this._data.to.map(addr => new MessageAddressObject(addr)) : null;
|
||||
}
|
||||
|
||||
get cc(): Array<MessageAddressObject> | null {
|
||||
return this._data.cc ? this._data.cc.map(addr => new MessageAddressObject(addr)) : null;
|
||||
}
|
||||
|
||||
get bcc(): Array<MessageAddressObject> | null {
|
||||
return this._data.bcc ? this._data.bcc.map(addr => new MessageAddressObject(addr)) : null;
|
||||
}
|
||||
|
||||
get replyTo(): Array<MessageAddressObject> | null {
|
||||
return this._data.replyTo ? this._data.replyTo.map(addr => new MessageAddressObject(addr)) : null;
|
||||
}
|
||||
|
||||
get flags(): { read?: boolean; flagged?: boolean; answered?: boolean; draft?: boolean } | {} {
|
||||
return this._data.flags ?? {};
|
||||
}
|
||||
|
||||
get body(): MessagePartObject | null {
|
||||
if (this._body) {
|
||||
return this._body;
|
||||
}
|
||||
else if (this._data.body) {
|
||||
this._body = new MessagePartObject(this._data.body);
|
||||
return this._body;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
get attachments(): Array<MessagePartObject> {
|
||||
return this._data.attachments ? this._data.attachments.map(att => new MessagePartObject(att)) : [];
|
||||
}
|
||||
|
||||
/** 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) : [];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export class MessageAddressObject implements MessageAddressInterface {
|
||||
|
||||
_data: MessageAddressInterface;
|
||||
|
||||
constructor(data: MessageAddressInterface) {
|
||||
this._data = data;
|
||||
}
|
||||
|
||||
fromJson(data: MessageAddressInterface): MessageAddressObject {
|
||||
this._data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): MessageAddressInterface {
|
||||
return this._data;
|
||||
}
|
||||
|
||||
clone(): MessageAddressObject {
|
||||
return new MessageAddressObject({ ...this._data });
|
||||
}
|
||||
|
||||
/** Properties */
|
||||
|
||||
get address(): string {
|
||||
return this._data.address;
|
||||
}
|
||||
|
||||
get label(): string | undefined {
|
||||
return this._data.label;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* MessagePart class for working with message body parts
|
||||
*/
|
||||
export class MessagePartObject {
|
||||
export class MessagePartObject implements MessagePartModelInterface {
|
||||
|
||||
_data: MessagePartInterface;
|
||||
_subParts: MessagePartObject[] = [];
|
||||
|
||||
constructor(data?: Partial<MessagePartInterface>) {
|
||||
this._data = {
|
||||
@@ -17,14 +237,14 @@ export class MessagePartObject {
|
||||
blobId: data?.blobId ?? null,
|
||||
size: data?.size ?? null,
|
||||
name: data?.name ?? null,
|
||||
type: data?.type ?? undefined,
|
||||
type: data?.type ?? null,
|
||||
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,
|
||||
content: data?.content ?? null,
|
||||
subParts: data?.subParts ?? [],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -34,7 +254,13 @@ export class MessagePartObject {
|
||||
}
|
||||
|
||||
toJson(): MessagePartInterface {
|
||||
return this._data;
|
||||
const json = {
|
||||
...this._data,
|
||||
};
|
||||
if (this._subParts.length > 0) {
|
||||
json.subParts = this._subParts.map(subPart => subPart.toJson());
|
||||
}
|
||||
return json
|
||||
}
|
||||
|
||||
clone(): MessagePartObject {
|
||||
@@ -43,52 +269,59 @@ export class MessagePartObject {
|
||||
|
||||
/** Properties */
|
||||
|
||||
get partId(): string | null | undefined {
|
||||
return this._data.partId;
|
||||
get partId(): string | null {
|
||||
return this._data.partId ?? null;
|
||||
}
|
||||
|
||||
get blobId(): string | null | undefined {
|
||||
return this._data.blobId;
|
||||
get blobId(): string | null {
|
||||
return this._data.blobId ?? null;
|
||||
}
|
||||
|
||||
get size(): number | null | undefined {
|
||||
return this._data.size;
|
||||
get size(): number | null {
|
||||
return this._data.size ?? null;
|
||||
}
|
||||
|
||||
get name(): string | null | undefined {
|
||||
return this._data.name;
|
||||
get name(): string | null {
|
||||
return this._data.name ?? null;
|
||||
}
|
||||
|
||||
get type(): string | undefined {
|
||||
return this._data.type;
|
||||
get type(): string | null {
|
||||
return this._data.type ?? null;
|
||||
}
|
||||
|
||||
get charset(): string | null | undefined {
|
||||
return this._data.charset;
|
||||
get charset(): string | null {
|
||||
return this._data.charset ?? null;
|
||||
}
|
||||
|
||||
get disposition(): string | null | undefined {
|
||||
return this._data.disposition;
|
||||
get disposition(): string | null {
|
||||
return this._data.disposition ?? null;
|
||||
}
|
||||
|
||||
get cid(): string | null | undefined {
|
||||
return this._data.cid;
|
||||
get cid(): string | null {
|
||||
return this._data.cid ?? null;
|
||||
}
|
||||
|
||||
get language(): string | null | undefined {
|
||||
return this._data.language;
|
||||
get language(): string | null {
|
||||
return this._data.language ?? null;
|
||||
}
|
||||
|
||||
get location(): string | null | undefined {
|
||||
return this._data.location;
|
||||
get location(): string | null {
|
||||
return this._data.location ?? null;
|
||||
}
|
||||
|
||||
get content(): string | undefined {
|
||||
return this._data.content;
|
||||
get content(): string | null {
|
||||
return this._data.content ?? null;
|
||||
}
|
||||
|
||||
get subParts(): MessagePartInterface[] | undefined {
|
||||
return this._data.subParts;
|
||||
get subParts(): MessagePartModelInterface[] {
|
||||
if (this._subParts) {
|
||||
return this._subParts;
|
||||
}
|
||||
else if (this._data.subParts) {
|
||||
this._subParts = this._data.subParts.map((subPart) => new MessagePartObject(subPart));
|
||||
return this._subParts;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
/** Helper methods */
|
||||
@@ -98,7 +331,7 @@ export class MessagePartObject {
|
||||
}
|
||||
|
||||
hasSubParts(): boolean {
|
||||
return !!this._data.subParts && this._data.subParts.length > 0;
|
||||
return this.subParts.length > 0;
|
||||
}
|
||||
|
||||
isMultipart(): boolean {
|
||||
@@ -206,171 +439,3 @@ export class MessagePartObject {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) : [];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,16 +4,18 @@
|
||||
|
||||
import type {
|
||||
ProviderInterface,
|
||||
ProviderCapabilitiesInterface
|
||||
ProviderCapabilitiesInterface,
|
||||
ProviderModelInterface
|
||||
} from "@/types/provider";
|
||||
|
||||
export class ProviderObject implements ProviderInterface {
|
||||
export class ProviderObject implements ProviderModelInterface {
|
||||
|
||||
_data!: ProviderInterface;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'mail:provider',
|
||||
version: 1,
|
||||
identifier: '',
|
||||
label: '',
|
||||
capabilities: {},
|
||||
@@ -29,6 +31,12 @@ export class ProviderObject implements ProviderInterface {
|
||||
return this._data;
|
||||
}
|
||||
|
||||
clone(): ProviderObject {
|
||||
const cloned = new ProviderObject();
|
||||
cloned._data = { ...this._data };
|
||||
return cloned;
|
||||
}
|
||||
|
||||
capable(capability: keyof ProviderCapabilitiesInterface): boolean {
|
||||
const value = this._data.capabilities?.[capability];
|
||||
return value !== undefined && value !== false;
|
||||
@@ -43,10 +51,6 @@ export class ProviderObject implements ProviderInterface {
|
||||
|
||||
/** Immutable Properties */
|
||||
|
||||
get '@type'(): string {
|
||||
return this._data['@type'];
|
||||
}
|
||||
|
||||
get identifier(): string {
|
||||
return this._data.identifier;
|
||||
}
|
||||
|
||||
@@ -5,19 +5,22 @@
|
||||
import type {
|
||||
ServiceInterface,
|
||||
ServiceCapabilitiesInterface,
|
||||
ServiceIdentity,
|
||||
ServiceLocation
|
||||
ServiceLocation,
|
||||
ServiceModelInterface
|
||||
} from "@/types/service";
|
||||
import { Identity } from './identity';
|
||||
import { Location } from './location';
|
||||
|
||||
export class ServiceObject implements ServiceInterface {
|
||||
export class ServiceObject implements ServiceModelInterface {
|
||||
|
||||
_data!: ServiceInterface;
|
||||
_location: Location | null = null;
|
||||
_identity: Identity | null = null;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'mail:service',
|
||||
version: 1,
|
||||
provider: '',
|
||||
identifier: null,
|
||||
label: null,
|
||||
@@ -32,7 +35,35 @@ export class ServiceObject implements ServiceInterface {
|
||||
}
|
||||
|
||||
toJson(): ServiceInterface {
|
||||
return this._data;
|
||||
const json = {
|
||||
...this._data,
|
||||
capabilities: this._data.capabilities ? { ...this._data.capabilities } : this._data.capabilities,
|
||||
secondaryAddresses: this._data.secondaryAddresses ? [...this._data.secondaryAddresses] : this._data.secondaryAddresses,
|
||||
auxiliary: this._data.auxiliary ? { ...this._data.auxiliary } : this._data.auxiliary,
|
||||
};
|
||||
|
||||
if (this._location !== null) {
|
||||
json.location = this._location.toJson();
|
||||
}
|
||||
|
||||
if (this._identity !== null) {
|
||||
json.identity = this._identity.toJson();
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
clone(): ServiceObject {
|
||||
const cloned = new ServiceObject();
|
||||
cloned._data = {
|
||||
...this._data,
|
||||
capabilities: this._data.capabilities ? { ...this._data.capabilities } : this._data.capabilities,
|
||||
secondaryAddresses: this._data.secondaryAddresses ? [...this._data.secondaryAddresses] : this._data.secondaryAddresses,
|
||||
auxiliary: this._data.auxiliary ? { ...this._data.auxiliary } : this._data.auxiliary,
|
||||
};
|
||||
cloned._location = this._location ? this._location.clone() : null;
|
||||
cloned._identity = this._identity ? this._identity.clone() : null;
|
||||
return cloned;
|
||||
}
|
||||
|
||||
capable(capability: keyof ServiceCapabilitiesInterface): boolean {
|
||||
@@ -49,10 +80,6 @@ export class ServiceObject implements ServiceInterface {
|
||||
|
||||
/** Immutable Properties */
|
||||
|
||||
get '@type'(): string {
|
||||
return this._data['@type'];
|
||||
}
|
||||
|
||||
get provider(): string {
|
||||
return this._data.provider;
|
||||
}
|
||||
@@ -61,8 +88,8 @@ export class ServiceObject implements ServiceInterface {
|
||||
return this._data.identifier;
|
||||
}
|
||||
|
||||
get capabilities(): ServiceCapabilitiesInterface | undefined {
|
||||
return this._data.capabilities;
|
||||
get capabilities(): ServiceCapabilitiesInterface {
|
||||
return this._data.capabilities ?? {};
|
||||
}
|
||||
|
||||
get primaryAddress(): string | null {
|
||||
@@ -91,20 +118,38 @@ export class ServiceObject implements ServiceInterface {
|
||||
this._data.enabled = value;
|
||||
}
|
||||
|
||||
get location(): ServiceLocation | null {
|
||||
return this._data.location ?? null;
|
||||
get location(): Location | null {
|
||||
if (this._location) {
|
||||
return this._location;
|
||||
}
|
||||
else if (this._location === null && this._data.location) {
|
||||
const location = Location.fromJson(this._data.location as ServiceLocation);
|
||||
this._location = location;
|
||||
return location;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
set location(value: ServiceLocation | null) {
|
||||
this._data.location = value;
|
||||
set location(value: Location | null) {
|
||||
this._location = value;
|
||||
}
|
||||
|
||||
get identity(): ServiceIdentity | null {
|
||||
return this._data.identity ?? null;
|
||||
get identity(): Identity | null {
|
||||
if (this._identity) {
|
||||
return this._identity;
|
||||
}
|
||||
else if (this._data.identity) {
|
||||
const identity = Identity.fromJson(this._data.identity);
|
||||
this._identity = identity;
|
||||
return identity;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
set identity(value: ServiceIdentity | null) {
|
||||
this._data.identity = value;
|
||||
set identity(value: Identity | null) {
|
||||
this._identity = value;
|
||||
}
|
||||
|
||||
get auxiliary(): Record<string, any> {
|
||||
@@ -115,22 +160,4 @@ export class ServiceObject implements ServiceInterface {
|
||||
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