Skip to content

Commit

Permalink
refs h3poteto#1663 Add friendica to detector
Browse files Browse the repository at this point in the history
  • Loading branch information
h3poteto committed Apr 21, 2023
1 parent fd26a32 commit 0988d8b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
33 changes: 32 additions & 1 deletion megalodon/src/detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import proxyAgent, { ProxyConfig } from './proxy_config'
import { DEFAULT_UA } from './default'
import { NodeinfoError } from './megalodon'

const NODEINFO_10 = 'http://nodeinfo.diaspora.software/ns/schema/1.0'
const NODEINFO_20 = 'http://nodeinfo.diaspora.software/ns/schema/2.0'
const NODEINFO_21 = 'http://nodeinfo.diaspora.software/ns/schema/2.1'

Expand All @@ -15,6 +16,11 @@ type Link = {
rel: string
}

type Nodeinfo10 = {
software: Software
metadata: Metadata
}

type Nodeinfo20 = {
software: Software
metadata: Metadata
Expand Down Expand Up @@ -43,7 +49,10 @@ type Metadata = {
* @param proxyConfig Proxy setting, or set false if don't use proxy.
* @return SNS name.
*/
export const detector = async (url: string, proxyConfig: ProxyConfig | false = false): Promise<'mastodon' | 'pleroma' | 'misskey'> => {
export const detector = async (
url: string,
proxyConfig: ProxyConfig | false = false
): Promise<'mastodon' | 'pleroma' | 'misskey' | 'friendica'> => {
let options: AxiosRequestConfig = {
headers: {
'User-Agent': DEFAULT_UA
Expand All @@ -59,6 +68,24 @@ export const detector = async (url: string, proxyConfig: ProxyConfig | false = f
const link = res.data.links.find(l => l.rel === NODEINFO_20 || l.rel === NODEINFO_21)
if (!link) throw new NodeinfoError('Could not find nodeinfo')
switch (link.rel) {
case NODEINFO_10: {
const res = await axios.get<Nodeinfo10>(link.href, options)
switch (res.data.software.name) {
case 'pleroma':
return 'pleroma'
case 'mastodon':
return 'mastodon'
case 'misskey':
return 'misskey'
case 'friendica':
return 'friendica'
default:
if (res.data.metadata.upstream?.name && res.data.metadata.upstream.name === 'mastodon') {
return 'mastodon'
}
throw new NodeinfoError('Unknown SNS')
}
}
case NODEINFO_20: {
const res = await axios.get<Nodeinfo20>(link.href, options)
switch (res.data.software.name) {
Expand All @@ -68,6 +95,8 @@ export const detector = async (url: string, proxyConfig: ProxyConfig | false = f
return 'mastodon'
case 'misskey':
return 'misskey'
case 'friendica':
return 'friendica'
default:
if (res.data.metadata.upstream?.name && res.data.metadata.upstream.name === 'mastodon') {
return 'mastodon'
Expand All @@ -84,6 +113,8 @@ export const detector = async (url: string, proxyConfig: ProxyConfig | false = f
return 'mastodon'
case 'misskey':
return 'misskey'
case 'friendica':
return 'friendica'
default:
if (res.data.metadata.upstream?.name && res.data.metadata.upstream.name === 'mastodon') {
return 'mastodon'
Expand Down
8 changes: 8 additions & 0 deletions megalodon/test/integration/megalodon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ describe('detector', () => {
})
})

describe('friendica', () => {
const url = 'https://squeet.me'
it('should be friendica', async () => {
const friendica = await detector(url)
expect(friendica).toEqual('friendica')
})
})

describe('unknown', () => {
const url = 'https://google.com'
it('should be null', async () => {
Expand Down

0 comments on commit 0988d8b

Please sign in to comment.