receiver.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. export class ReceiverService {
  18. private $log: ng.ILogService;
  19. private activeReceiver: threema.Receiver;
  20. public static $inject = ['$log'];
  21. constructor($log: ng.ILogService) {
  22. // Angular services
  23. this.$log = $log;
  24. }
  25. /**
  26. * Set the currently active receiver.
  27. */
  28. public setActive(activeReceiver: threema.Receiver): void {
  29. this.activeReceiver = activeReceiver;
  30. }
  31. /**
  32. * Return the currently active receiver.
  33. */
  34. public getActive(): threema.Receiver {
  35. return this.activeReceiver;
  36. }
  37. public isConversationActive(conversation: threema.Conversation): boolean {
  38. if (!this.activeReceiver) {
  39. return false;
  40. }
  41. return this.compare(conversation, this.activeReceiver);
  42. }
  43. /**
  44. * Compare two conversations and/or receivers.
  45. * Return `true` if they both have the same type and id.
  46. */
  47. public compare(a: threema.Conversation | threema.Receiver,
  48. b: threema.Conversation | threema.Receiver): boolean {
  49. return a !== undefined
  50. && b !== undefined
  51. && a.type === b.type
  52. && a.id === b.id;
  53. }
  54. public isContact(receiver: threema.Receiver): boolean {
  55. return receiver !== undefined
  56. && receiver.type === 'contact';
  57. }
  58. public isGroup(receiver: threema.Receiver): boolean {
  59. return receiver !== undefined
  60. && receiver.type === 'group';
  61. }
  62. public isDistributionList(receiver: threema.Receiver): boolean {
  63. return receiver !== undefined
  64. && receiver.type === 'distributionList';
  65. }
  66. public isBusinessContact(receiver: threema.Receiver): boolean {
  67. return this.isContact(receiver)
  68. && receiver.id.substr(0, 1) === '*';
  69. }
  70. }