2024-03-18 13:20:47 +01:00
|
|
|
import { Interaction, InteractionType, InteractionValue } from '../../types/booru-object';
|
|
|
|
import { FieldMatcher } from './types';
|
|
|
|
|
2024-07-03 22:54:14 +02:00
|
|
|
function interactionMatch(
|
|
|
|
imageId: number,
|
|
|
|
type: InteractionType,
|
|
|
|
value: InteractionValue,
|
|
|
|
interactions: Interaction[],
|
|
|
|
): boolean {
|
|
|
|
return interactions.some(
|
2024-07-03 23:03:46 +02:00
|
|
|
v => v.image_id === imageId && v.interaction_type === type && (value === null || v.value === value),
|
2024-07-03 22:54:14 +02:00
|
|
|
);
|
2024-03-18 13:20:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function makeUserMatcher(term: string): FieldMatcher {
|
|
|
|
// Should work with most my:conditions except watched.
|
|
|
|
return (value, field, documentId) => {
|
|
|
|
switch (term) {
|
|
|
|
case 'faves':
|
|
|
|
return interactionMatch(documentId, 'faved', null, window.booru.interactions);
|
|
|
|
case 'upvotes':
|
|
|
|
return interactionMatch(documentId, 'voted', 'up', window.booru.interactions);
|
|
|
|
case 'downvotes':
|
|
|
|
return interactionMatch(documentId, 'voted', 'down', window.booru.interactions);
|
|
|
|
case 'watched':
|
|
|
|
case 'hidden':
|
|
|
|
default:
|
|
|
|
// Other my: interactions aren't supported, return false to prevent them from triggering spoiler.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|