philomena/assets/js/query/user.ts
liamwhite 3590be1429
match_query: unit test and rewrite for TypeScript (#208)
* match_query: unit test and rewrite for TypeScript

* match_query: use new type for parse errors

* match_query: avoid exceptional control flow in date parsing
2024-03-18 08:20:47 -04:00

25 lines
1.1 KiB
TypeScript

import { Interaction, InteractionType, InteractionValue } from '../../types/booru-object';
import { FieldMatcher } from './types';
function interactionMatch(imageId: number, type: InteractionType, value: InteractionValue, interactions: Interaction[]): boolean {
return interactions.some(v => v.image_id === imageId && v.interaction_type === type && (value === null || v.value === value));
}
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;
}
};
}