refs #125 Parse direct mention in websocket for misskey
This commit is contained in:
parent
493439e7ff
commit
58a14f4b56
20
example/typescript/package-lock.json
generated
20
example/typescript/package-lock.json
generated
|
@ -79,6 +79,8 @@
|
|||
"moment": "^2.24.0",
|
||||
"oauth": "^0.9.15",
|
||||
"socks-proxy-agent": "github:h3poteto/node-socks-proxy-agent#master",
|
||||
"typescript": "3.8.2",
|
||||
"uuid": "^7.0.2",
|
||||
"ws": "^7.2.1"
|
||||
},
|
||||
"dependencies": {
|
||||
|
@ -4376,8 +4378,7 @@
|
|||
"qs": "~6.5.2",
|
||||
"safe-buffer": "^5.1.2",
|
||||
"tough-cookie": "~2.4.3",
|
||||
"tunnel-agent": "^0.6.0",
|
||||
"uuid": "^3.3.2"
|
||||
"tunnel-agent": "^0.6.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"punycode": {
|
||||
|
@ -5443,8 +5444,7 @@
|
|||
"minimatch": "^3.0.0",
|
||||
"progress": "^2.0.3",
|
||||
"shelljs": "^0.8.3",
|
||||
"typedoc-default-themes": "^0.7.2",
|
||||
"typescript": "3.7.x"
|
||||
"typedoc-default-themes": "^0.7.2"
|
||||
}
|
||||
},
|
||||
"typedoc-default-themes": {
|
||||
|
@ -5459,9 +5459,9 @@
|
|||
}
|
||||
},
|
||||
"typescript": {
|
||||
"version": "3.7.5",
|
||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.5.tgz",
|
||||
"integrity": "sha512-/P5lkRXkWHNAbcJIiHPfRoKqyd7bsyCma1hZNUGfn20qm64T6ZBlrzprymeu918H+mB/0rIg2gGK/BXkhhYgBw=="
|
||||
"version": "3.8.2",
|
||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.8.2.tgz",
|
||||
"integrity": "sha512-EgOVgL/4xfVrCMbhYKUQTdF37SQn4Iw73H5BgCrF1Abdun7Kwy/QZsE/ssAy0y4LxBbvua3PIbFsbRczWWnDdQ=="
|
||||
},
|
||||
"uglify-js": {
|
||||
"version": "3.7.2",
|
||||
|
@ -5558,9 +5558,9 @@
|
|||
}
|
||||
},
|
||||
"uuid": {
|
||||
"version": "3.3.2",
|
||||
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz",
|
||||
"integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA=="
|
||||
"version": "7.0.2",
|
||||
"resolved": "https://registry.npmjs.org/uuid/-/uuid-7.0.2.tgz",
|
||||
"integrity": "sha512-vy9V/+pKG+5ZTYKf+VcphF5Oc6EFiu3W8Nv3P3zIh0EqVI80ZxOzuPfe9EHjkFNvf8+xuTHVeei4Drydlx4zjw=="
|
||||
},
|
||||
"v8-compile-cache": {
|
||||
"version": "2.0.3",
|
||||
|
|
|
@ -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.userSocket()
|
||||
const stream: WebSocketInterface = client.directSocket()
|
||||
|
||||
const logger = log4js.getLogger()
|
||||
logger.level = 'debug'
|
||||
|
@ -33,6 +33,10 @@ stream.on('notification', (notification: Entity.Notification) => {
|
|||
logger.debug(notification)
|
||||
})
|
||||
|
||||
stream.on('conversation', (con: Entity.Conversation) => {
|
||||
logger.debug(con)
|
||||
})
|
||||
|
||||
stream.on('error', (err: Error) => {
|
||||
console.error(err)
|
||||
})
|
||||
|
|
|
@ -1880,6 +1880,6 @@ export default class Misskey implements MegalodonInterface {
|
|||
}
|
||||
|
||||
public directSocket(): WebSocketInterface {
|
||||
throw new NoImplementedError('TODO: implement')
|
||||
return this.client.socket('conversation')
|
||||
}
|
||||
}
|
||||
|
|
|
@ -460,7 +460,7 @@ namespace MisskeyAPI {
|
|||
return this.cancelTokenSource.cancel('Request is canceled by user')
|
||||
}
|
||||
|
||||
public socket(channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline'): WebSocket {
|
||||
public socket(channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline' | 'conversation'): 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: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline'
|
||||
public channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline' | 'conversation'
|
||||
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: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline', accessToken: string) {
|
||||
constructor(url: string, channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline' | 'conversation', accessToken: string) {
|
||||
super()
|
||||
this.url = url
|
||||
this.parser = new Parser()
|
||||
|
@ -73,35 +73,49 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
|
|||
if (!this._client) {
|
||||
return
|
||||
}
|
||||
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
|
||||
}
|
||||
})
|
||||
)
|
||||
switch (this.channel) {
|
||||
case 'conversation':
|
||||
this._client.send(
|
||||
JSON.stringify({
|
||||
type: 'connect',
|
||||
body: {
|
||||
channel: 'main',
|
||||
id: this._channelID
|
||||
}
|
||||
})
|
||||
)
|
||||
break
|
||||
case '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
|
||||
}
|
||||
})
|
||||
)
|
||||
break
|
||||
default:
|
||||
this._client.send(
|
||||
JSON.stringify({
|
||||
type: 'connect',
|
||||
body: {
|
||||
channel: this.channel,
|
||||
id: this._channelID
|
||||
}
|
||||
})
|
||||
)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -175,6 +189,9 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
|
|||
this.parser.on('notification', (notification: MisskeyAPI.Entity.Notification) => {
|
||||
this.emit('notification', MisskeyAPI.Converter.notification(notification))
|
||||
})
|
||||
this.parser.on('conversation', (note: MisskeyAPI.Entity.Note) => {
|
||||
this.emit('conversation', MisskeyAPI.Converter.noteToConversation(note))
|
||||
})
|
||||
this.parser.on('error', (err: Error) => {
|
||||
this.emit('parser-error', err)
|
||||
})
|
||||
|
@ -238,9 +255,14 @@ export class Parser extends EventEmitter {
|
|||
case 'notification':
|
||||
this.emit('notification', body.body as MisskeyAPI.Entity.Notification)
|
||||
break
|
||||
case 'mention':
|
||||
const note = body.body as MisskeyAPI.Entity.Note
|
||||
if (note.visibility === 'specified') {
|
||||
this.emit('conversation', note)
|
||||
}
|
||||
break
|
||||
case 'renote':
|
||||
case 'followed':
|
||||
case 'mention':
|
||||
case 'receiveFollowRequest':
|
||||
case 'meUpdated':
|
||||
case 'readAllNotifications':
|
||||
|
|
Loading…
Reference in a new issue