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

Add Pixelfed client #2244

Merged
merged 1 commit into from
Jan 5, 2025
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
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 = process.env.PIXELFED_URL!

let clientId: string
let clientSecret: string

const client = generator('pixelfed', BASE_URL)

client
.registerApp('Test App', {
scopes: SCOPES
})
.then(appData => {
clientId = appData.client_id
clientSecret = appData.client_secret
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)
})
.then((tokenData: OAuth.TokenData) => {
console.log('\naccess_token:')
console.log(tokenData.access_token)
console.log('\nrefresh_token:')
console.log(tokenData.refresh_token)
console.log()
})
.catch((err: Error) => console.error(err))
9 changes: 9 additions & 0 deletions example/typescript/src/pixelfed/instance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import generator, { Entity, Response } from 'megalodon'

const BASE_URL: string = process.env.PIXELFED_URL!

const client = generator('pixelfed', BASE_URL)

client.getInstance().then((res: Response<Entity.Instance>) => {
console.log(res.data)
})
11 changes: 11 additions & 0 deletions example/typescript/src/pixelfed/timeline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import generator, { MegalodonInterface, Entity, Response } from 'megalodon'

const BASE_URL: string = process.env.PIXELFED_URL!

const access_token: string = process.env.PIXELFED_ACCESS_TOKEN!

const client: MegalodonInterface = generator('pixelfed', BASE_URL, access_token)

client.getPublicTimeline().then((resp: Response<Array<Entity.Status>>) => {
console.log(resp.data)
})
8 changes: 7 additions & 1 deletion megalodon/src/detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Metadata = {
* @param proxyConfig Proxy setting, or set false if don't use proxy.
* @return SNS name.
*/
export const detector = async (url: string): Promise<'mastodon' | 'pleroma' | 'friendica' | 'firefish' | 'gotosocial'> => {
export const detector = async (url: string): Promise<'mastodon' | 'pleroma' | 'friendica' | 'firefish' | 'gotosocial' | 'pixelfed'> => {
const options: AxiosRequestConfig = {
timeout: 20000
}
Expand All @@ -73,6 +73,8 @@ export const detector = async (url: string): Promise<'mastodon' | 'pleroma' | 'f
return 'firefish'
case 'mastodon':
return 'mastodon'
case 'pixelfed':
return 'pixelfed'
case 'pleroma':
return 'pleroma'
case 'sharkey':
Expand Down Expand Up @@ -101,6 +103,8 @@ export const detector = async (url: string): Promise<'mastodon' | 'pleroma' | 'f
return 'firefish'
case 'mastodon':
return 'mastodon'
case 'pixelfed':
return 'pixelfed'
case 'pleroma':
return 'pleroma'
case 'sharkey':
Expand Down Expand Up @@ -129,6 +133,8 @@ export const detector = async (url: string): Promise<'mastodon' | 'pleroma' | 'f
return 'firefish'
case 'mastodon':
return 'mastodon'
case 'pixelfed':
return 'pixelfed'
case 'pleroma':
return 'pleroma'
case 'sharkey':
Expand Down
2 changes: 2 additions & 0 deletions megalodon/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import generator, { MegalodonInterface, WebSocketInterface } from './megalodon'
import { detector } from './detector'
import Mastodon from './mastodon'
import Pleroma from './pleroma'
import Pixelfed from './pixelfed'
import Firefish from './firefish'
import Gotosocial from './gotosocial'
import Entity from './entity'
Expand All @@ -23,6 +24,7 @@ export {
FilterContext,
Mastodon,
Pleroma,
Pixelfed,
Firefish,
Gotosocial,
Entity
Expand Down
2 changes: 1 addition & 1 deletion megalodon/src/mastodon/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ namespace MastodonAPI {
* Get connection and receive websocket connection for Pleroma API.
*
* @param url Streaming url.
* @param stream Stream name, please refer: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/web/mastodon_api/mastodon_socket.ex#L19-28
* @param stream Stream name
* @returns WebSocket, which inherits from EventEmitter
*/
public socket(url: string, stream: string, params?: string): Streaming {
Expand Down
4 changes: 2 additions & 2 deletions megalodon/src/mastodon/web_socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export default class Streaming extends EventEmitter implements WebSocketInterfac
private _pongWaiting: boolean = false

/**
* @param url Full url of websocket: e.g. https://pleroma.io/api/v1/streaming
* @param stream Stream name, please refer: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/web/mastodon_api/mastodon_socket.ex#L19-28
* @param url Full url of websocket: e.g. https://mastodon.social/api/v1/streaming
* @param stream Stream name
* @param accessToken The access token.
* @param userAgent The specified User Agent.
*/
Expand Down
9 changes: 7 additions & 2 deletions megalodon/src/megalodon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Entity from './entity'
import Friendica from './friendica'
import Firefish from './firefish'
import Gotosocial from './gotosocial'
import Pixelfed from './pixelfed'

export interface WebSocketInterface {
start(): void
Expand Down Expand Up @@ -1436,14 +1437,14 @@ export class NodeinfoError extends Error {
/**
* Get client for each SNS according to megalodon interface.
*
* @param sns Name of your SNS, `mastodon`, `pleroma`, `firefish`, or `gotosocial`.
* @param sns Name of your SNS, `mastodon`, `pleroma`, `firefish`, `gotosocial`, or `pixelfed`.
* @param baseUrl hostname or base URL.
* @param accessToken access token from OAuth2 authorization
* @param userAgent UserAgent is specified in header on request.
* @return Client instance for each SNS you specified.
*/
const generator = (
sns: 'mastodon' | 'pleroma' | 'friendica' | 'firefish' | 'gotosocial',
sns: 'mastodon' | 'pleroma' | 'friendica' | 'firefish' | 'gotosocial' | 'pixelfed',
baseUrl: string,
accessToken: string | null = null,
userAgent: string | null = null
Expand All @@ -1469,6 +1470,10 @@ const generator = (
const gotosocial = new Gotosocial(baseUrl, accessToken, userAgent)
return gotosocial
}
case 'pixelfed': {
const pixelfed = new Pixelfed(baseUrl, accessToken, userAgent)
return pixelfed
}
}
}

Expand Down
Loading
Loading