2022-06-14 09:01:23 +00:00
|
|
|
import { Brackets } from 'typeorm';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { NotesRepository } from '@/models/index.js';
|
2022-06-14 09:01:23 +00:00
|
|
|
import { safeForSql } from '@/misc/safe-for-sql.js';
|
|
|
|
import { normalizeForSearch } from '@/misc/normalize-for-search.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { QueryService } from '@/core/QueryService.js';
|
|
|
|
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2018-06-10 03:19:19 +00:00
|
|
|
|
2018-09-05 14:55:51 +00:00
|
|
|
export const meta = {
|
2019-02-23 02:20:58 +00:00
|
|
|
tags: ['notes', 'hashtags'],
|
|
|
|
|
|
|
|
res: {
|
2022-01-18 13:27:10 +00:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2019-02-23 02:20:58 +00:00
|
|
|
items: {
|
2022-01-18 13:27:10 +00:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 13:35:26 +00:00
|
|
|
ref: 'Note',
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
2019-02-23 02:20:58 +00:00
|
|
|
},
|
2022-01-18 13:27:10 +00:00
|
|
|
} as const;
|
2018-09-05 14:55:51 +00:00
|
|
|
|
2022-02-20 04:15:40 +00:00
|
|
|
export const paramDef = {
|
2022-02-19 05:05:32 +00:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
reply: { type: 'boolean', nullable: true, default: null },
|
|
|
|
renote: { type: 'boolean', nullable: true, default: null },
|
2022-04-03 04:57:26 +00:00
|
|
|
withFiles: {
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
|
|
|
description: 'Only show notes that have attached files.',
|
|
|
|
},
|
2022-02-19 05:05:32 +00:00
|
|
|
poll: { type: 'boolean', nullable: true, default: null },
|
|
|
|
sinceId: { type: 'string', format: 'misskey:id' },
|
|
|
|
untilId: { type: 'string', format: 'misskey:id' },
|
|
|
|
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
|
|
|
},
|
2022-04-03 04:57:26 +00:00
|
|
|
anyOf: [
|
|
|
|
{
|
|
|
|
properties: {
|
|
|
|
tag: { type: 'string', minLength: 1 },
|
|
|
|
},
|
|
|
|
required: ['tag'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
properties: {
|
|
|
|
query: {
|
|
|
|
type: 'array',
|
|
|
|
description: 'The outer arrays are chained with OR, the inner arrays are chained with AND.',
|
|
|
|
items: {
|
|
|
|
type: 'array',
|
|
|
|
items: {
|
|
|
|
type: 'string',
|
|
|
|
minLength: 1,
|
|
|
|
},
|
|
|
|
minItems: 1,
|
|
|
|
},
|
|
|
|
minItems: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
required: ['query'],
|
|
|
|
},
|
|
|
|
],
|
2022-02-19 05:05:32 +00:00
|
|
|
} as const;
|
|
|
|
|
2022-01-02 17:12:50 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 18:27:08 +00:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
private noteEntityService: NoteEntityService,
|
|
|
|
private queryService: QueryService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'), ps.sinceId, ps.untilId)
|
|
|
|
.innerJoinAndSelect('note.user', 'user')
|
|
|
|
.leftJoinAndSelect('user.avatar', 'avatar')
|
|
|
|
.leftJoinAndSelect('user.banner', 'banner')
|
|
|
|
.leftJoinAndSelect('note.reply', 'reply')
|
|
|
|
.leftJoinAndSelect('note.renote', 'renote')
|
|
|
|
.leftJoinAndSelect('reply.user', 'replyUser')
|
|
|
|
.leftJoinAndSelect('replyUser.avatar', 'replyUserAvatar')
|
|
|
|
.leftJoinAndSelect('replyUser.banner', 'replyUserBanner')
|
|
|
|
.leftJoinAndSelect('renote.user', 'renoteUser')
|
|
|
|
.leftJoinAndSelect('renoteUser.avatar', 'renoteUserAvatar')
|
|
|
|
.leftJoinAndSelect('renoteUser.banner', 'renoteUserBanner');
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
this.queryService.generateVisibilityQuery(query, me);
|
|
|
|
if (me) this.queryService.generateMutedUserQuery(query, me);
|
|
|
|
if (me) this.queryService.generateBlockedUserQuery(query, me);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (ps.tag) {
|
|
|
|
if (!safeForSql(ps.tag)) throw 'Injection';
|
|
|
|
query.andWhere(`'{"${normalizeForSearch(ps.tag)}"}' <@ note.tags`);
|
|
|
|
} else {
|
|
|
|
query.andWhere(new Brackets(qb => {
|
|
|
|
for (const tags of ps.query!) {
|
|
|
|
qb.orWhere(new Brackets(qb => {
|
|
|
|
for (const tag of tags) {
|
|
|
|
if (!safeForSql(tag)) throw 'Injection';
|
|
|
|
qb.andWhere(`'{"${normalizeForSearch(tag)}"}' <@ note.tags`);
|
|
|
|
}
|
|
|
|
}));
|
2021-05-23 09:57:12 +00:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (e === 'Injection') return [];
|
|
|
|
throw e;
|
|
|
|
}
|
2018-06-10 03:19:19 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (ps.reply != null) {
|
|
|
|
if (ps.reply) {
|
|
|
|
query.andWhere('note.replyId IS NOT NULL');
|
|
|
|
} else {
|
|
|
|
query.andWhere('note.replyId IS NULL');
|
|
|
|
}
|
|
|
|
}
|
2018-06-10 03:19:19 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (ps.renote != null) {
|
|
|
|
if (ps.renote) {
|
|
|
|
query.andWhere('note.renoteId IS NOT NULL');
|
|
|
|
} else {
|
|
|
|
query.andWhere('note.renoteId IS NULL');
|
|
|
|
}
|
|
|
|
}
|
2018-06-10 03:19:19 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (ps.withFiles) {
|
|
|
|
query.andWhere('note.fileIds != \'{}\'');
|
|
|
|
}
|
2018-06-10 03:19:19 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (ps.poll != null) {
|
|
|
|
if (ps.poll) {
|
|
|
|
query.andWhere('note.hasPoll = TRUE');
|
|
|
|
} else {
|
|
|
|
query.andWhere('note.hasPoll = FALSE');
|
|
|
|
}
|
|
|
|
}
|
2018-06-10 03:19:19 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
// Search notes
|
|
|
|
const notes = await query.take(ps.limit).getMany();
|
2018-06-10 03:19:19 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
return await this.noteEntityService.packMany(notes, me);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|