refs #125 Parse notification in websocket for misskey
This commit is contained in:
parent
a249fd2948
commit
493439e7ff
|
@ -13,7 +13,7 @@ const access_token: string = process.env.MISSKEY_ACCESS_TOKEN
|
|||
|
||||
const client = generator('misskey', BASE_URL, access_token)
|
||||
|
||||
const stream: WebSocketInterface = client.publicSocket()
|
||||
const stream: WebSocketInterface = client.userSocket()
|
||||
|
||||
const logger = log4js.getLogger()
|
||||
logger.level = 'debug'
|
||||
|
@ -29,6 +29,10 @@ stream.on('update', (status: Entity.Status) => {
|
|||
logger.debug(status)
|
||||
})
|
||||
|
||||
stream.on('notification', (notification: Entity.Notification) => {
|
||||
logger.debug(notification)
|
||||
})
|
||||
|
||||
stream.on('error', (err: Error) => {
|
||||
console.error(err)
|
||||
})
|
||||
|
@ -36,3 +40,7 @@ stream.on('error', (err: Error) => {
|
|||
stream.on('close', () => {
|
||||
logger.debug('close')
|
||||
})
|
||||
|
||||
stream.on('parser-error', (err: Error) => {
|
||||
console.error(err)
|
||||
})
|
||||
|
|
|
@ -1860,7 +1860,7 @@ export default class Misskey implements MegalodonInterface {
|
|||
}
|
||||
|
||||
public userSocket(): WebSocketInterface {
|
||||
return this.client.socket('homeTimeline')
|
||||
return this.client.socket('user')
|
||||
}
|
||||
|
||||
public publicSocket(): WebSocketInterface {
|
||||
|
|
|
@ -60,7 +60,7 @@ namespace MisskeyAPI {
|
|||
following_count: 0,
|
||||
statuses_count: 0,
|
||||
note: '',
|
||||
url: '',
|
||||
url: acct,
|
||||
avatar: u.avatarUrl,
|
||||
avatar_static: u.avatarColor,
|
||||
header: '',
|
||||
|
@ -187,8 +187,8 @@ namespace MisskeyAPI {
|
|||
export const note = (n: Entity.Note): MegalodonEntity.Status => {
|
||||
return {
|
||||
id: n.id,
|
||||
uri: '',
|
||||
url: '',
|
||||
uri: n.uri ? n.uri : '',
|
||||
url: n.uri ? n.uri : '',
|
||||
account: user(n.user),
|
||||
in_reply_to_id: n.replyId,
|
||||
in_reply_to_account_id: null,
|
||||
|
@ -460,7 +460,7 @@ namespace MisskeyAPI {
|
|||
return this.cancelTokenSource.cancel('Request is canceled by user')
|
||||
}
|
||||
|
||||
public socket(channel: 'homeTimeline' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline'): WebSocket {
|
||||
public socket(channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline'): WebSocket {
|
||||
if (!this.accessToken) {
|
||||
throw new Error('accessToken is required')
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import MisskeyAPI from './api_client'
|
|||
|
||||
export default class WebSocket extends EventEmitter implements WebSocketInterface {
|
||||
public url: string
|
||||
public channel: 'homeTimeline' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline'
|
||||
public channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline'
|
||||
public parser: Parser
|
||||
private _accessToken: string
|
||||
private _reconnectInterval: number
|
||||
|
@ -16,7 +16,7 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
|
|||
private _client: WS | null = null
|
||||
private _channelID: string
|
||||
|
||||
constructor(url: string, channel: 'homeTimeline' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline', accessToken: string) {
|
||||
constructor(url: string, channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline', accessToken: string) {
|
||||
super()
|
||||
this.url = url
|
||||
this.parser = new Parser()
|
||||
|
@ -73,15 +73,36 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
|
|||
if (!this._client) {
|
||||
return
|
||||
}
|
||||
this._client.send(
|
||||
JSON.stringify({
|
||||
type: 'connect',
|
||||
body: {
|
||||
channel: this.channel,
|
||||
id: this._channelID
|
||||
}
|
||||
})
|
||||
)
|
||||
if (this.channel === 'user') {
|
||||
this._client.send(
|
||||
JSON.stringify({
|
||||
type: 'connect',
|
||||
body: {
|
||||
channel: 'main',
|
||||
id: this._channelID
|
||||
}
|
||||
})
|
||||
)
|
||||
this._client.send(
|
||||
JSON.stringify({
|
||||
type: 'connect',
|
||||
body: {
|
||||
channel: 'homeTimeline',
|
||||
id: this._channelID
|
||||
}
|
||||
})
|
||||
)
|
||||
} else {
|
||||
this._client.send(
|
||||
JSON.stringify({
|
||||
type: 'connect',
|
||||
body: {
|
||||
channel: this.channel,
|
||||
id: this._channelID
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private _reconnect() {
|
||||
|
@ -151,6 +172,12 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
|
|||
this.parser.on('update', (note: MisskeyAPI.Entity.Note) => {
|
||||
this.emit('update', MisskeyAPI.Converter.note(note))
|
||||
})
|
||||
this.parser.on('notification', (notification: MisskeyAPI.Entity.Notification) => {
|
||||
this.emit('notification', MisskeyAPI.Converter.notification(notification))
|
||||
})
|
||||
this.parser.on('error', (err: Error) => {
|
||||
this.emit('parser-error', err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,8 +235,22 @@ export class Parser extends EventEmitter {
|
|||
case 'note':
|
||||
this.emit('update', body.body as MisskeyAPI.Entity.Note)
|
||||
break
|
||||
case 'notification':
|
||||
this.emit('notification', body.body as MisskeyAPI.Entity.Notification)
|
||||
break
|
||||
case 'renote':
|
||||
case 'followed':
|
||||
case 'mention':
|
||||
case 'receiveFollowRequest':
|
||||
case 'meUpdated':
|
||||
case 'readAllNotifications':
|
||||
case 'readAllUnreadSpecifiedNotes':
|
||||
case 'readAllAntennas':
|
||||
case 'readAllUnreadMentions':
|
||||
// Ignore these events
|
||||
break
|
||||
default:
|
||||
this.emit('error', new Error(`Unknown event has received: ${body}`))
|
||||
this.emit('error', new Error(`Unknown event has received: ${JSON.stringify(body)}`))
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue