38 lines
1.4 KiB
TypeScript
38 lines
1.4 KiB
TypeScript
import ModelComponent from "swaf/db/ModelComponent";
|
|
import User from "swaf/auth/models/User";
|
|
import {ManyModelRelation, OneModelRelation} from "swaf/db/ModelRelation";
|
|
import MailIdentity from "./MailIdentity";
|
|
import MailDomain from "./MailDomain";
|
|
import config from "config";
|
|
|
|
export default class UserMailIdentityComponent extends ModelComponent<User> {
|
|
public main_mail_identity_id?: number = undefined;
|
|
|
|
public readonly mailIdentities: ManyModelRelation<User, MailIdentity> = new ManyModelRelation(this._model,
|
|
MailIdentity, {
|
|
localKey: 'id',
|
|
foreignKey: 'user_id',
|
|
});
|
|
public readonly publicMailIdentities: ManyModelRelation<User, MailIdentity> = this.mailIdentities.clone()
|
|
.filter(async model => !!(await model.domain.get())?.isPublic());
|
|
|
|
public readonly mailDomains: ManyModelRelation<User, MailDomain> = new ManyModelRelation(this._model, MailDomain, {
|
|
localKey: 'id',
|
|
foreignKey: 'user_id',
|
|
});
|
|
|
|
public readonly mainMailIdentity: OneModelRelation<User, MailIdentity> = new OneModelRelation(this._model,
|
|
MailIdentity, {
|
|
foreignKey: 'id',
|
|
localKey: 'main_mail_identity_id',
|
|
});
|
|
|
|
public getMaxPublicAddressesCount(): number {
|
|
return config.get<number>('mail.max_public_identities_per_user');
|
|
}
|
|
|
|
public async getPublicAddressesCount(): Promise<number> {
|
|
return (await this.publicMailIdentities.get()).length;
|
|
}
|
|
}
|