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

Apply interface for stream listener and websocket #240

Merged
merged 2 commits into from
Mar 9, 2020
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
4 changes: 2 additions & 2 deletions example/typescript/src/mastodon/proxy_streaming.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import generator, { Entity, StreamListener, ProxyConfig } from 'megalodon'
import generator, { Entity, StreamListenerInterface, ProxyConfig } from 'megalodon'

declare var process: {
env: {
Expand All @@ -21,7 +21,7 @@ const proxy: ProxyConfig = {

const client = generator('mastodon', BASE_URL, access_token, null, proxy)

const stream: StreamListener = client.userStream()
const stream: StreamListenerInterface = client.userStream()
stream.on('connect', _ => {
console.log('connect')
})
Expand Down
4 changes: 2 additions & 2 deletions example/typescript/src/mastodon/streaming.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import generator, { Entity, StreamListener } from 'megalodon'
import generator, { Entity, StreamListenerInterface } from 'megalodon'

declare var process: {
env: {
Expand All @@ -12,7 +12,7 @@ const access_token: string = process.env.MASTODON_ACCESS_TOKEN

const client = generator('mastodon', BASE_URL, access_token)

const stream: StreamListener = client.publicStream()
const stream: StreamListenerInterface = client.publicStream()
stream.on('connect', _ => {
console.log('connect')
})
Expand Down
4 changes: 2 additions & 2 deletions example/typescript/src/pleroma/proxy_web_socket.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import generator, { Entity, WebSocket, ProxyConfig } from 'megalodon'
import generator, { Entity, WebSocketInterface, ProxyConfig } from 'megalodon'
import log4js from 'log4js'

declare var process: {
Expand All @@ -22,7 +22,7 @@ const proxy: ProxyConfig = {

const client = generator('pleroma', BASE_URL, access_token, null, proxy)

const stream: WebSocket = client.userSocket()
const stream: WebSocketInterface = client.userSocket()

const logger = log4js.getLogger()
logger.level = 'debug'
Expand Down
4 changes: 2 additions & 2 deletions example/typescript/src/pleroma/web_socket.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import generator, { Entity, WebSocket } from 'megalodon'
import generator, { Entity, WebSocketInterface } from 'megalodon'
import log4js from 'log4js'

declare var process: {
Expand All @@ -13,7 +13,7 @@ const access_token: string = process.env.PLEROMA_ACCESS_TOKEN

const client = generator('pleroma', BASE_URL, access_token)

const stream: WebSocket = client.userSocket()
const stream: WebSocketInterface = client.userSocket()

const logger = log4js.getLogger()
logger.level = 'debug'
Expand Down
8 changes: 3 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import MastodonAPI from './mastodon/api_client'
import StreamListener from './stream_listener'
import WebSocket from './web_socket'
import Response from './response'
import OAuth from './oauth'
import { isCancel, RequestCanceledError } from './cancel'
import { ProxyConfig } from './proxy_config'
//
import generator, { MegalodonInterface } from './megalodon'
import generator, { MegalodonInterface, WebSocketInterface, StreamListenerInterface } from './megalodon'
import Mastodon from './mastodon'
import Pleroma from './pleroma'
import Misskey from './misskey'
import Entity from './entity'

export {
MastodonAPI,
StreamListener,
WebSocket,
Response,
OAuth,
RequestCanceledError,
isCancel,
ProxyConfig,
//
MegalodonInterface,
WebSocketInterface,
StreamListenerInterface,
Mastodon,
Pleroma,
Misskey,
Expand Down
17 changes: 8 additions & 9 deletions src/mastodon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import MastodonAPI from './mastodon/api_client'
import { ProxyConfig } from './proxy_config'
import OAuth from './oauth'
import Response from './response'
import StreamListener from './stream_listener'
import WebSocket from './web_socket'
import { MegalodonInterface, NoImplementedError } from './megalodon'
import WebSocket from './mastodon/web_socket'
import { MegalodonInterface, StreamListenerInterface, NoImplementedError } from './megalodon'
import Entity from './entity'
import { NO_REDIRECT, DEFAULT_SCOPE, DEFAULT_UA } from './default'

Expand Down Expand Up @@ -1913,27 +1912,27 @@ export default class Mastodon implements MegalodonInterface {
// ======================================
// HTTP Streaming
// ======================================
public userStream(): StreamListener {
public userStream(): StreamListenerInterface {
return this.client.stream('/api/v1/streaming/user')
}

public publicStream(): StreamListener {
public publicStream(): StreamListenerInterface {
return this.client.stream('/api/v1/streaming/public')
}

public localStream(): StreamListener {
public localStream(): StreamListenerInterface {
return this.client.stream('/api/v1/streaming/public/local')
}

public tagStream(tag: string): StreamListener {
public tagStream(tag: string): StreamListenerInterface {
return this.client.stream(`/api/v1/streaming/hashtag?tag=${tag}`)
}

public listStream(list_id: string): StreamListener {
public listStream(list_id: string): StreamListenerInterface {
return this.client.stream(`/api/v1/streaming/list?list=${list_id}`)
}

public directStream(): StreamListener {
public directStream(): StreamListenerInterface {
return this.client.stream('/api/v1/streaming/direct')
}

Expand Down
4 changes: 2 additions & 2 deletions src/mastodon/api_client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios, { AxiosResponse, CancelTokenSource, AxiosRequestConfig } from 'axios'
import StreamListener from '../stream_listener'
import WebSocket from '../web_socket'
import StreamListener from './stream_listener'
import WebSocket from './web_socket'
import Response from '../response'
import { RequestCanceledError } from '../cancel'
import proxyAgent, { ProxyConfig } from '../proxy_config'
Expand Down
14 changes: 8 additions & 6 deletions src/stream_listener.ts → src/mastodon/stream_listener.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { EventEmitter } from 'events'
import axios, { CancelTokenSource, AxiosRequestConfig } from 'axios'
import httpAdapter from 'axios/lib/adapters/http'
import { Parser } from './parser'
import proxyAgent, { ProxyConfig } from './proxy_config'
import { Parser } from '../parser'
import proxyAgent, { ProxyConfig } from '../proxy_config'
import Entity from './entity'
import { StreamListenerInterface } from '../megalodon'
import MastodonAPI from './api_client'

const STATUS_CODES_TO_ABORT_ON: Array<number> = [400, 401, 403, 404, 406, 410, 422]

Expand All @@ -22,7 +24,7 @@ export class StreamListenerError extends Error {
* EventStream
* Listener of text/event-stream. It receives data, and parse when streaming.
*/
export default class StreamListener extends EventEmitter {
export default class StreamListener extends EventEmitter implements StreamListenerInterface {
public url: string
public headers: object
public parser: Parser
Expand Down Expand Up @@ -161,13 +163,13 @@ export default class StreamListener extends EventEmitter {
**/
private _setupParser() {
this.parser.on('update', (status: Entity.Status) => {
this.emit('update', status)
this.emit('update', MastodonAPI.Converter.status(status))
})
this.parser.on('notification', (notification: Entity.Notification) => {
this.emit('notification', notification)
this.emit('notification', MastodonAPI.Converter.notification(notification))
})
this.parser.on('conversation', (conversation: Entity.Conversation) => {
this.emit('conversation', conversation)
this.emit('conversation', MastodonAPI.Converter.conversation(conversation))
})
this.parser.on('delete', (id: string) => {
this.emit('delete', id)
Expand Down
14 changes: 8 additions & 6 deletions src/web_socket.ts → src/mastodon/web_socket.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import WS from 'ws'
import moment, { Moment } from 'moment'
import { EventEmitter } from 'events'
import proxyAgent, { ProxyConfig } from './proxy_config'
import Entity from './entity'
import proxyAgent, { ProxyConfig } from '../proxy_config'
import Entity from '../entity'
import { WebSocketInterface } from '../megalodon'
import MastodonAPI from './api_client'

/**
* WebSocket
* Pleroma is not support streaming. It is support websocket instead of streaming.
* So this class connect to Phoenix websocket for Pleroma.
*/
export default class WebSocket extends EventEmitter {
export default class WebSocket extends EventEmitter implements WebSocketInterface {
public url: string
public stream: string
public params: string | null
Expand Down Expand Up @@ -232,16 +234,16 @@ export default class WebSocket extends EventEmitter {
*/
private _setupParser() {
this.parser.on('update', (status: Entity.Status) => {
this.emit('update', status)
this.emit('update', MastodonAPI.Converter.status(status))
})
this.parser.on('notification', (notification: Entity.Notification) => {
this.emit('notification', notification)
this.emit('notification', MastodonAPI.Converter.notification(notification))
})
this.parser.on('delete', (id: string) => {
this.emit('delete', id)
})
this.parser.on('conversation', (conversation: Entity.Conversation) => {
this.emit('conversation', conversation)
this.emit('conversation', MastodonAPI.Converter.conversation(conversation))
})
this.parser.on('error', (err: Error) => {
this.emit('parser-error', err)
Expand Down
46 changes: 32 additions & 14 deletions src/megalodon.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import StreamListener from './stream_listener'
import WebSocket from './web_socket'
import Response from './response'
import OAuth from './oauth'
import Pleroma from './pleroma'
Expand All @@ -8,6 +6,26 @@ import Mastodon from './mastodon'
import Entity from './entity'
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'

export interface WebSocketInterface {
start(): void
stop(): void
// EventEmitter
on(event: string | symbol, listener: (...args: any[]) => void): this
once(event: string | symbol, listener: (...args: any[]) => void): this
removeListener(event: string | symbol, listener: (...args: any[]) => void): this
removeAllListeners(event?: string | symbol): this
}

export interface StreamListenerInterface {
start(): void
stop(): void
// EventEmitter
on(event: string | symbol, listener: (...args: any[]) => void): this
once(event: string | symbol, listener: (...args: any[]) => void): this
removeListener(event: string | symbol, listener: (...args: any[]) => void): this
removeAllListeners(event?: string | symbol): this
}

export interface MegalodonInterface {
/**
* Cancel all requests in this instance.
Expand Down Expand Up @@ -1132,22 +1150,22 @@ export interface MegalodonInterface {
// ======================================
// HTTP Streaming
// ======================================
userStream(): StreamListener
publicStream(): StreamListener
localStream(): StreamListener
tagStream(tag: string): StreamListener
listStream(list_id: string): StreamListener
directStream(): StreamListener
userStream(): StreamListenerInterface
publicStream(): StreamListenerInterface
localStream(): StreamListenerInterface
tagStream(tag: string): StreamListenerInterface
listStream(list_id: string): StreamListenerInterface
directStream(): StreamListenerInterface

// ======================================
// WebSocket
// ======================================
userSocket(): WebSocket
publicSocket(): WebSocket
localSocket(): WebSocket
tagSocket(tag: string): WebSocket
listSocket(list_id: string): WebSocket
directSocket(): WebSocket
userSocket(): WebSocketInterface
publicSocket(): WebSocketInterface
localSocket(): WebSocketInterface
tagSocket(tag: string): WebSocketInterface
listSocket(list_id: string): WebSocketInterface
directSocket(): WebSocketInterface
}

export class NoImplementedError extends Error {
Expand Down
35 changes: 20 additions & 15 deletions src/misskey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import { DEFAULT_UA } from './default'
import { ProxyConfig } from './proxy_config'
import OAuth from './oauth'
import Response from './response'
import { MegalodonInterface, NoImplementedError, ArgumentError, UnexpectedError } from './megalodon'
import StreamListener from './stream_listener'
import WebSocket from './web_socket'
import {
MegalodonInterface,
StreamListenerInterface,
WebSocketInterface,
NoImplementedError,
ArgumentError,
UnexpectedError
} from './megalodon'

export default class Misskey implements MegalodonInterface {
public client: MisskeyAPI.Client
Expand Down Expand Up @@ -1830,51 +1835,51 @@ export default class Misskey implements MegalodonInterface {
// ======================================
// HTTP Streaming
// ======================================
public userStream(): StreamListener {
public userStream(): StreamListenerInterface {
throw new NoImplementedError('misskey does not support')
}

public publicStream(): StreamListener {
public publicStream(): StreamListenerInterface {
throw new NoImplementedError('misskey does not support')
}

public localStream(): StreamListener {
public localStream(): StreamListenerInterface {
throw new NoImplementedError('misskey does not support')
}

public tagStream(_tag: string): StreamListener {
public tagStream(_tag: string): StreamListenerInterface {
throw new NoImplementedError('misskey does not support')
}

public listStream(_list_id: string): StreamListener {
public listStream(_list_id: string): StreamListenerInterface {
throw new NoImplementedError('misskey does not support')
}

public directStream(): StreamListener {
public directStream(): StreamListenerInterface {
throw new NoImplementedError('misskey does not support')
}

public userSocket(): WebSocket {
public userSocket(): WebSocketInterface {
throw new NoImplementedError('TODO: implement')
}

public publicSocket(): WebSocket {
public publicSocket(): WebSocketInterface {
throw new NoImplementedError('TODO: implement')
}

public localSocket(): WebSocket {
public localSocket(): WebSocketInterface {
throw new NoImplementedError('TODO: implement')
}

public tagSocket(_tag: string): WebSocket {
public tagSocket(_tag: string): WebSocketInterface {
throw new NoImplementedError('TODO: implement')
}

public listSocket(_list_id: string): WebSocket {
public listSocket(_list_id: string): WebSocketInterface {
throw new NoImplementedError('TODO: implement')
}

public directSocket(): WebSocket {
public directSocket(): WebSocketInterface {
throw new NoImplementedError('TODO: implement')
}
}
Loading