Skip to content

Commit

Permalink
feat(database): bitwise message.flag
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 6, 2024
1 parent 2abeded commit 152ff1e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 75 deletions.
20 changes: 10 additions & 10 deletions packages/database/src/channel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Bot, Context, Logger, Session, Universal } from '@satorijs/satori'
import { Message, SyncFlag } from '.'
import { Message } from './types'
import { Span } from './span'

const logger = new Logger('sync')
Expand Down Expand Up @@ -29,25 +29,25 @@ export class SyncChannel {
.select('satori.message')
.where({
...this._query,
syncFlag: { $bitsAnySet: SyncFlag.FRONT | SyncFlag.BACK },
flag: { $bitsAnySet: Message.Flag.FRONT | Message.Flag.BACK },
})
.orderBy('sid', 'asc')
.project(['id', 'sid', 'syncFlag'])
.project(['id', 'sid', 'flag'])
.execute()
while (data.length) {
const { syncFlag, id: frontId, sid: frontUid } = data.pop()!
const { flag, id: frontId, sid: frontUid } = data.pop()!
const front: Span.Endpoint = [frontUid, frontId]
if (syncFlag === (SyncFlag.FRONT | SyncFlag.BACK)) {
if (!(flag & Message.Flag.FRONT)) {
throw new Error('malformed sync flag')
} else if (flag & Message.Flag.BACK) {
this._spans.push(new Span(this, Span.Type.REMOTE, front, front))
} else if (syncFlag === SyncFlag.FRONT) {
const { syncFlag, id, sid } = data.pop()!
if (syncFlag === SyncFlag.BACK) {
} else {
const { flag, id, sid } = data.pop()!
if (flag & Message.Flag.BACK) {
this._spans.push(new Span(this, Span.Type.REMOTE, front, [sid, id]))
} else {
throw new Error('malformed sync flag')
}
} else {
throw new Error('malformed sync flag')
}
}
}
Expand Down
60 changes: 1 addition & 59 deletions packages/database/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@ import * as Universal from '@satorijs/protocol'
import { SyncChannel } from './channel'
import { SyncGuild } from './guild'

declare module 'minato' {
interface Tables {
'satori.message': Message
'satori.user': Universal.User & { platform: string }
'satori.guild': Universal.Guild & { platform: string }
'satori.channel': Universal.Channel & { platform: string }
}
}
export * from './types'

declare module '@satorijs/core' {
// https://github.com/typescript-eslint/typescript-eslint/issues/6720
Expand All @@ -21,56 +14,6 @@ declare module '@satorijs/core' {
}
}

declare module '@satorijs/protocol' {
interface Message {
sid?: bigint
}
}

export enum SyncFlag {
NONE = 0,
FRONT = 1,
BACK = 2,
}

export interface Message extends Universal.Message {
uid: number
sid: bigint
platform: string
syncFlag: SyncFlag
sendFlag: number
deleted: boolean
edited: boolean
}

export namespace Message {
export type Ref = ['before' | 'after', bigint]

function sequence(ts: bigint, dir?: 'before' | 'after', ref?: bigint) {
if (!dir || !ref) return (ts << 12n) + 0x800n
if (ts === ref >> 12n) {
return ref + (dir === 'before' ? -1n : 1n)
} else {
return (ts << 12n) + (dir === 'before' ? 0xfffn : 0n)
}
}

export const from = (message: Universal.Message, platform: string, dir?: 'before' | 'after', ref?: bigint) => ({
platform,
id: message.id,
sid: sequence(BigInt(message.timestamp!), dir, ref),
content: message.content,
timestamp: message.timestamp,
channel: { id: message.channel?.id },
user: { id: message.user?.id },
guild: { id: message.guild?.id },
quote: { id: message.quote?.id },
createdAt: message.createdAt,
updatedAt: message.updatedAt,
syncFlag: 0,
} as Message)
}

class SatoriDatabase extends Service<SatoriDatabase.Config, Context> {
inject = ['model', 'database']

Expand Down Expand Up @@ -102,7 +45,6 @@ class SatoriDatabase extends Service<SatoriDatabase.Config, Context> {
'createdAt': 'unsigned(8)',
'updatedAt': 'unsigned(8)',
'syncFlag': 'unsigned(1)',
'sendFlag': 'unsigned(1)',
// 'deleted': 'boolean',
// 'edited': 'boolean',
}, {
Expand Down
12 changes: 6 additions & 6 deletions packages/database/src/span.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { clone, remove } from '@satorijs/satori'
import { Message, SyncFlag } from '.'
import { SyncChannel } from './channel'
import { $, Update } from 'minato'
import { Message } from './types'
import { SyncChannel } from './channel'

export class Span {
prev?: Span
Expand Down Expand Up @@ -60,19 +60,19 @@ export class Span {
data.push({
...this.channel._query,
sid: this.next.back[0],
syncFlag: $.bitAnd(row.syncFlag, $.bitNot(SyncFlag.BACK)),
flag: $.bitAnd(row.flag, $.bitNot(Message.Flag.BACK)),
})
} else {
data[data.length - 1].syncFlag |= SyncFlag.FRONT
data[data.length - 1].flag |= Message.Flag.FRONT
}
if (this.prev?.type === Span.Type.REMOTE) {
data.unshift({
...this.channel._query,
sid: this.prev.front[0],
syncFlag: $.bitAnd(row.syncFlag, $.bitNot(SyncFlag.FRONT)),
flag: $.bitAnd(row.flag, $.bitNot(Message.Flag.FRONT)),
})
} else {
data[0].syncFlag |= SyncFlag.BACK
data[0].flag |= Message.Flag.BACK
}
return data
}, ['sid', 'channel.id', 'platform'])
Expand Down
57 changes: 57 additions & 0 deletions packages/database/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Universal } from '@satorijs/satori'

declare module 'minato' {
interface Tables {
'satori.message': Message
'satori.user': Universal.User & { platform: string }
'satori.guild': Universal.Guild & { platform: string }
'satori.channel': Universal.Channel & { platform: string }
}
}

declare module '@satorijs/protocol' {
interface Message {
sid?: bigint
}
}

export interface Message extends Universal.Message {
uid: number
sid: bigint
platform: string
flag: number
deleted: boolean
edited: boolean
}

export namespace Message {
export enum Flag {
FRONT = 1,
BACK = 2,
FINAL = 4,
}

function sequence(ts: bigint, dir?: 'before' | 'after', ref?: bigint) {
if (!dir || !ref) return (ts << 12n) + 0x800n
if (ts === ref >> 12n) {
return ref + (dir === 'before' ? -1n : 1n)
} else {
return (ts << 12n) + (dir === 'before' ? 0xfffn : 0n)
}
}

export const from = (message: Universal.Message, platform: string, dir?: 'before' | 'after', ref?: bigint) => ({
platform,
id: message.id,
sid: sequence(BigInt(message.timestamp!), dir, ref),
content: message.content,
timestamp: message.timestamp,
channel: { id: message.channel?.id },
user: { id: message.user?.id },
guild: { id: message.guild?.id },
quote: { id: message.quote?.id },
createdAt: message.createdAt,
updatedAt: message.updatedAt,
flag: 0,
} as Message)
}

0 comments on commit 152ff1e

Please sign in to comment.