Skip to content

Commit

Permalink
fix(vercel): comment and article count api support multiple url query
Browse files Browse the repository at this point in the history
  • Loading branch information
lizheming committed Jan 13, 2021
1 parent c449353 commit 3fc206b
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 20 deletions.
25 changes: 18 additions & 7 deletions packages/client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,31 @@ export default function Waline({
}

//comment count
const $counts = [].slice.call(document.querySelectorAll('.waline-comment-count'));
const $counts = [].filter.call(document.querySelectorAll('.waline-comment-count'), el => {
if(!el.getAttribute('data-xid') && !el.getAttribute('id')) {
return false;
}
if(el.innerText && el.innerText.trim()) {
return false;
}
return true;
});
if($counts.length) {
$counts.filter(
el => el.getAttribute('data-xid') || el.getAttribute('id')
).filter(
el => !el.innerText?.trim()
).map(el => {
const paths = $counts.map(el => {
let path = el.getAttribute('data-xid') || el.getAttribute('id');
try {
path = decodeURI(path);
} catch(e) {
//ignore error
}
return fetchCount({serverURL, path}).then(count => (el.innerText = count));
return path;
});

fetchCount({serverURL, path: paths}).then(counts => {
if(!Array.isArray(counts)) {
counts = [counts];
}
$counts.forEach((el, idx) => (el.innerText = counts[idx]));
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/utils/fetch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export function fetchCount({serverURL, path}) {
const url = `${serverURL}/comment?type=count&url=${encodeURIComponent(path)}`;
return fetch(url).then(resp => resp.text());
return fetch(url).then(resp => resp.json());
}

export function fetchRecent({serverURL, count}) {
Expand Down
20 changes: 14 additions & 6 deletions packages/client/src/utils/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,27 @@ export default {
return;
}

[].forEach.call(countEls, el => {
const ids = [].map.call(countEls, el => {
let id = el.getAttribute('id');
try {
id = decodeURI(id);
} catch(e) {
//ignore error
}

let counterEl = el.querySelector('.leancloud-visitors-count');
if(!counterEl) {
counterEl = el;
return id;
});

fetchVisitCount({serverURL, path: ids.join()}).then(counts => {
if(!Array.isArray(counts)) {
counts = [counts];
}
fetchVisitCount({serverURL, path: id}).then(count => (counterEl.innerText = count));
countEls.forEach((el, idx) => {
let counterEl = el.querySelector('.leancloud-visitors-count');
if(!counterEl) {
counterEl = el;
}
counterEl.innerText = counts[idx];
});
});
}
}
16 changes: 14 additions & 2 deletions packages/server/src/controller/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,23 @@ module.exports = class extends BaseRest {

async getAction() {
const {path} = this.get();
const resp = await this.modelInstance.select({url: path}, {limit: 1});
if(!Array.isArray(path) || !path.length) {
return this.json(0);
}

const resp = await this.modelInstance.select({url: ['IN', path]});
if(think.isEmpty(resp)) {
return this.json(0);
}
return this.json(resp[0].time);

if(path.length === 1) {
return this.json(resp[0].time);
}
const respObj = resp.reduce((o, n) => {
o[n.url] = n.time;
return o;
}, {});
return this.json(path.map(url => respObj[url] || 0));
}

async postAction() {
Expand Down
10 changes: 8 additions & 2 deletions packages/server/src/controller/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,14 @@ module.exports = class extends BaseRest {

case 'count': {
const {url} = this.get();
const count = await this.modelInstance.count({url, status: ['NOT IN', ['waiting', 'spam']]});
return this.json(count);
const data = await this.modelInstance.select({
url: ['IN', url],
status: ['NOT IN', ['waiting', 'spam']]
}, {
field: ['url']
});
const counts = url.map(u => data.filter(({url}) => url === u).length);
return this.json(counts.length === 1 ? counts[0] : counts);
}

case 'list': {
Expand Down
8 changes: 7 additions & 1 deletion packages/server/src/logic/article.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const Base = require('./base');

module.exports = class extends Base {

getAction() {
this.rules = {
path: {
array: true
}
};
}
}
2 changes: 1 addition & 1 deletion packages/server/src/logic/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = class extends Base {
case 'count':
this.rules = {
url: {
string: true,
array: true,
required: true
}
};
Expand Down

0 comments on commit 3fc206b

Please sign in to comment.