diff --git a/config/default.json b/config/default.json index 31462adf2..ee40150e8 100644 --- a/config/default.json +++ b/config/default.json @@ -31,5 +31,6 @@ "-----BEGIN CERTIFICATE-----\nMIID7zCCAtegAwIBAgIJAIm6LatK5PNiMA0GCSqGSIb3DQEBBQUAMIGNMQswCQYD\nVQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNU2FuIEZyYW5j\naXNjbzEdMBsGA1UECgwUT3BlbiBXaGlzcGVyIFN5c3RlbXMxHTAbBgNVBAsMFE9w\nZW4gV2hpc3BlciBTeXN0ZW1zMRMwEQYDVQQDDApUZXh0U2VjdXJlMB4XDTEzMDMy\nNTIyMTgzNVoXDTIzMDMyMzIyMTgzNVowgY0xCzAJBgNVBAYTAlVTMRMwEQYDVQQI\nDApDYWxpZm9ybmlhMRYwFAYDVQQHDA1TYW4gRnJhbmNpc2NvMR0wGwYDVQQKDBRP\ncGVuIFdoaXNwZXIgU3lzdGVtczEdMBsGA1UECwwUT3BlbiBXaGlzcGVyIFN5c3Rl\nbXMxEzARBgNVBAMMClRleHRTZWN1cmUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw\nggEKAoIBAQDBSWBpOCBDF0i4q2d4jAXkSXUGpbeWugVPQCjaL6qD9QDOxeW1afvf\nPo863i6Crq1KDxHpB36EwzVcjwLkFTIMeo7t9s1FQolAt3mErV2U0vie6Ves+yj6\ngrSfxwIDAcdsKmI0a1SQCZlr3Q1tcHAkAKFRxYNawADyps5B+Zmqcgf653TXS5/0\nIPPQLocLn8GWLwOYNnYfBvILKDMItmZTtEbucdigxEA9mfIvvHADEbteLtVgwBm9\nR5vVvtwrD6CCxI3pgH7EH7kMP0Od93wLisvn1yhHY7FuYlrkYqdkMvWUrKoASVw4\njb69vaeJCUdU+HCoXOSP1PQcL6WenNCHAgMBAAGjUDBOMB0GA1UdDgQWBBQBixjx\nP/s5GURuhYa+lGUypzI8kDAfBgNVHSMEGDAWgBQBixjxP/s5GURuhYa+lGUypzI8\nkDAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4IBAQB+Hr4hC56m0LvJAu1R\nK6NuPDbTMEN7/jMojFHxH4P3XPFfupjR+bkDq0pPOU6JjIxnrD1XD/EVmTTaTVY5\niOheyv7UzJOefb2pLOc9qsuvI4fnaESh9bhzln+LXxtCrRPGhkxA1IMIo3J/s2WF\n/KVYZyciu6b4ubJ91XPAuBNZwImug7/srWvbpk0hq6A6z140WTVSKtJG7EP41kJe\n/oF4usY5J7LPkxK3LWzMJnb5EIJDmRvyH8pyRwWg6Qm6qiGFaI4nL8QU4La1x2en\n4DGXRaLMPRwjELNgQPodR38zoCMuA8gHZfZYYoZ7D7Q1wNUiVHcxuFrEeBaYJbLE\nrwLV\n-----END CERTIFICATE-----\n", "import": false, "serverTrustRoot": "BbqY1DzohE4NUZoVF+L18oUPrK3kILllLEJh2UnPSsEx", - "defaultPublicChatServer": "https://chat.lokinet.org/" + "defaultPublicChatServer": "https://chat.lokinet.org/", + "defaultFileServer": "https://file.lokinet.org" } diff --git a/js/background.js b/js/background.js index 5e656247c..3f6e34673 100644 --- a/js/background.js +++ b/js/background.js @@ -229,7 +229,10 @@ const ourKey = textsecure.storage.user.getNumber(); window.feeds = []; window.lokiMessageAPI = new window.LokiMessageAPI(ourKey); + // singleton to relay events to libtextsecure/message_receiver window.lokiPublicChatAPI = new window.LokiPublicChatAPI(ourKey); + // singleton to interface the File server + window.lokiFileServerAPI = new window.LokiFileServerAPI(ourKey); window.lokiP2pAPI = new window.LokiP2pAPI(ourKey); window.lokiP2pAPI.on('pingContact', pubKey => { const isPing = true; diff --git a/js/modules/loki_file_server_api.js b/js/modules/loki_file_server_api.js new file mode 100644 index 000000000..a07d20f5d --- /dev/null +++ b/js/modules/loki_file_server_api.js @@ -0,0 +1,40 @@ +/* global storage: false */ +/* global libloki: false */ +/* global Signal: false */ + +const LokiAppDotNetAPI = require('./loki_app_dot_net_api'); + +const DEVICE_MAPPING_ANNOTATION_KEY = 'network.loki.messenger.devicemapping'; + +// returns the LokiFileServerAPI constructor with the serverUrl already consumed +function LokiFileServerAPIWrapper(serverUrl) { + return LokiFileServerAPI.bind(null, serverUrl); +} + +class LokiFileServerAPI { + constructor(serverUrl, ourKey) { + this.ourKey = ourKey; + this._adnApi = new LokiAppDotNetAPI(ourKey); + this._server = this._adnApi.findOrCreateServer(serverUrl); + } + + async getUserDeviceMapping(pubKey) { + const annotations = await this._server.getUserAnnotations(pubKey); + return annotations.find( + annotation => annotation.type === DEVICE_MAPPING_ANNOTATION_KEY + ); + } + + setOurDeviceMapping(authorisations, isPrimary) { + const content = { + isPrimary: isPrimary ? '1' : '0', + authorisations, + }; + return this._server.setSelfAnnotation( + DEVICE_MAPPING_ANNOTATION_KEY, + content + ); + } +} + +module.exports = LokiFileServerAPIWrapper; diff --git a/main.js b/main.js index 8b545fafc..f5e608004 100644 --- a/main.js +++ b/main.js @@ -166,6 +166,7 @@ function prepareURL(pathSegments, moreKeys) { contentProxyUrl: config.contentProxyUrl, importMode: importMode ? true : undefined, // for stringify() serverTrustRoot: config.get('serverTrustRoot'), + defaultFileServer: config.get('defaultFileServer'), ...moreKeys, }, }); diff --git a/preload.js b/preload.js index fc5b12ca0..39ffb5bc2 100644 --- a/preload.js +++ b/preload.js @@ -326,6 +326,11 @@ window.LokiMessageAPI = require('./js/modules/loki_message_api'); window.LokiPublicChatAPI = require('./js/modules/loki_public_chat_api'); +const LokiFileServerAPIWrapper = require('./js/modules/loki_file_server_api'); + +// bind first argument as we have it here already +window.LokiFileServerAPI = LokiFileServerAPIWrapper(config.defaultFileServer); + window.LokiRssAPI = require('./js/modules/loki_rss_api'); window.LocalLokiServer = require('./libloki/modules/local_loki_server');