Skip to content

Commit

Permalink
feat(route): add back matters (#15687)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyRL authored May 24, 2024
1 parent 3a58d90 commit f93bb8c
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 180 deletions.
6 changes: 0 additions & 6 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,12 +594,6 @@ router.get('/zucc/cssearch/latest/:webVpn/:key', lazyloadRouteHandler('./routes/
// checkee
router.get('/checkee/:dispdate', lazyloadRouteHandler('./routes/checkee/index'));

// Matters
router.get('/matters/latest/:type?', lazyloadRouteHandler('./routes/matters/latest'));
router.redirect('/matters/hot', '/matters/latest/heat'); // Deprecated
router.get('/matters/tags/:tid', lazyloadRouteHandler('./routes/matters/tags'));
router.get('/matters/author/:uid', lazyloadRouteHandler('./routes/matters/author'));

// 古诗文网
router.get('/gushiwen/recommend/:annotation?', lazyloadRouteHandler('./routes/gushiwen/recommend'));

Expand Down
49 changes: 0 additions & 49 deletions lib/routes-deprecated/matters/author.js

This file was deleted.

69 changes: 0 additions & 69 deletions lib/routes-deprecated/matters/latest.js

This file was deleted.

56 changes: 0 additions & 56 deletions lib/routes-deprecated/matters/tags.js

This file was deleted.

63 changes: 63 additions & 0 deletions lib/routes/matters/author.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Route } from '@/types';
import ofetch from '@/utils/ofetch';
import { baseUrl, gqlEndpoint, parseItem } from './utils';

const handler = async (ctx) => {
const { uid } = ctx.req.param();
const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 20;
const response = await ofetch(gqlEndpoint, {
method: 'POST',
body: {
query: `{
user(input: {userName: "${uid}"}) {
displayName
avatar
info {
description
}
articles(input: {first: ${limit}}) {
edges {
node {
shortHash
title
content
createdAt
author {
displayName
}
tags {
content
}
}
}
}
}
}`,
},
});

const user = response.data.user;

return {
title: `Matters | ${user.displayName}`,
link: `${baseUrl}/@${uid}`,
description: user.info.description,
image: user.avatar,
item: user.articles.edges.map(({ node }) => parseItem(node)),
};
};

export const route: Route = {
path: '/author/:uid',
name: 'Author',
example: '/matters/author/robertu',
parameters: { uid: "Author id, can be found at author's homepage url" },
maintainers: ['Cerebrater', 'xosdy'],
handler,
radar: [
{
source: ['matters.town/:uid'],
target: (params) => `/matters/author/${params.uid.slice(1)}`,
},
],
};
74 changes: 74 additions & 0 deletions lib/routes/matters/latest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Route } from '@/types';
import ofetch from '@/utils/ofetch';
import { baseUrl, gqlEndpoint, parseItem } from './utils';

const handler = async (ctx) => {
const { type = 'latest' } = ctx.req.param();
const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 20;
const options = {
latest: {
title: '最新',
apiType: 'newest',
},
heat: {
title: '熱議',
apiType: 'hottest',
},
essence: {
title: '精華',
apiType: 'icymi',
},
};

const response = await ofetch(gqlEndpoint, {
method: 'POST',
body: {
query: `{
viewer {
recommendation {
feed: ${options[type].apiType}(input: {first: ${limit}}) {
edges {
node {
shortHash
title
content
createdAt
author {
displayName
}
tags {
content
}
}
}
}
}
}
}`,
},
});

const item = response.data.viewer.recommendation.feed.edges.map(({ node }) => parseItem(node));

return {
title: `Matters | ${options[type].title}`,
link: baseUrl,
item,
};
};
export const route: Route = {
path: '/latest/:type?',
name: 'Latest, heat, essence',
example: '/matters/latest/heat',
parameters: { uid: 'Defaults to latest, see table below' },
maintainers: ['xyqfer', 'Cerebrater', 'xosdy'],
handler,
radar: [
{
source: ['matters.town'],
},
],
description: `| 最新 | 热门 | 精华 |
| ------ | ---- | ------- |
| latest | heat | essence |`,
};
7 changes: 7 additions & 0 deletions lib/routes/matters/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: 'Matters',
url: 'matters.town',
categories: ['new-media'],
};
Loading

0 comments on commit f93bb8c

Please sign in to comment.