Skip to content

Commit 0343e0c

Browse files
committed
feat: v4.40.0
1 parent 6556c66 commit 0343e0c

File tree

17 files changed

+157
-276
lines changed

17 files changed

+157
-276
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "surmon.me",
3-
"version": "4.39.0",
3+
"version": "4.40.0",
44
"description": "Surmon.me blog",
55
"author": "Surmon",
66
"license": "MIT",

src/bff.ts

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import { getGTagScript } from './server/getters/gtag'
1616
import { getAllWallpapers } from './server/getters/wallpaper'
1717
import { getMyGoogleMap } from './server/getters/my-google-map'
1818
import { getTwitterProfile, getTwitterTweets } from './server/getters/twitter'
19-
import { getGitHubSponsors, getGitHubContributions } from './server/getters/github'
2019
import { getInstagramMedias, getInstagramMediaChildren, getInstagramCalendar } from './server/getters/instagram'
2120
import { getYouTubeChannelPlayLists, getYouTubeVideoListByPlayerlistId } from './server/getters/youtube'
22-
import { getGitHubStatistic, getNPMStatistic } from './server/getters/open-srouce'
21+
import { getGitHubStatistic, getGitHubSponsors, getGitHubContributions } from './server/getters/github'
22+
import { getNPMStatistic } from './server/getters/npm'
2323
import { getDoubanMovies } from './server/getters/douban'
2424
import { getSongList } from './server/getters/netease-music'
2525
import { getWebFont, WebFontContentType } from './server/getters/webfont'
@@ -122,20 +122,6 @@ createExpressApp().then(async ({ app, server, cache }) => {
122122
responser(() => getWallpaperCache())
123123
)
124124

125-
// GitHub sponsors
126-
const getSponsorsCache = cacher.interval(cache, {
127-
key: TunnelModule.GitHubSponsors,
128-
ttl: days(3),
129-
interval: hours(18),
130-
retry: minutes(10),
131-
getter: getGitHubSponsors
132-
})
133-
134-
app.get(
135-
`${TUN}/${TunnelModule.GitHubSponsors}`,
136-
responser(() => getSponsorsCache())
137-
)
138-
139125
// 163 music BGM list
140126
const get163MusicCache = cacher.interval(cache, {
141127
key: TunnelModule.NetEaseMusic,
@@ -240,30 +226,36 @@ createExpressApp().then(async ({ app, server, cache }) => {
240226
})(request, response, next)
241227
})
242228

229+
// GitHub sponsors
230+
app.get(
231+
`${TUN}/${TunnelModule.GitHubSponsors}`,
232+
responser(() => {
233+
return cacher.passive(cache, {
234+
key: TunnelModule.GitHubSponsors,
235+
ttl: hours(4),
236+
getter: getGitHubSponsors
237+
})
238+
})
239+
)
240+
243241
// GitHub contributions
244242
app.get(
245243
`${TUN}/${TunnelModule.GitHubContributions}`,
246244
responser(() => {
247245
return cacher.passive(cache, {
248246
key: TunnelModule.GitHubContributions,
249-
ttl: hours(8),
250-
getter: () => {
251-
const now = new Date()
252-
const end = now.toISOString()
253-
now.setFullYear(now.getFullYear() - 1)
254-
const start = now.toISOString()
255-
return getGitHubContributions(start, end)
256-
}
247+
ttl: hours(4),
248+
getter: getGitHubContributions
257249
})
258250
})
259251
)
260252

261253
// GitHub statistic
262254
app.get(
263-
`${TUN}/${TunnelModule.OpenSourceGitHubStatistic}`,
255+
`${TUN}/${TunnelModule.StatisticGitHubJson}`,
264256
responser(() => {
265257
return cacher.passive(cache, {
266-
key: TunnelModule.OpenSourceGitHubStatistic,
258+
key: TunnelModule.StatisticGitHubJson,
267259
ttl: hours(8),
268260
getter: getGitHubStatistic
269261
})
@@ -272,10 +264,10 @@ createExpressApp().then(async ({ app, server, cache }) => {
272264

273265
// NPM statistic
274266
app.get(
275-
`${TUN}/${TunnelModule.OpenSourceNPMStatistic}`,
267+
`${TUN}/${TunnelModule.StatisticNpmJson}`,
276268
responser(() => {
277269
return cacher.passive(cache, {
278-
key: TunnelModule.OpenSourceNPMStatistic,
270+
key: TunnelModule.StatisticNpmJson,
279271
ttl: hours(8),
280272
getter: getNPMStatistic
281273
})

src/components/widget/sponsor/provider.vue

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
const { isZhLang } = useEnhancer()
1818
const activeProvider = computed(() => props.state.activeProvider.value)
1919
const allGitHubSponsors = computed(() => {
20+
if (!props.state.githubSponsors.data) {
21+
return []
22+
}
2023
return [
21-
...props.state.githubSponsor.activeSponsors.map((sponsor) => ({
24+
...props.state.githubSponsors.data.currentSponsors.map((sponsor) => ({
2225
active: true,
2326
_: sponsor
2427
})),
25-
...props.state.githubSponsor.inactiveSponsors.map((sponsor) => ({
28+
...props.state.githubSponsors.data.pastSponsors.map((sponsor) => ({
2629
active: false,
2730
_: sponsor
2831
}))
@@ -70,13 +73,13 @@
7073
<i18n>
7174
<template #zh>
7275
我在 GitHub Sponsors 累计已得到
73-
<span class="active-total"> {{ state.githubSponsor.activeSponsors.length }} </span>
74-
+ {{ state.githubSponsor.inactiveSponsors.length }} 位赞助者的支持
76+
<span class="active-total"> {{ state.githubSponsors.data?.currentSponsors.length }} </span>
77+
+ {{ state.githubSponsors.data?.pastSponsors.length }} 位赞助者的支持
7578
</template>
7679
<template #en>
7780
I have accumulated
78-
<span class="active-total">{{ state.githubSponsor.activeSponsors.length }}</span>
79-
+ {{ state.githubSponsor.inactiveSponsors.length }} backers on GitHub Sponsors
81+
<span class="active-total">{{ state.githubSponsors.data?.currentSponsors.length }}</span>
82+
+ {{ state.githubSponsors.data?.pastSponsors.length }} backers on GitHub Sponsors
8083
</template>
8184
</i18n>
8285
</p>

src/components/widget/sponsor/state.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ref, computed } from 'vue'
22
import { useEnhancer } from '/@/app/enhancer'
33
import { GAEventCategories } from '/@/constants/gtag'
4-
import { useSponsorStore } from '/@/stores/sponsor'
4+
import { useGitHubSponsorsStore } from '/@/stores/sponsors'
55
import { IDENTITIES, VALUABLE_LINKS } from '/@/config/app.config'
66

77
export enum ProviderId {
@@ -60,7 +60,7 @@ export const PROVIDERS = [
6060
export type SponsorState = ReturnType<typeof useSponsorState>
6161
export const useSponsorState = (initId?: ProviderId) => {
6262
const { gtag } = useEnhancer()
63-
const githubSponsor = useSponsorStore()
63+
const githubSponsors = useGitHubSponsorsStore()
6464
const activeId = ref(initId && PROVIDER_IDS.includes(initId) ? initId : PROVIDERS[0].id)
6565
const activeProvider = computed(() => PROVIDERS.find((t) => t.id === activeId.value)!)
6666

@@ -77,6 +77,6 @@ export const useSponsorState = (initId?: ProviderId) => {
7777
activeId,
7878
activeProvider,
7979
setProviderId,
80-
githubSponsor
80+
githubSponsors
8181
}
8282
}

src/config/bff.yargs.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import yargs from 'yargs'
88
const argv = yargs(process.argv.slice(2)).argv
99

10-
export const GITHUB_BEARER_TOKEN = argv.github_token
1110
export const INSTAGRAM_TOKEN = argv.instagram_token
1211
export const YOUTUBE_API_KEY = argv.youtube_token
1312
export const TWITTER_COOKIE = argv.twitter_cookie

src/constants/tunnel.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ export enum TunnelModule {
1616
InstagramMediaChildren = 'instagram_media_children',
1717
InstagramCalendar = 'instagram_calendar',
1818
BingWallpaper = 'bing_wallpaper',
19-
GitHubSponsors = 'github_sponsors',
20-
GitHubContributions = 'github_contributions',
2119
NetEaseMusic = 'netease_music',
2220
DoubanMovies = 'douban_movies',
23-
OpenSourceGitHubStatistic = 'open_source_github_statistic',
24-
OpenSourceNPMStatistic = 'open_source_npm_statistic'
21+
GitHubSponsors = 'github_sponsors',
22+
GitHubContributions = 'github_contributions',
23+
StatisticGitHubJson = 'statistic_github_json',
24+
StatisticNpmJson = 'statistic_npm_json'
2525
}

src/pages/about/desktop.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import { useAboutPageMeta, useAdminAvatar, i18ns, SPECIAL_LINKS } from './shared'
2020
2121
const { gtag, gState, isZhLang, cdnDomain } = useEnhancer()
22-
const { adminInfo, appOption, sponsor } = useStores()
22+
const { adminInfo, appOption, githubSponsors } = useStores()
2323
2424
const handleGTagEvent = (event: string) => {
2525
gtag?.event(event, {
@@ -43,7 +43,7 @@
4343
}
4444
4545
const handleSponsor = () => {
46-
sponsor.fetch()
46+
githubSponsors.fetch()
4747
gState.toggleSwitcher('sponsor', true)
4848
handleGTagEvent('sponsor_modal')
4949
}

src/pages/article/index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
}>()
3131
3232
const { i18n: _i18n, head, seoMeta, route, gtag, gState } = useEnhancer()
33-
const { identity, sponsor, comment: commentStore, articleDetail: articleDetailStore } = useStores()
33+
const { identity, githubSponsors, comment: commentStore, articleDetail: articleDetailStore } = useStores()
3434
const { article, fetching, prevArticle, nextArticle, relatedArticles } = storeToRefs(articleDetailStore)
3535
const isLiked = computed(() => Boolean(article.value && identity.isLikedPage(article.value.id)))
3636
const articleExtends = computed(() => article.value?.extends || [])
@@ -54,7 +54,7 @@
5454
}
5555
5656
const handleSponsor = () => {
57-
sponsor.fetch()
57+
githubSponsors.fetch()
5858
gState.toggleSwitcher('sponsor', true)
5959
gtag?.event('article_sponsor', {
6060
event_category: GAEventCategories.Article

src/pages/sponsor.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
})
1919
2020
onBeforeMount(() => {
21-
sponsorState.githubSponsor.fetch()
21+
sponsorState.githubSponsors.fetch()
2222
})
2323
2424
onMounted(() => {

src/server/getters/github.ts

Lines changed: 43 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,33 @@
66

77
import axios from '@/server/services/axios'
88
import { IDENTITIES } from '@/config/app.config'
9-
import { GITHUB_BEARER_TOKEN } from '@/config/bff.yargs'
109

11-
const graphqlGitHub = <T = any>(query: string): Promise<T> => {
12-
return axios
13-
.request<any>({
14-
// https://github.com/settings/tokens
15-
headers: { Authorization: `bearer ${GITHUB_BEARER_TOKEN}` },
16-
url: `https://api.github.com/graphql`,
17-
method: 'POST',
18-
data: JSON.stringify({
19-
query: `query {
20-
user(login: "${IDENTITIES.GITHUB_USER_NAME}") {
21-
${query}
22-
}
23-
}`
24-
})
25-
})
26-
.then((response) => {
27-
return response.data.errors
28-
? Promise.reject(response.data.errors.map((error) => error.message).join('; '))
29-
: Promise.resolve(response.data.data.user)
30-
})
10+
export const fetchGitHubStatisticJSON = async <T = any>(fileName: string): Promise<T> => {
11+
const url = `https://raw.githubusercontent.com/${IDENTITIES.GITHUB_USER_NAME}/${IDENTITIES.GITHUB_USER_NAME}/release/${fileName}`
12+
const response = await axios.get<T>(url, { timeout: 6000 })
13+
return response.data
14+
}
15+
16+
export interface GitHubStatistic {
17+
followerCount: number
18+
followingCount: number
19+
gistCount: number
20+
repositoryCount: number
21+
organizationCount: number
22+
totalStarCount: number
23+
totalCodeSize: number
24+
}
25+
26+
export const getGitHubStatistic = () => {
27+
return fetchGitHubStatisticJSON<any>('github.json').then((data) => ({
28+
followerCount: data.userinfo.followers,
29+
followingCount: data.userinfo.following,
30+
gistCount: data.userinfo.public_gists,
31+
repositoryCount: data.userinfo.public_repos,
32+
organizationCount: data.organizations.length,
33+
totalStarCount: data.statistics.stars,
34+
totalCodeSize: data.statistics.size
35+
}))
3136
}
3237

3338
export interface GitHubSponsorUser {
@@ -39,94 +44,29 @@ export interface GitHubSponsorUser {
3944
}
4045

4146
export interface GitHubSponsorsResponse {
42-
sponsorsActivities: {
43-
nodes: Array<null | {
44-
action: string
45-
sponsorsTier: {
46-
isOneTime: boolean
47-
}
48-
sponsor: GitHubSponsorUser
49-
}>
50-
}
51-
sponsors: {
52-
totalCount: number
53-
edges: Array<{
54-
node: GitHubSponsorUser
55-
}>
56-
}
47+
totalCount: number
48+
currentSponsors: GitHubSponsorUser[]
49+
pastSponsors: GitHubSponsorUser[]
5750
}
5851

5952
export const getGitHubSponsors = () => {
60-
const SPONSOR_NODE_QUERY = `
61-
... on User {
62-
login
63-
name
64-
url
65-
avatarUrl
66-
websiteUrl
67-
}
68-
... on Organization {
69-
login
70-
name
71-
url
72-
avatarUrl
73-
websiteUrl
74-
}
75-
`
76-
77-
// https://github.com/orgs/community/discussions/37234#discussioncomment-4047906
78-
// https://github.com/dohooo/get-past-sponsors
79-
// https://github.com/community/community/discussions/3818#discussioncomment-2155340
80-
// https://docs.github.com/en/graphql/reference/objects#sponsorsactivity
81-
// https://docs.github.com/en/graphql/reference/enums#sponsorsactivityaction
82-
return graphqlGitHub(`
83-
sponsorsActivities(first:100, period: ALL, orderBy: { direction: DESC, field: TIMESTAMP }, actions: [NEW_SPONSORSHIP, CANCELLED_SPONSORSHIP]) {
84-
nodes {
85-
action,
86-
sponsorsTier {
87-
isOneTime
88-
},
89-
sponsor {
90-
${SPONSOR_NODE_QUERY}
91-
}
92-
}
93-
},
94-
sponsors(first: 100) {
95-
totalCount
96-
edges {
97-
node {
98-
${SPONSOR_NODE_QUERY}
99-
}
100-
}
101-
}
102-
`)
53+
return fetchGitHubStatisticJSON<GitHubSponsorsResponse>('github.sponsors.json')
10354
}
10455

105-
const isISODateString = (dateString: string) => {
106-
if (!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(dateString)) return false
107-
return new Date(dateString).toISOString() === dateString
56+
export interface GitHubContributionDay {
57+
weekday: number
58+
date: string // YYYY-MM-DD
59+
contributionCount: number
60+
color: string
10861
}
10962

110-
export const getGitHubContributions = async (from: string, to: string): Promise<any> => {
111-
if (!isISODateString(from) || !isISODateString(to)) {
112-
return Promise.reject('Invalid date string!')
113-
}
114-
115-
const result = await graphqlGitHub(`
116-
contributionsCollection(from: "${from}", to: "${to}") {
117-
contributionCalendar {
118-
totalContributions
119-
weeks {
120-
contributionDays {
121-
weekday
122-
date
123-
contributionCount
124-
color
125-
}
126-
}
127-
}
128-
}
129-
`)
63+
export interface GitHubContributionsResponse {
64+
totalContributions: number
65+
weeks: Array<{
66+
contributionDays: Array<GitHubContributionDay>
67+
}>
68+
}
13069

131-
return result.contributionsCollection.contributionCalendar
70+
export const getGitHubContributions = () => {
71+
return fetchGitHubStatisticJSON<GitHubContributionsResponse>('github.contributions.json')
13272
}

0 commit comments

Comments
 (0)