Commit 7fafdfc1 authored by nanahira's avatar nanahira

fix private detect

parent 764b8fff
......@@ -2,12 +2,17 @@ import { Adapter, Bot, Context, Filter } from 'koishi';
import _ from 'lodash';
import { RegisterSchema, SchemaProperty } from 'schemastery-gen';
export interface ChannelTargetLike {
channelId: string;
guildId?: string;
}
@RegisterSchema()
export class ChannelTarget {
export class ChannelTarget implements ChannelTargetLike {
@SchemaProperty({ description: 'Channel ID. ', required: true })
channelId: string;
@SchemaProperty({ description: 'Guild ID. ' })
guildId: string;
guildId?: string;
toDesc(): string | [string, string] {
return this.guildId ? [this.channelId, this.guildId] : this.channelId;
......@@ -18,7 +23,7 @@ export class ChannelTarget {
if (this.channelId !== s.channelId) {
return false;
}
if (this.guildId && this.guildId !== s.guildId) {
if (this.guildId && this.guildId !== s.guildId) {
return false;
}
return true;
......@@ -26,27 +31,35 @@ export class ChannelTarget {
}
}
export interface SendTargetLike {
bot?: string;
users?: string[];
channels?: ChannelTargetLike[];
}
@RegisterSchema()
export class SendTarget {
constructor(_: SendTargetLike) {}
@SchemaProperty({
description:
'Bot identifier. eg. onebot:123456789. Will use first bot if not specified. s',
})
bot: string;
bot?: string;
@SchemaProperty({
type: String,
description: 'Private chat send targets. ',
default: [],
})
users: string[];
users?: string[];
@SchemaProperty({
type: ChannelTarget,
description: 'Channel send targets. ',
default: [],
})
channels: ChannelTarget[];
channels?: ChannelTarget[];
getBot(bots: Adapter.BotList) {
if (this.bot) {
......@@ -63,23 +76,25 @@ export class SendTarget {
);
}
getContext(ctx: Context) {
if (this.bot) {
const [platform, id] = this.bot.split(':');
ctx = ctx.platform(platform).self(id);
}
getFilter(): Filter {
let filters: Filter[] = [];
if (this.users?.length) {
const userSet = new Set(this.users);
filters.push((s) => s.subtype === 'private' && userSet.has(s.userId));
filters.push((s) => !s.guildId && userSet.has(s.userId));
}
if (this.channels?.length) {
filters = filters.concat(this.channels.map((c) => c.toFilter()));
}
if (filters.length) {
ctx = ctx.intersect((s) => filters.some((f) => f(s)));
}
return ctx;
return (s) => {
if (this.bot && `${s.platform}:${s.selfId}` !== this.bot) {
return false;
}
return filters.some((f) => f(s));
};
}
getContext(ctx: Context) {
return ctx.intersect(this.getFilter());
}
async send(bots: Adapter.BotList, content: string) {
......
import { Session } from 'koishi';
import { SendTarget } from '..';
describe('Target def.', () => {
it('register correct context', () => {
expect(true).toBe(true);
const def = new SendTarget({
bot: 'onebot:123456789',
users: ['1111111111'],
channels: [{ channelId: '1111111111' }],
});
const filter = def.getFilter();
it('should detect platform', () => {
expect(
filter({
platform: 'onebot',
selfId: '123456789',
userId: '1111111111',
} as Session),
).toBe(true);
expect(
filter({
platform: 'onebot1',
selfId: '123456789',
userId: '1111111111',
} as Session),
).toBe(false);
expect(
filter({
platform: 'onebot',
selfId: '123456782',
userId: '1111111111',
} as Session),
).toBe(false);
});
it('should detect private', () => {
expect(
filter({
platform: 'onebot',
selfId: '123456789',
userId: '1111111111',
} as Session),
).toBe(true);
expect(
filter({
platform: 'onebot',
selfId: '123456789',
userId: '1111111112',
} as Session),
).toBe(false);
expect(
filter({
platform: 'onebot',
selfId: '123456789',
userId: '1111111111',
guildId: '111',
} as Session),
).toBe(false);
});
it('should detect channel', () => {
expect(
filter({
platform: 'onebot',
selfId: '123456789',
channelId: '1111111111',
} as Session),
).toBe(true);
expect(
filter({
platform: 'onebot',
selfId: '123456789',
channelId: '1111111112',
} as Session),
).toBe(false);
expect(
filter({
platform: 'onebot',
selfId: '123456789',
channelId: '1111111111',
guildId: '1111111112',
} as Session),
).toBe(true);
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment