Skip to content

Commit

Permalink
feat(route): daily.dev user (DIYgod#15403)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyRL authored Apr 29, 2024
1 parent 3cb4b74 commit 715bad0
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 45 deletions.
11 changes: 0 additions & 11 deletions lib/routes/daily/discussed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,10 @@ const graphqlQuery = {

export const route: Route = {
path: '/discussed',
categories: ['social-media'],
example: '/daily/discussed',
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['daily.dev/popular'],
target: '',
},
],
name: 'Most Discussed',
Expand Down
4 changes: 2 additions & 2 deletions lib/routes/daily/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ const graphqlQuery = {

export const route: Route = {
path: '/',
example: '/daily',
radar: [
{
source: ['daily.dev/popular'],
target: '',
},
],
name: 'Unknown',
name: 'Popular',
maintainers: ['Rjnishant530'],
handler,
url: 'daily.dev/popular',
Expand Down
1 change: 1 addition & 0 deletions lib/routes/daily/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import type { Namespace } from '@/types';
export const namespace: Namespace = {
name: 'Daily.dev',
url: 'daily.dev',
categories: ['social-media'],
};
7 changes: 7 additions & 0 deletions lib/routes/daily/templates/posts.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{ if image }}
<img src="{{ image }}"><br>
{{ /if }}

{{ if content }}
{{@ content }}
{{ /if }}
21 changes: 5 additions & 16 deletions lib/routes/daily/upvoted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ const query = `
...FeedPostConnection
}
}
fragment FeedPostConnection on PostConnection {
edges {
node {
...FeedPost
}
}
}
fragment FeedPost on Post {
...SharedPostInfo
}
fragment SharedPostInfo on Post {
id
title
Expand All @@ -44,7 +44,7 @@ const query = `
}
tags
}
fragment UserShortInfo on User {
id
name
Expand All @@ -53,7 +53,7 @@ const query = `
username
bio
}
`;

const graphqlQuery = {
Expand All @@ -63,21 +63,10 @@ const graphqlQuery = {

export const route: Route = {
path: '/upvoted',
categories: ['social-media'],
example: '/daily/upvoted',
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['daily.dev/popular'],
target: '',
},
],
name: 'Most upvoted',
Expand Down
227 changes: 227 additions & 0 deletions lib/routes/daily/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
import { Route } from '@/types';
import { baseUrl, getBuildId, getData } from './utils';
import ofetch from '@/utils/ofetch';
import cache from '@/utils/cache';
import { config } from '@/config';
import { parseDate } from '@/utils/parse-date';
import { art } from '@/utils/render';
import path from 'path';
import { getCurrentPath } from '@/utils/helpers';
const __dirname = getCurrentPath(import.meta.url);

const userPostQuery = `
query AuthorFeed(
$loggedIn: Boolean! = false
$userId: ID!
$after: String
$first: Int
$supportedTypes: [String!] = [
"article"
"share"
"freeform"
"video:youtube"
"collection"
]
) {
page: authorFeed(
author: $userId
after: $after
first: $first
ranking: TIME
supportedTypes: $supportedTypes
) {
...FeedPostConnection
}
}
fragment FeedPostConnection on PostConnection {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
...FeedPost
contentHtml
...UserPost @include(if: $loggedIn)
}
}
}
fragment FeedPost on Post {
...SharedPostInfo
sharedPost {
...SharedPostInfo
}
trending
feedMeta
collectionSources {
handle
image
}
numCollectionSources
updatedAt
slug
}
fragment SharedPostInfo on Post {
id
title
titleHtml
image
readTime
permalink
commentsPermalink
summary
createdAt
private
upvoted
commented
bookmarked
views
numUpvotes
numComments
videoId
scout {
...UserShortInfo
}
author {
...UserShortInfo
}
type
tags
source {
...SourceBaseInfo
}
downvoted
flags {
promoteToPublic
}
userState {
vote
flags {
feedbackDismiss
}
}
slug
}
fragment SourceBaseInfo on Source {
id
active
handle
name
permalink
public
type
description
image
membersCount
privilegedMembers {
user {
id
}
role
}
currentMember {
...CurrentMember
}
memberPostingRole
memberInviteRole
}
fragment CurrentMember on SourceMember {
user {
id
}
permissions
role
referralToken
flags {
hideFeedPosts
collapsePinnedPosts
}
}
fragment UserShortInfo on User {
id
name
image
permalink
username
bio
createdAt
reputation
}
fragment UserPost on Post {
read
upvoted
commented
bookmarked
downvoted
}`;

const render = (data) => art(path.join(__dirname, 'templates/posts.art'), data);

export const route: Route = {
path: '/user/:userId',
example: '/daily/user/kramer',
radar: [
{
source: ['daily.dev/:userId/posts', 'daily.dev/:userId'],
},
],
name: 'User Posts',
maintainers: ['TonyRL'],
handler,
url: 'daily.dev',
};

async function handler(ctx) {
const userId = ctx.req.param('userId');
const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 7;

const buildId = await getBuildId();

const userData = await cache.tryGet(`daily:user:${userId}`, async () => {
const resposne = await ofetch(`${baseUrl}/_next/data/${buildId}/en/${userId}.json`, {
query: {
userId,
},
});
return resposne.pageProps;
});
const user = (userData as any).user;

const items = await cache.tryGet(
`daily:user:${userId}:posts`,
async () => {
const edges = await getData({
query: userPostQuery,
variables: {
userId: user.id,
first: limit,
loggedIn: false,
},
});
return edges.map(({ node }) => ({
title: node.title,
description: render({
image: node.image,
content: node.contentHtml?.replaceAll('\n', '<br>') ?? node.summary,
}),
link: node.permalink,
author: node.author?.name,
category: node.tags,
pubDate: parseDate(node.createdAt),
}));
},
config.cache.routeExpire,
false
);

return {
title: `${user.name} | daily.dev`,
description: user.bio,
link: `${baseUrl}/${userId}/posts`,
item: items,
image: user.image,
logo: user.image,
icon: user.image,
language: 'en-us',
};
}
Loading

0 comments on commit 715bad0

Please sign in to comment.