Added notes/events/search endpoint logic

This commit is contained in:
ssmucny 2023-04-16 21:59:18 -04:00
parent 44d05d58c4
commit 1a797eee35
5 changed files with 1074 additions and 913 deletions

View File

@ -254,6 +254,7 @@ import * as ep___notes_localTimeline from './endpoints/notes/local-timeline.js';
import * as ep___notes_mentions from './endpoints/notes/mentions.js'; import * as ep___notes_mentions from './endpoints/notes/mentions.js';
import * as ep___notes_polls_recommendation from './endpoints/notes/polls/recommendation.js'; import * as ep___notes_polls_recommendation from './endpoints/notes/polls/recommendation.js';
import * as ep___notes_polls_vote from './endpoints/notes/polls/vote.js'; import * as ep___notes_polls_vote from './endpoints/notes/polls/vote.js';
import * as ep___notes_events_search from './endpoints/notes/events/search.js';
import * as ep___notes_reactions from './endpoints/notes/reactions.js'; import * as ep___notes_reactions from './endpoints/notes/reactions.js';
import * as ep___notes_reactions_create from './endpoints/notes/reactions/create.js'; import * as ep___notes_reactions_create from './endpoints/notes/reactions/create.js';
import * as ep___notes_reactions_delete from './endpoints/notes/reactions/delete.js'; import * as ep___notes_reactions_delete from './endpoints/notes/reactions/delete.js';
@ -588,6 +589,7 @@ const $notes_localTimeline: Provider = { provide: 'ep:notes/local-timeline', use
const $notes_mentions: Provider = { provide: 'ep:notes/mentions', useClass: ep___notes_mentions.default }; const $notes_mentions: Provider = { provide: 'ep:notes/mentions', useClass: ep___notes_mentions.default };
const $notes_polls_recommendation: Provider = { provide: 'ep:notes/polls/recommendation', useClass: ep___notes_polls_recommendation.default }; const $notes_polls_recommendation: Provider = { provide: 'ep:notes/polls/recommendation', useClass: ep___notes_polls_recommendation.default };
const $notes_polls_vote: Provider = { provide: 'ep:notes/polls/vote', useClass: ep___notes_polls_vote.default }; const $notes_polls_vote: Provider = { provide: 'ep:notes/polls/vote', useClass: ep___notes_polls_vote.default };
const $notes_events_search: Provider = { provide: 'ep:notes/events/search', useClass: ep___notes_events_search.default };
const $notes_reactions: Provider = { provide: 'ep:notes/reactions', useClass: ep___notes_reactions.default }; const $notes_reactions: Provider = { provide: 'ep:notes/reactions', useClass: ep___notes_reactions.default };
const $notes_reactions_create: Provider = { provide: 'ep:notes/reactions/create', useClass: ep___notes_reactions_create.default }; const $notes_reactions_create: Provider = { provide: 'ep:notes/reactions/create', useClass: ep___notes_reactions_create.default };
const $notes_reactions_delete: Provider = { provide: 'ep:notes/reactions/delete', useClass: ep___notes_reactions_delete.default }; const $notes_reactions_delete: Provider = { provide: 'ep:notes/reactions/delete', useClass: ep___notes_reactions_delete.default };
@ -926,6 +928,7 @@ const $retention: Provider = { provide: 'ep:retention', useClass: ep___retention
$notes_mentions, $notes_mentions,
$notes_polls_recommendation, $notes_polls_recommendation,
$notes_polls_vote, $notes_polls_vote,
$notes_events_search,
$notes_reactions, $notes_reactions,
$notes_reactions_create, $notes_reactions_create,
$notes_reactions_delete, $notes_reactions_delete,
@ -1258,6 +1261,7 @@ const $retention: Provider = { provide: 'ep:retention', useClass: ep___retention
$notes_mentions, $notes_mentions,
$notes_polls_recommendation, $notes_polls_recommendation,
$notes_polls_vote, $notes_polls_vote,
$notes_events_search,
$notes_reactions, $notes_reactions,
$notes_reactions_create, $notes_reactions_create,
$notes_reactions_delete, $notes_reactions_delete,

View File

@ -254,6 +254,7 @@ import * as ep___notes_localTimeline from './endpoints/notes/local-timeline.js';
import * as ep___notes_mentions from './endpoints/notes/mentions.js'; import * as ep___notes_mentions from './endpoints/notes/mentions.js';
import * as ep___notes_polls_recommendation from './endpoints/notes/polls/recommendation.js'; import * as ep___notes_polls_recommendation from './endpoints/notes/polls/recommendation.js';
import * as ep___notes_polls_vote from './endpoints/notes/polls/vote.js'; import * as ep___notes_polls_vote from './endpoints/notes/polls/vote.js';
import * as ep___notes_events_search from './endpoints/notes/events/search.js';
import * as ep___notes_reactions from './endpoints/notes/reactions.js'; import * as ep___notes_reactions from './endpoints/notes/reactions.js';
import * as ep___notes_reactions_create from './endpoints/notes/reactions/create.js'; import * as ep___notes_reactions_create from './endpoints/notes/reactions/create.js';
import * as ep___notes_reactions_delete from './endpoints/notes/reactions/delete.js'; import * as ep___notes_reactions_delete from './endpoints/notes/reactions/delete.js';
@ -586,6 +587,7 @@ const eps = [
['notes/mentions', ep___notes_mentions], ['notes/mentions', ep___notes_mentions],
['notes/polls/recommendation', ep___notes_polls_recommendation], ['notes/polls/recommendation', ep___notes_polls_recommendation],
['notes/polls/vote', ep___notes_polls_vote], ['notes/polls/vote', ep___notes_polls_vote],
['notes/events/search', ep___notes_events_search],
['notes/reactions', ep___notes_reactions], ['notes/reactions', ep___notes_reactions],
['notes/reactions/create', ep___notes_reactions_create], ['notes/reactions/create', ep___notes_reactions_create],
['notes/reactions/delete', ep___notes_reactions_delete], ['notes/reactions/delete', ep___notes_reactions_delete],

View File

@ -0,0 +1,134 @@
import { Inject, Injectable } from '@nestjs/common';
import { Brackets } from 'typeorm';
import { Event } from '@/models/entities/Event.js';
import type { NotesRepository } from '@/models/index.js';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { QueryService } from '@/core/QueryService.js';
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
import type { Config } from '@/config.js';
import { DI } from '@/di-symbols.js';
import { RoleService } from '@/core/RoleService.js';
import { ApiError } from '../../../error.js';
export const meta = {
tags: ['notes'],
requireCredential: false,
res: {
type: 'array',
optional: false, nullable: false,
items: {
type: 'object',
optional: false, nullable: false,
ref: 'Note',
},
},
errors: {
unavailable: {
message: 'Search of notes unavailable.',
code: 'UNAVAILABLE',
id: '0b44998d-77aa-4427-80d0-d2c9b8523011',
},
invalidParam: {
message: 'Invalid Parameter',
code: 'INVALID_PARAM',
id: 'e70903d3-0aa2-44d5-a955-4de5723c603d',
}
},
} as const;
export const paramDef = {
type: 'object',
properties: {
sinceId: { type: 'string', format: 'misskey:id' },
untilId: { type: 'string', format: 'misskey:id' },
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
host: {
type: 'string',
nullable: true,
description: 'The local host is represented with `null`.',
},
users: { type: 'array', nullable: true, items: { type: 'object', format: 'misskey:id' } },
sinceDate: { type: 'integer', nullable: true },
untilDate: { type: 'integer', nullable: true },
filters: {
type: 'object',
nullable: true,
description: 'mapping of string -> [string] that filters events based on metadata',
},
sortBy: { type: 'string', nullable: true, default: 'startDate', enum: ['startDate', 'createdAt'] },
},
} as const;
// eslint-disable-next-line import/no-default-export
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> {
constructor(
@Inject(DI.config)
private config: Config,
@Inject(DI.notesRepository)
private notesRepository: NotesRepository,
private noteEntityService: NoteEntityService,
private queryService: QueryService,
private roleService: RoleService,
) {
super(meta, paramDef, async (ps, me) => {
const policies = await this.roleService.getUserPolicies(me ? me.id : null);
if (!policies.canSearchNotes) {
throw new ApiError(meta.errors.unavailable);
}
const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'), ps.sinceId, ps.untilId);
if (ps.users) {
if (ps.users.length < 1) throw new ApiError(meta.errors.invalidParam);
query.andWhere('note.userId IN (:...users)', { users: ps.users });
}
query
.innerJoinAndSelect(Event, 'event', 'event.noteId = note.id')
.innerJoinAndSelect('note.user', 'user');
if (ps.filters) {
const filters: Record<string, (string | null)[]> = ps.filters;
Object.keys(filters).forEach(f => {
const matches = filters[f].filter(x => x !== null);
if (matches.length < 1) throw new ApiError(meta.errors.invalidParam);
query.andWhere(new Brackets((qb) => {
qb.where('event.metadata ->> :key IN (:...values)', { key: f, values: filters[f].filter(x => x !== null) });
if (filters[f].filter(x => x === null).length > 0) {
qb.orWhere('event.metadata ->> :key IS NULL', { key: f });
}
}));
});
}
if (ps.sinceDate && ps.untilDate && ps.sinceDate > ps.untilDate) throw new ApiError(meta.errors.invalidParam);
const sinceDate = ps.sinceDate ? new Date(ps.sinceDate) : new Date();
query.andWhere('event.start > :sinceDate', { sinceDate: sinceDate })
.andWhere('(event.end IS NULL OR event.end > :sinceDate)', { sinceDate: sinceDate });
if (ps.untilDate) {
query.andWhere('event.start < :untilDate', { untilDate: new Date(ps.untilDate) });
}
if (ps.sortBy === 'createdAt') {
query.orderBy('note.createdAt', 'ASC');
} else {
query.orderBy('event.start', 'ASC');
}
this.queryService.generateVisibilityQuery(query, me);
if (me) this.queryService.generateMutedUserQuery(query, me);
if (me) this.queryService.generateBlockedUserQuery(query, me);
const notes = await query.take(ps.limit).getMany();
return await this.noteEntityService.packMany(notes, me);
});
}
}

File diff suppressed because it is too large Load Diff

View File

@ -499,6 +499,7 @@ export type Endpoints = {
'notes/mentions': { req: { following?: boolean; limit?: number; sinceId?: Note['id']; untilId?: Note['id']; }; res: Note[]; }; 'notes/mentions': { req: { following?: boolean; limit?: number; sinceId?: Note['id']; untilId?: Note['id']; }; res: Note[]; };
'notes/polls/recommendation': { req: TODO; res: TODO; }; 'notes/polls/recommendation': { req: TODO; res: TODO; };
'notes/polls/vote': { req: { noteId: Note['id']; choice: number; }; res: null; }; 'notes/polls/vote': { req: { noteId: Note['id']; choice: number; }; res: null; };
'notes/events/search': { req: TODO; res: Note[]; };
'notes/reactions': { req: { noteId: Note['id']; type?: string | null; limit?: number; }; res: NoteReaction[]; }; 'notes/reactions': { req: { noteId: Note['id']; type?: string | null; limit?: number; }; res: NoteReaction[]; };
'notes/reactions/create': { req: { noteId: Note['id']; reaction: string; }; res: null; }; 'notes/reactions/create': { req: { noteId: Note['id']; reaction: string; }; res: null; };
'notes/reactions/delete': { req: { noteId: Note['id']; }; res: null; }; 'notes/reactions/delete': { req: { noteId: Note['id']; }; res: null; };