threema.d.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. declare const angular: ng.IAngularStatic;
  18. declare namespace threema {
  19. interface Avatar {
  20. // Low resolution avatar URI
  21. low?: ArrayBuffer;
  22. // High resolution avatar URI
  23. high?: ArrayBuffer;
  24. }
  25. /**
  26. * Messages that are sent through the secure data channel as encrypted msgpack bytes.
  27. */
  28. interface WireMessage {
  29. type: string;
  30. subType: string;
  31. args?: any;
  32. data?: any;
  33. }
  34. type MessageType = 'text' | 'image' | 'video' | 'audio' | 'location' | 'contact' |
  35. 'status' | 'ballot' | 'file' | 'voipStatus' | 'unknown';
  36. type MessageState = 'delivered' | 'read' | 'send-failed' | 'sent' | 'user-ack' |
  37. 'user-dec' | 'pending' | 'sending';
  38. const enum InitializationStep {
  39. ClientInfo = 'client info',
  40. Conversations = 'conversations',
  41. Receivers = 'receivers',
  42. Profile = 'profile',
  43. }
  44. interface InitializationStepRoutine {
  45. requiredSteps: InitializationStep[];
  46. callback: any;
  47. }
  48. interface Thumbnail {
  49. img?: string;
  50. preview: string;
  51. width: number;
  52. height: number;
  53. }
  54. const enum EventType {
  55. Created = 'created',
  56. Sent = 'sent',
  57. Delivered = 'delivered',
  58. Read = 'read',
  59. Acked = 'acked',
  60. Modified = 'modified',
  61. }
  62. /**
  63. * A message event, e.g. when it was delivered, read or modified.
  64. */
  65. interface MessageEvent {
  66. // The event type
  67. type: EventType;
  68. // Unix timestamp in seconds
  69. date: number;
  70. }
  71. /**
  72. * A Threema chat message.
  73. */
  74. interface Message {
  75. type: MessageType;
  76. id: string;
  77. body: string;
  78. thumbnail?: Thumbnail;
  79. date?: number;
  80. events?: MessageEvent[];
  81. sortKey: number;
  82. partnerId: string;
  83. isOutbox: boolean;
  84. isStatus: boolean;
  85. caption?: string;
  86. statusType?: 'text' | 'firstUnreadMessage';
  87. unread?: boolean;
  88. state?: MessageState;
  89. quote?: Quote;
  90. file?: FileInfo;
  91. video?: VideoInfo;
  92. audio?: AudioInfo;
  93. voip?: VoipStatusInfo;
  94. location?: LocationInfo;
  95. // only for temporary Messages
  96. temporaryId?: string;
  97. errorMessage?: string;
  98. }
  99. interface FileInfo {
  100. description: string;
  101. name: string;
  102. size: number;
  103. type: string;
  104. inApp: boolean;
  105. }
  106. interface VideoInfo {
  107. duration: number;
  108. size: number;
  109. }
  110. interface AudioInfo {
  111. duration: number;
  112. }
  113. interface VoipStatusInfo {
  114. status: number;
  115. }
  116. interface LocationInfo {
  117. lat: number;
  118. lon: number;
  119. accuracy: number;
  120. description: string;
  121. address?: string;
  122. }
  123. interface BlobInfo {
  124. buffer: ArrayBuffer;
  125. mimetype: string;
  126. filename: string;
  127. }
  128. /**
  129. * All possible receiver types.
  130. */
  131. type ReceiverType = 'me' | 'contact' | 'group' | 'distributionList';
  132. /**
  133. * Access Object for receivers
  134. */
  135. interface ReceiverAccess {
  136. canDelete?: boolean;
  137. }
  138. interface ContactReceiverAccess extends ReceiverAccess {
  139. canChangeAvatar: boolean;
  140. canChangeFirstName: boolean;
  141. canChangeLastName: boolean;
  142. }
  143. interface GroupReceiverAccess extends ReceiverAccess {
  144. canChangeAvatar: boolean;
  145. canChangeName: boolean;
  146. canChangeMembers?: boolean;
  147. canLeave?: boolean;
  148. canSync?: boolean;
  149. }
  150. interface DistributionListReceiverAccess extends ReceiverAccess {
  151. canChangeMembers?: boolean;
  152. }
  153. const enum IdentityType {
  154. Regular = 0,
  155. Work,
  156. }
  157. /**
  158. * The base class for a receiver. Only type and id.
  159. */
  160. interface BaseReceiver {
  161. id: string;
  162. type: ReceiverType;
  163. }
  164. /**
  165. * A generic receiver.
  166. *
  167. * Note that the id is not unique for all receivers, only for all receivers
  168. * of a certain type. The primary key for a receiver is the tuple (type, id).
  169. */
  170. interface Receiver extends BaseReceiver {
  171. // The display name
  172. displayName: string;
  173. // The color used for the avatar
  174. color: string;
  175. // The avatar, may be set if already fetched
  176. avatar?: Avatar;
  177. // Permissions towards this receiver
  178. access: ReceiverAccess;
  179. // Whether the chat with this receiver is locked.
  180. locked?: boolean;
  181. // Whether the chat with this receiver is visible.
  182. visible?: boolean;
  183. }
  184. /**
  185. * A contact.
  186. */
  187. interface ContactReceiver extends Receiver {
  188. // Flag indicating whether this is the own profile or another contact
  189. type: 'contact' | 'me';
  190. // Public nickname, if set
  191. publicNickname?: string;
  192. // First name, if set
  193. firstName?: string;
  194. // Last name, if set
  195. lastName?: string;
  196. // Verification level integer (1-3)
  197. verificationLevel?: number;
  198. // Feature level (0-3)
  199. featureLevel: number | null;
  200. // The identity state
  201. state: 'ACTIVE' | 'INACTIVE';
  202. // The Threema public key
  203. publicKey: ArrayBuffer;
  204. // System confact information
  205. systemContact?: SystemContact;
  206. // Permissions towards this contact
  207. access: ContactReceiverAccess;
  208. // Whether this is a contact from the same Threema Work package.
  209. // Only relevant for Threema Work users.
  210. isWork?: boolean;
  211. // Whether this contact is blocked
  212. isBlocked?: boolean;
  213. // The identity type.
  214. // 0 - Regular Threema user.
  215. // 1 - Threema Work user.
  216. identityType?: IdentityType;
  217. }
  218. /**
  219. * Own contact.
  220. */
  221. interface MeReceiver extends ContactReceiver {
  222. type: 'me';
  223. }
  224. /**
  225. * A group.
  226. */
  227. interface GroupReceiver extends Receiver {
  228. type: 'group';
  229. disabled: boolean;
  230. members: string[];
  231. administrator: string;
  232. access: GroupReceiverAccess;
  233. createdAt?: string;
  234. }
  235. /**
  236. * A distribution list.
  237. */
  238. interface DistributionListReceiver extends Receiver {
  239. type: 'distributionList';
  240. members: string[];
  241. access: DistributionListReceiverAccess;
  242. }
  243. interface SystemContact {
  244. emails?: SystemContactEmail[];
  245. phoneNumbers?: SystemContactPhone[];
  246. }
  247. interface SystemContactEmail {
  248. label: string;
  249. address: string;
  250. }
  251. interface SystemContactPhone {
  252. label: string;
  253. number: string;
  254. }
  255. /**
  256. * A conversation.
  257. */
  258. interface Conversation {
  259. type: ReceiverType;
  260. id: string;
  261. position: number;
  262. messageCount: number;
  263. unreadCount: number;
  264. latestMessage: Message | null;
  265. receiver?: Receiver;
  266. avatar?: ArrayBuffer;
  267. isMuted?: boolean;
  268. isStarred?: boolean;
  269. }
  270. /**
  271. * Connection state in the welcome dialog.
  272. *
  273. * States:
  274. *
  275. * - new: Initial state
  276. * - connecting: Connecting to signaling server
  277. * - push: When trying to reconnect, waiting for push notification to arrive
  278. * - manual_start: When trying to reconnect, waiting for manual session start
  279. * - already_connected: When the user is already connected in another tab or window
  280. * - waiting: Waiting for new-responder message from signaling server
  281. * - peer_handshake: Doing SaltyRTC handshake with the peer
  282. * - loading: Loading initial data
  283. * - done: Initial loading is finished
  284. * - closed: Connection is closed
  285. *
  286. */
  287. type ConnectionBuildupState = 'new' | 'connecting' | 'push' | 'manual_start' | 'already_connected'
  288. | 'waiting' | 'peer_handshake' | 'loading' | 'done' | 'closed';
  289. interface ConnectionBuildupStateChange {
  290. state: ConnectionBuildupState;
  291. prevState: ConnectionBuildupState;
  292. }
  293. /**
  294. * Connection state of the WebRTC peer connection.
  295. */
  296. type RTCConnectionState = 'new' | 'connecting' | 'connected' | 'disconnected';
  297. /**
  298. * Connection state of the WebRTC peer connection.
  299. */
  300. const enum GlobalConnectionState {
  301. Ok = 'ok',
  302. Warning = 'warning',
  303. Error = 'error',
  304. }
  305. /**
  306. * Type of message to be sent to a receiver.
  307. */
  308. type MessageContentType = 'text' | 'file';
  309. interface MessageData {
  310. // optional quote object
  311. quote?: Quote;
  312. }
  313. /**
  314. * Payload for a file message.
  315. */
  316. interface FileMessageData extends MessageData {
  317. // File name
  318. name: string;
  319. // File MIME type
  320. fileType: string;
  321. // Size in bytes
  322. size: number;
  323. // File bytes
  324. data: ArrayBuffer;
  325. // Caption string
  326. caption?: string;
  327. // Send as file message
  328. sendAsFile?: boolean;
  329. }
  330. /**
  331. * Payload for a text message.
  332. */
  333. interface TextMessageData extends MessageData {
  334. // Text to be sent
  335. text: string;
  336. }
  337. /**
  338. * The $stateParams format used for the welcome controller.
  339. */
  340. interface WelcomeStateParams extends ng.ui.IStateParamsService {
  341. initParams: null | {keyStore: saltyrtc.KeyStore, peerTrustedKey: Uint8Array};
  342. }
  343. interface CreateReceiverStateParams extends ng.ui.IStateParamsService {
  344. type: ReceiverType;
  345. initParams: null | {identity: string | null};
  346. }
  347. interface ConversationStateParams extends ng.ui.IStateParamsService {
  348. type: ReceiverType;
  349. id: string;
  350. initParams: null | {text: string | null};
  351. }
  352. interface Quote {
  353. identity: string;
  354. text: string;
  355. }
  356. const enum PushTokenType {
  357. Gcm = 'gcm',
  358. Apns = 'apns',
  359. }
  360. const enum PushTokenPrefix {
  361. Gcm = 'g',
  362. Apns = 'a',
  363. }
  364. interface TrustedKeyStoreData {
  365. ownPublicKey: Uint8Array;
  366. ownSecretKey: Uint8Array;
  367. peerPublicKey: Uint8Array;
  368. pushToken: string | null;
  369. pushTokenType: PushTokenType | null;
  370. }
  371. const enum BrowserName {
  372. Chrome = 'chrome',
  373. Firefox = 'firefox',
  374. InternetExplorer = 'ie',
  375. Edge = 'edge',
  376. Opera = 'opera',
  377. Safari = 'safari',
  378. }
  379. interface BrowserInfo {
  380. chrome: boolean;
  381. firefox: boolean;
  382. ie: boolean;
  383. edge: boolean;
  384. opera: boolean;
  385. safari: boolean;
  386. name?: BrowserName;
  387. version?: number;
  388. textInfo?: string;
  389. }
  390. interface PromiseCallbacks {
  391. resolve: (arg: any) => void;
  392. reject: (arg: any) => void;
  393. }
  394. interface PromiseRequestResult<T> {
  395. success: boolean;
  396. error?: string;
  397. data?: T;
  398. }
  399. type OnRemovedCallback = (identity: string) => void;
  400. const enum ControllerModelMode {
  401. NEW = 'new',
  402. VIEW = 'view',
  403. EDIT = 'edit',
  404. CHAT = 'chat',
  405. }
  406. interface ControllerModel<T extends BaseReceiver> {
  407. /**
  408. * The title shown in the header.
  409. */
  410. subject: string;
  411. /**
  412. * Loading state.
  413. */
  414. isLoading: boolean;
  415. /**
  416. * Save the changes, return a promise with the receiver.
  417. */
  418. save(): Promise<T>;
  419. /**
  420. * Delete all messages in this conversation.
  421. */
  422. clean(ev: any): any;
  423. /**
  424. * Validate this receiver.
  425. */
  426. isValid(): boolean;
  427. /*
  428. * Return whether this receiver can be chatted with.
  429. */
  430. canChat(): boolean;
  431. /**
  432. * Can this receiver be edited?
  433. */
  434. canEdit(): boolean;
  435. /**
  436. * Can this receiver be cleaned?
  437. */
  438. canClean(): boolean;
  439. /*
  440. * Return whether this receiver can show a QR code of the public key.
  441. */
  442. canShowQr(): boolean;
  443. /**
  444. * The editing mode, e.g. view or edit this receiver.
  445. */
  446. getMode(): ControllerModelMode;
  447. /**
  448. * Set the on removed callback.
  449. */
  450. setOnRemoved(callback: OnRemovedCallback): void;
  451. /**
  452. * Callback called when the members change.
  453. */
  454. onChangeMembers(identities: string[]): void;
  455. /**
  456. * Return the members of this receiver.
  457. */
  458. getMembers(): string[];
  459. }
  460. interface Alert {
  461. source: string;
  462. type: string;
  463. message: string;
  464. }
  465. interface ReceiverListener {
  466. onRemoved(receiver: Receiver);
  467. }
  468. interface Config {
  469. SELF_HOSTED: boolean;
  470. PREV_PROTOCOL_LAST_VERSION: string | null;
  471. SALTYRTC_PORT: number;
  472. SALTYRTC_SERVER_KEY: string | null;
  473. SALTYRTC_HOST: string | null;
  474. SALTYRTC_HOST_PREFIX: string | null;
  475. SALTYRTC_HOST_SUFFIX: string | null;
  476. ICE_SERVERS: RTCIceServer[];
  477. PUSH_URL: string;
  478. DEBUG: boolean;
  479. MSG_DEBUGGING: boolean;
  480. MSGPACK_DEBUGGING: boolean;
  481. ICE_DEBUGGING: boolean;
  482. }
  483. interface InitialConversationData {
  484. draft: string;
  485. initialText: string;
  486. }
  487. interface BrowserMinVersions {
  488. FF: number;
  489. CHROME: number;
  490. OPERA: number;
  491. }
  492. interface BatteryStatus {
  493. percent: number;
  494. isCharging: boolean;
  495. }
  496. const enum OperatingSystem {
  497. Android = 'android',
  498. Ios = 'ios',
  499. }
  500. interface ClientInfo {
  501. device: string;
  502. os: OperatingSystem;
  503. osVersion: string;
  504. isWork: boolean;
  505. pushToken?: string;
  506. configuration: AppConfig;
  507. capabilities: AppCapabilities;
  508. }
  509. interface AppConfig {
  510. showLocationPreview: boolean;
  511. voipEnabled: boolean;
  512. voipForceTurn: boolean;
  513. largeSingleEmoji: boolean;
  514. showInactiveIds: boolean;
  515. }
  516. interface AppCapabilities {
  517. maxGroupSize: number;
  518. distributionLists: boolean;
  519. mdm?: MdmRestrictions;
  520. }
  521. interface MdmRestrictions {
  522. disableAddContact?: boolean;
  523. disableCreateGroup?: boolean;
  524. disableSaveToGallery?: boolean;
  525. disableExport?: boolean;
  526. disableMessagePreview?: boolean;
  527. disableCalls?: boolean;
  528. readonlyProfile?: boolean;
  529. }
  530. interface Mention {
  531. identity: string;
  532. query: string;
  533. isAll: boolean;
  534. }
  535. interface WordResult {
  536. // The trimmed word
  537. word: string;
  538. // The length of the untrimmed word
  539. realLength: number;
  540. }
  541. const enum ChosenTask {
  542. None,
  543. WebRTC,
  544. RelayedData,
  545. }
  546. namespace Container {
  547. interface ReceiverData {
  548. contacts: ContactReceiver[];
  549. groups: GroupReceiver[];
  550. distributionLists: DistributionListReceiver[];
  551. }
  552. interface Converter {
  553. unicodeToEmoji(message);
  554. addReceiverToConversation(receivers: Receivers);
  555. }
  556. interface Filters {
  557. hasData(receivers);
  558. hasContact(contacts);
  559. isValidMessage(contacts);
  560. }
  561. interface Receivers {
  562. me: MeReceiver;
  563. contacts: Map<string, ContactReceiver>;
  564. groups: Map<string, GroupReceiver>;
  565. distributionLists: Map<string, DistributionListReceiver>;
  566. get(receiverType: ReceiverType): Receiver | Map<string, Receiver>;
  567. getData(receiver: BaseReceiver): Receiver | null;
  568. set(data: ReceiverData): void;
  569. setMe(data: MeReceiver): void;
  570. setContacts(data: ContactReceiver[]): void;
  571. setGroups(data: GroupReceiver[]): void;
  572. setDistributionLists(data: DistributionListReceiver[]): void;
  573. extend(receiverType: ReceiverType, data: Receiver): Receiver;
  574. extendDistributionList(data: DistributionListReceiver): DistributionListReceiver;
  575. extendGroup(data: GroupReceiver): GroupReceiver;
  576. extendMe(data: MeReceiver): MeReceiver;
  577. extendContact(data: ContactReceiver): ContactReceiver;
  578. }
  579. interface Conversations {
  580. get(): Conversation[];
  581. set(data: Conversation[]): void;
  582. find(pattern: Conversation | Receiver): Conversation | null;
  583. add(conversation: Conversation): void;
  584. updateOrAdd(conversation: Conversation): void;
  585. remove(conversation: Conversation): void;
  586. setFilter(filter: (data: Conversation[]) => Conversation[]): void;
  587. setConverter(converter: (data: Conversation) => Conversation): void;
  588. }
  589. interface Messages {
  590. converter: (data: Message) => Message;
  591. getList(receiver: Receiver): Message[];
  592. clear($scope: ng.IScope): void;
  593. clearReceiverMessages(receiver: Receiver): number;
  594. contains(receiver: Receiver): boolean;
  595. hasMore(receiver: Receiver): boolean;
  596. setMore(receiver: Receiver, more: boolean): void;
  597. getReferenceMsgId(receiver: Receiver): string;
  598. isRequested(receiver: Receiver): boolean;
  599. setRequested(receiver: Receiver): void;
  600. clearRequested(receiver): void;
  601. addNewer(receiver: Receiver, messages: Message[]): void;
  602. addOlder(receiver: Receiver, messages: Message[]): void;
  603. update(receiver: Receiver, message: Message): boolean;
  604. setThumbnail(receiver: Receiver, messageId: string, thumbnailImage: string): boolean;
  605. remove(receiver: Receiver, messageId: string): boolean;
  606. removeTemporary(receiver: Receiver, temporaryMessageId: string): boolean;
  607. bindTemporaryToMessageId(receiver: Receiver, temporaryId: string, messageId: string): boolean;
  608. notify(receiver: Receiver, $scope: ng.IScope): void;
  609. register(receiver: Receiver, $scope: ng.IScope, callback: any): Message[];
  610. updateFirstUnreadMessage(receiver: Receiver);
  611. }
  612. interface Typing {
  613. setTyping(receiver: ContactReceiver): void;
  614. unsetTyping(receiver: ContactReceiver): void;
  615. isTyping(receiver: ContactReceiver): boolean;
  616. }
  617. interface Drafts {
  618. setQuote(receiver: Receiver, quote: Quote): void;
  619. removeQuote(receiver: Receiver): void;
  620. getQuote(receiver: Receiver): Quote;
  621. setText(receiver: Receiver, draftMessage: string): void;
  622. removeText(receiver: Receiver): void;
  623. getText(receiver: Receiver): string;
  624. }
  625. interface Factory {
  626. Converter: Container.Converter;
  627. Filters: Container.Filters;
  628. createReceivers: () => Receivers;
  629. createConversations: () => Conversations;
  630. createMessages: () => Messages;
  631. createTyping: () => Typing;
  632. createDrafts: () => Drafts;
  633. }
  634. }
  635. }