1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434 |
- /**
- * This file is part of Threema Web.
- *
- * Threema Web is free software: you can redistribute it and/or modify it
- * under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or (at
- * your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
- * General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
- */
- import * as msgpack from 'msgpack-lite';
- import {hexToU8a} from '../helpers';
- import {BrowserService} from './browser';
- import {FingerPrintService} from './fingerprint';
- import {TrustedKeyStoreService} from './keystore';
- import {MessageService} from './message';
- import {MimeService} from './mime';
- import {NotificationService} from './notification';
- import {PeerConnectionHelper} from './peerconnection';
- import {PushService} from './push';
- import {QrCodeService} from './qrcode';
- import {ReceiverService} from './receiver';
- import {StateService} from './state';
- import {TitleService} from './title';
- import {VersionService} from './version';
- class WebClientDefault {
- private avatar: threema.AvatarRegistry = {
- group: {
- low: 'img/ic_group_t.png',
- high: 'img/ic_group_picture_big.png',
- },
- contact: {
- low: 'img/ic_contact_picture_t.png',
- high: 'img/ic_contact_picture_big.png',
- },
- distributionList: {
- low: 'img/ic_distribution_list_t.png',
- high: 'img/ic_distribution_list_t.png',
- },
- };
- /**
- * Return path to avatar.
- *
- * If the avatar type is invalid, return null.
- */
- public getAvatar(type: string, highResolution: boolean): string {
- let field: string = highResolution ? 'high' : 'low';
- if (typeof this.avatar[type] === 'undefined') {
- return null;
- }
- return this.avatar[type][field];
- }
- }
- /**
- * This service handles everything related to the communication with the peer.
- */
- export class WebClientService {
- private static AVATAR_LOW_MAX_SIZE = 48;
- private static MAX_TEXT_LENGTH = 3500;
- private static MAX_FILE_SIZE = 15 * 1024 * 1024;
- private static TYPE_REQUEST = 'request';
- private static TYPE_RESPONSE = 'response';
- private static TYPE_UPDATE = 'update';
- private static TYPE_CREATE = 'create';
- private static TYPE_DELETE = 'delete';
- private static SUB_TYPE_RECEIVER = 'receiver';
- private static SUB_TYPE_CONVERSATIONS = 'conversations';
- private static SUB_TYPE_CONVERSATION = 'conversation';
- private static SUB_TYPE_MESSAGE = 'message';
- private static SUB_TYPE_TEXT_MESSAGE = 'textMessage';
- private static SUB_TYPE_FILE_MESSAGE = 'fileMessage';
- private static SUB_TYPE_AVATAR = 'avatar';
- private static SUB_TYPE_THUMBNAIL = 'thumbnail';
- private static SUB_TYPE_BLOB = 'blob';
- private static SUB_TYPE_TYPING = 'typing';
- private static SUB_TYPE_READ = 'read';
- private static SUB_TYPE_CLIENT_INFO = 'clientInfo';
- private static SUB_TYPE_KEY_PERSISTED = 'keyPersisted';
- private static SUB_TYPE_ACK = 'ack';
- private static SUB_TYPE_CONTACT_DETAIL = 'contactDetail';
- private static SUB_TYPE_CONTACT = 'contact';
- private static SUB_TYPE_GROUP = 'group';
- private static SUB_TYPE_DISTRIBUTION_LIST = 'distributionList';
- private static SUB_TYPE_ALERT = 'alert';
- private static SUB_TYPE_GROUP_SYNC = 'groupSync';
- private static ARGUMENT_MODE = 'mode';
- private static ARGUMENT_MODE_REFRESH = 'refresh';
- private static ARGUMENT_MODE_NEW = 'new';
- private static ARGUMENT_MODE_MODIFIED = 'modified';
- private static ARGUMENT_MODE_REMOVED = 'removed';
- private static ARGUMENT_RECEIVER_TYPE = 'type';
- private static ARGUMENT_RECEIVER_ID = 'id';
- private static ARGUMENT_TEMPORARY_ID = 'temporaryId';
- private static ARGUMENT_REFERENCE_MSG_ID = 'refMsgId';
- private static ARGUMENT_AVATAR_HIGH_RESOLUTION = 'highResolution';
- private static ARGUMENT_CONTACT_IS_TYPING = 'isTyping';
- private static ARGUMENT_MESSAGE_ID = 'messageId';
- private static ARGUMENT_HAS_MORE = 'more';
- private static ARGUMENT_MESSAGE_ACKNOWLEDGED = 'acknowledged';
- private static ARGUMENT_IDENTITY = 'identity';
- private static ARGUMENT_SUCCESS = 'success';
- private static ARGUMENT_MESSAGE = 'message';
- private static ARGUMENT_SYSTEM_CONTACT = 'systemContact';
- private static ARGUMENT_NAME = 'name';
- private static ARGUMENT_MEMBERS = 'members';
- private static ARGUMENT_FIRST_NAME = 'firstName';
- private static ARGUMENT_LAST_NAME = 'lastName';
- private static ARGUMENT_DELETE_TYPE = 'deleteType';
- private static ARGUMENT_ERROR = 'error';
- private static ARGUMENT_MAX_SIZE = 'maxSize';
- private static DATA_FIELD_BLOB_BLOB = 'blob';
- private static DC_LABEL = 'THREEMA';
- private logTag: string = '[WebClientService]';
- // Angular services
- private $state: ng.ui.IStateService;
- private $log: ng.ILogService;
- private $rootScope: any;
- private $q: ng.IQService;
- private $window: ng.IWindowService;
- private $translate: ng.translate.ITranslateService;
- private $filter: any;
- private $timeout: ng.ITimeoutService;
- // Custom services
- private notificationService: NotificationService;
- private messageService: MessageService;
- private pushService: PushService;
- private browserService: BrowserService;
- private titleService: TitleService;
- private fingerPrintService: FingerPrintService;
- private qrCodeService: QrCodeService;
- private mimeService: MimeService;
- private receiverService: ReceiverService;
- private versionService: VersionService;
- // State handling
- private startupPromise: ng.IDeferred<{}> = null; // TODO: deferred type
- private startupDone: boolean = false;
- private pendingInitializationStepRoutines: threema.InitializationStepRoutine[] = [];
- private initialized: Set<threema.InitializationStep> = new Set();
- private initializedThreshold = 3;
- private stateService: StateService;
- // SaltyRTC
- private saltyRtcHost: string = null;
- public salty: saltyrtc.SaltyRTC = null;
- private webrtcTask: saltyrtc.tasks.webrtc.WebRTCTask = null;
- private secureDataChannel: saltyrtc.tasks.webrtc.SecureDataChannel = null;
- // Messenger data
- public messages: threema.Container.Messages;
- public conversations: threema.Container.Conversations;
- public receivers: threema.Container.Receivers;
- public alerts: threema.Alert[] = [];
- public defaults: WebClientDefault;
- private myIdentity: threema.Identity;
- private pushToken: string = null;
- // Other
- private config: threema.Config;
- private container: threema.Container.Factory;
- private typingInstance: threema.Container.Typing;
- private drafts: threema.Container.Drafts;
- private pcHelper: PeerConnectionHelper = null;
- private deviceInfo: string = null;
- private trustedKeyStore: TrustedKeyStoreService;
- public version = null;
- private blobCache = new Map<String, ArrayBuffer>();
- private loadingMessages = new Map<String, boolean>();
- public receiverListener: threema.ReceiverListener[] = [];
- // Msgpack
- private msgpackOptions: msgpack.BufferOptions = {
- codec: msgpack.createCodec({binarraybuffer: true}),
- };
- // pending rtc promises
- private requestPromises: Map<string, threema.PromiseCallbacks> = new Map();
- public static $inject = [
- '$log', '$rootScope', '$q', '$state', '$window', '$translate', '$filter', '$timeout',
- 'Container', 'TrustedKeyStore',
- 'StateService', 'NotificationService', 'MessageService', 'PushService', 'BrowserService',
- 'TitleService', 'FingerPrintService', 'QrCodeService', 'MimeService', 'ReceiverService',
- 'VersionService',
- 'CONFIG',
- ];
- constructor($log: ng.ILogService,
- $rootScope: any,
- $q: ng.IQService,
- $state: ng.ui.IStateService,
- $window: ng.IWindowService,
- $translate: ng.translate.ITranslateService,
- $filter: ng.IFilterService,
- $timeout: ng.ITimeoutService,
- container: threema.Container.Factory,
- trustedKeyStore: TrustedKeyStoreService,
- stateService: StateService,
- notificationService: NotificationService,
- messageService: MessageService,
- pushService: PushService,
- browserService: BrowserService,
- titleService: TitleService,
- fingerPrintService: FingerPrintService,
- qrCodeService: QrCodeService,
- mimeService: MimeService,
- receiverService: ReceiverService,
- VersionService: VersionService,
- CONFIG: threema.Config) {
- // Angular services
- this.$log = $log;
- this.$rootScope = $rootScope;
- this.$q = $q;
- this.$state = $state;
- this.$window = $window;
- this.$translate = $translate;
- this.$filter = $filter;
- this.$timeout = $timeout;
- // Own services
- this.notificationService = notificationService;
- this.messageService = messageService;
- this.pushService = pushService;
- this.browserService = browserService;
- this.titleService = titleService;
- this.fingerPrintService = fingerPrintService;
- this.qrCodeService = qrCodeService;
- this.mimeService = mimeService;
- this.receiverService = receiverService;
- this.versionService = VersionService;
- // Configuration object
- this.config = CONFIG;
- // State
- this.stateService = stateService;
- // Other properties
- this.container = container;
- this.trustedKeyStore = trustedKeyStore;
- // Get default class
- this.defaults = new WebClientDefault();
- // Initialize drafts
- this.drafts = this.container.createDrafts();
- // Setup fields
- this._resetFields();
- // Register event handlers
- this.stateService.evtConnectionBuildupStateChange.attach(
- (stateChange: threema.ConnectionBuildupStateChange) => {
- if (this.startupPromise !== null) {
- this.startupPromise.notify(stateChange);
- }
- },
- );
- }
- get me(): threema.MeReceiver {
- return this.receivers.me;
- }
- get contacts(): Map<string, threema.ContactReceiver> {
- return this.receivers.contacts;
- }
- get groups(): Map<string, threema.GroupReceiver> {
- return this.receivers.groups;
- }
- get distributionLists(): Map<string, threema.DistributionListReceiver> {
- return this.receivers.distributionLists;
- }
- get typing(): threema.Container.Typing {
- return this.typingInstance;
- }
- /**
- * Return QR code payload.
- */
- public buildQrCodePayload(persistent: boolean): string {
- return this.qrCodeService.buildQrCodePayload(
- this.salty.permanentKeyBytes,
- this.salty.authTokenBytes,
- hexToU8a(this.config.SALTYRTC_SERVER_KEY),
- this.saltyRtcHost, this.config.SALTYRTC_PORT,
- persistent);
- }
- /**
- * Initialize the webclient service.
- */
- public init(keyStore?: saltyrtc.KeyStore, peerTrustedKey?: Uint8Array, resetFields = true): void {
- // Reset state
- this.stateService.reset();
- // Create WebRTC task instance
- const maxPacketSize = this.browserService.getBrowser().firefox ? 16384 : 65536;
- this.webrtcTask = new saltyrtcTaskWebrtc.WebRTCTask(true, maxPacketSize);
- // Create new keystore if necessary
- if (!keyStore) {
- keyStore = new saltyrtcClient.KeyStore();
- }
- // Determine SaltyRTC host
- if (this.config.SALTYRTC_HOST !== null) {
- // Static URL
- this.saltyRtcHost = this.config.SALTYRTC_HOST;
- } else {
- // Construct URL using prefix and suffix
- this.saltyRtcHost = this.config.SALTYRTC_HOST_PREFIX
- + keyStore.publicKeyHex.substr(0, 2)
- + this.config.SALTYRTC_HOST_SUFFIX;
- }
- // Create SaltyRTC client
- let builder = new saltyrtcClient.SaltyRTCBuilder()
- .connectTo(this.saltyRtcHost, this.config.SALTYRTC_PORT)
- .withServerKey(this.config.SALTYRTC_SERVER_KEY)
- .withKeyStore(keyStore)
- .usingTasks([this.webrtcTask])
- .withPingInterval(30);
- if (keyStore !== undefined && peerTrustedKey !== undefined) {
- builder = builder.withTrustedPeerKey(peerTrustedKey);
- }
- this.salty = builder.asInitiator();
- // We want to know about new responders.
- this.salty.on('new-responder', () => {
- if (!this.startupDone) {
- // Peer handshake
- this.stateService.updateConnectionBuildupState('peer_handshake');
- }
- });
- // We want to know about state changes
- this.salty.on('state-change', (ev: saltyrtc.SaltyRTCEvent) => {
- // Wrap this in a $timeout to execute at the end of the event loop.
- this.$timeout(() => {
- let state: saltyrtc.SignalingState = ev.data;
- if (!this.startupDone) {
- switch (state) {
- case 'new':
- case 'ws-connecting':
- case 'server-handshake':
- if (this.stateService.connectionBuildupState !== 'push'
- && this.stateService.connectionBuildupState !== 'manual_start') {
- this.stateService.updateConnectionBuildupState('connecting');
- }
- break;
- case 'peer-handshake':
- // Waiting for peer
- if (this.stateService.connectionBuildupState !== 'push'
- && this.stateService.connectionBuildupState !== 'manual_start') {
- this.stateService.updateConnectionBuildupState('waiting');
- }
- break;
- case 'task':
- // Do nothing, state will be updated once SecureDataChannel is open
- break;
- case 'closing':
- case 'closed':
- this.stateService.updateConnectionBuildupState('closed');
- break;
- default:
- this.$log.warn('Unknown signaling state:', state);
- }
- }
- this.stateService.updateSignalingConnectionState(state);
- }, 0);
- });
- // Once the connection is established, initiate the peer connection
- // and start the handover.
- this.salty.once('state-change:task', () => {
- // Firefox <53 does not yet support TLS. Skip it, to save allocations.
- const browser = this.browserService.getBrowser();
- if (browser.firefox && parseFloat(browser.version) < 53) {
- this.skipIceTls();
- }
- this.pcHelper = new PeerConnectionHelper(this.$log, this.$q, this.$timeout,
- this.$rootScope, this.webrtcTask,
- this.config.ICE_SERVERS);
- // On state changes in the PeerConnectionHelper class, let state service know about it
- this.pcHelper.onConnectionStateChange = (state: threema.RTCConnectionState) => {
- if (state === 'connected' && this.stateService.wasConnected) {
- // this happens if a lost connection could be restored
- // without reset the peer connection
- this._requestInitialData();
- }
- this.stateService.updateRtcConnectionState(state);
- };
- // Initiate handover
- this.webrtcTask.handover(this.pcHelper.peerConnection);
- });
- // Handle a disconnect request
- this.salty.on('application', (applicationData: any) => {
- if (applicationData.data.type === 'disconnect') {
- this.$log.debug('Disconnecting requested');
- const deleteStoredData = applicationData.data.forget === true;
- const resetPush = true;
- const redirect = true;
- this.stop(false, deleteStoredData, resetPush, redirect);
- }
- });
- // Wait for handover to be finished
- this.salty.on('handover', () => {
- this.$log.debug(this.logTag, 'Handover done');
- // Initialize NotificationService
- this.$log.debug(this.logTag, 'Initializing NotificationService...');
- this.notificationService.init();
- // Create secure data channel
- this.$log.debug(this.logTag, 'Create SecureDataChannel "' + WebClientService.DC_LABEL + '"...');
- this.secureDataChannel = this.pcHelper.createSecureDataChannel(
- WebClientService.DC_LABEL,
- (event: Event) => {
- this.$log.debug(this.logTag, 'SecureDataChannel open');
- // Initialize fields
- if (resetFields) {
- this._resetFields();
- }
- // Request initial data
- this._requestInitialData();
- // Fetch current version
- this.versionService.checkForUpdate();
- // Notify state service about data loading
- this.stateService.updateConnectionBuildupState('loading');
- },
- );
- // Handle incoming messages
- type RTCMessageEvent = (event: MessageEvent) => void;
- this.secureDataChannel.onmessage = (event: MessageEvent) => {
- this.$log.debug('New incoming message (' + event.data.byteLength + ' bytes)');
- // Decode bytes
- const bytes = new Uint8Array(event.data);
- const message: threema.WireMessage = this.msgpackDecode(bytes);
- // Validate message to keep contract defined by `threema.WireMessage` type
- if (message.type === undefined) {
- this.$log.warn('Ignoring invalid message (no type attribute)');
- return;
- } else if (message.subType === undefined) {
- this.$log.warn('Ignoring invalid message (no subType attribute)');
- return;
- }
- // Process data
- this.$rootScope.$apply(() => {
- this.receive(message);
- });
- };
- this.secureDataChannel.onbufferedamountlow = (e: Event) => {
- this.$log.debug('Secure data channel: Buffered amount low');
- };
- this.secureDataChannel.onerror = (e: ErrorEvent) => {
- this.$log.warn('Secure data channel: Error:', e.message);
- this.$log.debug(e);
- };
- this.secureDataChannel.onclose = (e: Event) => {
- this.$log.warn('Secure data channel: Closed');
- };
- });
- // Handle SaltyRTC errors
- this.salty.on('connection-error', (ev) => {
- this.$log.error('Connection error:', ev);
- });
- this.salty.on('connection-closed', (ev) => {
- this.$log.warn('Connection closed:', ev);
- });
- }
- /**
- * Start the webclient service.
- * Return a promise that resolves once connected.
- */
- public start(): ng.IPromise<any> {
- this.$log.debug('Starting WebClientService...');
- // Promise to track startup state
- this.startupPromise = this.$q.defer();
- this.startupDone = false;
- // Connect
- this.salty.connect();
- // If push service is available, notify app
- if (this.pushService.isAvailable()) {
- this.pushService.sendPush(this.salty.permanentKeyBytes)
- .catch(() => this.$log.warn('Could not notify app!'))
- .then(() => {
- this.$log.debug('Requested app wakeup');
- this.$rootScope.$apply(() => {
- this.stateService.updateConnectionBuildupState('push');
- });
- });
- } else if (this.trustedKeyStore.hasTrustedKey()) {
- this.$log.debug('Push service not available');
- this.stateService.updateConnectionBuildupState('manual_start');
- }
- return this.startupPromise.promise;
- }
- /**
- * Stop the webclient service.
- *
- * This is a forced stop, meaning that all channels are closed.
- *
- * Parameters:
- *
- * - `requestedByUs`: Set this to `false` if the app requested to close the session.
- * - `deleteStoredData`: Whether to clear any trusted key or push token from the keystore.
- * - `resetPush`: Whether to reset the push service.
- * - `redirect`: Whether to redirect to the welcome page.
- */
- public stop(requestedByUs: boolean,
- deleteStoredData: boolean = false,
- resetPush: boolean = true,
- redirect: boolean = false): void {
- this.$log.info('Disconnecting...');
- if (requestedByUs && this.stateService.rtcConnectionState === 'connected') {
- // Ask peer to disconnect too
- this.salty.sendApplicationMessage({type: 'disconnect', forget: deleteStoredData});
- }
- this.stateService.reset();
- // Reset the unread count
- this.resetUnreadCount();
- // Clear stored data (trusted key, push token, etc)
- if (deleteStoredData === true) {
- this.trustedKeyStore.clearTrustedKey();
- }
- // Clear push token
- if (resetPush === true) {
- this.pushService.reset();
- }
- // Close data channel
- if (this.secureDataChannel !== null) {
- this.$log.debug('Closing secure datachannel');
- this.secureDataChannel.close();
- }
- // Close SaltyRTC connection
- if (this.salty !== null) {
- this.$log.debug('Closing signaling');
- this.salty.disconnect();
- }
- // Function to redirect to welcome screen
- const redirectToWelcome = () => {
- if (redirect === true) {
- this.$timeout(() => {
- this.$state.go('welcome');
- }, 0);
- }
- };
- // Close peer connection
- if (this.pcHelper !== null) {
- this.$log.debug(this.logTag, 'Closing peer connection');
- this.pcHelper.close()
- .then(
- () => this.$log.debug(this.logTag, 'Peer connection was closed'),
- (reason: string) => this.$log.warn(this.logTag, 'Peer connection could not be closed:', reason),
- )
- .finally(() => redirectToWelcome());
- } else {
- this.$log.debug(this.logTag, 'Peer connection was null');
- redirectToWelcome();
- }
- }
- /**
- * Remove "turns:" servers from the ICE_SERVERS configuration
- * if at least one "turn:" server with tcp transport is in the list.
- */
- public skipIceTls(): void {
- this.$log.debug(this.logTag, 'Requested to remove TURNS server from ICE configuration');
- const allUrls = [].concat(...this.config.ICE_SERVERS.map((conf) => conf.urls));
- if (allUrls.some((url) => url.startsWith('turn:') && url.endsWith('=tcp'))) {
- // There's at least one TURN server with TCP transport in the list
- for (let server of this.config.ICE_SERVERS) {
- // Remove TLS entries
- server.urls = server.urls.filter((url) => !url.startsWith('turns:'));
- }
- } else {
- this.$log.debug(this.logTag, 'No fallback TURN TCP server present, keeping TURNS server');
- }
- }
- /**
- * Mark a component as initialized
- */
- public registerInitializationStep(name: threema.InitializationStep) {
- if (this.initialized.has(name) ) {
- this.$log.warn(this.logTag, 'initialization step', name, 'already registered');
- return;
- }
- this.$log.debug(this.logTag, 'Initialization step', name, 'done');
- this.initialized.add(name);
- // check pending routines
- this.pendingInitializationStepRoutines = this.pendingInitializationStepRoutines.filter((routine) => {
- let isValid = true;
- for (let requiredStep of routine.requiredSteps) {
- if (!this.initialized.has(requiredStep)) {
- isValid = false;
- break;
- }
- }
- if (isValid) {
- this.$log.log('run routine after initialisation', name, ' completed');
- routine.callback();
- }
- return !isValid;
- });
- if (this.initialized.size >= this.initializedThreshold) {
- this.stateService.updateConnectionBuildupState('done');
- this.startupPromise.resolve();
- this.startupPromise = null;
- this.startupDone = true;
- this._resetInitializationSteps();
- }
- }
- public setReceiverListener(listener: threema.ReceiverListener): void {
- this.receiverListener.push(listener);
- }
- /**
- * Send all client info request.
- */
- public requestClientInfo(): void {
- this.$log.debug('Sending client info request');
- this._sendRequest(WebClientService.SUB_TYPE_CLIENT_INFO, {
- browser: navigator.userAgent,
- });
- }
- /**
- * Send a receiver request.
- */
- public requestReceivers(): void {
- this.$log.debug('Sending receiver request');
- this._sendRequest(WebClientService.SUB_TYPE_RECEIVER);
- }
- /**
- * Send a conversation request.
- */
- public requestConversations(): void {
- this.$log.debug('Sending conversation request');
- this._sendRequest(WebClientService.SUB_TYPE_CONVERSATIONS, {
- [WebClientService.ARGUMENT_MAX_SIZE]: WebClientService.AVATAR_LOW_MAX_SIZE,
- });
- }
- /**
- * Send a message request for the specified receiver.
- *
- * This method will only be called when initializing a conversation in the
- * webclient. It is used to download all existing messages.
- *
- * New messages are not requested this way, instead they are sent as a
- * message update.
- */
- public requestMessages(receiver: threema.Receiver): number {
- // If there are no more messages available, stop here.
- if (!this.messages.hasMore(receiver)) {
- this.messages.notify(receiver, this.$rootScope);
- return null;
- }
- this.loadingMessages.set(receiver.type + receiver.id, true);
- // Check if messages have already been requested
- if (this.messages.isRequested(receiver)) {
- return null;
- }
- // Get the reference msg id
- const refMsgId = this.messages.getReferenceMsgId(receiver);
- // Set requested
- // TODO: Add timeout to reset flag
- this.messages.setRequested(receiver);
- // Create arguments
- const args = {
- [WebClientService.ARGUMENT_RECEIVER_TYPE]: receiver.type,
- [WebClientService.ARGUMENT_RECEIVER_ID]: receiver.id,
- } as any;
- // If a reference msg id has been set, send it along
- const msgId = this.messages.getReferenceMsgId(receiver);
- if (msgId !== null) {
- args[WebClientService.ARGUMENT_REFERENCE_MSG_ID] = msgId;
- }
- // Send request
- this.$log.debug('Sending message request for', receiver.type, receiver.id,
- 'with message id', msgId);
- this._sendRequest(WebClientService.SUB_TYPE_MESSAGE, args);
- return refMsgId;
- }
- /**
- * Send an avatar request for the specified receiver.
- */
- public requestAvatar(receiver: threema.Receiver, highResolution: boolean): Promise<any> {
- // Check if the receiver has an avatar or the avatar already exists
- let resolution = highResolution ? 'high' : 'low';
- let receiverInfo = this.receivers.getData(receiver);
- if (receiverInfo && receiverInfo.avatar && receiverInfo.avatar[resolution]) {
- // Avatar already exists
- // TODO: Do we get avatar changes via update?
- return new Promise<any>((e) => {
- e(receiverInfo.avatar[resolution]);
- });
- }
- // Create arguments and send request
- let args = {
- [WebClientService.ARGUMENT_RECEIVER_TYPE]: receiver.type,
- [WebClientService.ARGUMENT_RECEIVER_ID]: receiver.id,
- [WebClientService.ARGUMENT_AVATAR_HIGH_RESOLUTION]: highResolution,
- } as any;
- if (!highResolution) {
- args[WebClientService.ARGUMENT_MAX_SIZE] = WebClientService.AVATAR_LOW_MAX_SIZE;
- }
- this.$log.debug('Sending', resolution, 'res avatar request for', receiver.type, receiver.id);
- return this._sendRequestPromise(WebClientService.SUB_TYPE_AVATAR, args, 10000);
- }
- /**
- * Send an thumbnail request for the specified receiver.
- */
- public requestThumbnail(receiver: threema.Receiver, message: threema.Message): Promise<any> {
- // Check if the receiver has an avatar or the avatar already exists
- if (message.thumbnail !== undefined && message.thumbnail.img !== undefined) {
- return new Promise<any>((e) => {
- e(message.thumbnail.img);
- });
- }
- // Create arguments and send request
- const args = {
- [WebClientService.ARGUMENT_MESSAGE_ID]: message.id,
- [WebClientService.ARGUMENT_RECEIVER_TYPE]: receiver.type,
- [WebClientService.ARGUMENT_RECEIVER_ID]: receiver.id,
- };
- this.$log.debug('Sending', 'thumbnail request for', receiver.type, message.id);
- return this._sendRequestPromise(WebClientService.SUB_TYPE_THUMBNAIL, args, 10000);
- }
- /**
- * Request a blob.
- */
- public requestBlob(msgId: number, receiver: threema.Receiver): Promise<ArrayBuffer> {
- let cached = this.blobCache.get(msgId + receiver.type);
- if (cached !== undefined) {
- this.$log.debug('Use cached blob');
- return new Promise((resolve) => {
- resolve(cached);
- });
- }
- const args = {
- [WebClientService.ARGUMENT_RECEIVER_TYPE]: receiver.type,
- [WebClientService.ARGUMENT_RECEIVER_ID]: receiver.id,
- [WebClientService.ARGUMENT_MESSAGE_ID]: msgId,
- };
- this.$log.debug('Sending blob request for message', msgId);
- return this._sendRequestPromise(WebClientService.SUB_TYPE_BLOB, args);
- }
- /**
- */
- public requestRead(receiver, newestMessageId: number): void {
- // Check if the receiver has an avatar or the avatar already exists
- // let field: string = highResolution ? 'high' : 'low';
- // let data = this.receivers.getData(receiver);
- // if (data && data['avatar'] && data['avatar'][field]) {
- // return;
- // }
- // if (data && data.hasAvatar === false) {
- // return;
- // }
- // Create arguments and send request
- let args = {
- [WebClientService.ARGUMENT_RECEIVER_TYPE]: receiver.type,
- [WebClientService.ARGUMENT_RECEIVER_ID]: receiver.id,
- [WebClientService.ARGUMENT_MESSAGE_ID]: newestMessageId,
- };
- this.$log.debug('Sending read request for', receiver.type, receiver.id, '(msg ' + newestMessageId + ')');
- this._sendRequest(WebClientService.SUB_TYPE_READ, args);
- }
- public requestContactDetail(contactReceiver: threema.ContactReceiver): Promise<any> {
- return this._sendRequestPromise(WebClientService.SUB_TYPE_CONTACT_DETAIL, {
- [WebClientService.ARGUMENT_IDENTITY]: contactReceiver.id,
- });
- }
- /**
- * Send a message to the specified receiver.
- */
- public sendMessage(receiver,
- type: threema.MessageContentType,
- message: threema.MessageData): Promise<Promise<any>> {
- return new Promise<any> (
- (resolve, reject) => {
- // Decide on subtype
- let subType;
- switch (type) {
- case 'text':
- subType = WebClientService.SUB_TYPE_TEXT_MESSAGE;
- // Ignore empty text messages
- let textSize = (message as threema.TextMessageData).text.length;
- if (textSize === 0) {
- return reject();
- } else if (textSize > WebClientService.MAX_TEXT_LENGTH) {
- return reject(this.$translate.instant('error.TEXT_TOO_LONG', {
- max: WebClientService.MAX_TEXT_LENGTH,
- }));
- }
- break;
- case 'file':
- // validate max file size
- if ((message as threema.FileMessageData).size > WebClientService.MAX_FILE_SIZE) {
- return reject(this.$translate.instant('error.FILE_TOO_LARGE'));
- }
- // Determine required feature level
- let requiredFeatureLevel = 3;
- let invalidFeatureLevelMessage = 'error.FILE_MESSAGES_NOT_SUPPORTED';
- if ((message as threema.FileMessageData).sendAsFile !== true) {
- // check mime type
- let mime = (message as threema.FileMessageData).fileType;
- if (this.mimeService.isAudio(mime)) {
- requiredFeatureLevel = 1;
- invalidFeatureLevelMessage = 'error.AUDIO_MESSAGES_NOT_SUPPORTED';
- } else if (this.mimeService.isImage(mime)
- || this.mimeService.isVideo(mime)) {
- requiredFeatureLevel = 0;
- invalidFeatureLevelMessage = 'error.MESSAGE_NOT_SUPPORTED';
- }
- }
- subType = WebClientService.SUB_TYPE_FILE_MESSAGE;
- // check receiver
- switch (receiver.type) {
- case 'distributionList':
- return reject(this.$translate.instant(invalidFeatureLevelMessage, {
- receiverName: receiver.displayName}));
- case 'group':
- let unsupportedMembers = [];
- let group = this.groups.get(receiver.id);
- if (group === undefined) {
- return reject();
- }
- group.members.forEach((identity: string) => {
- if (identity !== this.me.id) {
- // ignore "me"
- let contact = this.contacts.get(identity);
- if (contact !== undefined && contact.featureLevel < requiredFeatureLevel) {
- unsupportedMembers.push(contact.displayName);
- }
- }
- });
- if (unsupportedMembers.length > 0) {
- return reject(this.$translate.instant(invalidFeatureLevelMessage, {
- receiverName: unsupportedMembers.join(',')}));
- }
- break;
- case 'contact':
- const contact = this.contacts.get(receiver.id);
- if (contact === undefined) {
- this.$log.error('Cannot retrieve contact');
- return reject(this.$translate.instant('error.ERROR_OCCURRED'));
- } else if (contact.featureLevel < requiredFeatureLevel) {
- this.$log.debug('Cannot send message: Feature level mismatch:',
- contact.featureLevel, '<', requiredFeatureLevel);
- return reject(this.$translate.instant(invalidFeatureLevelMessage, {
- receiverName: contact.displayName}));
- }
- break;
- default:
- return reject();
- }
- break;
- default:
- this.$log.warn('Invalid message type:', type);
- return reject();
- }
- let temporaryMessage = this.messageService.createTemporary(receiver, type, message);
- this.messages.addNewer(receiver, [temporaryMessage]);
- let args = {
- [WebClientService.ARGUMENT_RECEIVER_TYPE]: receiver.type,
- [WebClientService.ARGUMENT_RECEIVER_ID]: receiver.id,
- [WebClientService.ARGUMENT_TEMPORARY_ID]: temporaryMessage.temporaryId,
- };
- // send message and handling error promise
- this._sendCreatePromise(subType, args, message)
- .catch((error) => {
- this.$log.error('error sending message', error);
- // remove temporary message
- this.messages.removeTemporary(receiver, temporaryMessage.temporaryId);
- let errorMessage;
- switch (error) {
- case 'file_too_large':
- errorMessage = this.$translate.instant('error.FILE_TOO_LARGE');
- break;
- default:
- errorMessage = this.$translate.instant('error.ERROR_OCCURRED');
- }
- this.alerts.push({
- source: 'sendMessage',
- type: 'alert',
- message: errorMessage,
- } as threema.Alert);
- });
- resolve();
- });
- }
- /**
- * Send a message a ack/decline message
- */
- public ackMessage(receiver, message: threema.Message, acknowledged: boolean = true): void {
- // Ignore empty text messages
- // TODO check into a util class
- if (message === undefined
- || message.isOutbox) {
- return;
- }
- let args = {
- [WebClientService.ARGUMENT_RECEIVER_TYPE]: receiver.type,
- [WebClientService.ARGUMENT_RECEIVER_ID]: receiver.id,
- [WebClientService.ARGUMENT_MESSAGE_ID]: message.id,
- [WebClientService.ARGUMENT_MESSAGE_ACKNOWLEDGED]: acknowledged,
- };
- this._sendRequest(WebClientService.SUB_TYPE_ACK, args);
- }
- /**
- * Send a message a ack/decline message
- */
- public deleteMessage(receiver, message: threema.Message): void {
- // Ignore empty text messages
- if (message === undefined) {
- return;
- }
- let args = {
- [WebClientService.ARGUMENT_RECEIVER_TYPE]: receiver.type,
- [WebClientService.ARGUMENT_RECEIVER_ID]: receiver.id,
- [WebClientService.ARGUMENT_MESSAGE_ID]: message.id,
- };
- this._sendDelete(WebClientService.SUB_TYPE_MESSAGE, args);
- }
- public sendMeIsTyping(receiver, isTyping: boolean): void {
- // Create arguments and send create
- let args = {
- [WebClientService.ARGUMENT_RECEIVER_TYPE]: receiver.type,
- [WebClientService.ARGUMENT_RECEIVER_ID]: receiver.id,
- [WebClientService.ARGUMENT_CONTACT_IS_TYPING]: isTyping,
- };
- this._sendRequest(WebClientService.SUB_TYPE_TYPING, args);
- }
- public sendKeyPersisted(): void {
- this._sendRequest(WebClientService.SUB_TYPE_KEY_PERSISTED);
- }
- /**
- * Send a add Contact request
- */
- public addContact(threemaId: String): Promise<threema.ContactReceiver> {
- return this._sendCreatePromise(WebClientService.SUB_TYPE_CONTACT, {
- [WebClientService.ARGUMENT_IDENTITY]: threemaId,
- });
- }
- /**
- * Modify a contact name or a avatar
- */
- public modifyContact(threemaId: String,
- firstName: String,
- lastName: String,
- avatar?: ArrayBuffer): Promise<threema.ContactReceiver> {
- let promise = this._sendUpdatePromise(WebClientService.SUB_TYPE_CONTACT, {
- [WebClientService.ARGUMENT_IDENTITY]: threemaId,
- }, {
- [WebClientService.ARGUMENT_FIRST_NAME]: firstName,
- [WebClientService.ARGUMENT_LAST_NAME]: lastName,
- [WebClientService.ARGUMENT_AVATAR_HIGH_RESOLUTION]: avatar,
- });
- if (avatar !== undefined) {
- // reset avatar to force a avatar reload
- this.receivers.getData({
- type: 'contact',
- id: threemaId,
- } as threema.Receiver).avatar = {};
- }
- return promise;
- }
- /**
- * Create a group receiver
- */
- public createGroup(members: String[],
- name: String = null,
- avatar?: ArrayBuffer): Promise<threema.GroupReceiver> {
- let data = {
- [WebClientService.ARGUMENT_MEMBERS]: members,
- [WebClientService.ARGUMENT_NAME]: name,
- } as any;
- if (avatar !== undefined) {
- data[WebClientService.ARGUMENT_AVATAR_HIGH_RESOLUTION] = avatar;
- }
- return this._sendCreatePromise(WebClientService.SUB_TYPE_GROUP, data);
- }
- public modifyGroup(id: string,
- members: String[],
- name: String = null,
- avatar?: ArrayBuffer): Promise<threema.GroupReceiver> {
- let data = {
- [WebClientService.ARGUMENT_MEMBERS]: members,
- [WebClientService.ARGUMENT_NAME]: name,
- } as any;
- if (avatar !== undefined) {
- data[WebClientService.ARGUMENT_AVATAR_HIGH_RESOLUTION] = avatar;
- }
- let promise = this._sendUpdatePromise(WebClientService.SUB_TYPE_GROUP, {
- [WebClientService.ARGUMENT_RECEIVER_ID]: id,
- }, data);
- if (avatar !== undefined) {
- // reset avatar to force a avatar reload
- this.receivers.getData({
- type: 'group',
- id: id,
- } as threema.GroupReceiver).avatar = {};
- }
- return promise;
- }
- public leaveGroup(group: threema.GroupReceiver): Promise<any> {
- if (group === undefined || !group.access.canLeave) {
- return new Promise((resolve, reject) => reject('not allowed'));
- }
- let args = {
- [WebClientService.ARGUMENT_RECEIVER_ID]: group.id,
- // TODO: delete type into const
- [WebClientService.ARGUMENT_DELETE_TYPE]: 'leave',
- };
- return this._sendDeletePromise(WebClientService.SUB_TYPE_GROUP, args);
- }
- public deleteGroup(group: threema.GroupReceiver): Promise<any> {
- if (group === undefined || !group.access.canDelete) {
- return new Promise<any> (
- (resolve, reject) => {
- reject('not allowed');
- });
- }
- let args = {
- [WebClientService.ARGUMENT_RECEIVER_ID]: group.id,
- // TODO: delete type into const
- [WebClientService.ARGUMENT_DELETE_TYPE]: 'delete',
- };
- return this._sendDeletePromise(WebClientService.SUB_TYPE_GROUP, args);
- }
- public syncGroup(group: threema.GroupReceiver): Promise<any> {
- if (group === undefined || !group.access.canSync) {
- return new Promise<any> (
- (resolve, reject) => {
- reject('not allowed');
- });
- }
- let args = {
- [WebClientService.ARGUMENT_RECEIVER_ID]: group.id,
- };
- return this._sendRequestPromise(WebClientService.SUB_TYPE_GROUP_SYNC, args);
- }
- public createDistributionList(members: String[], name: String = null): Promise<threema.DistributionListReceiver> {
- let data = {
- [WebClientService.ARGUMENT_MEMBERS]: members,
- [WebClientService.ARGUMENT_NAME]: name,
- } as any;
- return this._sendCreatePromise(WebClientService.SUB_TYPE_DISTRIBUTION_LIST, data);
- }
- public modifyDistributionList(id: string,
- members: String[],
- name: String = null): Promise<threema.DistributionListReceiver> {
- let data = {
- [WebClientService.ARGUMENT_MEMBERS]: members,
- [WebClientService.ARGUMENT_NAME]: name,
- } as any;
- return this._sendUpdatePromise(WebClientService.SUB_TYPE_DISTRIBUTION_LIST, {
- [WebClientService.ARGUMENT_RECEIVER_ID]: id,
- }, data);
- }
- public deleteDistributionList(distributionList: threema.DistributionListReceiver): Promise<any> {
- if (distributionList === undefined || !distributionList.access.canDelete) {
- return new Promise((resolve, reject) => reject('not allowed'));
- }
- let args = {
- [WebClientService.ARGUMENT_RECEIVER_ID]: distributionList.id,
- };
- return this._sendDeletePromise(WebClientService.SUB_TYPE_DISTRIBUTION_LIST, args);
- }
- /**
- * Return whether the specified contact is currently typing.
- *
- * This always returns false for groups and distribution lists.
- */
- public isTyping(receiver: threema.ContactReceiver): boolean {
- return this.typing.isTyping(receiver);
- }
- /**
- * Return own identity.
- */
- public getMyIdentity(): threema.Identity {
- return this.myIdentity;
- }
- /**
- * Return the curring quoted message model
- */
- public getQuote(receiver: threema.Receiver): threema.Quote {
- return this.drafts.getQuote(receiver);
- }
- /**
- * Set or remove (if message is null) a quoted message model.
- */
- public setQuote(receiver: threema.Receiver, message: threema.Message = null): void {
- if (message === null) {
- this.drafts.removeQuote(receiver);
- } else {
- let quoteText;
- switch (message.type) {
- case 'text':
- quoteText = message.body;
- break;
- case 'location':
- quoteText = message.location.poi;
- break;
- case 'file':
- case 'image':
- quoteText = message.caption;
- break;
- default:
- // Ignore (handled below)
- }
- if (quoteText !== undefined) {
- this.drafts.setQuote(receiver, {
- identity: message.isOutbox ? this.me.id : message.partnerId,
- text: quoteText,
- });
- }
- }
- }
- /**
- * Set or remove (if string is null) a draft message
- */
- public setDraft(receiver: threema.Receiver, message: string = null): void {
- if (message === null || message.trim().length === 0) {
- this.drafts.removeText(receiver);
- } else {
- this.drafts.setText(receiver, message.trim());
- }
- }
- /**
- * return draft text
- */
- public getDraft(receiver: threema.Receiver): string {
- return this.drafts.getText(receiver);
- }
- /**
- * Reset data related to initialization.
- */
- private _resetInitializationSteps(): void {
- this.$log.debug(this.logTag, 'Reset initialization steps');
- this.initialized.clear();
- this.pendingInitializationStepRoutines = [];
- }
- /**
- * Reset data fields.
- */
- private _resetFields(): void {
- // Reset initialization data
- this._resetInitializationSteps();
- // Create container instances
- this.receivers = this.container.createReceivers();
- this.conversations = this.container.createConversations();
- this.messages = this.container.createMessages();
- this.typingInstance = this.container.createTyping();
- // Add converters (pre-processors)
- this.messages.converter = this.container.Converter.unicodeToEmoji;
- this.conversations.setConverter(this.container.Converter.addReceiverToConversation(this.receivers));
- // Add filters
- this.conversations.setFilter(this.container.Filters.hasData(this.receivers));
- }
- private _requestInitialData(): void {
- // if all conversations are reloaded, clear the message cache
- // to get in sync (we dont know if a message was removed, updated etc..)
- this.messages.clear(this.$rootScope);
- // Request initial data
- this.requestClientInfo();
- this.requestReceivers();
- this.requestConversations();
- }
- private _receiveResponseReceivers(message: threema.WireMessage) {
- this.$log.debug('Received receiver response');
- // Unpack and validate data
- const data = message.data;
- if (data === undefined) {
- this.$log.warn('Invalid receiver response, data missing');
- return;
- }
- // Store receivers
- this.receivers.set(data);
- this.registerInitializationStep('receivers');
- }
- private _receiveResponseContactDetail(message: threema.WireMessage): any {
- this.$log.debug('Received contact detail');
- // Unpack and validate data
- const data = message.data;
- if (data === undefined) {
- this.$log.warn('Invalid contact response, data missing');
- return;
- }
- if (data[WebClientService.ARGUMENT_SUCCESS]) {
- let contactReceiver = this.receivers.contacts
- .get(message.args[WebClientService.ARGUMENT_IDENTITY]) as threema.ContactReceiver;
- // get system contact
- if (data[WebClientService.SUB_TYPE_RECEIVER]) {
- contactReceiver.systemContact =
- data[WebClientService.SUB_TYPE_RECEIVER][WebClientService.ARGUMENT_SYSTEM_CONTACT];
- }
- return {
- success: true,
- contactReceiver: contactReceiver,
- };
- }
- return data;
- }
- private _receiveAlert(message: threema.WireMessage): any {
- this.$log.debug('Received alert from device');
- this.alerts.push({
- source: message.args.source,
- type: message.data.type,
- message: message.data.message,
- } as threema.Alert);
- }
- private _receiveGroupSync(message: threema.WireMessage): any {
- this.$log.debug('Received group sync');
- // to finish the promise
- return {
- success: true,
- data: null,
- };
- }
- /**
- * handling new or modified contact
- */
- // tslint:disable-next-line: max-line-length
- private _receiveResponseContact(message: threema.WireMessage): threema.PromiseRequestResult<threema.ContactReceiver> {
- this.$log.debug('Received contact response');
- // Unpack and validate data
- const data = message.data;
- if (data === undefined) {
- this.$log.warn('Invalid add contact response, data missing');
- return;
- }
- if (data[WebClientService.ARGUMENT_SUCCESS]
- && data[WebClientService.SUB_TYPE_RECEIVER] !== undefined) {
- let receiver = data[WebClientService.SUB_TYPE_RECEIVER] as threema.ContactReceiver;
- // Add or update a certain receiver
- if (receiver.type === undefined) {
- receiver.type = 'contact';
- }
- this.receivers.extendContact(receiver);
- return {
- success: true,
- data: receiver,
- };
- }
- let msg = null;
- if (data[WebClientService.ARGUMENT_ERROR] !== undefined) {
- msg = data[WebClientService.ARGUMENT_ERROR];
- }
- return {
- success: false,
- message: msg,
- };
- }
- /**
- * handling new or modified group
- */
- private _receiveResponseGroup(message: threema.WireMessage): threema.PromiseRequestResult<threema.GroupReceiver> {
- this.$log.debug('Received group response');
- // Unpack and validate data
- const data = message.data;
- if (data === undefined) {
- this.$log.warn('Invalid create group response, data missing');
- return;
- }
- if (data[WebClientService.ARGUMENT_SUCCESS]
- && data[WebClientService.SUB_TYPE_RECEIVER] !== undefined) {
- let receiver = data[WebClientService.SUB_TYPE_RECEIVER] as threema.GroupReceiver;
- // Add or update a certain receiver
- if (receiver.type === undefined) {
- receiver.type = 'group';
- }
- this.receivers.extendGroup(receiver);
- return {
- success: true,
- data: receiver,
- };
- }
- let msg = null;
- if (data[WebClientService.ARGUMENT_ERROR] !== undefined) {
- msg = data[WebClientService.ARGUMENT_ERROR];
- }
- return {
- success: false,
- message: msg,
- };
- }
- /**
- * Handling new or modified group
- */
- // tslint:disable-next-line: max-line-length
- private _receiveResponseDistributionList(message: threema.WireMessage): threema.PromiseRequestResult<threema.DistributionListReceiver> {
- this.$log.debug('Received distribution list response');
- // Unpack and validate data
- const data = message.data;
- if (data === undefined) {
- this.$log.warn('Invalid distribution list response, data missing');
- return;
- }
- if (data[WebClientService.ARGUMENT_SUCCESS]
- && data[WebClientService.SUB_TYPE_RECEIVER] !== undefined) {
- let receiver = data[WebClientService.SUB_TYPE_RECEIVER] as threema.DistributionListReceiver;
- // Add or update a certain receiver
- if (receiver.type === undefined) {
- receiver.type = 'distributionList';
- }
- this.receivers.extendDistributionList(receiver);
- return {
- success: true,
- data: receiver,
- };
- }
- let msg = null;
- if (data[WebClientService.ARGUMENT_MESSAGE] !== undefined) {
- msg = data[WebClientService.ARGUMENT_MESSAGE];
- }
- return {
- success: false,
- message: msg,
- };
- }
- private _receiveResponseCreateMessage(message: threema.WireMessage):
- threema.PromiseRequestResult<String> {
- this.$log.debug('Received create message response');
- // Unpack data and arguments
- const args = message.args;
- const data = message.data;
- if (args === undefined
- || data === undefined) {
- this.$log.warn('Invalid create received, arguments or data missing');
- return;
- }
- const receiverType = args[WebClientService.ARGUMENT_RECEIVER_TYPE];
- const receiverId = args[WebClientService.ARGUMENT_RECEIVER_ID];
- const temporaryId = args[WebClientService.ARGUMENT_TEMPORARY_ID];
- if (data[WebClientService.ARGUMENT_SUCCESS]) {
- const messageId = data[WebClientService.ARGUMENT_MESSAGE_ID];
- if (receiverType === undefined || receiverId === undefined ||
- temporaryId === undefined || messageId === undefined) {
- this.$log.warn('Invalid create received [type, id or temporaryId arg ' +
- 'or messageId in data missing]');
- return;
- }
- this.messages.bindTemporaryToMessageId({
- type: receiverType,
- id: receiverId,
- } as threema.Receiver, temporaryId, messageId);
- return {
- success: true,
- data: messageId,
- };
- }
- return {
- success: false,
- message: data[WebClientService.ARGUMENT_ERROR],
- };
- }
- private _receiveResponseConversations(message: threema.WireMessage) {
- this.$log.debug('Received conversations response');
- let data = message.data;
- if (data === undefined) {
- this.$log.warn('Invalid conversation response, data missing');
- } else {
- // if a avatar was set on a conversation
- // convert and copy to the receiver
- for (let conversation of data) {
- if (conversation.avatar !== undefined) {
- let receiver = this.receivers.getData({
- id: conversation.id,
- type: conversation.type,
- } as threema.Receiver);
- if (receiver !== undefined
- && receiver.avatar === undefined) {
- receiver.avatar = {
- low: this.$filter('bufferToUrl')(conversation.avatar, 'image/png'),
- };
- }
- // reset avatar from object
- delete conversation.avatar;
- }
- this.conversations.updateOrAdd(conversation);
- }
- }
- this.updateUnreadCount();
- this.registerInitializationStep('conversations');
- }
- private _receiveResponseConversation(message: threema.WireMessage) {
- this.$log.debug('Received conversation response');
- let args = message.args;
- let data = message.data;
- if (args === undefined || data === undefined) {
- this.$log.warn('Invalid conversation response, data or arguments missing');
- return;
- }
- // Unpack required argument fields
- let type: string = args[WebClientService.ARGUMENT_MODE];
- switch (type) {
- case WebClientService.ARGUMENT_MODE_NEW:
- this.conversations.add(data);
- break;
- case WebClientService.ARGUMENT_MODE_MODIFIED:
- // A conversation update *can* mean that a new message arrived.
- // To find out, we'll look at the unread count. If it has been
- // incremented, it must be a new message.
- if (data.unreadCount > 0) {
- // Find the correct conversation in the conversation list
- for (let conversation of this.conversations.get()) {
- if (this.receiverService.compare(conversation, data)) {
- if (data.unreadCount > conversation.unreadCount) {
- // This is our conversation! If the unreadcount
- // has increased, we received a new message.
- this.onNewMessage(data.latestMessage, conversation);
- } else if (data.unreadCount < conversation.unreadCount) {
- // Otherwise, if it has decreased, hide the notification.
- this.notificationService.hideNotification(data.type + '-' + data.id);
- }
- break;
- }
- }
- } else {
- this.notificationService.hideNotification(data.type + '-' + data.id);
- }
- // we have to call update or add, we are not sure if this
- // conversation is already fetched
- this.conversations.updateOrAdd(data);
- break;
- case WebClientService.ARGUMENT_MODE_REMOVED:
- this.conversations.remove(data);
- this.receiverListener.forEach((listener: threema.ReceiverListener) => {
- this.$log.debug('call on removed listener');
- listener.onRemoved(data);
- });
- break;
- default:
- this.$log.warn('Received conversation without a mode');
- return;
- }
- this.updateUnreadCount();
- }
- private _receiveResponseMessages(message: threema.WireMessage): void {
- this.$log.debug('Received message response');
- // Unpack data and arguments
- const args = message.args;
- const data = message.data as threema.Message[];
- if (args === undefined || data === undefined) {
- this.$log.warn('Invalid message response, data or arguments missing');
- return;
- }
- // Unpack required argument fields
- const type: string = args[WebClientService.ARGUMENT_RECEIVER_TYPE];
- const id: string = args[WebClientService.ARGUMENT_RECEIVER_ID];
- let more: boolean = args[WebClientService.ARGUMENT_HAS_MORE];
- if (type === undefined || id === undefined || more === undefined) {
- this.$log.warn('Invalid message response, argument field missing');
- return;
- }
- // If there's no data returned, override `more` field.
- if (data.length === 0) {
- more = false;
- }
- // Check if the page was requested
- let receiver = {type: type, id: id} as threema.Receiver;
- // Set as loaded
- this.loadingMessages.delete(receiver.type + receiver.id);
- if (this.messages.isRequested(receiver)) {
- // Add messages
- this.messages.addOlder(receiver, data);
- // Clear pending request
- this.messages.clearRequested(receiver);
- // Set "more" flag to indicate that more (older) messages are available.
- this.messages.setMore(receiver, more);
- this.messages.notify(receiver, this.$rootScope);
- } else {
- this.$log.warn("Ignoring message response that hasn't been requested");
- return;
- }
- }
- private _receiveResponseAvatar(message: threema.WireMessage): any {
- this.$log.debug('Received avatar response');
- // Unpack data and arguments
- const args = message.args;
- if (args === undefined) {
- this.$log.warn('Invalid message response: arguments missing');
- return {
- success: false,
- data: 'invalid_response',
- };
- }
- const data = message.data;
- if ( data === undefined) {
- // It's ok, a receiver without a avatar
- return {
- success: true,
- data: null,
- };
- }
- // Unpack required argument fields
- const type = args[WebClientService.ARGUMENT_RECEIVER_TYPE];
- const id = args[WebClientService.ARGUMENT_RECEIVER_ID];
- const highResolution = args[WebClientService.ARGUMENT_AVATAR_HIGH_RESOLUTION];
- if (type === undefined || id === undefined || highResolution === undefined) {
- this.$log.warn('Invalid avatar response, argument field missing');
- return {
- success: false,
- data: 'invalid_response',
- };
- }
- // Set avatar for receiver according to resolution
- let field: string = highResolution ? 'high' : 'low';
- let receiverData = this.receivers.getData(args);
- if (receiverData.avatar === null || receiverData.avatar === undefined) {
- receiverData.avatar = {};
- }
- let avatar = this.$filter('bufferToUrl')(data, 'image/png');
- receiverData.avatar[field] = avatar;
- return {
- success: true,
- data: avatar,
- };
- }
- private _receiveResponseThumbnail(message: threema.WireMessage): any {
- this.$log.debug('Received thumbnail response');
- // Unpack data and arguments
- const args = message.args;
- if (args === undefined) {
- this.$log.warn('Invalid message response: arguments missing');
- return {
- success: false,
- data: 'invalid_response',
- };
- }
- const data = message.data;
- if ( data === undefined) {
- // It's ok, a message without a thumbnail
- return {
- success: true,
- data: null,
- };
- }
- // Unpack required argument fields
- const type = args[WebClientService.ARGUMENT_RECEIVER_TYPE];
- const id = args[WebClientService.ARGUMENT_RECEIVER_ID];
- const messageId = args[WebClientService.ARGUMENT_MESSAGE_ID];
- if (type === undefined || id === undefined || messageId === undefined ) {
- this.$log.warn('Invalid thumbnail response, argument field missing');
- return {
- success: false,
- data: 'invalid_response',
- };
- }
- this.messages.setThumbnail( this.receivers.getData(args), messageId, data);
- return {
- success: true,
- data: data};
- }
- private _receiveResponseBlob(message: threema.WireMessage): threema.PromiseRequestResult<ArrayBuffer> {
- this.$log.debug('Received blob response');
- // Unpack data and arguments
- const args = message.args;
- const data = message.data;
- if (args === undefined || data === undefined) {
- this.$log.warn('Invalid message response, data or arguments missing');
- return {
- success: false,
- };
- }
- // Unpack required argument fields
- const receiverType = args[WebClientService.ARGUMENT_RECEIVER_TYPE];
- const receiverId = args[WebClientService.ARGUMENT_RECEIVER_ID];
- const msgId = args[WebClientService.ARGUMENT_MESSAGE_ID];
- if (receiverType === undefined || receiverId === undefined || msgId === undefined) {
- this.$log.warn('Invalid blob response, argument field missing');
- return {
- success: false,
- };
- }
- // Unpack data
- const buffer: ArrayBuffer = data[WebClientService.DATA_FIELD_BLOB_BLOB];
- if (buffer === undefined) {
- this.$log.warn('Invalid blob response, data field missing');
- return {
- success: false,
- };
- }
- this.blobCache.set(msgId + receiverType, buffer);
- // Download to browser
- return {
- success: true,
- data: buffer,
- };
- }
- private _receiveUpdateMessage(message: threema.WireMessage): void {
- this.$log.debug('Received message update');
- // Unpack data and arguments
- const args = message.args;
- const data: threema.Message = message.data;
- if (args === undefined || data === undefined) {
- this.$log.warn('Invalid message update, data or arguments missing');
- return;
- }
- // Unpack required argument fields
- const type: string = args[WebClientService.ARGUMENT_RECEIVER_TYPE];
- const id: string = args[WebClientService.ARGUMENT_RECEIVER_ID];
- const mode: string = args[WebClientService.ARGUMENT_MODE];
- if (type === undefined || id === undefined || mode === undefined) {
- this.$log.warn('Invalid message update, argument field missing');
- return;
- }
- let receiver = {type: type, id: id} as threema.Receiver;
- // React depending on mode
- switch (mode) {
- case WebClientService.ARGUMENT_MODE_NEW:
- this.$log.debug('New message', data.id);
- // It's possible that this message already exists (placeholder message on send).
- // Try to update it first. If not, add it as a new msg.
- if (!this.messages.update(receiver, data)) {
- this.messages.addNewer(receiver, [data]);
- }
- break;
- case WebClientService.ARGUMENT_MODE_MODIFIED:
- this.$log.debug('Modified message', data.id);
- this.messages.update(receiver, data);
- break;
- case WebClientService.ARGUMENT_MODE_REMOVED:
- this.messages.remove(receiver, data.id);
- break;
- default:
- this.$log.warn('Invalid message response, unknown mode:', mode);
- }
- }
- private _receiveUpdateReceiver(message: threema.WireMessage): void {
- this.$log.debug('Received receiver update');
- // Unpack data and arguments
- const args = message.args;
- const data = message.data;
- if (args === undefined || data === undefined) {
- this.$log.warn('Invalid receiver update, data or arguments missing');
- return;
- }
- // Unpack required argument fields
- const type = args[WebClientService.ARGUMENT_RECEIVER_TYPE] as threema.ReceiverType;
- const id = args[WebClientService.ARGUMENT_RECEIVER_ID];
- const mode = args[WebClientService.ARGUMENT_MODE];
- if (type === undefined || mode === undefined ||
- (mode !== WebClientService.ARGUMENT_MODE_REFRESH && id === undefined)) {
- this.$log.warn('Invalid receiver update, argument field missing');
- return;
- }
- // React depending on mode
- switch (mode) {
- case WebClientService.ARGUMENT_MODE_NEW:
- case WebClientService.ARGUMENT_MODE_MODIFIED:
- // Add or update a certain receiver
- let updatedReceiver = this.receivers.extend(type, data);
- // remove all cached messages if the receiver was moved to "locked" state
- if (updatedReceiver !== undefined && updatedReceiver.locked) {
- this.messages.clearReceiverMessages(updatedReceiver);
- }
- break;
- case WebClientService.ARGUMENT_MODE_REMOVED:
- // Remove a certain receiver
- (this.receivers.get(type) as Map<string, threema.Receiver>).delete(id);
- break;
- case WebClientService.ARGUMENT_MODE_REFRESH:
- // Refresh lists of receivers
- if (type === 'me') {
- this.receivers.setMe(data);
- } else if (type === 'contact') {
- this.receivers.setContacts(data);
- } else if (type === 'group') {
- this.receivers.setGroups(data);
- } else if (type === 'distributionList') {
- this.receivers.setDistributionLists(data);
- } else {
- this.$log.warn('Unknown receiver type:', type);
- }
- break;
- default:
- this.$log.warn('Invalid receiver response, unknown mode:', mode);
- }
- }
- private _receiveUpdateTyping(message: threema.WireMessage): void {
- this.$log.debug('Received typing update');
- // Unpack data and arguments
- const args = message.args;
- const data = message.data;
- if (args === undefined || data === undefined) {
- this.$log.warn('Invalid typing update, data or arguments missing');
- return;
- }
- // Unpack required argument fields
- const id: string = args[WebClientService.ARGUMENT_RECEIVER_ID];
- const isTyping: boolean = args[WebClientService.ARGUMENT_CONTACT_IS_TYPING];
- if (id === undefined || isTyping === undefined) {
- this.$log.warn('Invalid typing update, argument field missing');
- return;
- }
- // Store or remove typing notification.
- // Note that we know that the receiver must be a contact, because
- // groups and distribution lists can't type.
- const receiver = {id: id, type: 'contact'} as threema.ContactReceiver;
- if (isTyping === true) {
- this.typing.setTyping(receiver);
- } else {
- this.typing.unsetTyping(receiver);
- }
- }
- /**
- * The peer sends the device information string. This can be used to
- * identify the active session.
- */
- private _receiveResponseClientInfo(message: threema.WireMessage): void {
- this.$log.debug('Received client info');
- const args = message.args;
- if (args === undefined) {
- this.$log.warn('Invalid client info, argument field missing');
- return;
- }
- this.deviceInfo = args.device;
- this.$log.debug('Client device:', this.deviceInfo);
- // Store push token
- if (args.myPushToken) {
- this.pushToken = args.myPushToken;
- this.pushService.init(args.myPushToken);
- }
- // Set own identity
- let myAccount = args.myAccount;
- this.myIdentity = {
- identity: myAccount.identity,
- publicKey: myAccount.publicKey,
- publicNickname: myAccount.publicNickname,
- fingerprint: this.fingerPrintService.generate(myAccount.publicKey),
- };
- this.registerInitializationStep('client info');
- }
- public setPassword(password: string) {
- // If a password has been set, store trusted key and push token
- if (this._maybeTrustKeys(password)) {
- // Saved trusted key, send information to client
- this.sendKeyPersisted();
- }
- }
- /**
- * Reset all Fields and clear the blob cache
- */
- public clearCache(): void {
- this._resetFields();
- this.blobCache.clear();
- }
- /**
- * Return the max text length
- * @returns {number}
- */
- public getMaxTextLength(): number {
- return WebClientService.MAX_TEXT_LENGTH;
- }
- /**
- * Called when a new message arrives.
- */
- private onNewMessage(message: threema.Message, conversation: threema.Conversation): void {
- // ignore message from active receivers (and if the browser tab is visible
- if (this.browserService.isVisible()
- && this.receiverService.compare(conversation, this.receiverService.getActive())) {
- this.$log.debug('do not show a notification of a active chat');
- return;
- }
- const sender: threema.Receiver = conversation.receiver;
- if (sender === undefined || sender.locked) {
- // do not show any notifications on private chats
- return;
- }
- // Determine sender and partner name (used for notification)
- let senderName = sender.id;
- if (sender.displayName) {
- senderName = sender.displayName;
- } else if (sender.type === 'contact') {
- senderName = '~' + (sender as threema.ContactReceiver).publicNickname;
- }
- const partner = this.receivers.getData({
- id: message.partnerId,
- type: 'contact',
- } as threema.Receiver) as threema.ContactReceiver;
- let partnerName = partner.displayName || ('~' + partner.publicNickname);
- // Show notification
- this.$translate('messenger.MESSAGE_NOTIFICATION_SUBJECT', {messageCount: 1 + conversation.unreadCount})
- .then((titlePrefix) => {
- const title = `${titlePrefix} ${senderName}`;
- let body = '';
- let messageType = message.type;
- let caption = message.caption;
- let captionString = '';
- if (caption !== undefined) {
- captionString = captionString + ': ' + caption;
- }
- let messageTypeString = this.$translate.instant('messageTypes.' + messageType);
- switch (messageType) {
- case 'text':
- body = message.body;
- break;
- case 'location':
- body = messageTypeString + ': ' + message.location.poi;
- break;
- case 'file':
- if (message.file.type === 'image/gif') {
- body = this.$translate.instant('messageTypes.' + 'gif') + captionString;
- break;
- }
- // Display caption, if available otherwise use filename
- if (captionString.length > 0) {
- body = messageTypeString + captionString;
- } else {
- body = messageTypeString + ': ' + message.file.name;
- }
- break;
- case 'ballot':
- // TODO Show ballot title if ballot messages are implemented in the web version
- body = messageTypeString;
- break;
- default:
- // Image, video and audio
- body = messageTypeString + captionString;
- }
- if (conversation.type === 'group') {
- body = partnerName + ': ' + body;
- }
- const tag = conversation.type + '-' + conversation.id;
- const avatar = (sender.avatar && sender.avatar.low) ? sender.avatar.low : null;
- this.notificationService.showNotification(tag, title, body, avatar, () => {
- this.$state.go('messenger.home.conversation', {
- type: conversation.type,
- id: conversation.id,
- initParams: null,
- });
- });
- });
- }
- /**
- * If a password has been set, store own private permanent key and public
- * key of the peer in the trusted key store.
- */
- private _maybeTrustKeys(password: string): boolean {
- if (password !== undefined && password !== null && password.length > 0) {
- this.trustedKeyStore.storeTrustedKey(
- this.salty.keyStore.publicKeyBytes,
- this.salty.keyStore.secretKeyBytes,
- this.salty.peerPermanentKeyBytes,
- this.pushToken,
- password,
- );
- this.$log.info('Stored trusted key');
- return true;
- }
- return false;
- }
- private _sendRequest(type, args = null): void {
- let message: threema.WireMessage = {
- type: WebClientService.TYPE_REQUEST,
- subType: type,
- };
- if (args) {
- message.args = args;
- }
- this.send(message);
- }
- private _sendPromiseMessage(message: threema.WireMessage, timeout: number = null): Promise<any> {
- // create arguments on wired message
- if (message.args === undefined) {
- message.args = {};
- }
- let promiseId = message.args[WebClientService.ARGUMENT_TEMPORARY_ID];
- if (promiseId === undefined) {
- // create a random id to identity the promise
- promiseId = 'p' + Math.random().toString(36).substring(7);
- message.args[WebClientService.ARGUMENT_TEMPORARY_ID] = promiseId;
- }
- return new Promise(
- (resolve, reject) => {
- let p = {
- resolve: resolve,
- reject: reject,
- } as threema.PromiseCallbacks;
- this.requestPromises.set(promiseId, p);
- if (timeout !== null && timeout > 0) {
- this.$timeout(() => {
- p.reject('timeout');
- this.requestPromises.delete(promiseId);
- }, timeout);
- }
- this.send(message);
- },
- );
- }
- private _sendRequestPromise(type, args = null, timeout: number = null): Promise<any> {
- let message: threema.WireMessage = {
- type: WebClientService.TYPE_REQUEST,
- subType: type,
- args: args,
- };
- return this._sendPromiseMessage(message, timeout);
- }
- private _sendCreatePromise(type, args = null, data: any = null, timeout: number = null): Promise<any> {
- let message: threema.WireMessage = {
- type: WebClientService.TYPE_CREATE,
- subType: type,
- args: args,
- data: data,
- };
- return this._sendPromiseMessage(message, timeout);
- }
- private _sendUpdatePromise(type, args = null, data: any = null, timeout: number = null): Promise<any> {
- let message: threema.WireMessage = {
- type: WebClientService.TYPE_UPDATE,
- subType: type,
- data: data,
- args: args,
- };
- return this._sendPromiseMessage(message, timeout);
- }
- private _sendCreate(type, data, args = null): void {
- let message: threema.WireMessage = {
- type: WebClientService.TYPE_CREATE,
- subType: type,
- data: data,
- };
- if (args) {
- message.args = args;
- }
- this.send(message);
- }
- private _sendDelete(type, args, data = null): void {
- let message: threema.WireMessage = {
- type: WebClientService.TYPE_DELETE,
- subType: type,
- data: data,
- args: args,
- };
- this.send(message);
- }
- private _sendDeletePromise(type, args, data: any = null, timeout: number = null): Promise<any> {
- let message: threema.WireMessage = {
- type: WebClientService.TYPE_DELETE,
- subType: type,
- data: data,
- args: args,
- };
- return this._sendPromiseMessage(message, timeout);
- }
- private _receiveRequest(type, message): void {
- this.$log.warn('Ignored request with type:', type);
- }
- private _receivePromise(message: any, receiveResult: any) {
- if (
- message !== undefined
- && message.args !== undefined
- && message.args[WebClientService.ARGUMENT_TEMPORARY_ID] !== undefined) {
- // find pending promise
- let promiseId = message.args[WebClientService.ARGUMENT_TEMPORARY_ID];
- if (this.requestPromises.has(promiseId)) {
- if (receiveResult.success) {
- this.requestPromises.get(promiseId).resolve(receiveResult.data);
- } else {
- this.requestPromises.get(promiseId).reject(receiveResult.message);
- }
- // remove from map
- this.requestPromises.delete(promiseId);
- }
- }
- }
- private _receiveResponse(type, message): void {
- // Dispatch response
- let receiveResult;
- switch (type) {
- case WebClientService.SUB_TYPE_RECEIVER:
- this._receiveResponseReceivers(message);
- break;
- case WebClientService.SUB_TYPE_CONVERSATIONS:
- this.runAfterInitializationSteps(['receivers'], () => {
- this._receiveResponseConversations(message);
- });
- break;
- case WebClientService.SUB_TYPE_CONVERSATION:
- this._receiveResponseConversation(message);
- break;
- case WebClientService.SUB_TYPE_MESSAGE:
- this._receiveResponseMessages(message);
- break;
- case WebClientService.SUB_TYPE_AVATAR:
- receiveResult = this._receiveResponseAvatar(message);
- break;
- case WebClientService.SUB_TYPE_THUMBNAIL:
- receiveResult = this._receiveResponseThumbnail(message);
- break;
- case WebClientService.SUB_TYPE_BLOB:
- receiveResult = this._receiveResponseBlob(message);
- break;
- case WebClientService.SUB_TYPE_CLIENT_INFO:
- this._receiveResponseClientInfo(message);
- break;
- case WebClientService.SUB_TYPE_CONTACT_DETAIL:
- receiveResult = this._receiveResponseContactDetail(message);
- break;
- case WebClientService.SUB_TYPE_ALERT:
- receiveResult = this._receiveAlert(message);
- break;
- case WebClientService.SUB_TYPE_GROUP_SYNC:
- receiveResult = this._receiveGroupSync(message);
- break;
- default:
- this.$log.warn('Ignored response with type:', type);
- return;
- }
- this._receivePromise(message, receiveResult);
- }
- private _receiveUpdate(type, message): void {
- // Dispatch update
- let receiveResult;
- switch (type) {
- case WebClientService.SUB_TYPE_RECEIVER:
- this._receiveUpdateReceiver(message);
- break;
- case WebClientService.SUB_TYPE_MESSAGE:
- this._receiveUpdateMessage(message);
- break;
- case WebClientService.SUB_TYPE_TYPING:
- this._receiveUpdateTyping(message);
- break;
- case WebClientService.SUB_TYPE_CONTACT:
- receiveResult = this._receiveResponseContact(message);
- break;
- case WebClientService.SUB_TYPE_GROUP:
- receiveResult = this._receiveResponseGroup(message);
- break;
- case WebClientService.SUB_TYPE_DISTRIBUTION_LIST:
- receiveResult = this._receiveResponseDistributionList(message);
- break;
- default:
- this.$log.warn('Ignored update with type:', type);
- return;
- }
- this._receivePromise(message, receiveResult);
- }
- private _receiveCreate(type, message): void {
- // Dispatch response
- let receiveResult: threema.PromiseRequestResult<any>;
- switch (type) {
- case WebClientService.SUB_TYPE_CONTACT:
- receiveResult = this._receiveResponseContact(message);
- break;
- case WebClientService.SUB_TYPE_GROUP:
- receiveResult = this._receiveResponseGroup(message);
- break;
- case WebClientService.SUB_TYPE_DISTRIBUTION_LIST:
- receiveResult = this._receiveResponseDistributionList(message);
- break;
- case WebClientService.SUB_TYPE_TEXT_MESSAGE:
- case WebClientService.SUB_TYPE_FILE_MESSAGE:
- receiveResult = this._receiveResponseCreateMessage(message);
- break;
- default:
- this.$log.warn('Ignored response with type:', type);
- return;
- }
- this._receivePromise(message, receiveResult);
- }
- private _receiveDelete(type, message): void {
- // Dispatch update
- let receiveResult;
- switch (type) {
- case WebClientService.SUB_TYPE_CONTACT_DETAIL:
- receiveResult = this._receiveUpdateReceiver(message);
- break;
- default:
- this.$log.warn('Ignored delete with type:', type);
- return;
- }
- this._receivePromise(message, receiveResult);
- }
- /**
- * Encode an object using the msgpack format.
- */
- private msgpackEncode(data: any): Uint8Array {
- return msgpack.encode(data, this.msgpackOptions);
- }
- /**
- * Decode an object using the msgpack format.
- */
- private msgpackDecode(bytes: Uint8Array): any {
- return msgpack.decode(bytes, this.msgpackOptions);
- }
- /**
- * Send a message through the secure data channel.
- */
- private send(message: threema.WireMessage): void {
- this.$log.debug('Sending', message.type + '/' + message.subType, 'message');
- const bytes: Uint8Array = this.msgpackEncode(message);
- this.secureDataChannel.send(bytes);
- }
- /**
- * Receive a new incoming decrypted message.
- */
- private receive(message: threema.WireMessage): void {
- // Dispatch message
- switch (message.type) {
- case WebClientService.TYPE_REQUEST:
- this._receiveRequest(message.subType, message);
- break;
- case WebClientService.TYPE_RESPONSE:
- this._receiveResponse(message.subType, message);
- break;
- case WebClientService.TYPE_CREATE:
- this._receiveCreate(message.subType, message);
- break;
- case WebClientService.TYPE_UPDATE:
- this._receiveUpdate(message.subType, message);
- break;
- case WebClientService.TYPE_DELETE:
- this._receiveDelete(message.subType, message);
- break;
- default:
- this.$log.warn('Ignored message with type:', message.type);
- }
- }
- private runAfterInitializationSteps(requiredSteps: threema.InitializationStep[], callback: any): void {
- for (let requiredStep of requiredSteps) {
- if (!this.initialized.has(requiredStep)) {
- this.$log.debug(this.logTag, 'required step', requiredStep, 'not completed, add pending');
- this.pendingInitializationStepRoutines.push({
- requiredSteps: requiredSteps,
- callback: callback,
- } as threema.InitializationStepRoutine);
- return;
- }
- }
- callback();
- }
- private currentController: string;
- public setControllerName(name: string): void {
- this.currentController = name;
- }
- public getControllerName(): string {
- return this.currentController;
- }
- /**
- * Update the unread count in the window title.
- */
- private updateUnreadCount(): void {
- const totalUnreadCount = this.conversations
- .get()
- .reduce((a: number, b: threema.Conversation) => a + b.unreadCount, 0);
- this.titleService.updateUnreadCount(totalUnreadCount);
- }
- /**
- * Reset the unread count in the window title
- */
- private resetUnreadCount(): void {
- this.titleService.updateUnreadCount(0);
- }
- }
|