diff options
author | 2019-12-20 19:21:22 +0100 | |
---|---|---|
committer | 2019-12-20 19:21:22 +0100 | |
commit | 7dd617a524c85a14c266aa1568d3a9a77b791df5 (patch) | |
tree | 0094bcfc073ddf7825410840b9da3ba35679cf88 /uncompressed/mastodon/index.js | |
parent | Add update script (diff) | |
download | ferdium-recipes-7dd617a524c85a14c266aa1568d3a9a77b791df5.tar.gz ferdium-recipes-7dd617a524c85a14c266aa1568d3a9a77b791df5.tar.zst ferdium-recipes-7dd617a524c85a14c266aa1568d3a9a77b791df5.zip |
#15 Add mastodon recipe
Diffstat (limited to 'uncompressed/mastodon/index.js')
-rw-r--r-- | uncompressed/mastodon/index.js | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/uncompressed/mastodon/index.js b/uncompressed/mastodon/index.js new file mode 100644 index 0000000..a2ca0b7 --- /dev/null +++ b/uncompressed/mastodon/index.js | |||
@@ -0,0 +1,53 @@ | |||
1 | //'use strict'; | ||
2 | |||
3 | module.exports = Franz => class Mastodon extends Franz { | ||
4 | |||
5 | constructor(...args) { | ||
6 | let _temp; | ||
7 | // | ||
8 | let serviceCache = {}; | ||
9 | // send service store for webview | ||
10 | setInterval(() => { | ||
11 | if (!window.franz) { // not present(near equal not initialize) | ||
12 | return; | ||
13 | } | ||
14 | const services = window.franz.stores.services; | ||
15 | // filter this recipe class | ||
16 | const instancedServices = services.all.filter(service => service.recipe.constructor === this.constructor); | ||
17 | // send | ||
18 | instancedServices.forEach(service => { | ||
19 | let updated = false; | ||
20 | serviceCache[service.id] = Object.keys(service).reduce((r, key) => { | ||
21 | if (!service[key] || Object !== service[key].constructor) { | ||
22 | updated = updated || serviceCache[key] !== service[key]; | ||
23 | r[key] = service[key]; | ||
24 | } | ||
25 | return r; | ||
26 | }, serviceCache[service.id] || {}); | ||
27 | if (updated) { | ||
28 | service.webview.send('-service-update', service); | ||
29 | } | ||
30 | }); | ||
31 | }, 1000); | ||
32 | return _temp = super(...args), this.events = { | ||
33 | }, _temp; | ||
34 | } | ||
35 | |||
36 | async validateUrl(url) { | ||
37 | try { | ||
38 | const res = await window.fetch(`${url}/api/v1/instance/`, { | ||
39 | method: 'GET', | ||
40 | headers: { | ||
41 | 'Content-Type': 'application/json', | ||
42 | }, | ||
43 | }); | ||
44 | const data = await res.json(); | ||
45 | // check url field in API response | ||
46 | return Object.hasOwnProperty.call(data, 'uri'); | ||
47 | } catch (err) { | ||
48 | console.error(err); | ||
49 | } | ||
50 | return false; | ||
51 | } | ||
52 | |||
53 | }; | ||