receiver.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * This file is part of Threema Web.
  3. *
  4. * Threema Web is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU Affero General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or (at
  7. * your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. import {hasValue} from '../helpers';
  18. import {isContactReceiver} from '../typeguards';
  19. export class ReceiverService {
  20. private activeReceiver: threema.Receiver;
  21. /**
  22. * Set the currently active receiver.
  23. */
  24. public setActive(activeReceiver: threema.Receiver): void {
  25. this.activeReceiver = activeReceiver;
  26. }
  27. /**
  28. * Return the currently active receiver.
  29. */
  30. public getActive(): threema.Receiver {
  31. return this.activeReceiver;
  32. }
  33. public isConversationActive(conversation: threema.Conversation): boolean {
  34. if (!this.activeReceiver) {
  35. return false;
  36. }
  37. return this.compare(conversation, this.activeReceiver);
  38. }
  39. /**
  40. * Compare two conversations and/or receivers.
  41. * Return `true` if they both have the same type and id.
  42. */
  43. public compare(a: threema.Conversation | threema.Receiver,
  44. b: threema.Conversation | threema.Receiver): boolean {
  45. return a !== undefined
  46. && b !== undefined
  47. && a.type === b.type
  48. && a.id === b.id;
  49. }
  50. public isBusinessContact(receiver: threema.Receiver): boolean {
  51. return isContactReceiver(receiver)
  52. && receiver.id.substr(0, 1) === '*';
  53. }
  54. /**
  55. * Check if a receiver is blocked.
  56. * If the receiver isn't a contact or does not have the blocked flag, he is not blocked.
  57. * Otherwise the isBlocked flag is evaluated
  58. * @param receiver
  59. */
  60. public isBlocked(receiver: threema.Receiver | null): boolean {
  61. if (!hasValue(receiver)) {
  62. return false;
  63. }
  64. if (isContactReceiver(receiver) && hasValue(receiver.isBlocked)) {
  65. return receiver.isBlocked;
  66. } else {
  67. return false;
  68. }
  69. }
  70. }