|
|
@@ -1,3 +1,4 @@
|
|
|
+import { ContactAddress } from "./contact-address";
|
|
|
import { ContactParams } from "./contact-params";
|
|
|
|
|
|
export class ContactItem {
|
|
|
@@ -5,30 +6,32 @@ export class ContactItem {
|
|
|
public readonly data: ContactParams;
|
|
|
public readonly id: string;
|
|
|
public readonly hash: string;
|
|
|
- public addrs: string[];
|
|
|
+ public name: string;
|
|
|
+ public addrs: ContactAddress[];
|
|
|
+ public me: boolean;
|
|
|
public groups: string[];
|
|
|
- public names: string[];
|
|
|
+ public alternateNames: string[];
|
|
|
public notes: string;
|
|
|
- public canSendFromAddrs: 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.canSendFromAddrs = props.canSendFromAddrs || [];
|
|
|
- this.addrs = props.addrs || [];
|
|
|
+ this.addrs = (props.addrs || []).map(addrString => ContactAddress.fromPrefixedString(addrString));
|
|
|
this.groups = props.groups || [];
|
|
|
- this.names = props.names || [];
|
|
|
+ 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 matchesAddressOrName(search: string): boolean {
|
|
|
- if (this.addrs.filter(x => x.indexOf(search.trim()) >= 0).length > 0) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
public matchesGroup(group: string): boolean {
|
|
|
if (this.groups.filter(x => x === group.trim())[0].length > 0) {
|
|
|
return true;
|
|
|
@@ -37,48 +40,75 @@ export class ContactItem {
|
|
|
}
|
|
|
|
|
|
public getFirstEmail(): string | undefined {
|
|
|
- const prefixed = this.addrs.filter(x => x.startsWith('email:'))[0];
|
|
|
- if (prefixed == null) {
|
|
|
- return undefined;
|
|
|
- }
|
|
|
- return prefixed.substring(6);
|
|
|
+ const first = this.addrs.filter(addr => addr.type === 'email')[0];
|
|
|
+ if (first == null) {
|
|
|
+ return undefined;
|
|
|
+ }
|
|
|
+ return first.address;
|
|
|
}
|
|
|
|
|
|
public getFirstPhone(): string | undefined {
|
|
|
- const prefixed = this.addrs.filter(x => x.startsWith('phone:'))[0];
|
|
|
- if (prefixed == null) {
|
|
|
- return undefined;
|
|
|
+ const first = this.addrs.filter(addr => addr.type === 'phone')[0];
|
|
|
+ if (first == null) {
|
|
|
+ return undefined;
|
|
|
}
|
|
|
- return prefixed.substring(6);
|
|
|
+ return first.address;
|
|
|
}
|
|
|
|
|
|
- public getMatchingAddresses(search: string): string[] {
|
|
|
- return this.addrs.filter(x => x.indexOf(search) >= 0);
|
|
|
+ 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 getName(): string {
|
|
|
- if (this.names.length > 0) {
|
|
|
- return this.names[0];
|
|
|
- }
|
|
|
- const firstEmail = this.getFirstEmail();
|
|
|
- if (firstEmail != null) {
|
|
|
- return firstEmail;
|
|
|
+ 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;
|
|
|
}
|
|
|
- const firstPhone = this.getFirstPhone();
|
|
|
- if (firstPhone != null) {
|
|
|
- return firstPhone;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public ensureAlternateNameExists(altName: string) {
|
|
|
+ if (!this.alternateNames.includes(altName)) {
|
|
|
+ this.alternateNames.push(altName);
|
|
|
}
|
|
|
- return 'New contact ' + this.id.substring(0,8);
|
|
|
}
|
|
|
|
|
|
+ // 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,
|
|
|
- canSendFromAddrs: this.canSendFromAddrs,
|
|
|
+ addrs: this.addrs.map(addr => addr.toPrefixedString()),
|
|
|
+ alternateNames: this.alternateNames.map(x => x),
|
|
|
groups: this.groups,
|
|
|
hash: this.hash,
|
|
|
id: this.id,
|
|
|
- names: this.names,
|
|
|
+ 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
|
|
|
};
|
|
|
}
|