receiver.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 {isContactReceiver} from '../typeguards';
  18. export class ReceiverService {
  19. private activeReceiver: threema.Receiver;
  20. /**
  21. * Set the currently active receiver.
  22. */
  23. public setActive(activeReceiver: threema.Receiver): void {
  24. this.activeReceiver = activeReceiver;
  25. }
  26. /**
  27. * Return the currently active receiver.
  28. */
  29. public getActive(): threema.Receiver {
  30. return this.activeReceiver;
  31. }
  32. public isConversationActive(conversation: threema.Conversation): boolean {
  33. if (!this.activeReceiver) {
  34. return false;
  35. }
  36. return this.compare(conversation, this.activeReceiver);
  37. }
  38. /**
  39. * Compare two conversations and/or receivers.
  40. * Return `true` if they both have the same type and id.
  41. */
  42. public compare(a: threema.Conversation | threema.Receiver,
  43. b: threema.Conversation | threema.Receiver): boolean {
  44. return a !== undefined
  45. && b !== undefined
  46. && a.type === b.type
  47. && a.id === b.id;
  48. }
  49. public isBusinessContact(receiver: threema.Receiver): boolean {
  50. return isContactReceiver(receiver)
  51. && receiver.id.substr(0, 1) === '*';
  52. }
  53. }