mirror of
https://github.com/SableClient/Sable.git
synced 2026-12-03 23:32:22 -08:00
Remove dead code
This commit is contained in:
1 parent
8153b01652
commit
189ee33105
3 files changed
-220
No files matched your search
@@ -1,7 +1,6 @@
|
||||
import type {
|
||||
PerMessageProfileMsc4461,
|
||||
PerMessageProfileProxyAssociationV2,
|
||||
ProfileTrigger,
|
||||
} from '$hooks/usePerMessageProfile';
|
||||
import {
|
||||
addOrUpdatePerMessageProfile,
|
||||
@@ -50,65 +49,6 @@ export function buildProxyRegex({ prefix, suffix }: PerMessageProfileProxyAssoci
|
||||
return new RegExp(`^${pattern}$`);
|
||||
}
|
||||
|
||||
export function testTriggers(triggers: ProfileTrigger, input: string): boolean {
|
||||
const matchesPrefix = triggers.prefix.some((prefix) => input.startsWith(prefix));
|
||||
if (matchesPrefix) return true;
|
||||
|
||||
const matchesSuffix = triggers[MATRIX_SABLE_UNSTABLE_MSC4461_TRIGGER_SUFFIX_PROPERTY_NAME]?.some(
|
||||
(suffix) => input.startsWith(suffix)
|
||||
);
|
||||
if (matchesSuffix) return true;
|
||||
|
||||
const matchesCircumfix = triggers[
|
||||
MATRIX_SABLE_UNSTABLE_MSC4461_TRIGGER_CIRCUMFIX_PROPERTY_NAME
|
||||
]?.some(({ prefix, suffix }) => {
|
||||
const casePrefix = prefix ? input.startsWith(prefix) : true;
|
||||
const caseSuffix = suffix ? input.endsWith(suffix) : true;
|
||||
|
||||
return casePrefix && caseSuffix;
|
||||
});
|
||||
|
||||
return !!matchesCircumfix;
|
||||
}
|
||||
|
||||
export function testProxy(
|
||||
{ prefix, suffix }: PerMessageProfileProxyAssociationV2,
|
||||
input: string
|
||||
): boolean {
|
||||
const matchesPrefix = prefix ? input.startsWith(prefix) : true;
|
||||
const matchesSuffix = suffix ? input.endsWith(suffix) : true;
|
||||
|
||||
return matchesPrefix && matchesSuffix;
|
||||
}
|
||||
|
||||
export function stripTrigger(triggers: ProfileTrigger, input: string): string {
|
||||
const prefix = triggers.prefix.find((value) => input.startsWith(value));
|
||||
if (prefix !== undefined) return stripProxy({ prefix }, input);
|
||||
|
||||
const suffix = triggers[MATRIX_SABLE_UNSTABLE_MSC4461_TRIGGER_SUFFIX_PROPERTY_NAME]?.find(
|
||||
(value) => input.endsWith(value)
|
||||
);
|
||||
if (suffix !== undefined) return stripProxy({ suffix }, input);
|
||||
|
||||
const circumfix = triggers[MATRIX_SABLE_UNSTABLE_MSC4461_TRIGGER_CIRCUMFIX_PROPERTY_NAME]?.find(
|
||||
(trigger) => input.startsWith(trigger.prefix) && input.endsWith(trigger.suffix)
|
||||
);
|
||||
if (circumfix) return stripProxy(circumfix, input);
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
export function stripProxy(
|
||||
{ prefix, suffix }: { prefix?: string; suffix?: string },
|
||||
input: string
|
||||
): string {
|
||||
let message = input;
|
||||
if (prefix) message = message.slice(prefix.length);
|
||||
if (suffix) message = message.slice(0, message.length - suffix.length);
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* a class to use as PluralKit command message handler
|
||||
*
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
import type { Mock } from 'vitest';
|
||||
import { describe, expect, it, vi, beforeEach } from 'vitest';
|
||||
|
||||
import { PKitProxyMessageHandler } from './PKitProxyMessageHandler';
|
||||
import type { MatrixClient } from '$types/matrix-sdk';
|
||||
|
||||
// Mock the hook module that provides proxy associations + profile lookup
|
||||
vi.mock('$hooks/usePerMessageProfile', () => ({
|
||||
getAllPerMessageProfiles: vi.fn<() => Promise<unknown[]>>(),
|
||||
getPerMessageProfileById: vi.fn<() => Promise<unknown>>(),
|
||||
parsePerMessageProfileProxyAssociation: vi.fn<() => unknown>(),
|
||||
}));
|
||||
|
||||
const mocked = await import('$hooks/usePerMessageProfile');
|
||||
|
||||
describe('PKitProxyMessageHandler', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
});
|
||||
|
||||
it('returns false for isAProxiedMessage before init', () => {
|
||||
const handler = new PKitProxyMessageHandler({} as unknown as MatrixClient);
|
||||
expect(handler.isAProxiedMessage('[test] hi')).toBe(false);
|
||||
});
|
||||
|
||||
it('matches a proxied message, returns pmp, and strips content', async () => {
|
||||
(mocked.getAllPerMessageProfiles as unknown as Mock).mockResolvedValueOnce([
|
||||
{
|
||||
id: 'p1',
|
||||
name: 'Test',
|
||||
trigger: {
|
||||
prefix: [],
|
||||
'net.f0rest.circumfix': [{ prefix: '[', suffix: ']' }],
|
||||
},
|
||||
},
|
||||
]);
|
||||
(mocked.getPerMessageProfileById as unknown as Mock).mockResolvedValueOnce({
|
||||
id: 'p1',
|
||||
name: 'Test',
|
||||
trigger: {
|
||||
prefix: [],
|
||||
'net.f0rest.circumfix': [{ prefix: '[', suffix: ']' }],
|
||||
},
|
||||
});
|
||||
|
||||
const handler = new PKitProxyMessageHandler({} as unknown as MatrixClient);
|
||||
|
||||
const pmp = await handler.getPmpBasedOnMessage('[hello]');
|
||||
expect(pmp).toEqual({
|
||||
id: 'p1',
|
||||
name: 'Test',
|
||||
trigger: {
|
||||
prefix: [],
|
||||
'net.f0rest.circumfix': [{ prefix: '[', suffix: ']' }],
|
||||
},
|
||||
});
|
||||
|
||||
// getPmpBasedOnMessage refreshes/init() so we should be inited now
|
||||
expect(handler.isAProxiedMessage('[hello]')).toBe(true);
|
||||
expect(handler.stripProxyFromMessage('[hello]')).toBe('hello');
|
||||
|
||||
expect(handler.isAProxiedMessage('[hello\nworld]')).toBe(true);
|
||||
expect(handler.stripProxyFromMessage('[hello\nworld]')).toBe('hello\nworld');
|
||||
});
|
||||
});
|
||||
@@ -1,95 +0,0 @@
|
||||
import type { PerMessageProfileMsc4461 } from '$hooks/usePerMessageProfile';
|
||||
import { getAllPerMessageProfiles, getPerMessageProfileById } from '$hooks/usePerMessageProfile';
|
||||
import type { MatrixClient } from '$types/matrix-sdk';
|
||||
import { stripTrigger, testTriggers } from './PKitCommandMessageHandler';
|
||||
|
||||
/**
|
||||
* proxy message handler
|
||||
* @author Rye
|
||||
*/
|
||||
export class PKitProxyMessageHandler {
|
||||
/**
|
||||
* the matrix client we use, we init that in the constructor
|
||||
*
|
||||
* @private
|
||||
* @type {MatrixClient}
|
||||
* @memberof PKitProxyMessageHandler
|
||||
*/
|
||||
private readonly mx: MatrixClient;
|
||||
|
||||
/**
|
||||
* a list of profiles; is not initialized in the constructor
|
||||
* @private
|
||||
* @type {PerMessageProfileMsc4461[]}
|
||||
* @memberof PKitProxyMessageHandler
|
||||
*/
|
||||
private profiles: PerMessageProfileMsc4461[];
|
||||
|
||||
private succInit: boolean;
|
||||
|
||||
/**
|
||||
* a pk proxy message handler
|
||||
* @param mx the matrix client
|
||||
*/
|
||||
public constructor(mx: MatrixClient) {
|
||||
this.mx = mx;
|
||||
this.profiles = [];
|
||||
this.succInit = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* initialize the handler, as this is not necessarily fast, it shouldn't happen in the constructor
|
||||
*/
|
||||
public async init(): Promise<void> {
|
||||
try {
|
||||
this.profiles = await getAllPerMessageProfiles(this.mx);
|
||||
this.succInit = true;
|
||||
} catch (err) {
|
||||
this.succInit = false;
|
||||
throw new Error(`failed to init pmp proxy handler: ${String(err)}`, {
|
||||
cause: err,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* you should probably check this before running `getPmpBasedOnMessage`, as this is faster
|
||||
* @param message the message to check
|
||||
*/
|
||||
public isAProxiedMessage(message: string): boolean {
|
||||
if (!this.succInit) return false;
|
||||
return this.profiles.some((profile) => testTriggers(profile.trigger, message));
|
||||
}
|
||||
|
||||
/**
|
||||
* get PmP based on message
|
||||
* @param message the message to look at
|
||||
* @returns the matching Per-Message-Profile, if any
|
||||
*/
|
||||
public async getPmpBasedOnMessage(
|
||||
message: string
|
||||
): Promise<PerMessageProfileMsc4461 | undefined> {
|
||||
// Always refresh so newly-added proxies apply immediately.
|
||||
await this.init();
|
||||
// check if the message matches our formats
|
||||
const profileId = this.profiles.find((profile) => testTriggers(profile.trigger, message))?.id;
|
||||
if (!profileId) return undefined;
|
||||
return getPerMessageProfileById(this.mx, profileId);
|
||||
}
|
||||
|
||||
/**
|
||||
* this runs synchronously, so it needs to be inited beforehand
|
||||
*
|
||||
* @param {string} message the message you want to extract from
|
||||
* @return {*} {(string | undefined)} the message without the proxy
|
||||
* @memberof PKitProxyMessageHandler
|
||||
*/
|
||||
public stripProxyFromMessage(message: string): string | undefined {
|
||||
if (!this.succInit) return undefined;
|
||||
let m;
|
||||
this.profiles.forEach((profile) => {
|
||||
if (testTriggers(profile.trigger, message)) m = stripTrigger(profile.trigger, message);
|
||||
});
|
||||
return m;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user