troubleshooting.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 {Logger} from 'ts-log';
  18. import {arrayToBuffer, hasFeature, sleep} from '../helpers';
  19. import * as clipboard from '../helpers/clipboard';
  20. import {BrowserService} from '../services/browser';
  21. import {LogService} from '../services/log';
  22. import {WebClientService} from '../services/webclient';
  23. import {DialogController} from './dialog';
  24. export class TroubleshootingController extends DialogController {
  25. public static readonly $inject = [
  26. '$scope', '$mdDialog', '$mdToast', '$translate',
  27. 'LogService', 'BrowserService', 'WebClientService',
  28. ];
  29. private readonly $scope: ng.IScope;
  30. private readonly $mdToast: ng.material.IToastService;
  31. private readonly $translate: ng.translate.ITranslateService;
  32. private readonly logService: LogService;
  33. private readonly browserService: BrowserService;
  34. private readonly webClientService: WebClientService;
  35. private readonly log: Logger;
  36. public isSending: boolean = false;
  37. public sendingFailed: boolean = false;
  38. public description: string = '';
  39. constructor(
  40. $scope: ng.IScope,
  41. $mdDialog: ng.material.IDialogService,
  42. $mdToast: ng.material.IToastService,
  43. $translate: ng.translate.ITranslateService,
  44. logService: LogService,
  45. browserService: BrowserService,
  46. webClientService: WebClientService,
  47. ) {
  48. super($mdDialog);
  49. this.$scope = $scope;
  50. this.$mdToast = $mdToast;
  51. this.$translate = $translate;
  52. this.logService = logService;
  53. this.browserService = browserService;
  54. this.webClientService = webClientService;
  55. this.log = logService.getLogger('Troubleshooting-C');
  56. }
  57. /**
  58. * Return whether the web client is currently connected (or able to
  59. * reconnect on its own).
  60. */
  61. public get isConnected(): boolean {
  62. return this.webClientService.readyToSubmit;
  63. }
  64. /**
  65. * Return whether the log is ready to be sent.
  66. *
  67. * This requires...
  68. *
  69. * - the web client to be connected (or able to reconnect on its own),
  70. * - a description of the problem to be populated, and
  71. * - sending to be not in progress already.
  72. */
  73. public get canSend(): boolean {
  74. return this.isConnected && this.description.length > 0 && !this.isSending;
  75. }
  76. /**
  77. * Send the log to *SUPPORT.
  78. */
  79. public async send(): Promise<void> {
  80. this.isSending = true;
  81. this.sendingFailed = false;
  82. // Serialise the log
  83. const log = new TextEncoder().encode(this.logService.memory.serialize());
  84. // Error handler
  85. const fail = () => {
  86. this.$scope.$apply(() => {
  87. this.isSending = false;
  88. this.sendingFailed = true;
  89. // Show toast
  90. this.$mdToast.show(this.$mdToast.simple()
  91. .textContent(this.$translate.instant('troubleshooting.REPORT_VIA_THREEMA_FAILED'))
  92. .position('bottom center'));
  93. });
  94. };
  95. // Add contact *SUPPORT (if needed)
  96. const support: threema.BaseReceiver = {
  97. id: '*SUPPORT',
  98. type: 'contact',
  99. };
  100. if (!this.webClientService.contacts.has(support.id)) {
  101. try {
  102. await this.webClientService.addContact(support.id);
  103. } catch (error) {
  104. this.log.error('Unable to add contact *SUPPORT:', error);
  105. return fail();
  106. }
  107. }
  108. // Workaround for iOS which does not fetch the feature mask immediately
  109. // TODO: Remove once IOS-809 has been resolved
  110. for (let i = 0; i < 50; ++i) {
  111. const contact = this.webClientService.contacts.get(support.id);
  112. if (hasFeature(contact, threema.ContactReceiverFeature.FILE, this.log)) {
  113. break;
  114. }
  115. await sleep(100);
  116. }
  117. // Send as file to *SUPPORT
  118. const browser = this.browserService.getBrowser();
  119. let browserShortInfo = 'unknown';
  120. if (browser.wasDetermined()) {
  121. browserShortInfo = `${browser.name}-${browser.version}`;
  122. if (browser.mobile) {
  123. browserShortInfo += '-mobile';
  124. }
  125. }
  126. const message: threema.FileMessageData = {
  127. name: `webclient-[[VERSION]]-${browserShortInfo}.log`,
  128. fileType: 'text/plain',
  129. size: log.byteLength,
  130. data: arrayToBuffer(log),
  131. caption: this.description,
  132. sendAsFile: true,
  133. };
  134. try {
  135. await this.webClientService.sendMessage(support, 'file', message, { waitUntilAcknowledged: true });
  136. } catch (error) {
  137. this.log.error('Unable to send log report to *SUPPORT:', error);
  138. return fail();
  139. }
  140. // Done
  141. this.isSending = false;
  142. this.$mdToast.show(this.$mdToast.simple()
  143. .textContent(this.$translate.instant('troubleshooting.REPORT_VIA_THREEMA_SUCCESS'))
  144. .position('bottom center'));
  145. // Hide dialog
  146. this.hide();
  147. }
  148. /**
  149. * Copy the log into the clipboard.
  150. */
  151. public copyToClipboard(): void {
  152. // Get the log
  153. const log = this.getLog();
  154. // Copy to clipboard
  155. let toastString = 'messenger.COPIED';
  156. try {
  157. clipboard.copyString(log, this.browserService.getBrowser().isSafari());
  158. } catch (error) {
  159. this.log.warn('Could not copy text to clipboard:', error);
  160. toastString = 'messenger.COPY_ERROR';
  161. }
  162. // Show toast
  163. this.$mdToast.show(this.$mdToast.simple()
  164. .textContent(this.$translate.instant(toastString))
  165. .position('bottom center'));
  166. }
  167. /**
  168. * Serialise the memory log.
  169. */
  170. private getLog(): string {
  171. // TODO: Add metadata
  172. return this.logService.memory.serialize();
  173. }
  174. }