Skip to content

Commit

Permalink
feat(protocol): deprecate selfId, prefer login
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 7, 2024
1 parent 83742e6 commit 22d57a7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
6 changes: 4 additions & 2 deletions adapters/satori/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export class SatoriAdapter<C extends Context = Context> extends Adapter.WsClient
bot.adapter = this
bot.http = this.http.extend({
headers: {
'Satori-Platform': platform,
'Satori-Login-ID': selfId,
'X-Platform': platform,
'X-Self-ID': selfId,
},
Expand Down Expand Up @@ -95,12 +97,12 @@ export class SatoriAdapter<C extends Context = Context> extends Adapter.WsClient
if (parsed.op === Universal.Opcode.READY) {
this.logger.debug('ready')
for (const login of parsed.body.logins) {
this.getBot(login.platform, login.selfId, login)
this.getBot(login.platform, login.user.id, login)
}
}

if (parsed.op === Universal.Opcode.EVENT) {
const { id, type, selfId, platform, login } = parsed.body
const { id, type, login, selfId = login?.user.id, platform = login?.platform } = parsed.body
this.sequence = id
// `login-*` events will be dispatched by the bot,
// so there is no need to create sessions manually.
Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/session.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Channel, Event, GuildMember, Message, User } from '@satorijs/protocol'
import { Channel, Event, GuildMember, Login, Message, User } from '@satorijs/protocol'
import { defineProperty, isNullable } from 'cosmokit'
import { Context, Service } from 'cordis'
import { Bot } from './bot'
Expand Down Expand Up @@ -137,7 +137,14 @@ export class Session<C extends Context = Context> {
}

toJSON(): Event {
return { ...this.event, id: this.id }
return {
login: {
platform: this.platform,
user: { id: this.userId },
} as Login,
...this.event,
id: this.id,
}
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/protocol/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export interface GuildMember {
export interface Login {
user?: User
platform?: string
/** @deprecated use `login.user.id` instead */
selfId?: string
hidden?: boolean
status: Status
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class SatoriServer extends Service<SatoriServer.Config> {

if (checkAuth(koa)) return

const selfId = koa.request.headers['x-self-id']
const platform = koa.request.headers['x-platform']
const selfId = koa.request.headers['satori-login-id'] ?? koa.request.headers['x-self-id']
const platform = koa.request.headers['satori-platform'] ?? koa.request.headers['x-platform']
const bot = ctx.bots.find(bot => bot.selfId === selfId && bot.platform === platform)
if (!bot) {
koa.body = 'login not found'
Expand Down

0 comments on commit 22d57a7

Please sign in to comment.