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

refs #223 Add Pixelfed client #225

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
22 changes: 17 additions & 5 deletions example/typescript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions example/typescript/src/pixelfed/authorization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as readline from 'readline'
import generator, { OAuth } from 'megalodon'

const rl: readline.ReadLine = readline.createInterface({
input: process.stdin,
output: process.stdout
})

const SCOPES: Array<string> = ['read', 'write', 'follow']
const BASE_URL: string = 'https://pixelfed.social'

let clientId: string
let clientSecret: string

const client = generator('pixelfed', BASE_URL)

client
.registerApp('Test App', {
scopes: SCOPES
})
.then(appData => {
clientId = appData.clientId
clientSecret = appData.clientSecret
console.log('Authorization URL is generated.')
console.log(appData.url)
console.log()
return new Promise<string>(resolve => {
rl.question('Enter the authorization code from website: ', code => {
resolve(code)
rl.close()
})
})
})
.then((code: string) => {
return client.fetchAccessToken(clientId, clientSecret, code, BASE_URL)
})
.then((tokenData: OAuth.TokenData) => {
console.log('\naccess_token:')
console.log(tokenData.accessToken)
console.log('\nrefresh_token:')
console.log(tokenData.refreshToken)
console.log()
})
.catch((err: Error) => console.error(err))
11 changes: 8 additions & 3 deletions src/megalodon.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Response from './response'
import OAuth from './oauth'
import Pleroma from './pleroma'
import proxyAgent, { ProxyConfig } from './proxy_config'
import Mastodon from './mastodon'
import Pleroma from './pleroma'
import Pixelfed from './pixelfed'
import Entity from './entity'
import axios, { AxiosRequestConfig } from 'axios'
import Misskey from './misskey'
Expand Down Expand Up @@ -1295,15 +1296,15 @@ export const detector = async (url: string, proxyConfig: ProxyConfig | false = f
/**
* Get client for each SNS according to megalodon interface.
*
* @param sns Name of your SNS, `mastodon` or `pleroma`.
* @param sns Name of your SNS, `mastodon`, `pleroma` or `pixelfed`.
* @param baseUrl hostname or base URL.
* @param accessToken access token from OAuth2 authorization
* @param userAgent UserAgent is specified in header on request.
* @param proxyConfig Proxy setting, or set false if don't use proxy.
* @return Client instance for each SNS you specified.
*/
const generator = (
sns: 'mastodon' | 'pleroma' | 'misskey',
sns: 'mastodon' | 'pleroma' | 'misskey' | 'pixelfed',
baseUrl: string,
accessToken: string | null = null,
userAgent: string | null = null,
Expand All @@ -1318,6 +1319,10 @@ const generator = (
const misskey = new Misskey(baseUrl, accessToken, userAgent, proxyConfig)
return misskey
}
case 'pixelfed': {
const pixelfed = new Pixelfed(baseUrl, accessToken, userAgent, proxyConfig)
return pixelfed
}
default: {
const mastodon = new Mastodon(baseUrl, accessToken, userAgent, proxyConfig)
return mastodon
Expand Down
91 changes: 91 additions & 0 deletions src/pixelfed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { OAuth2 } from 'oauth'
import OAuth from './oauth'
import { MegalodonInterface } from './megalodon'
import Mastodon from './mastodon'
import { NO_REDIRECT, DEFAULT_SCOPE } from './default'
import Response from './response'

export default class Pixelfed extends Mastodon implements MegalodonInterface {
public async registerApp(
client_name: string,
options: Partial<{ scopes: Array<string>; redirect_uris: string; website: string }> = {
scopes: DEFAULT_SCOPE,
redirect_uris: NO_REDIRECT
}
): Promise<OAuth.AppData> {
return this.createApp(client_name, options).then(async appData => {
return this.generateAuthUrl(appData.client_id, appData.client_secret, {
scope: options.scopes,
redirect_uri: appData.redirect_uri
}).then(url => {
appData.url = url
return appData
})
})
}

/**
* Call /api/v1/apps
*
* Create an application.
* @param client_name your application's name
* @param options Form Data
*/
public async createApp(
client_name: string,
options: Partial<{ scopes: Array<string>; redirect_uris: string; website: string }> = {
redirect_uris: NO_REDIRECT,
scopes: DEFAULT_SCOPE
}
): Promise<OAuth.AppData> {
const redirect_uris = options.redirect_uris || NO_REDIRECT
const scopes = options.scopes || DEFAULT_SCOPE

const params: {
client_name: string
redirect_uris: string
scopes: Array<string>
website?: string
} = {
client_name: client_name,
redirect_uris: redirect_uris,
scopes: scopes
}
if (options.website) params.website = options.website

return this.client
.post<OAuth.AppDataFromServer>('/api/v1/apps', params)
.then((res: Response<OAuth.AppDataFromServer>) => OAuth.AppData.from(res.data))
}

/**
* Generate authorization url using OAuth2.
*
* @param clientId your OAuth app's client ID
* @param clientSecret your OAuth app's client Secret
* @param options as property, redirect_uri and scope are available, and must be the same as when you register your app
*/
public generateAuthUrl(
clientId: string,
clientSecret: string,
options: Partial<{ scope: Array<string>; redirect_uri: string }> = {
redirect_uri: NO_REDIRECT,
scope: DEFAULT_SCOPE
}
): Promise<string> {
return new Promise(resolve => {
const oauth = new OAuth2(clientId, clientSecret, this.baseUrl, undefined, '/oauth/token')
const url = oauth.getAuthorizeUrl(
Object.assign(
{
redirect_uri: options.redirect_uri,
response_type: 'code',
client_id: clientId
},
...options.scope!.map((s, i) => ({ [`scope[${i}]`]: s }))
)
)
resolve(url)
})
}
}