From 252d496dc2f972decb6c8d97385406b2456c6d12 Mon Sep 17 00:00:00 2001 From: Jiiks Date: Wed, 6 Mar 2019 02:57:57 +0200 Subject: [PATCH] Get release from github, todo use common --- core/src/modules/axi.js | 53 +++++++++++++++++++++++++++++++++++++ core/src/modules/updater.js | 30 +++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 core/src/modules/axi.js diff --git a/core/src/modules/axi.js b/core/src/modules/axi.js new file mode 100644 index 00000000..84023088 --- /dev/null +++ b/core/src/modules/axi.js @@ -0,0 +1,53 @@ +/** + * BetterDiscord axios wrapper + * Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks + * All rights reserved. + * https://betterdiscord.net + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. +*/ + +import axios from 'axios'; + +export default class AxiosWrapper { + + static get axios() { return axios; } + + static get(url) { return axios.get(url) } + + static get github() { + return this._github ? this._github : ( + this._github = { + main: this.create('https://github.com'), + api: this.create('https://api.github.com') + } + ); + } + + static get zl() { + return this._zl ? this._zl : (this._zl = { + api: this.create('https://zl', 1000, this.zlHeaders), + cdn: this.create('https://zl', 1000, this.zlHeaders) + }); + } + + static create(baseUrl, timeout = 1000, headers = null) { + return axios.create({ baseURL: baseUrl, timeout, headers: headers ? headers : this.defaultHeaders }); + } + + static get defaultHeaders() { + return { + 'User-Agent': 'BetterDiscordApp User' + }; + } + + static get zlHeaders() { + return { + 'User-Agent': 'BetterDiscordApp User', + 'X-ZL-Apikey': '1a20cce89a2dbd163fc9570f3246c20891e62b2818ada55f82fa3d1d96fa7ef4', + 'X-ZL-User': 'anonymous' + } + } + +} diff --git a/core/src/modules/updater.js b/core/src/modules/updater.js index 70a27c76..c5f8c954 100644 --- a/core/src/modules/updater.js +++ b/core/src/modules/updater.js @@ -10,6 +10,7 @@ import Module from './modulebase'; import semver from 'semver'; +import Axi from './axi'; const TEST_UPDATE = [ { @@ -84,6 +85,32 @@ export default class Updater extends Module { this.updaterThread = setInterval(this.checkForUpdates, interval * 60 * 1000); } + validate(releaseInfo) { + return releaseInfo && + typeof releaseInfo === 'object' && + releaseInfo.files && + Array.isArray(releaseInfo.files) && + releaseInfo.files.length >= 4; + } + + async latestRelease() { + try { + const release = await Axi.github.api.get('repos/JsSucks/BetterDiscordApp/releases/latest'); // TODO replace with config + const releaseInfoAsset = release.data.assets.find(asset => asset.name === 'releaseinfo.json'); + const releaseInfo = await Axi.get(releaseInfoAsset['browser_download_url']); + + if (this.validate(releaseInfo.data)) return releaseInfo.data; + return this.latestReleaseFallback(); + } catch (err) { + console.log(err); + return this.latestReleaseFallback(); + } + } + + async latestReleaseFallback() { + console.log('fallback'); + } + async checkForUpdates() { console.log('[BetterDiscord:Updater] Checking for updates'); this.bd.sendToDiscord('updater-checkForUpdates', ''); @@ -92,6 +119,9 @@ export default class Updater extends Module { const { coreVersion, clientVersion, editorVersion } = this.bd.config; const releaseInfo = new ReleaseInfo({ core: coreVersion, client: clientVersion, editor: editorVersion }); + const latestRelease = await this.latestRelease(); + console.log(latestRelease); + releaseInfo.test(); this.debug(releaseInfo);