receiver.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 !== undefined) {
  39. return this.compare(conversation, this.activeReceiver);
  40. }
  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 isContact(receiver: threema.Receiver): boolean {
  50. return receiver !== undefined
  51. && receiver.type === 'contact';
  52. }
  53. public isGroup(receiver: threema.Receiver): boolean {
  54. return receiver !== undefined
  55. && receiver.type === 'group';
  56. }
  57. public isDistributionList(receiver: threema.Receiver): boolean {
  58. return receiver !== undefined
  59. && receiver.type === 'distributionList';
  60. }
  61. public isBusinessContact(receiver: threema.Receiver): boolean {
  62. return this.isContact(receiver)
  63. && receiver.id.substr(0, 1) === '*';
  64. }
  65. }