feat: Add artifactStore URL in settings

This commit is contained in:
Peng-YM 2022-07-07 22:40:43 +08:00
parent 240156daef
commit 07cae95ff4
5 changed files with 36 additions and 13 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{ {
"name": "sub-store", "name": "sub-store",
"version": "2.8.0", "version": "2.8.1",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js", "main": "src/main.js",
"scripts": { "scripts": {

View File

@ -1,16 +1,18 @@
import { SETTINGS_KEY } from '@/constants'; import { SETTINGS_KEY, ARTIFACT_REPOSITORY_KEY } from '@/constants';
import { success } from './response'; import { success } from './response';
import $ from '@/core/app'; import $ from '@/core/app';
import Gist from '@/utils/gist';
export default function register($app) { export default function register($app) {
const settings = $.read(SETTINGS_KEY); const settings = $.read(SETTINGS_KEY);
if (!settings) $.write({}, SETTINGS_KEY); if (!settings) $.write({}, SETTINGS_KEY);
if (!settings.avatarUrl) updateGitHubAvatar();
$app.route('/api/settings').get(getSettings).patch(updateSettings); $app.route('/api/settings').get(getSettings).patch(updateSettings);
} }
function getSettings(req, res) { async function getSettings(req, res) {
const settings = $.read(SETTINGS_KEY); const settings = $.read(SETTINGS_KEY);
if (!settings.avatarUrl) await updateGitHubAvatar();
if (!settings.artifactStore) await updateArtifactStore();
success(res, settings); success(res, settings);
} }
@ -22,6 +24,7 @@ async function updateSettings(req, res) {
}; };
$.write(newSettings, SETTINGS_KEY); $.write(newSettings, SETTINGS_KEY);
await updateGitHubAvatar(); await updateGitHubAvatar();
await updateArtifactStore();
success(res, newSettings); success(res, newSettings);
} }
@ -46,3 +49,23 @@ async function updateGitHubAvatar() {
} }
} }
} }
async function updateArtifactStore() {
console.log('Updating artifact store');
const settings = $.read(SETTINGS_KEY);
const { githubUser, gistToken } = settings;
if (githubUser && gistToken) {
const manager = new Gist({
token: gistToken,
key: ARTIFACT_REPOSITORY_KEY,
});
try {
const gistId = await manager.locate();
settings.artifactStore = `https://gist.github.com/${githubUser}/${gistId}`;
$.write(settings, SETTINGS_KEY);
} catch (err) {
$.error('Failed to fetch artifact store for User: ' + githubUser);
}
}
}

File diff suppressed because one or more lines are too long