| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- import { ContactAddress } from "./contact-address";
- import { ContactParams } from "./contact-params";
- export class ContactItem {
- public readonly data: ContactParams;
- public readonly id: string;
- public readonly hash: string;
- public name: string;
- public addrs: ContactAddress[];
- public me: boolean;
- public groups: string[];
- public alternateNames: string[];
- public notes: string;
- public lastSent?: Date;
- public lastReceived?: Date;
- public lastChanged?: Date;
-
- constructor(props: ContactParams) {
- this.data = props;
- this.hash = props.hash;
- this.id = props.id;
- this.addrs = (props.addrs || []).map(addrString => ContactAddress.fromPrefixedString(addrString));
- this.groups = props.groups || [];
- this.alternateNames = props.alternateNames || [];
- this.name = props.name || '';
- this.me = props.me;
- this.lastSent = props.lastSent ? new Date(props.lastSent) : undefined;
- this.lastReceived = props.lastReceived ? new Date(props.lastReceived) : undefined;
- this.lastChanged = props.lastChanged ? new Date(props.lastChanged) : undefined;
- this.notes = props.notes || '';
- }
- public matchesGroup(group: string): boolean {
- if (this.groups.filter(x => x === group.trim())[0].length > 0) {
- return true;
- }
- return false;
- }
- public getFirstEmail(): string | undefined {
- const first = this.addrs.filter(addr => addr.type === 'email')[0];
- if (first == null) {
- return undefined;
- }
- return first.address;
- }
- public getFirstPhone(): string | undefined {
- const first = this.addrs.filter(addr => addr.type === 'phone')[0];
- if (first == null) {
- return undefined;
- }
- return first.address;
- }
- public search(search: string): boolean {
- if (this.name.toLowerCase().indexOf(search.toLowerCase().trim()) >= 0) {
- return true;
- }
- if (this.alternateNames.find(i => i.toLowerCase().trim().indexOf(search.toLowerCase().trim()) >= 0)) {
- return true;
- }
- if (this.addrs.find(addr => addr.matches(search))) {
- return true;
- }
- return false;
- }
- public getMatchingAddresses(search: string): ContactAddress[] {
- return this.addrs.filter(addr => addr.matches(search));
- }
- public matchesAddressExactly(addressType: string, addressValue: string): boolean {
- if (this.addrs.find(addr => addr.matchesExactly(addressType, addressValue))) {
- return true;
- }
- return false;
- }
- public ensureAlternateNameExists(altName: string) {
- if (!this.alternateNames.includes(altName)) {
- this.alternateNames.push(altName);
- }
- }
- // public generateName() {
- // const firstEmail = this.getFirstEmail();
- // if (firstEmail != null) {
- // return firstEmail;
- // }
- // const firstPhone = this.getFirstPhone();
- // if (firstPhone != null) {
- // return firstPhone;
- // }
- // return 'New contact ' + this.id.substring(0,8);
- // }
- public getData(): ContactParams {
- return {
- addrs: this.addrs.map(addr => addr.toPrefixedString()),
- alternateNames: this.alternateNames.map(x => x),
- groups: this.groups,
- hash: this.hash,
- id: this.id,
- lastChanged: this.lastChanged ? this.lastChanged.toISOString() : undefined,
- lastReceived: this.lastReceived ? this.lastReceived.toISOString() : undefined,
- lastSent: this.lastSent ? this.lastSent.toISOString() : undefined,
- me: this.me,
- name: this.name,
- notes: this.notes
- };
- }
- }
|