Skip to content

Commit

Permalink
feat: remove UserData.name
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 16, 2020
1 parent 5c38348 commit 20ebbae
Show file tree
Hide file tree
Showing 11 changed files with 11 additions and 101 deletions.
2 changes: 1 addition & 1 deletion packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ export class App extends Context {
}

// attach user data
const userFields = new Set<UserField>(['name', 'flag'])
const userFields = new Set<UserField>(['flag'])
this.receiver.emit('before-user', userFields, parsedArgv || { meta })
const user = await this.database.observeUser(meta.userId, Array.from(userFields))
Object.defineProperty(meta, '$user', { value: user, writable: true })
Expand Down
1 change: 0 additions & 1 deletion packages/koishi-core/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const userFlags: (keyof typeof UserFlag)[] = ['ignore']

export interface UserData {
id: number
name: string
flag: number
authority: number
usage: Record<string, Usage>
Expand Down
8 changes: 1 addition & 7 deletions packages/koishi-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ import { messages } from './messages'
import { format } from 'util'
import leven from 'leven'

export function getSenderName (meta: MessageMeta) {
const userId = '' + meta.userId
return meta.$user && meta.$user.name !== userId ? meta.$user.name
: meta.sender ? meta.sender.card || meta.sender.nickname : userId
}

export function getTargetId (target: string | number) {
if (typeof target !== 'string' && typeof target !== 'number') return
let qq = +target
Expand Down Expand Up @@ -51,7 +45,7 @@ export function showSuggestions (options: SuggestOptions): Promise<void> {
const [suggestion] = suggestions
const command = typeof options.command === 'function' ? options.command(suggestion) : options.command
const identifier = meta.userId + meta.$ctxType + meta.$ctxId
const userFields = new Set<UserField>(['name'])
const userFields = new Set<UserField>()
const groupFields = new Set<GroupField>()
Command.attachUserFields(userFields, { command, meta })
Command.attachGroupFields(groupFields, { command, meta })
Expand Down
37 changes: 1 addition & 36 deletions packages/koishi-core/tests/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,4 @@
import { getSenderName, createUser, getTargetId } from 'koishi-core'
import { createSender } from 'koishi-test-utils'
import { observe } from 'koishi-utils'

describe('getSenderName', () => {
test('userData with userId', () => {
expect(getSenderName({
userId: 123,
$user: observe(createUser(123, 1)),
})).toBe('123')
})

test('userData with name', () => {
expect(getSenderName({
userId: 123,
$user: observe({ ...createUser(123, 1), name: '456' }),
sender: createSender(123, 'bar', 'foo'),
})).toBe('456')
})

test('userData with card', () => {
expect(getSenderName({
userId: 123,
$user: observe(createUser(123, 1)),
sender: createSender(123, 'bar', 'foo'),
})).toBe('foo')
})

test('userData with nickname', () => {
expect(getSenderName({
userId: 123,
$user: observe(createUser(123, 1)),
sender: createSender(123, 'bar'),
})).toBe('bar')
})
})
import { getTargetId } from 'koishi-core'

describe('getTargetId', () => {
test('with id', () => {
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-common/src/authorize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ interface AuthorizeInfo {
export default function apply (ctx: Context, config: AuthorizeConfig) {
const { app, database } = ctx
const { authorizeUser = {}, authorizeGroup = {} } = config
const logger = ctx.logger('authorize')
const authorityMap: Record<number, AuthorizeInfo> = []

/**
Expand Down
38 changes: 0 additions & 38 deletions packages/plugin-common/src/callme.ts

This file was deleted.

8 changes: 2 additions & 6 deletions packages/plugin-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { Context, CommandConfig } from 'koishi-core'
import admin from './admin'
import authorize, { AuthorizeConfig } from './authorize'
import broadcast, { BroadcastOptions } from './broadcast'
import callme, { CallmeOptions } from './callme'
import contextify from './contextify'
import echo from './echo'
import exit from './exit'
import help from './help'
import info from './info'
import info, { InfoOptions } from './info'
import repeater, { RepeaterOptions } from './repeater'
import requestHandler, { HandlerConfig } from './request-handler'
import respondent, { Respondent } from './respondent'
Expand All @@ -20,7 +19,6 @@ export {
admin,
authorize,
broadcast,
callme,
contextify,
echo,
exit,
Expand All @@ -35,12 +33,11 @@ export {
interface CommonPluginConfig extends HandlerConfig, AuthorizeConfig {
admin?: false | CommandConfig
broadcast?: false | BroadcastOptions
callme?: false | CallmeOptions
contextify?: false | CommandConfig
echo?: false | CommandConfig
exit?: false | CommandConfig
help?: false | CommandConfig
info?: false | CommandConfig
info?: false | InfoOptions
repeater?: false | RepeaterOptions
respondent?: Respondent[]
welcome?: false | WelcomeMessage
Expand All @@ -64,7 +61,6 @@ export function apply (ctx: Context, options: CommonPluginConfig = {}) {
.plugin(admin, options.admin)
.plugin(authorize, options)
.plugin(broadcast, options.broadcast)
.plugin(callme, options.callme)
.plugin(info, options.info)
}
}
6 changes: 3 additions & 3 deletions packages/plugin-common/src/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export function registerUserInfo <K extends UserField> (callback: UserInfoCallba
}
}

export interface InfoConfig extends CommandConfig {
export interface InfoOptions extends CommandConfig {
getSenderName? (user: UserData, meta: Meta<'message'>): string
}

const defaultConfig: InfoConfig = {
const defaultConfig: InfoOptions = {
authority: 0,
getSenderName (user, meta) {
if (meta.userId === user.id && meta.sender) {
Expand All @@ -25,7 +25,7 @@ const defaultConfig: InfoConfig = {
},
}

export default function apply (ctx: Context, config: InfoConfig = {}) {
export default function apply (ctx: Context, config: InfoOptions = {}) {
config = { ...defaultConfig, ...config }
ctx.command('info', '查看用户信息', config)
.alias('i')
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-common/tests/info.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MockedApp, MemoryDatabase, Session } from 'koishi-test-utils'
import { registerDatabase } from 'koishi-core'
import info, { registerUserInfo, InfoConfig } from '../src/info'
import info, { registerUserInfo, InfoOptions } from '../src/info'

registerDatabase('memory', MemoryDatabase)

Expand Down Expand Up @@ -38,7 +38,7 @@ test('basic support', async () => {
})

test('getSenderName', async () => {
app.plugin<InfoConfig>(info, {
app.plugin<InfoOptions>(info, {
getSenderName (user, meta) {
if (user.id !== meta.userId) return 'bar'
},
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export function testDatabase (config: DatabaseConfig, options: TestDatabaseOptio
})

test('observeUser merge', async () => {
const user: UserData = { id: 1000, flag: 3, name: '', authority: 1, usage: {} }
const user: UserData = { id: 1000, flag: 3, authority: 1, usage: {} }
const observedUser = await db.observeUser(user, 1)
expect(observedUser).toMatchObject(user)
observedUser.flag = 5
Expand Down
5 changes: 0 additions & 5 deletions packages/test-utils/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SenderInfo } from 'koishi-core'
import debug from 'debug'

export const BASE_SELF_ID = 514
Expand All @@ -20,7 +19,3 @@ export function fromEntries <T> (entries: Iterable<readonly [string, T]>) {
export function createArray <T> (length: number, create: (index: number) => T) {
return Array(length).fill(undefined).map((_, index) => create(index))
}

export function createSender (userId: number, nickname: string, card = '') {
return { userId, nickname, card, sex: 'unknown', age: 20 } as SenderInfo
}

0 comments on commit 20ebbae

Please sign in to comment.