Change events to use schema.org/Event for metadata
This commit is contained in:
parent
a12152cd4f
commit
47f43e897b
|
@ -35,10 +35,13 @@ export class Event {
|
||||||
public title: string;
|
public title: string;
|
||||||
|
|
||||||
@Column('jsonb', {
|
@Column('jsonb', {
|
||||||
default: {},
|
default: {
|
||||||
comment: 'metadata mapping for event with more user configurable optional information',
|
'@context': 'https://schema.org/',
|
||||||
|
'@type': 'Event',
|
||||||
|
},
|
||||||
|
comment: 'metadata object describing the event. Follows https://schema.org/Event',
|
||||||
})
|
})
|
||||||
public metadata: Record<string, string>;
|
public metadata: unknown;
|
||||||
|
|
||||||
//#region Denormalized fields
|
//#region Denormalized fields
|
||||||
@Column('enum', {
|
@Column('enum', {
|
||||||
|
|
|
@ -8,8 +8,8 @@ import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
||||||
import type { Config } from '@/config.js';
|
import type { Config } from '@/config.js';
|
||||||
import { DI } from '@/di-symbols.js';
|
import { DI } from '@/di-symbols.js';
|
||||||
import { RoleService } from '@/core/RoleService.js';
|
import { RoleService } from '@/core/RoleService.js';
|
||||||
import { ApiError } from '../../../error.js';
|
|
||||||
import { sqlLikeEscape } from '@/misc/sql-like-escape.js';
|
import { sqlLikeEscape } from '@/misc/sql-like-escape.js';
|
||||||
|
import { ApiError } from '../../../error.js';
|
||||||
|
|
||||||
export const meta = {
|
export const meta = {
|
||||||
tags: ['notes'],
|
tags: ['notes'],
|
||||||
|
@ -58,7 +58,7 @@ export const paramDef = {
|
||||||
items: {
|
items: {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
properties: {
|
properties: {
|
||||||
key: { type: 'string', description: 'the metadata property to filter on.' },
|
key: { type: 'array', items: { type: 'string', nullable: false }, description: 'the metadata string property to filter on. Can filter on nested properties using an array. such as `["location", "postalCode"]`.' },
|
||||||
values: { type: 'array', items: { type: 'string', nullable: true }, description: 'The values to match the metadata against (case insensitive regex). Each item in this array is applied as an OR. Include null to indicate match on missing metadata' },
|
values: { type: 'array', items: { type: 'string', nullable: true }, description: 'The values to match the metadata against (case insensitive regex). Each item in this array is applied as an OR. Include null to indicate match on missing metadata' },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -67,6 +67,10 @@ export const paramDef = {
|
||||||
},
|
},
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
|
function notAlphaNumeric(s: string) {
|
||||||
|
return null !== s.match(/[^\w]/);
|
||||||
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line import/no-default-export
|
// eslint-disable-next-line import/no-default-export
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
||||||
|
@ -112,13 +116,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
|
||||||
filters.forEach(f => {
|
filters.forEach(f => {
|
||||||
if (!f.key || !f.values) throw new ApiError(meta.errors.invalidParam);
|
if (!f.key || !f.values) throw new ApiError(meta.errors.invalidParam);
|
||||||
const filterKey = f.key;
|
const filterKey = f.key;
|
||||||
|
if (filterKey.some(notAlphaNumeric)) throw new ApiError(meta.errors.invalidParam); // schema properties don't have special characters
|
||||||
const filterValues = f.values as (string | null)[];
|
const filterValues = f.values as (string | null)[];
|
||||||
const matches = filterValues.filter(x => x !== null) as string[];
|
const matches = filterValues.filter(x => x !== null) as string[];
|
||||||
const hasNull = filterValues.length !== matches.length;
|
const hasNull = filterValues.length !== matches.length;
|
||||||
if (matches.length < 1) throw new ApiError(meta.errors.invalidParam);
|
if (matches.length < 1) throw new ApiError(meta.errors.invalidParam);
|
||||||
query.andWhere(new Brackets((qb) => {
|
query.andWhere(new Brackets((qb) => {
|
||||||
// regex match metadata values case insensitive
|
// regex match metadata values case insensitive
|
||||||
qb.where('event.metadata ->> :key ~* :values', { key: filterKey, values: `(${ matches.map(m => m.trim()).filter(m => m.length).join('|') })` });
|
qb.where('event.metadata #>> :key ~* :values', { key: `{${filterKey.join(',')}}`, values: `(${ matches.map(m => m.trim()).filter(m => m.length).join('|') })` });
|
||||||
if (hasNull) {
|
if (hasNull) {
|
||||||
qb.orWhere('NOT (event.metadata ? :key)', { key: filterKey });
|
qb.orWhere('NOT (event.metadata ? :key)', { key: filterKey });
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue