@@ -0,0 +1,13 @@
|
||||
export class AnnouncementDisplayOrder1690463372775 {
|
||||
name = 'AnnouncementDisplayOrder1690463372775'
|
||||
|
||||
async up(queryRunner) {
|
||||
await queryRunner.query(`ALTER TABLE "announcement" ADD "displayOrder" integer NOT NULL DEFAULT '0'`);
|
||||
await queryRunner.query(`CREATE INDEX "IDX_b64d293ca4bef21e91963054b0" ON "announcement" ("displayOrder") `);
|
||||
}
|
||||
|
||||
async down(queryRunner) {
|
||||
await queryRunner.query(`DROP INDEX "public"."IDX_b64d293ca4bef21e91963054b0"`);
|
||||
await queryRunner.query(`ALTER TABLE "announcement" DROP COLUMN "displayOrder"`);
|
||||
}
|
||||
}
|
@@ -33,6 +33,13 @@ export class Announcement {
|
||||
})
|
||||
public imageUrl: string | null;
|
||||
|
||||
// UIに表示する際の並び順用(大きいほど先頭)
|
||||
@Index()
|
||||
@Column('integer', {
|
||||
default: 0,
|
||||
})
|
||||
public displayOrder: number;
|
||||
|
||||
@Index()
|
||||
@Column('varchar', {
|
||||
...id(),
|
||||
|
@@ -42,6 +42,10 @@ export const meta = {
|
||||
type: 'string',
|
||||
optional: false, nullable: true,
|
||||
},
|
||||
displayOrder: {
|
||||
type: 'number',
|
||||
optional: false, nullable: false,
|
||||
},
|
||||
userId: {
|
||||
type: 'string',
|
||||
optional: false, nullable: true,
|
||||
@@ -60,6 +64,7 @@ export const paramDef = {
|
||||
title: { type: 'string', minLength: 1 },
|
||||
text: { type: 'string', minLength: 1 },
|
||||
imageUrl: { type: 'string', nullable: true, minLength: 1 },
|
||||
displayOrder: { type: 'number' },
|
||||
userId: { type: 'string', nullable: true, format: 'misskey:id' },
|
||||
closeDuration: { type: 'number', nullable: false },
|
||||
},
|
||||
@@ -83,6 +88,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
|
||||
title: ps.title,
|
||||
text: ps.text,
|
||||
imageUrl: ps.imageUrl,
|
||||
displayOrder: ps.displayOrder,
|
||||
userId: ps.userId ?? null,
|
||||
closeDuration: ps.closeDuration,
|
||||
}).then(x => this.announcementsRepository.findOneByOrFail(x.identifiers[0]));
|
||||
|
@@ -48,6 +48,10 @@ export const meta = {
|
||||
type: 'string',
|
||||
optional: false, nullable: true,
|
||||
},
|
||||
displayOrder: {
|
||||
type: 'number',
|
||||
optional: false, nullable: false,
|
||||
},
|
||||
userId: {
|
||||
type: 'string',
|
||||
optional: false, nullable: true,
|
||||
@@ -74,8 +78,7 @@ export const paramDef = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
||||
sinceId: { type: 'string', format: 'misskey:id' },
|
||||
untilId: { type: 'string', format: 'misskey:id' },
|
||||
offset: { type: 'integer', default: 0 },
|
||||
userId: { type: 'string', format: 'misskey:id' },
|
||||
},
|
||||
required: [],
|
||||
@@ -94,20 +97,25 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
|
||||
@Inject(DI.usersRepository)
|
||||
private usersRepository: UsersRepository,
|
||||
|
||||
private queryService: QueryService,
|
||||
private userEntityService: UserEntityService,
|
||||
) {
|
||||
super(meta, paramDef, async (ps, me) => {
|
||||
const builder = this.announcementsRepository.createQueryBuilder('announcement');
|
||||
const query = this.announcementsRepository.createQueryBuilder('announcement');
|
||||
if (ps.userId) {
|
||||
builder.where('"userId" = :userId', { userId: ps.userId });
|
||||
query.where('"userId" = :userId', { userId: ps.userId });
|
||||
} else {
|
||||
builder.where('"userId" IS NULL');
|
||||
query.where('"userId" IS NULL');
|
||||
}
|
||||
|
||||
const query = this.queryService.makePaginationQuery(builder, ps.sinceId, ps.untilId);
|
||||
query.orderBy({
|
||||
'announcement."displayOrder"': 'DESC',
|
||||
'announcement."createdAt"': 'DESC',
|
||||
});
|
||||
|
||||
const announcements = await query.limit(ps.limit).getMany();
|
||||
const announcements = await query
|
||||
.offset(ps.offset)
|
||||
.limit(ps.limit)
|
||||
.getMany();
|
||||
|
||||
const reads = new Map<Announcement, number>();
|
||||
|
||||
@@ -131,6 +139,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
|
||||
title: announcement.title,
|
||||
text: announcement.text,
|
||||
imageUrl: announcement.imageUrl,
|
||||
displayOrder: announcement.displayOrder,
|
||||
userId: announcement.userId,
|
||||
user: packedUsers.find(user => user.id === announcement.userId),
|
||||
reads: reads.get(announcement)!,
|
||||
|
@@ -26,6 +26,7 @@ export const paramDef = {
|
||||
title: { type: 'string', minLength: 1 },
|
||||
text: { type: 'string', minLength: 1 },
|
||||
imageUrl: { type: 'string', nullable: true, minLength: 0 },
|
||||
displayOrder: { type: 'number' },
|
||||
userId: { type: 'string', nullable: true, format: 'misskey:id' },
|
||||
closeDuration: { type: 'number', nullable: false },
|
||||
},
|
||||
@@ -57,6 +58,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
|
||||
text: ps.text,
|
||||
/* eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- 空の文字列の場合、nullを渡すようにするため */
|
||||
imageUrl: ps.imageUrl || null,
|
||||
displayOrder: ps.displayOrder,
|
||||
userId: ps.userId ?? null,
|
||||
closeDuration: ps.closeDuration,
|
||||
});
|
||||
|
@@ -1,8 +1,8 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Endpoint } from '@/server/api/endpoint-base.js';
|
||||
import { QueryService } from '@/core/QueryService.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import type { AnnouncementReadsRepository, AnnouncementsRepository } from '@/models/index.js';
|
||||
import type { AnnouncementsRepository } from '@/models/index.js';
|
||||
import { Announcement, AnnouncementRead } from '@/models/index.js';
|
||||
|
||||
export const meta = {
|
||||
tags: ['meta'],
|
||||
@@ -65,9 +65,8 @@ export const paramDef = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
||||
offset: { type: 'integer', default: 0 },
|
||||
withUnreads: { type: 'boolean', default: false },
|
||||
sinceId: { type: 'string', format: 'misskey:id' },
|
||||
untilId: { type: 'string', format: 'misskey:id' },
|
||||
privateOnly: { type: 'boolean', default: false },
|
||||
},
|
||||
required: [],
|
||||
@@ -79,39 +78,37 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
|
||||
constructor(
|
||||
@Inject(DI.announcementsRepository)
|
||||
private announcementsRepository: AnnouncementsRepository,
|
||||
|
||||
@Inject(DI.announcementReadsRepository)
|
||||
private announcementReadsRepository: AnnouncementReadsRepository,
|
||||
|
||||
private queryService: QueryService,
|
||||
) {
|
||||
super(meta, paramDef, async (ps, me) => {
|
||||
const builder = this.announcementsRepository.createQueryBuilder('announcement');
|
||||
const query = this.announcementsRepository.createQueryBuilder('announcement');
|
||||
if (me) {
|
||||
query.leftJoinAndSelect(AnnouncementRead, 'reads', 'reads."announcementId" = announcement.id AND reads."userId" = :userId', { userId: me.id });
|
||||
query.select([
|
||||
'announcement.*',
|
||||
'CASE WHEN reads.id IS NULL THEN FALSE ELSE TRUE END as "isRead"',
|
||||
]);
|
||||
if (ps.privateOnly) {
|
||||
builder.where('"userId" = :userId', { userId: me.id });
|
||||
query.where('announcement."userId" = :userId', { userId: me.id });
|
||||
} else {
|
||||
builder.where('"userId" IS NULL');
|
||||
builder.orWhere('"userId" = :userId', { userId: me.id });
|
||||
query.where('announcement."userId" IS NULL');
|
||||
query.orWhere('announcement."userId" = :userId', { userId: me.id });
|
||||
}
|
||||
} else {
|
||||
builder.where('"userId" IS NULL');
|
||||
query.where('announcement."userId" IS NULL');
|
||||
}
|
||||
|
||||
const query = this.queryService.makePaginationQuery(builder, ps.sinceId, ps.untilId);
|
||||
const announcements = await query.limit(ps.limit).getMany();
|
||||
query.orderBy({
|
||||
'"isRead"': 'ASC',
|
||||
'announcement."displayOrder"': 'DESC',
|
||||
'announcement."createdAt"': 'DESC',
|
||||
});
|
||||
|
||||
if (me) {
|
||||
const reads = (await this.announcementReadsRepository.findBy({
|
||||
userId: me.id,
|
||||
})).map(x => x.announcementId);
|
||||
const announcements = await query
|
||||
.offset(ps.offset)
|
||||
.limit(ps.limit)
|
||||
.getRawMany<Announcement & { isRead: boolean }>();
|
||||
|
||||
for (const announcement of announcements) {
|
||||
(announcement as any).isRead = reads.includes(announcement.id);
|
||||
}
|
||||
}
|
||||
|
||||
return (ps.withUnreads ? announcements.filter((a: any) => !a.isRead) : announcements).map((a) => ({
|
||||
return (ps.withUnreads ? announcements.filter(i => !i.isRead) : announcements).map((a) => ({
|
||||
...a,
|
||||
createdAt: a.createdAt.toISOString(),
|
||||
updatedAt: a.updatedAt?.toISOString() ?? null,
|
||||
|
Reference in New Issue
Block a user