Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace [twitter] badge with static fallback #8842

Merged
merged 5 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions services/twitter/twitter.service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Joi from 'joi'
import { metric } from '../text-formatters.js'
import { optionalUrl } from '../validators.js'
import { BaseService, BaseJsonService, NotFound } from '../index.js'
import { BaseService, BaseJsonService } from '../index.js'

const queryParamSchema = Joi.object({
url: optionalUrl.required(),
Expand Down Expand Up @@ -33,6 +32,8 @@ class TwitterUrl extends BaseService {
},
]

static _cacheLength = 86400

static defaultBadgeData = {
namedLogo: 'twitter',
}
Expand All @@ -51,8 +52,19 @@ class TwitterUrl extends BaseService {
}
}

const schema = Joi.any()
/*
This badge is unusual.
We don't usually host badges that don't show any dynamic information.
Also when an upstream API is removed, we usually deprecate/remove badges
according to the process in
https://github.com/badges/shields/blob/master/doc/deprecating-badges.md
In the case of twitter, we decided to provide a static fallback instead
due to how widely used the badge was. See
https://github.com/badges/shields/issues/8837
for related discussion.
*/
class TwitterFollow extends BaseJsonService {
static category = 'social'

Expand All @@ -65,51 +77,40 @@ class TwitterFollow extends BaseJsonService {
{
title: 'Twitter Follow',
namedParams: {
user: 'espadrine',
user: 'shields_io',
},
queryParams: { label: 'Follow' },
// hard code the static preview
// because link[] is not allowed in examples
staticPreview: {
label: 'Follow',
message: '393',
label: 'Follow @shields_io',
message: '',
style: 'social',
},
},
]

static _cacheLength = 86400

static defaultBadgeData = {
namedLogo: 'twitter',
}

static render({ user, followers }) {
static render({ user }) {
return {
label: `follow @${user}`,
message: metric(followers),
message: '',
style: 'social',
link: [
`https://twitter.com/intent/follow?screen_name=${encodeURIComponent(
user
)}`,
`https://twitter.com/${encodeURIComponent(user)}/followers`,
],
}
}

async fetch({ user }) {
return this._requestJson({
schema,
url: 'http://cdn.syndication.twimg.com/widgets/followbutton/info.json',
options: { searchParams: { screen_names: user } },
})
}

async handle({ user }) {
const data = await this.fetch({ user })
if (!Array.isArray(data) || data.length === 0) {
throw new NotFound({ prettyMessage: 'invalid user' })
}
return this.constructor.render({ user, followers: data[0].followers_count })
return this.constructor.render({ user })
}
}

Expand Down
22 changes: 2 additions & 20 deletions services/twitter/twitter.tester.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isMetric } from '../test-validators.js'
import { ServiceTester } from '../tester.js'

export const t = new ServiceTester({
Expand All @@ -10,25 +9,8 @@ t.create('Followers')
.get('/follow/shields_io.json')
.expectBadge({
label: 'follow @shields_io',
message: isMetric,
link: [
'https://twitter.com/intent/follow?screen_name=shields_io',
'https://twitter.com/shields_io/followers',
],
})

t.create('Invalid Username Specified (non-existent user)')
.get('/follow/invalidusernamethatshouldnotexist.json?label=Follow')
.expectBadge({
label: 'Follow',
message: 'invalid user',
})

t.create('Invalid Username Specified (only spaces)')
.get('/follow/%20%20.json?label=Follow')
.expectBadge({
label: 'Follow',
message: 'invalid user',
message: '',
link: ['https://twitter.com/intent/follow?screen_name=shields_io'],
})

t.create('URL')
Expand Down