mirror of
https://git.mirrors.martin98.com/https://github.com/sub-store-org/Sub-Store.git
synced 2025-07-29 21:52:03 +08:00
feat: Fetch avatar from GitHub automatically
This commit is contained in:
parent
7468089f04
commit
0e089ef8ce
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "sub-store",
|
"name": "sub-store",
|
||||||
"version": "2.5.4",
|
"version": "2.6.0",
|
||||||
"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": {
|
||||||
|
@ -3,7 +3,9 @@ import { success } from './response';
|
|||||||
import $ from '@/core/app';
|
import $ from '@/core/app';
|
||||||
|
|
||||||
export default function register($app) {
|
export default function register($app) {
|
||||||
if (!$.read(SETTINGS_KEY)) $.write({}, SETTINGS_KEY);
|
const settings = $.read(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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -12,12 +14,35 @@ function getSettings(req, res) {
|
|||||||
success(res, settings);
|
success(res, settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateSettings(req, res) {
|
async function updateSettings(req, res) {
|
||||||
const settings = $.read(SETTINGS_KEY);
|
const settings = $.read(SETTINGS_KEY);
|
||||||
const newSettings = {
|
const newSettings = {
|
||||||
...settings,
|
...settings,
|
||||||
...req.body,
|
...req.body,
|
||||||
};
|
};
|
||||||
$.write(newSettings, SETTINGS_KEY);
|
$.write(newSettings, SETTINGS_KEY);
|
||||||
|
await updateGitHubAvatar();
|
||||||
success(res, newSettings);
|
success(res, newSettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function updateGitHubAvatar() {
|
||||||
|
const settings = $.read(SETTINGS_KEY);
|
||||||
|
const username = settings.githubUser;
|
||||||
|
if (username) {
|
||||||
|
try {
|
||||||
|
const data = await $.http
|
||||||
|
.get({
|
||||||
|
url: `https://api.github.com/users/${username}`,
|
||||||
|
headers: {
|
||||||
|
'User-Agent':
|
||||||
|
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.then((resp) => JSON.parse(resp.body));
|
||||||
|
settings.avatarUrl = data['avatar_url'];
|
||||||
|
$.write(settings, SETTINGS_KEY);
|
||||||
|
} catch (e) {
|
||||||
|
$.error('Failed to fetch GitHub avatar for User: ' + username);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user