Commit 7fafdfc1 authored by nanahira's avatar nanahira

fix private detect

parent 764b8fff
...@@ -2,12 +2,17 @@ import { Adapter, Bot, Context, Filter } from 'koishi'; ...@@ -2,12 +2,17 @@ import { Adapter, Bot, Context, Filter } from 'koishi';
import _ from 'lodash'; import _ from 'lodash';
import { RegisterSchema, SchemaProperty } from 'schemastery-gen'; import { RegisterSchema, SchemaProperty } from 'schemastery-gen';
export interface ChannelTargetLike {
channelId: string;
guildId?: string;
}
@RegisterSchema() @RegisterSchema()
export class ChannelTarget { export class ChannelTarget implements ChannelTargetLike {
@SchemaProperty({ description: 'Channel ID. ', required: true }) @SchemaProperty({ description: 'Channel ID. ', required: true })
channelId: string; channelId: string;
@SchemaProperty({ description: 'Guild ID. ' }) @SchemaProperty({ description: 'Guild ID. ' })
guildId: string; guildId?: string;
toDesc(): string | [string, string] { toDesc(): string | [string, string] {
return this.guildId ? [this.channelId, this.guildId] : this.channelId; return this.guildId ? [this.channelId, this.guildId] : this.channelId;
...@@ -18,7 +23,7 @@ export class ChannelTarget { ...@@ -18,7 +23,7 @@ export class ChannelTarget {
if (this.channelId !== s.channelId) { if (this.channelId !== s.channelId) {
return false; return false;
} }
if (this.guildId && this.guildId !== s.guildId) { if (this.guildId && this.guildId !== s.guildId) {
return false; return false;
} }
return true; return true;
...@@ -26,27 +31,35 @@ export class ChannelTarget { ...@@ -26,27 +31,35 @@ export class ChannelTarget {
} }
} }
export interface SendTargetLike {
bot?: string;
users?: string[];
channels?: ChannelTargetLike[];
}
@RegisterSchema() @RegisterSchema()
export class SendTarget { export class SendTarget {
constructor(_: SendTargetLike) {}
@SchemaProperty({ @SchemaProperty({
description: description:
'Bot identifier. eg. onebot:123456789. Will use first bot if not specified. s', 'Bot identifier. eg. onebot:123456789. Will use first bot if not specified. s',
}) })
bot: string; bot?: string;
@SchemaProperty({ @SchemaProperty({
type: String, type: String,
description: 'Private chat send targets. ', description: 'Private chat send targets. ',
default: [], default: [],
}) })
users: string[]; users?: string[];
@SchemaProperty({ @SchemaProperty({
type: ChannelTarget, type: ChannelTarget,
description: 'Channel send targets. ', description: 'Channel send targets. ',
default: [], default: [],
}) })
channels: ChannelTarget[]; channels?: ChannelTarget[];
getBot(bots: Adapter.BotList) { getBot(bots: Adapter.BotList) {
if (this.bot) { if (this.bot) {
...@@ -63,23 +76,25 @@ export class SendTarget { ...@@ -63,23 +76,25 @@ export class SendTarget {
); );
} }
getContext(ctx: Context) { getFilter(): Filter {
if (this.bot) {
const [platform, id] = this.bot.split(':');
ctx = ctx.platform(platform).self(id);
}
let filters: Filter[] = []; let filters: Filter[] = [];
if (this.users?.length) { if (this.users?.length) {
const userSet = new Set(this.users); 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) { if (this.channels?.length) {
filters = filters.concat(this.channels.map((c) => c.toFilter())); filters = filters.concat(this.channels.map((c) => c.toFilter()));
} }
if (filters.length) { return (s) => {
ctx = ctx.intersect((s) => filters.some((f) => f(s))); if (this.bot && `${s.platform}:${s.selfId}` !== this.bot) {
} return false;
return ctx; }
return filters.some((f) => f(s));
};
}
getContext(ctx: Context) {
return ctx.intersect(this.getFilter());
} }
async send(bots: Adapter.BotList, content: string) { async send(bots: Adapter.BotList, content: string) {
......
import { Session } from 'koishi';
import { SendTarget } from '..';
describe('Target def.', () => { describe('Target def.', () => {
it('register correct context', () => { const def = new SendTarget({
expect(true).toBe(true); 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