enhance(backend): 個人宛のお知らせはわかったを押すと過去のお知らせに表示させる (MisskeyIO#736)

This commit is contained in:
まっちゃとーにゅ 2024-09-17 08:08:44 +09:00 committed by GitHub
parent 46e4b8f8e1
commit 8e9b6dc4d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 37 additions and 28 deletions

View File

@ -138,7 +138,7 @@ export class AnnouncementService {
limit: number,
offset: number,
moderator: MiUser,
): Promise<(MiAnnouncement & { userInfo: Packed<'UserLite'> | null, reads: number })[]> {
): Promise<(MiAnnouncement & { userInfo: Packed<'UserLite'> | null, reads: number, lastReadAt: Date | null })[]> {
const query = this.announcementsRepository.createQueryBuilder('announcement');
if (userId) {
@ -157,13 +157,14 @@ export class AnnouncementService {
.offset(offset)
.getMany();
const reads = new Map<MiAnnouncement, number>();
for (const announcement of announcements) {
reads.set(announcement, await this.announcementReadsRepository.countBy({
announcementId: announcement.id,
}));
}
const reads = announcements.length > 0
? await this.announcementReadsRepository.createQueryBuilder()
.select('"announcementId", count(*) as "reads", max("id") as "lastReadId"')
.where('"announcementId" IN (:...announcementIds)', { announcementIds: announcements.map(a => a.id) })
.groupBy('"announcementId"')
.getRawMany<{ announcementId: string, reads: number, lastReadId: string | null }>()
.then(rs => new Map(rs.map(r => [r.announcementId, { reads: r.reads, lastReadAt: r.lastReadId ? this.idService.parse(r.lastReadId).date : null }])))
: new Map();
const users = await this.usersRepository.findBy({
id: In(announcements.map(a => a.userId).filter(id => id != null)),
@ -174,8 +175,8 @@ export class AnnouncementService {
return announcements.map(announcement => ({
...announcement,
...reads.get(announcement.id) ?? { reads: 0, lastReadAt: null },
userInfo: packedUsers.find(u => u.id === announcement.userId) ?? null,
reads: reads.get(announcement) ?? 0,
}));
}
@ -293,18 +294,20 @@ export class AnnouncementService {
'read.id IS NOT NULL as "isRead"',
]);
query
.andWhere(
new Brackets((qb) => {
qb.orWhere('announcement."userId" = :userId', { userId: me.id });
qb.orWhere('announcement."userId" IS NULL');
}),
)
.andWhere(
new Brackets((qb) => {
qb.orWhere('announcement."forExistingUsers" = false');
qb.orWhere('announcement.id > :userId', { userId: me.id });
}),
);
.andWhere(new Brackets((qb) => {
qb.orWhere(new Brackets((nqb) => {
nqb.andWhere('announcement."userId" = :userId', { userId: me.id });
nqb.andWhere(isActive ? 'read.id IS NULL' : 'read.id IS NOT NULL');
}));
qb.orWhere(new Brackets((nqb) => {
nqb.andWhere('announcement."userId" IS NULL');
nqb.andWhere('announcement."isActive" = :isActive', { isActive });
}));
}))
.andWhere(new Brackets((qb) => {
qb.orWhere('announcement."forExistingUsers" = false');
qb.orWhere('announcement.id > :userId', { userId: me.id });
}));
} else {
query.select([
'announcement.*',
@ -312,12 +315,9 @@ export class AnnouncementService {
]);
query.andWhere('announcement."userId" IS NULL');
query.andWhere('announcement."forExistingUsers" = false');
query.andWhere('announcement."isActive" = :isActive', { isActive });
}
query.andWhere('announcement."isActive" = :isActive', {
isActive: isActive,
});
if (isActive) {
query.orderBy({
'"isRead"': 'ASC',

View File

@ -97,6 +97,11 @@ export const meta = {
type: 'number',
optional: false, nullable: false,
},
lastReadAt: {
type: 'string',
optional: false, nullable: true,
format: 'date-time',
},
},
},
},
@ -140,6 +145,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
userId: announcement.userId,
user: announcement.userInfo,
reads: announcement.reads,
lastReadAt: announcement.lastReadAt?.toISOString() ?? null,
}));
});
}

View File

@ -53,7 +53,7 @@ SPDX-License-Identifier: AGPL-3.0-only
{{ i18n.ts._announcement.silence }}
<template #caption>{{ i18n.ts._announcement.silenceDescription }}</template>
</MkSwitch>
<p v-if="reads">{{ i18n.tsx.nUsersRead({ n: reads }) }}</p>
<p v-if="reads">{{ i18n.tsx.nUsersRead({ n: reads }) }} <span v-if="lastReadAt">(<MkTime :time="lastReadAt" mode="absolute"/>)</span></p>
<MkUserCardMini v-if="props.user.id" :user="props.user"></MkUserCardMini>
<MkButton v-if="announcement" danger @click="del()"><i class="ti ti-trash"></i> {{ i18n.ts.delete }}</MkButton>
</div>
@ -94,6 +94,7 @@ const closeDuration = ref<number>(props.announcement ? props.announcement.closeD
const displayOrder = ref<number>(props.announcement ? props.announcement.displayOrder : 0);
const silence = ref<boolean>(props.announcement ? props.announcement.silence : false);
const reads = ref<number>(props.announcement ? props.announcement.reads : 0);
const lastReadAt = ref<string | null>(props.announcement ? props.announcement.lastReadAt : null);
const emit = defineEmits<{
(ev: 'done', v: { deleted?: boolean; updated?: any; created?: any }): void,

View File

@ -149,7 +149,7 @@ SPDX-License-Identifier: AGPL-3.0-only
<i v-else-if="announcement.icon === 'success'" class="ti ti-check" style="color: var(--success);"></i>
</span>
<span>{{ announcement.title }}</span>
<span v-if="announcement.reads > 0" style="margin-left: auto; opacity: 0.7;">{{ i18n.ts.messageRead }}</span>
<span v-if="announcement.reads > 0" style="margin-left: auto; opacity: 0.7;">{{ i18n.ts.messageRead }} <span v-if="announcement.lastReadAt">(<MkTime :time="announcement.lastReadAt" mode="absolute"/>)</span></span>
</div>
</div>
</template>

View File

@ -76,7 +76,7 @@ SPDX-License-Identifier: AGPL-3.0-only
<MkSwitch v-model="announcement.silence" :helpText="i18n.ts._announcement.silenceDescription">
{{ i18n.ts._announcement.silence }}
</MkSwitch>
<p v-if="announcement.reads">{{ i18n.tsx.nUsersRead({ n: announcement.reads }) }}</p>
<p v-if="announcement.reads">{{ i18n.tsx.nUsersRead({ n: announcement.reads }) }} <span v-if="announcement.lastReadAt">(<MkTime :time="announcement.lastReadAt" mode="absolute"/>)</span></p>
<MkUserCardMini v-if="announcement.userId" :user="announcement.user" @click="editUser(announcement)"></MkUserCardMini>
<MkButton v-else class="button" inline primary @click="editUser(announcement)">{{ i18n.ts.specifyUser }}</MkButton>
<div class="buttons _buttons">

View File

@ -6183,6 +6183,8 @@ export type operations = {
userId: string | null;
user: components['schemas']['UserLite'] | null;
reads: number;
/** Format: date-time */
lastReadAt: string | null;
})[];
};
};