2020-03-15 09:47:40 +01:00
|
|
|
import generator, { MegalodonInterface, WebSocketInterface, Entity, ProxyConfig } from 'megalodon'
|
2018-10-05 01:42:59 +02:00
|
|
|
import log from 'electron-log'
|
2019-06-06 16:44:50 +02:00
|
|
|
import { LocalAccount } from '~/src/types/localAccount'
|
2018-10-05 01:42:59 +02:00
|
|
|
|
2020-03-15 09:47:40 +01:00
|
|
|
const StreamingURL = async (
|
|
|
|
sns: 'mastodon' | 'pleroma' | 'misskey',
|
|
|
|
account: LocalAccount,
|
|
|
|
proxy: ProxyConfig | false
|
|
|
|
): Promise<string> => {
|
2019-09-14 15:44:43 +02:00
|
|
|
if (!account.accessToken) {
|
|
|
|
throw new Error('access token is empty')
|
|
|
|
}
|
2020-03-15 09:47:40 +01:00
|
|
|
const client = generator(sns, account.baseURL, account.accessToken, 'Whalebird', proxy)
|
|
|
|
const res = await client.getInstance()
|
2019-07-29 16:32:02 +02:00
|
|
|
return res.data.urls.streaming_api
|
|
|
|
}
|
|
|
|
|
|
|
|
export { StreamingURL }
|
|
|
|
|
2020-03-15 09:47:40 +01:00
|
|
|
class WebSocket {
|
|
|
|
public client: MegalodonInterface
|
|
|
|
public listener: WebSocketInterface | null
|
2019-04-16 18:09:29 +02:00
|
|
|
|
2020-03-15 09:47:40 +01:00
|
|
|
constructor(sns: 'mastodon' | 'pleroma' | 'misskey', account: LocalAccount, streamingURL: string, proxy: ProxyConfig | false) {
|
2019-07-29 16:32:02 +02:00
|
|
|
const url = streamingURL.replace(/^https:\/\//, 'wss://')
|
2020-03-15 09:47:40 +01:00
|
|
|
this.client = generator(sns, url, account.accessToken, 'Whalebird', proxy)
|
2018-10-05 01:42:59 +02:00
|
|
|
this.listener = null
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:47:40 +01:00
|
|
|
public bindListener(updateCallback: Function, deleteCallback: Function, errCallback: Function) {
|
|
|
|
if (!this.listener) {
|
|
|
|
log.error('listener does not exist')
|
|
|
|
return
|
|
|
|
}
|
2018-10-05 01:42:59 +02:00
|
|
|
|
2020-03-15 09:47:40 +01:00
|
|
|
this.listener.on('update', (status: Entity.Status) => {
|
2018-10-05 01:42:59 +02:00
|
|
|
updateCallback(status)
|
|
|
|
})
|
|
|
|
|
2019-05-29 15:06:56 +02:00
|
|
|
this.listener.on('delete', (id: string) => {
|
|
|
|
deleteCallback(id)
|
|
|
|
})
|
|
|
|
|
2019-04-16 18:09:29 +02:00
|
|
|
this.listener.on('error', (err: Error) => {
|
2018-10-05 01:42:59 +02:00
|
|
|
errCallback(err)
|
|
|
|
})
|
|
|
|
|
2019-04-16 18:09:29 +02:00
|
|
|
this.listener.on('parser-error', (err: Error) => {
|
2018-10-05 01:42:59 +02:00
|
|
|
errCallback(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:47:40 +01:00
|
|
|
public stop() {
|
2018-10-05 01:42:59 +02:00
|
|
|
if (this.listener) {
|
2018-10-09 18:19:55 +02:00
|
|
|
this.listener.removeAllListeners('connect')
|
|
|
|
this.listener.removeAllListeners('update')
|
|
|
|
this.listener.removeAllListeners('notification')
|
|
|
|
this.listener.removeAllListeners('error')
|
|
|
|
this.listener.removeAllListeners('parser-error')
|
2019-04-16 18:09:29 +02:00
|
|
|
this.listener.on('error', (e: Error) => {
|
2018-12-07 15:20:05 +01:00
|
|
|
log.error(e)
|
|
|
|
})
|
2019-04-16 18:09:29 +02:00
|
|
|
this.listener.on('parser-error', (e: Error) => {
|
2018-12-19 17:21:03 +01:00
|
|
|
log.error(e)
|
|
|
|
})
|
2018-12-07 15:20:05 +01:00
|
|
|
this.listener.stop()
|
2018-10-05 01:42:59 +02:00
|
|
|
log.info('streaming stopped')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-15 09:47:40 +01:00
|
|
|
|
|
|
|
export class UserStreaming extends WebSocket {
|
|
|
|
public start(updateCallback: Function, notificationCallback: Function, deleteCallback: Function, errCallback: Function) {
|
|
|
|
this.listener = this.client.userSocket()
|
|
|
|
|
|
|
|
this.listener.on('connect', _ => {
|
|
|
|
log.info('user streaming is started')
|
|
|
|
})
|
|
|
|
|
|
|
|
this.listener.on('notification', (notification: Entity.Notification) => {
|
|
|
|
notificationCallback(notification)
|
|
|
|
})
|
|
|
|
|
|
|
|
this.bindListener(updateCallback, deleteCallback, errCallback)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DirectStreaming extends WebSocket {
|
|
|
|
public start(updateCallback: Function, deleteCallback: Function, errCallback: Function) {
|
|
|
|
this.listener = this.client.directSocket()
|
|
|
|
|
|
|
|
this.listener.on('connect', _ => {
|
|
|
|
log.info('direct streaming is started')
|
|
|
|
})
|
|
|
|
|
|
|
|
this.bindListener(updateCallback, deleteCallback, errCallback)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class LocalStreaming extends WebSocket {
|
|
|
|
public start(updateCallback: Function, deleteCallback: Function, errCallback: Function) {
|
|
|
|
this.listener = this.client.localSocket()
|
|
|
|
|
|
|
|
this.listener.on('connect', _ => {
|
|
|
|
log.info('local streaming is started')
|
|
|
|
})
|
|
|
|
|
|
|
|
this.bindListener(updateCallback, deleteCallback, errCallback)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PublicStreaming extends WebSocket {
|
|
|
|
public start(updateCallback: Function, deleteCallback: Function, errCallback: Function) {
|
|
|
|
this.listener = this.client.publicSocket()
|
|
|
|
|
|
|
|
this.listener.on('connect', _ => {
|
|
|
|
log.info('public streaming is started')
|
|
|
|
})
|
|
|
|
|
|
|
|
this.bindListener(updateCallback, deleteCallback, errCallback)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ListStreaming extends WebSocket {
|
|
|
|
public start(listID: string, updateCallback: Function, deleteCallback: Function, errCallback: Function) {
|
|
|
|
this.listener = this.client.listSocket(listID)
|
|
|
|
|
|
|
|
this.listener.on('connect', _ => {
|
|
|
|
log.info('list streaming is started')
|
|
|
|
})
|
|
|
|
|
|
|
|
this.bindListener(updateCallback, deleteCallback, errCallback)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class TagStreaming extends WebSocket {
|
|
|
|
public start(tag: string, updateCallback: Function, deleteCallback: Function, errCallback: Function) {
|
|
|
|
this.listener = this.client.tagSocket(tag)
|
|
|
|
|
|
|
|
this.listener.on('connect', _ => {
|
|
|
|
log.info('tag streaming is started')
|
|
|
|
})
|
|
|
|
|
|
|
|
this.bindListener(updateCallback, deleteCallback, errCallback)
|
|
|
|
}
|
|
|
|
}
|