threema.d.ts 19 KB

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