2022-02-27 11:07:39 +09:00
|
|
|
import { publishMainStream } from '@/services/stream.js';
|
|
|
|
import define from '../../define.js';
|
|
|
|
import { MessagingMessages, UserGroupJoinings } from '@/models/index.js';
|
2018-12-28 05:06:25 +09:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['account', 'messaging'],
|
|
|
|
|
2022-01-18 22:27:10 +09:00
|
|
|
requireCredential: true,
|
2018-12-28 05:06:25 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
kind: 'write:account',
|
2022-02-19 14:05:32 +09:00
|
|
|
} as const;
|
2018-12-28 05:06:25 +09:00
|
|
|
|
2022-02-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09:00
|
|
|
type: 'object',
|
|
|
|
properties: {},
|
|
|
|
required: [],
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2018-12-28 05:06:25 +09:00
|
|
|
|
2022-01-03 02:12:50 +09:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 14:05:32 +09:00
|
|
|
export default define(meta, paramDef, async (ps, user) => {
|
2018-12-28 05:06:25 +09:00
|
|
|
// Update documents
|
2019-04-07 21:50:36 +09:00
|
|
|
await MessagingMessages.update({
|
|
|
|
recipientId: user.id,
|
2021-12-09 23:58:30 +09:00
|
|
|
isRead: false,
|
2018-12-28 05:06:25 +09:00
|
|
|
}, {
|
2021-12-09 23:58:30 +09:00
|
|
|
isRead: true,
|
2018-12-28 05:06:25 +09:00
|
|
|
});
|
|
|
|
|
2022-03-26 15:34:00 +09:00
|
|
|
const joinings = await UserGroupJoinings.findBy({ userId: user.id });
|
2020-07-04 10:45:36 +09:00
|
|
|
|
|
|
|
await Promise.all(joinings.map(j => MessagingMessages.createQueryBuilder().update()
|
|
|
|
.set({
|
2021-12-09 23:58:30 +09:00
|
|
|
reads: (() => `array_append("reads", '${user.id}')`) as any,
|
2020-07-04 10:45:36 +09:00
|
|
|
})
|
|
|
|
.where(`groupId = :groupId`, { groupId: j.userGroupId })
|
|
|
|
.andWhere('userId != :userId', { userId: user.id })
|
|
|
|
.andWhere('NOT (:userId = ANY(reads))', { userId: user.id })
|
|
|
|
.execute()));
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
publishMainStream(user.id, 'readAllMessagingMessages');
|
2019-02-22 11:46:58 +09:00
|
|
|
});
|