2021-08-19 21:55:45 +09:00
|
|
|
import { publishMainStream } from '@/services/stream';
|
|
|
|
import define from '../../define';
|
|
|
|
import { MessagingMessages, UserGroupJoinings } from '@/models/index';
|
2018-12-28 05:06:25 +09:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['account', 'messaging'],
|
|
|
|
|
2020-02-15 21:33:32 +09:00
|
|
|
requireCredential: true as const,
|
2018-12-28 05:06:25 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
kind: 'write:account',
|
2018-12-28 05:06:25 +09:00
|
|
|
|
|
|
|
params: {
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2018-12-28 05:06:25 +09:00
|
|
|
};
|
|
|
|
|
2022-01-03 02:12:50 +09:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2019-02-22 11:46:58 +09:00
|
|
|
export default define(meta, 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
|
|
|
});
|
|
|
|
|
2020-07-04 10:45:36 +09:00
|
|
|
const joinings = await UserGroupJoinings.find({ userId: user.id });
|
|
|
|
|
|
|
|
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
|
|
|
});
|