contact-item.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 addrs: ContactAddress[];
  8. public groups: string[];
  9. public names: string[];
  10. public notes: string;
  11. public canSendFromAddrs: ContactAddress[];
  12. constructor(props: ContactParams) {
  13. this.data = props;
  14. this.hash = props.hash;
  15. this.id = props.id;
  16. this.canSendFromAddrs = (props.canSendFromAddrs || []).map(addrString => ContactAddress.fromPrefixedString(addrString));
  17. this.addrs = (props.addrs || []).map(addrString => ContactAddress.fromPrefixedString(addrString));
  18. this.groups = props.groups || [];
  19. this.names = props.names || [];
  20. if (this.names.length === 0) {
  21. this.names.push(this.generateName());
  22. }
  23. this.notes = props.notes || '';
  24. }
  25. public matchesGroup(group: string): boolean {
  26. if (this.groups.filter(x => x === group.trim())[0].length > 0) {
  27. return true;
  28. }
  29. return false;
  30. }
  31. public getFirstEmail(): string | undefined {
  32. const first = this.addrs.filter(addr => addr.type === 'email')[0];
  33. if (first == null) {
  34. return undefined;
  35. }
  36. return first.address;
  37. }
  38. public getFirstPhone(): string | undefined {
  39. const first = this.addrs.filter(addr => addr.type === 'phone')[0];
  40. if (first == null) {
  41. return undefined;
  42. }
  43. return first.address;
  44. }
  45. public search(search: string): boolean {
  46. if (this.names.find(i => i.toLowerCase().trim().indexOf(search.toLowerCase().trim()) >= 0)) {
  47. return true;
  48. }
  49. if (this.addrs.find(addr => addr.matches(search))) {
  50. return true;
  51. }
  52. return false;
  53. }
  54. public getMatchingAddresses(search: string): ContactAddress[] {
  55. return this.addrs.filter(addr => addr.matches(search));
  56. }
  57. public matchesAddressExactly(addressType: string, addressValue: string): boolean {
  58. if (this.addrs.find(addr => addr.matchesExactly(addressType, addressValue))) {
  59. return true;
  60. }
  61. return false;
  62. }
  63. public generateName() {
  64. const firstEmail = this.getFirstEmail();
  65. if (firstEmail != null) {
  66. return firstEmail;
  67. }
  68. const firstPhone = this.getFirstPhone();
  69. if (firstPhone != null) {
  70. return firstPhone;
  71. }
  72. return 'New contact ' + this.id.substring(0,8);
  73. }
  74. public getName(): string {
  75. if (this.names.length > 0) {
  76. return this.names[0];
  77. } else {
  78. return '';
  79. }
  80. }
  81. public getData(): ContactParams {
  82. return {
  83. addrs: this.addrs.map(addr => addr.toPrefixedString()),
  84. canSendFromAddrs: this.canSendFromAddrs.map(addr => addr.toPrefixedString()),
  85. groups: this.groups,
  86. hash: this.hash,
  87. id: this.id,
  88. names: this.names,
  89. notes: this.notes
  90. };
  91. }
  92. }