diff --git a/services/twitter/twitter.service.js b/services/twitter/twitter.service.js index 64c650cda0a115cf7c8088eca38856887d5316f0..f674a1a430dd768056519c941ec50f44454e5dcf 100644 --- a/services/twitter/twitter.service.js +++ b/services/twitter/twitter.service.js @@ -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(), @@ -33,6 +32,8 @@ class TwitterUrl extends BaseService { }, ] + static _cacheLength = 86400 + static defaultBadgeData = { namedLogo: 'twitter', } @@ -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' @@ -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 }) } } diff --git a/services/twitter/twitter.tester.js b/services/twitter/twitter.tester.js index e15b79f08f4ddb0ca86f147bf36c5a82b81da0e9..b52b7c2b714bdcef2c8dbf47a2f084ab39d75266 100644 --- a/services/twitter/twitter.tester.js +++ b/services/twitter/twitter.tester.js @@ -1,4 +1,3 @@ -import { isMetric } from '../test-validators.js' import { ServiceTester } from '../tester.js' export const t = new ServiceTester({ @@ -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')