badge(peertube): Add PeerTube service (#476)

* feat: add peertube service

* refactor(peertube): update branding & add icon

* style(peertube): rename `videoId` to `videoUUID`
pull/478/head
Dario Vladović 2021-01-14 02:47:18 +01:00 zatwierdzone przez GitHub
rodzic cdaefa14ae
commit c6f2adfb18
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 101 dodań i 0 usunięć

99
api/peertube.ts 100644
Wyświetl plik

@ -0,0 +1,99 @@
import got from '../libs/got'
import { millify } from '../libs/utils'
import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler'
const BRAND_COLOR = 'F1680D'
export default createBadgenHandler({
title: 'PeerTube',
examples: {
'/peertube/framatube.org/comments/9c9de5e8-0a1e-484a-b099-e80766180a6d?icon=peertube': 'comments',
'/peertube/framatube.org/votes/9c9de5e8-0a1e-484a-b099-e80766180a6d?icon=peertube': 'votes (combined)',
'/peertube/framatube.org/votes/9c9de5e8-0a1e-484a-b099-e80766180a6d/likes?icon=peertube': 'votes (likes)',
'/peertube/framatube.org/votes/9c9de5e8-0a1e-484a-b099-e80766180a6d/dislikes?icon=peertube': 'votes (dislikes)',
'/peertube/framatube.org/views/9c9de5e8-0a1e-484a-b099-e80766180a6d?icon=peertube': 'views',
'/peertube/framatube.org/followers/framasoft?icon=peertube': 'followers (account)',
'/peertube/framatube.org/followers/framasoft/framablog.audio?icon=peertube': 'followers (channel)',
},
handlers: {
'/peertube/:instance/:topic<comments|views>/:video-uuid': handler,
'/peertube/:instance/:topic<votes>/:video-uuid/:format?<likes|dislikes>': votesHandler,
'/peertube/:instance/:topic<followers>/:account/:channel?': followersHandler
}
})
async function handler ({ instance, topic, 'video-uuid': videoUUID }: PathArgs) {
const client = createClient(instance)
switch (topic) {
case 'comments': {
const { total } = await client.get(`/videos/${videoUUID}/comment-threads`).json()
return {
subject: 'comments',
status: millify(total),
color: BRAND_COLOR
}
}
case 'views': {
const { views } = await client.get(`/videos/${videoUUID}`).json()
return {
subject: 'views',
status: millify(views),
color: BRAND_COLOR
}
}
}
}
async function votesHandler ({ instance, 'video-uuid': videoUUID, format }: PathArgs) {
const client = createClient(instance)
const { likes, dislikes } = await client.get(`/videos/${videoUUID}`).json()
switch (format) {
case 'likes': {
return {
subject: 'likes',
status: millify(likes),
color: BRAND_COLOR
}
}
case 'dislikes': {
return {
subject: 'dislikes',
status: millify(dislikes),
color: BRAND_COLOR
}
}
}
return {
subject: 'votes',
status: `${millify(likes)} 👍 ${millify(dislikes)} 👎`,
color: BRAND_COLOR
}
}
async function followersHandler ({ instance, account, channel }: PathArgs) {
const client = createClient(instance)
if (channel) {
const { followersCount } = await client.get(`/video-channels/${channel}`).json()
return {
subject: 'followers',
status: millify(followersCount),
color: BRAND_COLOR
}
}
const { followersCount } = await client.get(`/accounts/${account}`).json()
return {
subject: 'followers',
status: millify(followersCount),
color: BRAND_COLOR
}
}
function createClient (instance: string) {
const prefixUrl = `https://${instance}/api/v1`
return got.extend({ prefixUrl })
}

Wyświetl plik

@ -55,6 +55,8 @@ export const liveBadgeList = [
'xo',
'badgesize',
'jsdelivr',
// social
'peertube',
// utilities
'opencollective',
'keybase',