2021-08-19 12:55:45 +00:00
|
|
|
import { publishMainStream } from '@/services/stream';
|
|
|
|
import define from '../../define';
|
|
|
|
import { MessagingMessages, UserGroupJoinings } from '@/models/index';
|
2018-12-27 20:06:25 +00:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 02:20:58 +00:00
|
|
|
tags: ['account', 'messaging'],
|
|
|
|
|
2020-02-15 12:33:32 +00:00
|
|
|
requireCredential: true as const,
|
2018-12-27 20:06:25 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
kind: 'write:account',
|
2018-12-27 20:06:25 +00:00
|
|
|
|
|
|
|
params: {
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
2018-12-27 20:06:25 +00:00
|
|
|
};
|
|
|
|
|
2022-01-02 17:12:50 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2019-02-22 02:46:58 +00:00
|
|
|
export default define(meta, async (ps, user) => {
|
2018-12-27 20:06:25 +00:00
|
|
|
// Update documents
|
2019-04-07 12:50:36 +00:00
|
|
|
await MessagingMessages.update({
|
|
|
|
recipientId: user.id,
|
2021-12-09 14:58:30 +00:00
|
|
|
isRead: false,
|
2018-12-27 20:06:25 +00:00
|
|
|
}, {
|
2021-12-09 14:58:30 +00:00
|
|
|
isRead: true,
|
2018-12-27 20:06:25 +00:00
|
|
|
});
|
|
|
|
|
2020-07-04 01:45:36 +00:00
|
|
|
const joinings = await UserGroupJoinings.find({ userId: user.id });
|
|
|
|
|
|
|
|
await Promise.all(joinings.map(j => MessagingMessages.createQueryBuilder().update()
|
|
|
|
.set({
|
2021-12-09 14:58:30 +00:00
|
|
|
reads: (() => `array_append("reads", '${user.id}')`) as any,
|
2020-07-04 01:45:36 +00: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 12:50:36 +00:00
|
|
|
publishMainStream(user.id, 'readAllMessagingMessages');
|
2019-02-22 02:46:58 +00:00
|
|
|
});
|