threema.d.ts 21 KB

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