threema.d.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. // Types used for the Threema Webclient.
  18. declare const angular: ng.IAngularStatic;
  19. declare namespace threema {
  20. interface Avatar {
  21. // Low resolution avatar path
  22. low?: string;
  23. // High resolution avatar path
  24. high?: string;
  25. }
  26. interface AvatarRegistry {
  27. contact: Avatar;
  28. group: Avatar;
  29. distributionList: Avatar;
  30. }
  31. /**
  32. * Messages that are sent through the secure data channel as encrypted msgpack bytes.
  33. */
  34. interface WireMessage {
  35. type: string;
  36. subType: string;
  37. args?: any;
  38. data?: any;
  39. }
  40. type WireMessageCallback = (message: WireMessage, result: any) => boolean;
  41. type MessageType = 'text' | 'image' | 'video' | 'audio' | 'location' | 'status' | 'ballot' | 'file';
  42. type MessageState = 'delivered' | 'read' | 'send-failed' | 'sent' | 'user-ack' | 'user-dec' | 'pending' | 'sending';
  43. interface Thumbnail {
  44. img?: string;
  45. preview: string;
  46. width: number;
  47. height: number;
  48. }
  49. /**
  50. * A Threema chat message.
  51. */
  52. interface Message {
  53. type: MessageType;
  54. id: number;
  55. body: string;
  56. thumbnail?: Thumbnail;
  57. date: string;
  58. partnerId: string;
  59. isOutbox: boolean;
  60. isStatus: boolean;
  61. caption?: string;
  62. statusType?: 'text' | 'firstUnreadMessage';
  63. unread?: boolean;
  64. state?: MessageState;
  65. quote?: Quote;
  66. file?: FileInfo;
  67. video?: VideoInfo;
  68. audio?: AudioInfo;
  69. location?: LocationInfo;
  70. // only for temporary Messages
  71. temporaryId?: string;
  72. errorMessage?: string;
  73. }
  74. interface FileInfo {
  75. description: string;
  76. name: string;
  77. size: number;
  78. type: string;
  79. inApp: boolean;
  80. }
  81. interface VideoInfo {
  82. duration: number;
  83. size: number;
  84. }
  85. interface AudioInfo {
  86. duration: number;
  87. }
  88. interface LocationInfo {
  89. lat: number;
  90. lon: number;
  91. accuracy: number;
  92. address: string;
  93. poi: string;
  94. }
  95. /**
  96. * All possible receiver types.
  97. */
  98. type ReceiverType = 'me' | 'contact' | 'group' | 'distributionList';
  99. /**
  100. * Access Object for receivers
  101. */
  102. interface ReceiverAccess {
  103. canDelete?: boolean;
  104. }
  105. interface ContactReceiverAccess extends ReceiverAccess {
  106. canChangeAvatar: boolean;
  107. canChangeFirstName: boolean;
  108. canChangeLastName: boolean;
  109. }
  110. interface GroupReceiverAccess extends ReceiverAccess {
  111. canChangeAvatar: boolean;
  112. canChangeName: boolean;
  113. canChangeMembers?: boolean;
  114. canLeave?: boolean;
  115. canSync?: boolean;
  116. }
  117. interface DistributionListReceiverAccess extends ReceiverAccess {
  118. canChangeMembers?: boolean;
  119. }
  120. /**
  121. * A generic receiver.
  122. *
  123. * Note that the id is not unique for all receivers, only for all receivers
  124. * of a certain type. The primary key for a receiver is the tuple (type, id).
  125. */
  126. interface Receiver {
  127. type?: ReceiverType;
  128. id: string;
  129. displayName: string;
  130. color: string;
  131. avatar?: Avatar; // May be set if already fetched
  132. access: ReceiverAccess;
  133. locked?: boolean;
  134. visible?: boolean;
  135. }
  136. /**
  137. * A contact.
  138. */
  139. interface ContactReceiver extends Receiver {
  140. type?: 'contact' | 'me';
  141. publicNickname?: string;
  142. firstName?: string;
  143. lastName?: string;
  144. verificationLevel?: number;
  145. state: string;
  146. featureLevel: number | null;
  147. publicKey: ArrayBuffer;
  148. systemContact?: SystemContact;
  149. access: ContactReceiverAccess;
  150. }
  151. /**
  152. * Own contact.
  153. */
  154. interface MeReceiver extends ContactReceiver {
  155. type?: 'me';
  156. }
  157. /**
  158. * A group.
  159. */
  160. interface GroupReceiver extends Receiver {
  161. type?: 'group';
  162. disabled: boolean;
  163. members: string[];
  164. administrator: string;
  165. access: GroupReceiverAccess;
  166. }
  167. /**
  168. * A distribution list.
  169. */
  170. interface DistributionListReceiver extends Receiver {
  171. type?: 'distributionList';
  172. members: string[];
  173. access: DistributionListReceiverAccess;
  174. }
  175. interface SystemContact {
  176. emails?: SystemContactEmail[];
  177. phoneNumbers?: SystemContactPhone[];
  178. }
  179. interface SystemContactEmail {
  180. label: string;
  181. address: string;
  182. }
  183. interface SystemContactPhone {
  184. label: string;
  185. number: string;
  186. }
  187. /**
  188. * A conversation.
  189. */
  190. interface Conversation {
  191. type: ReceiverType;
  192. id: string;
  193. position: number;
  194. messageCount: number;
  195. unreadCount: number;
  196. latestMessage: Message;
  197. receiver?: Receiver;
  198. avatar?: ArrayBuffer;
  199. }
  200. /**
  201. * Connection state in the welcome dialog.
  202. */
  203. type ConnectionBuildupState = 'new' | 'push' | 'manual_start' | 'connecting' | 'waiting'
  204. | 'peer_handshake' | 'loading' | 'done' | 'closed';
  205. /**
  206. * Connection state of the WebRTC peer connection.
  207. */
  208. type RTCConnectionState = 'new' | 'connecting' | 'connected' | 'disconnected';
  209. /**
  210. * Connection state of the WebRTC peer connection.
  211. */
  212. type GlobalConnectionState = 'ok' | 'warning' | 'error';
  213. /**
  214. * Type of message to be sent to a receiver.
  215. */
  216. type MessageContentType = 'text' | 'file';
  217. /**
  218. * Possible message types sent to the receiver.
  219. */
  220. type MessageDataType = 'text' | 'file';
  221. interface MessageData {
  222. // optional quote object
  223. quote?: Quote;
  224. }
  225. /**
  226. * Payload for a file message.
  227. */
  228. interface FileMessageData extends MessageData {
  229. // File name
  230. name: string;
  231. // File MIME type
  232. fileType: string;
  233. // Size in bytes
  234. size: number;
  235. // File bytes
  236. data: ArrayBuffer;
  237. // Caption string
  238. caption?: String;
  239. // Send as file message
  240. sendAsFile?: boolean;
  241. }
  242. /**
  243. * Payload for a text message.
  244. */
  245. interface TextMessageData extends MessageData {
  246. // Text to be sent
  247. text: string;
  248. }
  249. /**
  250. * The $stateParams format used for the welcome controller.
  251. */
  252. interface WelcomeStateParams extends ng.ui.IStateParamsService {
  253. initParams: null | {keyStore: saltyrtc.KeyStore, peerTrustedKey: Uint8Array};
  254. }
  255. interface CreateReceiverStateParams extends ng.ui.IStateParamsService {
  256. type: string;
  257. initParams: null | {identity: null};
  258. }
  259. /**
  260. * State service.
  261. */
  262. interface StateService {
  263. // WebRTC states
  264. signalingConnectionState: saltyrtc.SignalingState;
  265. rtcConnectionState: RTCConnectionState;
  266. // Global connection state
  267. state: 'ok' | 'warning' | 'error';
  268. stage: 'signaling' | 'rtc';
  269. wasConnected: boolean;
  270. // Connection buildup
  271. connectionBuildupState: ConnectionBuildupState;
  272. progress: number;
  273. slowConnect: boolean;
  274. // Update states
  275. updateSignalingConnectionState(state: saltyrtc.SignalingState): void;
  276. updateRtcConnectionState(state: RTCConnectionState): void;
  277. updateConnectionBuildupState(state: ConnectionBuildupState): void;
  278. reset(): void;
  279. }
  280. /**
  281. * Notification service.
  282. */
  283. interface NotificationService {
  284. requestNotificationPermission(): void;
  285. showNotification(id: string, title: string, body: string,
  286. avatar: string | null, clickCallback: any | null): boolean;
  287. clearCache(tag: string): void;
  288. }
  289. interface MessageService {
  290. getAccess(message: Message, receiver: Receiver): MessageAccess;
  291. createTemporary(receiver: Receiver, type: string, messageData: MessageData): Message;
  292. showStatusIcon(message: Message, receiver: Receiver): boolean;
  293. getFileName(message: Message): string;
  294. }
  295. interface MessageAccess {
  296. quote: boolean;
  297. ack: boolean;
  298. dec: boolean;
  299. delete: boolean;
  300. download: boolean;
  301. copy: boolean;
  302. }
  303. interface Quote {
  304. identity: string;
  305. text: string;
  306. }
  307. interface Identity {
  308. identity: string;
  309. publicNickname: String;
  310. publicKey: ArrayBuffer;
  311. fingerprint: string;
  312. }
  313. interface TrustedKeyStoreData {
  314. ownPublicKey: Uint8Array;
  315. ownSecretKey: Uint8Array;
  316. peerPublicKey: Uint8Array;
  317. pushToken: string | null;
  318. }
  319. interface TrustedKeyStoreService {
  320. blocked: boolean;
  321. storeTrustedKey(ownPublicKey: Uint8Array, ownSecretKey: Uint8Array, peerPublicKey: Uint8Array,
  322. pushToken: string | null, password: string): void;
  323. hasTrustedKey(): boolean;
  324. retrieveTrustedKey(password: string): TrustedKeyStoreData;
  325. clearTrustedKey(): void;
  326. }
  327. interface PushService {
  328. init(pushToken: string): void;
  329. reset(): void;
  330. isAvailable(): boolean;
  331. sendPush(session: Uint8Array): Promise<boolean>;
  332. }
  333. interface BrowserInfo {
  334. chrome: boolean;
  335. firefox: boolean;
  336. msie: boolean;
  337. opera: boolean;
  338. safari: boolean;
  339. version: string;
  340. textInfo: string;
  341. }
  342. interface BrowserService {
  343. getBrowser(): BrowserInfo;
  344. isVisible(): boolean;
  345. }
  346. interface TitleService {
  347. updateUnreadCount(count: number): void;
  348. }
  349. interface FingerPrintService {
  350. generate(publicKey: ArrayBuffer): string;
  351. }
  352. interface ContactService {
  353. requiredDetails(contactReceiver: ContactReceiver): Promise<ContactReceiver>;
  354. }
  355. interface ControllerModelService {
  356. contact(receiver: ContactReceiver, mode: any): ControllerModel;
  357. group(receiver: GroupReceiver, mode: any): ControllerModel;
  358. distributionList(receiver: DistributionListReceiver, mode: any): ControllerModel;
  359. }
  360. interface QrCodeService {
  361. buildQrCodePayload(initiatorKey: Uint8Array, authToken: Uint8Array, serverKey: Uint8Array | null,
  362. host: string, port: number,
  363. persistent: boolean): string;
  364. }
  365. interface PromiseCallbacks {
  366. resolve: (arg: any) => void;
  367. reject: (arg: any) => void;
  368. }
  369. interface PromiseRequestResult<T> {
  370. success: boolean;
  371. message?: string;
  372. data?: T;
  373. }
  374. interface ControllerModel {
  375. subject: string;
  376. isLoading: boolean;
  377. save(): any;
  378. isValid(): boolean;
  379. canEdit(): boolean;
  380. getMode(): number;
  381. setOnRemoved(callback: any): void;
  382. }
  383. interface AvatarControllerModel {
  384. onChangeAvatar: (image: ArrayBuffer) => void;
  385. getAvatar(): ArrayBuffer | null;
  386. }
  387. interface Alert {
  388. source: string;
  389. type: string;
  390. message: string;
  391. }
  392. interface ReceiverListener {
  393. onRemoved(receiver: Receiver);
  394. }
  395. interface Config {
  396. SELF_HOSTED: boolean;
  397. SALTYRTC_PORT: number;
  398. SALTYRTC_SERVER_KEY: string | null;
  399. SALTYRTC_HOST: string | null;
  400. SALTYRTC_HOST_PREFIX: string | null;
  401. SALTYRTC_HOST_SUFFIX: string | null;
  402. SALTYRTC_STUN: RTCIceServer;
  403. SALTYRTC_TURN: RTCIceServer;
  404. PUSH_URL: string;
  405. }
  406. interface BrowserMinVersions {
  407. FF: number;
  408. CHROME: number;
  409. OPERA: number;
  410. }
  411. interface MimeService {
  412. isImage(mimeType: string): boolean;
  413. isAudio(mimeType: string): boolean;
  414. isVideo(mimeType: string): boolean;
  415. getLabel(mimeType: string): string;
  416. getIconUrl(mimeType: string): string;
  417. }
  418. interface ReceiverService {
  419. setActive(activeReceiver: Receiver): void;
  420. getActive(): Receiver;
  421. isConversationActive(conversation: Conversation): boolean;
  422. compare(a: Conversation | Receiver, b: Conversation| Receiver): boolean;
  423. isContact(receiver: Receiver): boolean;
  424. isGroup(receiver: Receiver): boolean;
  425. isDistributionList(receiver: Receiver): boolean;
  426. isBusinessContact(receiver: Receiver): boolean;
  427. }
  428. interface WebClientDefault {
  429. getAvatar(type: string, highResolution: boolean): string;
  430. }
  431. interface WebClientService {
  432. salty: saltyrtc.SaltyRTC;
  433. messages: Container.Messages;
  434. conversations: Container.Conversations;
  435. receivers: Container.Receivers;
  436. alerts: Alert[];
  437. defaults: WebClientDefault;
  438. receiverListener: ReceiverListener[];
  439. me: MeReceiver;
  440. contacts: Map<string, ContactReceiver>;
  441. groups: Map<string, GroupReceiver>;
  442. distributionLists: Map<string, DistributionListReceiver>;
  443. typing: Container.Typing;
  444. buildQrCodePayload(persistent: boolean): string;
  445. init(keyStore?: saltyrtc.KeyStore, peerTrustedKey?: Uint8Array, resetField?: boolean): void;
  446. start(): ng.IPromise<any>;
  447. stop(requestedByUs: boolean, deleteStoredData?: boolean, resetPush?: boolean, redirect?: boolean): void;
  448. registerInitializationStep(name: string): void;
  449. setReceiverListener(listener: ReceiverListener): void;
  450. requestClientInfo(): void;
  451. requestReceivers(): void;
  452. requestConversations(): void;
  453. requestMessages(receiver: Receiver, reloadExisting?: boolean): number;
  454. requestAvatar(receiver: Receiver, highResolution: boolean): Promise<any>;
  455. requestThumbnail(receiver: Receiver, message: Message): Promise<any>;
  456. requestBlob(msgId: number, receiver: Receiver): Promise<ArrayBuffer>;
  457. requestRead(receiver, newestMessageId: number): void;
  458. requestContactDetail(contactReceiver: ContactReceiver): Promise<any>;
  459. sendMessage(receiver, type: MessageContentType, message: MessageData): Promise<Promise<any>>;
  460. ackMessage(receiver, message: Message, acknowledged?: boolean): void;
  461. deleteMessage(receiver, message: Message): void;
  462. sendMeIsTyping(receiver, isTyping: boolean): void;
  463. sendKeyPersisted(): void;
  464. addContact(threemaId: String): Promise<ContactReceiver>;
  465. modifyContact(threemaId: String, firstName: String, lastName: String, avatar?: ArrayBuffer):
  466. Promise<ContactReceiver>;
  467. createGroup(members: String[], name: String, avatar?: ArrayBuffer): Promise<GroupReceiver>;
  468. modifyGroup(id: string, members: String[], name: String, avatar?: ArrayBuffer): Promise<GroupReceiver>;
  469. leaveGroup(group: GroupReceiver): Promise<any>;
  470. deleteGroup(group: GroupReceiver): Promise<any>;
  471. syncGroup(group: GroupReceiver): Promise<any>;
  472. createDistributionList(members: String[], name: String): Promise<DistributionListReceiver>;
  473. modifyDistributionList(id: string, members: String[], name: String): Promise<DistributionListReceiver>;
  474. deleteDistributionList(distributionList: DistributionListReceiver): Promise<any>;
  475. isTyping(receiver: ContactReceiver): boolean;
  476. getMyIdentity(): Identity;
  477. getQuote(receiver: Receiver): Quote;
  478. setQuote(receiver: Receiver, message?: Message): void;
  479. setDraft(receiver: Receiver, message: string): void;
  480. getDraft(receiver: Receiver): string;
  481. setPassword(password: string): void;
  482. clearCache(): void;
  483. getMaxTextLength(): number;
  484. }
  485. interface ControllerService {
  486. setControllerName(name: string): void;
  487. getControllerName(): string;
  488. }
  489. interface StringService {
  490. /**
  491. * create chunks of a string by counting the used bytes
  492. * @param str
  493. * @param byteLength
  494. * @param offset
  495. */
  496. byteChunk(str: string, byteLength: number, offset: number): string[];
  497. }
  498. namespace Container {
  499. interface ReceiverData {
  500. me: MeReceiver;
  501. contacts: ContactReceiver[];
  502. groups: GroupReceiver[];
  503. distributionLists: DistributionListReceiver[];
  504. }
  505. interface Converter {
  506. unicodeToEmoji(message);
  507. addReceiverToConversation(receivers: Receivers);
  508. }
  509. interface Filters {
  510. hasData(receivers);
  511. hasContact(contacts);
  512. isValidMessage(contacts);
  513. }
  514. interface Receivers {
  515. me: MeReceiver;
  516. contacts: Map<string, ContactReceiver>;
  517. groups: Map<string, GroupReceiver>;
  518. distributionLists: Map<string, DistributionListReceiver>;
  519. get(receiverType: ReceiverType): Receiver | Map<string, Receiver>;
  520. getData(receiver: Receiver): Receiver | null;
  521. set(data: ReceiverData): void;
  522. setMe(data: MeReceiver): void;
  523. setContacts(data: ContactReceiver[]): void;
  524. setGroups(data: GroupReceiver[]): void;
  525. setDistributionLists(data: DistributionListReceiver[]): void;
  526. extend(receiverType: ReceiverType, data: Receiver): Receiver;
  527. extendDistributionList(data: DistributionListReceiver): DistributionListReceiver;
  528. extendGroup(data: GroupReceiver): GroupReceiver;
  529. extendMe(data: MeReceiver): MeReceiver;
  530. extendContact(data: ContactReceiver): ContactReceiver;
  531. }
  532. interface Conversations {
  533. get(): Conversation[];
  534. set(data: Conversation[]): void;
  535. add(conversation: Conversation): void;
  536. updateOrAdd(conversation: Conversation): void;
  537. remove(conversation: Conversation): void;
  538. setFilter(filter: (data: Conversation[]) => Conversation[]): void;
  539. setConverter(converter: (data: Conversation) => Conversation): void;
  540. }
  541. interface Messages {
  542. converter: (data: Message) => Message;
  543. getList(receiver: Receiver): Message[];
  544. clear($scope: ng.IScope): void;
  545. clearReceiverMessages(receiver: Receiver): Number;
  546. contains(receiver: Receiver): boolean;
  547. hasMore(receiver: Receiver): boolean;
  548. setMore(receiver: Receiver, more: boolean): void;
  549. getReferenceMsgId(receiver: Receiver): number;
  550. isRequested(receiver: Receiver): boolean;
  551. setRequested(receiver: Receiver): void;
  552. clearRequested(receiver): void;
  553. addNewer(receiver: Receiver, messages: Message[]): void;
  554. addOlder(receiver: Receiver, messages: Message[]): void;
  555. update(receiver: Receiver, message: Message): boolean;
  556. setThumbnail(receiver: Receiver, messageId: number, thumbnailImage: string): boolean;
  557. remove(receiver: Receiver, messageId: number): boolean;
  558. removeTemporary(receiver: Receiver, temporaryMessageId: string): boolean;
  559. bindTemporaryToMessageId(receiver: Receiver, temporaryId: string, messageId: number): boolean;
  560. notify(receiver: Receiver, $scope: ng.IScope): void;
  561. register(receiver: Receiver, $scope: ng.IScope, callback: any): Message[];
  562. updateFirstUnreadMessage(receiver: Receiver);
  563. }
  564. interface Typing {
  565. setTyping(receiver: ContactReceiver): void;
  566. unsetTyping(receiver: ContactReceiver): void;
  567. isTyping(receiver: ContactReceiver): boolean;
  568. }
  569. interface Drafts {
  570. setQuote(receiver: Receiver, quote: Quote): void;
  571. removeQuote(receiver: Receiver): void;
  572. getQuote(receiver: Receiver): Quote;
  573. setText(receiver: Receiver, draftMessage: string): void;
  574. removeText(receiver: Receiver): void;
  575. getText(receiver: Receiver): string;
  576. }
  577. interface Factory {
  578. Converter: Container.Converter;
  579. Filters: Container.Filters;
  580. createReceivers: () => Receivers;
  581. createConversations: () => Conversations;
  582. createMessages: () => Messages;
  583. createTyping: () => Typing;
  584. createDrafts: () => Drafts;
  585. }
  586. }
  587. }