Skip to content

Commit

Permalink
feat(vercel): add avatar return support
Browse files Browse the repository at this point in the history
  • Loading branch information
lizheming committed Jan 10, 2021
1 parent 79f7966 commit 1ab3c9f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
6 changes: 4 additions & 2 deletions packages/server/src/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const {
TENCENTCLOUD_SECRETKEY,
TCB_KEY,
SECURE_DOMAINS,
DISABLE_USERAGENT
DISABLE_USERAGENT,
AVATAR_PROXY
} = process.env;

let storage = 'leancloud';
Expand Down Expand Up @@ -52,5 +53,6 @@ module.exports = {
forbiddenWords,
disallowIPList: [],
secureDomains: SECURE_DOMAINS ? SECURE_DOMAINS.split(/\s*,\s*/) : undefined,
disableUserAgent: DISABLE_USERAGENT && !['0', 'false'].includes(DISABLE_USERAGENT.toLowerCase())
disableUserAgent: DISABLE_USERAGENT && !['0', 'false'].includes(DISABLE_USERAGENT.toLowerCase()),
avatarProxy: AVATAR_PROXY || 'https://avatar.75cdn.workers.dev/'
};
22 changes: 15 additions & 7 deletions packages/server/src/controller/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ marked.setOptions({
smartypants: true
});

async function formatCmt({ua, user_id, ip, ...comment}, users = []) {
async function formatCmt({ua, user_id, ip, ...comment}, users = [], {avatarProxy}) {
ua = parser(ua);
if(!think.config('disableUserAgent')) {
comment.browser = [ua.browser.name, ua.browser.version].filter(v => v).join(' ');
Expand All @@ -26,11 +26,19 @@ async function formatCmt({ua, user_id, ip, ...comment}, users = []) {

if(user_id) {
const user = users.find(({objectId}) => user_id === objectId);

if(user) {
comment.nick = user.display_name;
comment.mail = user.email;
comment.link = user.link;
comment.type = user.type;

if(user.avatar) {
if(/(github)/i.test(user.avatar)) {
user.avatar = avatarProxy + '?url=' + encodeURIComponent(user.avatar);
}
comment.avatar = user.avatar;
}
}
}
comment.mail = helper.md5(comment.mail);
Expand Down Expand Up @@ -77,11 +85,11 @@ module.exports = class extends BaseRest {
let users = [];
if(user_ids.length) {
users = await userModel.select({objectId: ['IN', user_ids]}, {
field: ['display_name', 'email', 'url', 'type']
field: ['display_name', 'email', 'url', 'type', 'avatar']
});
}

return this.json(await Promise.all(comments.map(cmt => formatCmt(cmt, users))));
return this.json(await Promise.all(comments.map(cmt => formatCmt(cmt, users, this.config()))));
}

case 'count': {
Expand Down Expand Up @@ -147,7 +155,7 @@ module.exports = class extends BaseRest {
let users = [];
if(user_ids.length) {
users = await userModel.select({objectId: ['IN', user_ids]}, {
field: ['display_name', 'email', 'url', 'type']
field: ['display_name', 'email', 'url', 'type', 'avatar']
});
}

Expand All @@ -160,10 +168,10 @@ module.exports = class extends BaseRest {
totalPages: Math.ceil(rootCount / pageSize),
pageSize,
data: await Promise.all(rootComments.map(async comment => {
const cmt = await formatCmt(comment, users);
const cmt = await formatCmt(comment, users, this.config());
cmt.children = await Promise.all(comments
.filter(({rid}) => rid === cmt.objectId)
.map(cmt => formatCmt(cmt, users)));
.map(cmt => formatCmt(cmt, users, this.config())));
return cmt;
}))
});
Expand Down Expand Up @@ -249,7 +257,7 @@ module.exports = class extends BaseRest {
}

await this.hook('postSave', resp, pComment);
return this.success(await formatCmt(resp, [ this.ctx.state.userInfo ]));
return this.success(await formatCmt(resp, [ this.ctx.state.userInfo ], this.config()));
}

async putAction() {
Expand Down
15 changes: 12 additions & 3 deletions packages/server/src/controller/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = class extends think.Controller {
async githubAction() {
const instance = this.service('auth/github', this);
const userInfo = await instance.getUserInfo();
const {github} = userInfo;
const {github, avatar} = userInfo;

const userByGithub = await this.modelInstance.select({github});
if(!think.isEmpty(userByGithub)) {
Expand All @@ -30,7 +30,12 @@ module.exports = class extends think.Controller {
const {email} = userInfo;
const current = this.ctx.state.userInfo;
if(!think.isEmpty(current)) {
await this.modelInstance.update({github}, {objectId: current.objectId});
const updateData = {github};
if(!current.avatar) {
updateData.avatar = github.avatar;
}

await this.modelInstance.update(updateData, {objectId: current.objectId});
return this.success();
}

Expand All @@ -44,7 +49,11 @@ module.exports = class extends think.Controller {
};
await this.modelInstance.add(data);
} else {
await this.modelInstance.update({github}, {email});
const updateData = {github};
if(!userByEmail.avatar) {
updateData.avatar = avatar;
}
await this.modelInstance.update(updateData, {email});
}

const {redirect} = this.get();
Expand Down
6 changes: 6 additions & 0 deletions packages/server/src/controller/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ module.exports = class extends BaseRest {
return this.fail();
}

let avatar = users[0].avatar;
if(/(github)/i.test(avatar)) {
avatar = this.config('avatarProxy') + '?url=' + encodeURIComponent(avatar);
}
users[0].avatar = avatar;

return this.success({
...user[0],
password: null,
Expand Down

0 comments on commit 1ab3c9f

Please sign in to comment.