contact-item.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { ContactAddress } from "./contact-address";
  2. import { ContactParams } from "./contact-params";
  3. export class ContactItem {
  4. public readonly data: ContactParams;
  5. public readonly id: string;
  6. public readonly hash: string;
  7. public name: string;
  8. public addrs: ContactAddress[];
  9. public me: boolean;
  10. public groups: string[];
  11. public alternateNames: string[];
  12. public notes: string;
  13. public lastSent?: Date;
  14. public lastReceived?: Date;
  15. public lastChanged?: Date;
  16. constructor(props: ContactParams) {
  17. this.data = props;
  18. this.hash = props.hash;
  19. this.id = props.id;
  20. this.addrs = (props.addrs || []).map(addrString => ContactAddress.fromPrefixedString(addrString));
  21. this.groups = props.groups || [];
  22. this.alternateNames = props.alternateNames || [];
  23. this.name = props.name || '';
  24. this.me = props.me;
  25. this.lastSent = props.lastSent ? new Date(props.lastSent) : undefined;
  26. this.lastReceived = props.lastReceived ? new Date(props.lastReceived) : undefined;
  27. this.lastChanged = props.lastChanged ? new Date(props.lastChanged) : undefined;
  28. this.notes = props.notes || '';
  29. }
  30. public matchesGroup(group: string): boolean {
  31. if (this.groups.filter(x => x === group.trim())[0].length > 0) {
  32. return true;
  33. }
  34. return false;
  35. }
  36. public getFirstEmail(): string | undefined {
  37. const first = this.addrs.filter(addr => addr.type === 'email')[0];
  38. if (first == null) {
  39. return undefined;
  40. }
  41. return first.address;
  42. }
  43. public getFirstPhone(): string | undefined {
  44. const first = this.addrs.filter(addr => addr.type === 'phone')[0];
  45. if (first == null) {
  46. return undefined;
  47. }
  48. return first.address;
  49. }
  50. public search(search: string): boolean {
  51. if (this.name.toLowerCase().indexOf(search.toLowerCase().trim()) >= 0) {
  52. return true;
  53. }
  54. if (this.alternateNames.find(i => i.toLowerCase().trim().indexOf(search.toLowerCase().trim()) >= 0)) {
  55. return true;
  56. }
  57. if (this.addrs.find(addr => addr.matches(search))) {
  58. return true;
  59. }
  60. return false;
  61. }
  62. public getMatchingAddresses(search: string): ContactAddress[] {
  63. return this.addrs.filter(addr => addr.matches(search));
  64. }
  65. public matchesAddressExactly(addressType: string, addressValue: string): boolean {
  66. if (this.addrs.find(addr => addr.matchesExactly(addressType, addressValue))) {
  67. return true;
  68. }
  69. return false;
  70. }
  71. public ensureAlternateNameExists(altName: string) {
  72. if (!this.alternateNames.includes(altName)) {
  73. this.alternateNames.push(altName);
  74. }
  75. }
  76. // public generateName() {
  77. // const firstEmail = this.getFirstEmail();
  78. // if (firstEmail != null) {
  79. // return firstEmail;
  80. // }
  81. // const firstPhone = this.getFirstPhone();
  82. // if (firstPhone != null) {
  83. // return firstPhone;
  84. // }
  85. // return 'New contact ' + this.id.substring(0,8);
  86. // }
  87. public getData(): ContactParams {
  88. return {
  89. addrs: this.addrs.map(addr => addr.toPrefixedString()),
  90. alternateNames: this.alternateNames.map(x => x),
  91. groups: this.groups,
  92. hash: this.hash,
  93. id: this.id,
  94. lastChanged: this.lastChanged ? this.lastChanged.toISOString() : undefined,
  95. lastReceived: this.lastReceived ? this.lastReceived.toISOString() : undefined,
  96. lastSent: this.lastSent ? this.lastSent.toISOString() : undefined,
  97. me: this.me,
  98. name: this.name,
  99. notes: this.notes
  100. };
  101. }
  102. }