Skip to content

Commit

Permalink
Infer streaming URL from instance information
Browse files Browse the repository at this point in the history
  • Loading branch information
h3poteto committed Mar 11, 2024
1 parent 97bffca commit e07c171
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 67 deletions.
35 changes: 24 additions & 11 deletions megalodon/src/firefish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2350,27 +2350,40 @@ export default class Firefish implements MegalodonInterface {
})
}

public userSocket(): WebSocketInterface {
return this.client.socket('user')
public async streamingURL(): Promise<string> {
const instance = await this.getInstance()
if (instance.data.urls) {
return instance.data.urls.streaming_api
}
return this.baseUrl
}

public async userStreaming(): Promise<WebSocketInterface> {
const url = await this.streamingURL()
return this.client.socket(url, 'user')
}

public publicSocket(): WebSocketInterface {
return this.client.socket('globalTimeline')
public async publicStreaming(): Promise<WebSocketInterface> {
const url = await this.streamingURL()
return this.client.socket(url, 'globalTimeline')
}

public localSocket(): WebSocketInterface {
return this.client.socket('localTimeline')
public async localStreaming(): Promise<WebSocketInterface> {
const url = await this.streamingURL()
return this.client.socket(url, 'localTimeline')
}

public tagSocket(_tag: string): WebSocketInterface {
public async tagStreaming(_tag: string): Promise<WebSocketInterface> {
throw new NotImplementedError('TODO: implement')
}

public listSocket(list_id: string): WebSocketInterface {
return this.client.socket('list', list_id)
public async listStreaming(list_id: string): Promise<WebSocketInterface> {
const url = await this.streamingURL()
return this.client.socket(url, 'list', list_id)
}

public directSocket(): WebSocketInterface {
return this.client.socket('conversation')
public async directStreaming(): Promise<WebSocketInterface> {
const url = await this.streamingURL()
return this.client.socket(url, 'conversation')
}
}
9 changes: 7 additions & 2 deletions megalodon/src/firefish/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,11 @@ namespace FirefishAPI {
get<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
post<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
cancel(): void
socket(channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline' | 'conversation' | 'list', listId?: string): WebSocket
socket(
url: string,
channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline' | 'conversation' | 'list',
listId?: string
): WebSocket
}

/**
Expand Down Expand Up @@ -702,17 +706,18 @@ namespace FirefishAPI {
/**
* Get connection and receive websocket connection for Firefish API.
*
* @param url Streaming url.
* @param channel Channel name is user, localTimeline, hybridTimeline, globalTimeline, conversation or list.
* @param listId This parameter is required only list channel.
*/
public socket(
url: string,
channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline' | 'conversation' | 'list',
listId?: string
): WebSocket {
if (!this.accessToken) {
throw new Error('accessToken is required')
}
const url = this.baseUrl + '/streaming'
const streaming = new WebSocket(url, channel, this.accessToken, listId, this.userAgent)

streaming.start()
Expand Down
38 changes: 26 additions & 12 deletions megalodon/src/friendica.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2840,27 +2840,41 @@ export default class Friendica implements MegalodonInterface {
// ======================================
// WebSocket
// ======================================
public userSocket(): WebSocket {
return this.client.socket('/api/v1/streaming', 'user')
public async streamingURL(): Promise<string> {
const instance = await this.getInstance()
if (instance.data.urls) {
return instance.data.urls.streaming_api
}
return this.baseUrl
}

public async userStreaming(): Promise<WebSocket> {
const url = await this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'user')
}

public publicSocket(): WebSocket {
return this.client.socket('/api/v1/streaming', 'public')
public async publicStreaming(): Promise<WebSocket> {
const url = await this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'public')
}

public localSocket(): WebSocket {
return this.client.socket('/api/v1/streaming', 'public:local')
public async localStreaming(): Promise<WebSocket> {
const url = await this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'public:local')
}

public tagSocket(tag: string): WebSocket {
return this.client.socket('/api/v1/streaming', 'hashtag', `tag=${tag}`)
public async tagStreaming(tag: string): Promise<WebSocket> {
const url = await this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'hashtag', `tag=${tag}`)
}

public listSocket(list_id: string): WebSocket {
return this.client.socket('/api/v1/streaming', 'list', `list=${list_id}`)
public async listStreaming(list_id: string): Promise<WebSocket> {
const url = await this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'list', `list=${list_id}`)
}

public directSocket(): WebSocket {
return this.client.socket('/api/v1/streaming', 'direct')
public async directStreaming(): Promise<WebSocket> {
const url = await this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'direct')
}
}
7 changes: 3 additions & 4 deletions megalodon/src/friendica/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace FriendicaAPI {
postForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
del<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
cancel(): void
socket(path: string, stream: string, params?: string): WebSocket
socket(url: string, stream: string, params?: string): WebSocket
}

/**
Expand Down Expand Up @@ -367,15 +367,14 @@ namespace FriendicaAPI {
/**
* Get connection and receive websocket connection for Pleroma API.
*
* @param path relative path from baseUrl: normally it is `/streaming`.
* @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
* @returns WebSocket, which inherits from EventEmitter
*/
public socket(path: string, stream: string, params?: string): WebSocket {
public socket(url: string, stream: string, params?: string): WebSocket {
if (!this.accessToken) {
throw new Error('accessToken is required')
}
const url = this.baseUrl + path
const streaming = new WebSocket(url, stream, params, this.accessToken, this.userAgent)

streaming.start()
Expand Down
38 changes: 26 additions & 12 deletions megalodon/src/mastodon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3144,27 +3144,41 @@ export default class Mastodon implements MegalodonInterface {
// ======================================
// WebSocket
// ======================================
public userSocket(): Streaming {
return this.client.socket('/api/v1/streaming', 'user')
public async streamingURL(): Promise<string> {
const instance = await this.getInstance()
if (instance.data.urls) {
return instance.data.urls.streaming_api
}
return this.baseUrl
}

public async userStreaming(): Promise<Streaming> {
const url = await this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'user')
}

public publicSocket(): Streaming {
return this.client.socket('/api/v1/streaming', 'public')
public async publicStreaming(): Promise<Streaming> {
const url = await this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'public')
}

public localSocket(): Streaming {
return this.client.socket('/api/v1/streaming', 'public:local')
public async localStreaming(): Promise<Streaming> {
const url = await this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'public:local')
}

public tagSocket(tag: string): Streaming {
return this.client.socket('/api/v1/streaming', 'hashtag', `tag=${tag}`)
public async tagStreaming(tag: string): Promise<Streaming> {
const url = await this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'hashtag', `tag=${tag}`)
}

public listSocket(list_id: string): Streaming {
return this.client.socket('/api/v1/streaming', 'list', `list=${list_id}`)
public async listStreaming(list_id: string): Promise<Streaming> {
const url = await this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'list', `list=${list_id}`)
}

public directSocket(): Streaming {
return this.client.socket('/api/v1/streaming', 'direct')
public async directStreaming(): Promise<Streaming> {
const url = await this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'direct')
}
}
7 changes: 3 additions & 4 deletions megalodon/src/mastodon/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace MastodonAPI {
postForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
del<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
cancel(): void
socket(path: string, stream: string, params?: string): Streaming
socket(url: string, stream: string, params?: string): Streaming
}

/**
Expand Down Expand Up @@ -367,15 +367,14 @@ namespace MastodonAPI {
/**
* Get connection and receive websocket connection for Pleroma API.
*
* @param path relative path from baseUrl: normally it is `/streaming`.
* @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
* @returns WebSocket, which inherits from EventEmitter
*/
public socket(path: string, stream: string, params?: string): Streaming {
public socket(url: string, stream: string, params?: string): Streaming {
if (!this.accessToken) {
throw new Error('accessToken is required')
}
const url = this.baseUrl + path
const streaming = new Streaming(url, stream, params, this.accessToken, this.userAgent)

streaming.start()
Expand Down
13 changes: 7 additions & 6 deletions megalodon/src/megalodon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1370,12 +1370,13 @@ export interface MegalodonInterface {
// ======================================
// WebSocket
// ======================================
userSocket(): WebSocketInterface
publicSocket(): WebSocketInterface
localSocket(): WebSocketInterface
tagSocket(tag: string): WebSocketInterface
listSocket(list_id: string): WebSocketInterface
directSocket(): WebSocketInterface
streamingURL(): Promise<string>
userStreaming(): Promise<WebSocketInterface>
publicStreaming(): Promise<WebSocketInterface>
localStreaming(): Promise<WebSocketInterface>
tagStreaming(tag: string): Promise<WebSocketInterface>
listStreaming(list_id: string): Promise<WebSocketInterface>
directStreaming(): Promise<WebSocketInterface>
}

export class NotImplementedError extends Error {
Expand Down
38 changes: 26 additions & 12 deletions megalodon/src/pleroma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3192,27 +3192,41 @@ export default class Pleroma implements MegalodonInterface {
// ======================================
// WebSocket
// ======================================
public userSocket(): WebSocket {
return this.client.socket('/api/v1/streaming', 'user')
public async streamingURL(): Promise<string> {
const instance = await this.getInstance()
if (instance.data.urls) {
return instance.data.urls.streaming_api
}
return this.baseUrl
}

public async userStreaming(): Promise<WebSocket> {
const url = this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'user')
}

public publicSocket(): WebSocket {
return this.client.socket('/api/v1/streaming', 'public')
public async publicStreaming(): Promise<WebSocket> {
const url = this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'public')
}

public localSocket(): WebSocket {
return this.client.socket('/api/v1/streaming', 'public:local')
public async localStreaming(): Promise<WebSocket> {
const url = this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'public:local')
}

public tagSocket(tag: string): WebSocket {
return this.client.socket('/api/v1/streaming', 'hashtag', `tag=${tag}`)
public async tagStreaming(tag: string): Promise<WebSocket> {
const url = this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'hashtag', `tag=${tag}`)
}

public listSocket(list_id: string): WebSocket {
return this.client.socket('/api/v1/streaming', 'list', `list=${list_id}`)
public async listStreaming(list_id: string): Promise<WebSocket> {
const url = this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'list', `list=${list_id}`)
}

public directSocket(): WebSocket {
return this.client.socket('/api/v1/streaming', 'direct')
public async directStreaming(): Promise<WebSocket> {
const url = this.streamingURL()
return this.client.socket(`${url}/api/v1/streaming`, 'direct')
}
}
7 changes: 3 additions & 4 deletions megalodon/src/pleroma/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ namespace PleromaAPI {
postForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
del<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
cancel(): void
socket(path: string, stream: string, params?: string): WebSocket
socket(url: string, stream: string, params?: string): WebSocket
}

/**
Expand Down Expand Up @@ -775,15 +775,14 @@ namespace PleromaAPI {
/**
* Get connection and receive websocket connection for Pleroma API.
*
* @param path relative path from baseUrl: normally it is `/streaming`.
* @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
* @returns WebSocket, which inherits from EventEmitter
*/
public socket(path: string, stream: string, params?: string): WebSocket {
public socket(url: string, stream: string, params?: string): WebSocket {
if (!this.accessToken) {
throw new Error('accessToken is required')
}
const url = this.baseUrl + path
const streaming = new WebSocket(url, stream, params, this.accessToken, this.userAgent)

streaming.start()
Expand Down

0 comments on commit e07c171

Please sign in to comment.