2022-09-17 13:27:08 -05:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { MessagingMessagesRepository, UserGroupJoiningsRepository } from '@/models/index.js';
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2018-12-27 14:06:25 -06:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-22 20:20:58 -06:00
|
|
|
tags: ['account', 'messaging'],
|
|
|
|
|
2022-01-18 07:27:10 -06:00
|
|
|
requireCredential: true,
|
2018-12-27 14:06:25 -06:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
kind: 'write:account',
|
2022-02-18 23:05:32 -06:00
|
|
|
} as const;
|
2018-12-27 14:06:25 -06:00
|
|
|
|
2022-02-19 22:15:40 -06:00
|
|
|
export const paramDef = {
|
2022-02-18 23:05:32 -06:00
|
|
|
type: 'object',
|
|
|
|
properties: {},
|
|
|
|
required: [],
|
2022-01-18 07:27:10 -06:00
|
|
|
} as const;
|
2018-12-27 14:06:25 -06:00
|
|
|
|
2022-01-02 11:12:50 -06:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 13:27:08 -05:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.messagingMessagesRepository)
|
|
|
|
private messagingMessagesRepository: MessagingMessagesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userGroupJoiningsRepository)
|
|
|
|
private userGroupJoiningsRepository: UserGroupJoiningsRepository,
|
|
|
|
|
|
|
|
private globalEventService: GlobalEventService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
// Update documents
|
|
|
|
await this.messagingMessagesRepository.update({
|
|
|
|
recipientId: me.id,
|
|
|
|
isRead: false,
|
|
|
|
}, {
|
|
|
|
isRead: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const joinings = await this.userGroupJoiningsRepository.findBy({ userId: me.id });
|
|
|
|
|
|
|
|
await Promise.all(joinings.map(j => this.messagingMessagesRepository.createQueryBuilder().update()
|
|
|
|
.set({
|
|
|
|
reads: (() => `array_append("reads", '${me.id}')`) as any,
|
|
|
|
})
|
|
|
|
.where('groupId = :groupId', { groupId: j.userGroupId })
|
|
|
|
.andWhere('userId != :userId', { userId: me.id })
|
|
|
|
.andWhere('NOT (:userId = ANY(reads))', { userId: me.id })
|
|
|
|
.execute()));
|
|
|
|
|
|
|
|
this.globalEventService.publishMainStream(me.id, 'readAllMessagingMessages');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|