From 10bc69d3fe231d4b42b16ffccacc6c4d8a146d1e Mon Sep 17 00:00:00 2001 From: FledgeShiu Date: Thu, 9 Sep 2021 16:45:22 +0800 Subject: [PATCH] fix(route): matters pubDate and API add pubDate to /matters/* and move /matters/tags to graphql api. (#8056) Co-authored-by: GitHub Action --- docs/new-media.md | 10 ++-- lib/routes/matters/author.js | 3 ++ lib/routes/matters/latest.js | 3 +- lib/routes/matters/tags.js | 90 +++++++++++++++++++----------------- 4 files changed, 58 insertions(+), 48 deletions(-) diff --git a/docs/new-media.md b/docs/new-media.md index 35390873f7..cebd7eaa88 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -497,11 +497,11 @@ IPFS 网关有可能失效,那时候换成其他网关。 ## Matters -### 最新、熱議、精華 +### 最新、热门、精华 - + -| 最新 | 熱議 | 精華 | +| 最新 | 热门 | 精华 | | ------ | ---- | ------- | | latest | heat | essence | @@ -509,11 +509,11 @@ IPFS 网关有可能失效,那时候换成其他网关。 ### 标签 - + ### 作者 - + ## MIT 科技评论 diff --git a/lib/routes/matters/author.js b/lib/routes/matters/author.js index e552ada008..2e6e65730b 100644 --- a/lib/routes/matters/author.js +++ b/lib/routes/matters/author.js @@ -1,4 +1,5 @@ const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); module.exports = async (ctx) => { const uid = ctx.params.uid; @@ -22,6 +23,7 @@ module.exports = async (ctx) => { mediaHash title content + createdAt } } } @@ -41,6 +43,7 @@ module.exports = async (ctx) => { author: user.displayName, description: article.content, link: `${host}/@${uid}/${article.slug}-${article.mediaHash}`, + pubDate: parseDate(article.createdAt), })), }; }; diff --git a/lib/routes/matters/latest.js b/lib/routes/matters/latest.js index 0b47b72a53..a68eb37011 100644 --- a/lib/routes/matters/latest.js +++ b/lib/routes/matters/latest.js @@ -1,4 +1,5 @@ const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); module.exports = async (ctx) => { const type = ctx.params.type || 'latest'; @@ -56,7 +57,7 @@ module.exports = async (ctx) => { link, description: article, author: node.author.displayName, - pubDate: node.createdAt, + pubDate: parseDate(node.createdAt), }; }); diff --git a/lib/routes/matters/tags.js b/lib/routes/matters/tags.js index b86b1cadb0..14629f5a7a 100644 --- a/lib/routes/matters/tags.js +++ b/lib/routes/matters/tags.js @@ -1,50 +1,56 @@ -const cheerio = require('cheerio'); const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); module.exports = async (ctx) => { - const url = `https://matters.news/tags/${ctx.params.tid}`; + const tid = ctx.params.tid; + const host = `https://matters.news`; + const url = `https://server.matters.news/graphql`; + const response = await got({ + method: 'post', + url, + json: { + query: ` + { + node(input: { id: "${tid}" }) { + ... on Tag { + content + articles(input:{first: 20}){ + edges { + node { + id + title + slug + cover + summary + mediaHash + content + createdAt + author { + id + userName + displayName + } + } + } + } + } + } + }`, + }, + }); - const res = await got.get(url); - const $ = cheerio.load(res.data); - const list = $('section.list-item').get(); + const node = response.data.data.node; - const proList = []; - const indexList = []; - - const out = await Promise.all( - list.map(async (item, i) => { - const $ = cheerio.load(item); - const time = $('time').attr('datetime'); - const title = $('h2').text(); - const author = $('span.displayname').text(); - const postfix = encodeURI($('section.title > a').attr('href')); - const address = `https://matters.news${postfix}`; - const cache = await ctx.cache.get(address); - if (cache) { - return Promise.resolve(JSON.parse(cache)); - } - const single = { - title, - pubDate: new Date(time).toUTCString(), - author, - link: address, - guid: address, - }; - proList.push(got.get(address)); - indexList.push(i); - return Promise.resolve(single); - }) - ); - const responses = await got.all(proList); - for (let i = 0; i < responses.length; i++) { - const res = responses[i]; - const $ = cheerio.load(res.data); - out[indexList[i]].description = $('.u-content').html(); - ctx.cache.set(out[indexList[i]].link, JSON.stringify(out[i])); - } ctx.state.data = { - title: $('title').text(), - link: url, - item: out, + title: `Matters | ${node.content}`, + link: `${host}/tags/${tid}`, + description: node.content, + item: node.articles.edges.map(({ node: article }) => ({ + title: article.title, + author: article.author.displayName, + description: article.content, + link: `${host}/@${article.author.id}/${article.slug}-${article.mediaHash}`, + pubDate: parseDate(article.createdAt), + })), }; };