なんかもうめっちゃ変えた
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
import define from '../../define.js';
|
||||
import { ApiError } from '../../error.js';
|
||||
import { getUser } from '../../common/getters.js';
|
||||
import { MessagingMessages, UserGroups, UserGroupJoinings, Users } from '@/models/index.js';
|
||||
import { makePaginationQuery } from '../../common/make-pagination-query.js';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Brackets } from 'typeorm';
|
||||
import { readUserMessagingMessage, readGroupMessagingMessage, deliverReadActivity } from '../../common/read-messaging-message.js';
|
||||
import { Endpoint } from '@/server/api/endpoint-base.js';
|
||||
import type { UsersRepository } from '@/models/index.js';
|
||||
import { UserGroupsRepository, MessagingMessagesRepository, UserGroupJoiningsRepository } from '@/models/index.js';
|
||||
import { QueryService } from '@/core/QueryService.js';
|
||||
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
||||
import { MessagingMessageEntityService } from '@/core/entities/MessagingMessageEntityService.js';
|
||||
import { MessagingService } from '@/core/MessagingService.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import { ApiError } from '../../error.js';
|
||||
import { GetterService } from '../../common/GetterService.js';
|
||||
|
||||
export const meta = {
|
||||
tags: ['messaging'],
|
||||
@@ -69,73 +74,93 @@ export const paramDef = {
|
||||
} as const;
|
||||
|
||||
// eslint-disable-next-line import/no-default-export
|
||||
export default define(meta, paramDef, async (ps, user) => {
|
||||
if (ps.userId != null) {
|
||||
// Fetch recipient (user)
|
||||
const recipient = await getUser(ps.userId).catch(e => {
|
||||
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError(meta.errors.noSuchUser);
|
||||
throw e;
|
||||
});
|
||||
@Injectable()
|
||||
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
||||
constructor(
|
||||
@Inject(DI.messagingMessagesRepository)
|
||||
private messagingMessagesRepository: MessagingMessagesRepository,
|
||||
|
||||
const query = makePaginationQuery(MessagingMessages.createQueryBuilder('message'), ps.sinceId, ps.untilId)
|
||||
.andWhere(new Brackets(qb => { qb
|
||||
.where(new Brackets(qb => { qb
|
||||
.where('message.userId = :meId')
|
||||
.andWhere('message.recipientId = :recipientId');
|
||||
}))
|
||||
.orWhere(new Brackets(qb => { qb
|
||||
.where('message.userId = :recipientId')
|
||||
.andWhere('message.recipientId = :meId');
|
||||
}));
|
||||
}))
|
||||
.setParameter('meId', user.id)
|
||||
.setParameter('recipientId', recipient.id);
|
||||
@Inject(DI.userGroupsRepository)
|
||||
private userGroupRepository: UserGroupsRepository,
|
||||
|
||||
const messages = await query.take(ps.limit).getMany();
|
||||
@Inject(DI.userGroupJoiningsRepository)
|
||||
private userGroupJoiningsRepository: UserGroupJoiningsRepository,
|
||||
|
||||
// Mark all as read
|
||||
if (ps.markAsRead) {
|
||||
readUserMessagingMessage(user.id, recipient.id, messages.filter(m => m.recipientId === user.id).map(x => x.id));
|
||||
private messagingMessageEntityService: MessagingMessageEntityService,
|
||||
private messagingService: MessagingService,
|
||||
private userEntityService: UserEntityService,
|
||||
private queryService: QueryService,
|
||||
private getterService: GetterService,
|
||||
) {
|
||||
super(meta, paramDef, async (ps, me) => {
|
||||
if (ps.userId != null) {
|
||||
// Fetch recipient (user)
|
||||
const recipient = await this.getterService.getUser(ps.userId).catch(err => {
|
||||
if (err.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError(meta.errors.noSuchUser);
|
||||
throw err;
|
||||
});
|
||||
|
||||
// リモートユーザーとのメッセージだったら既読配信
|
||||
if (Users.isLocalUser(user) && Users.isRemoteUser(recipient)) {
|
||||
deliverReadActivity(user, recipient, messages);
|
||||
const query = this.queryService.makePaginationQuery(this.messagingMessagesRepository.createQueryBuilder('message'), ps.sinceId, ps.untilId)
|
||||
.andWhere(new Brackets(qb => { qb
|
||||
.where(new Brackets(qb => { qb
|
||||
.where('message.userId = :meId')
|
||||
.andWhere('message.recipientId = :recipientId');
|
||||
}))
|
||||
.orWhere(new Brackets(qb => { qb
|
||||
.where('message.userId = :recipientId')
|
||||
.andWhere('message.recipientId = :meId');
|
||||
}));
|
||||
}))
|
||||
.setParameter('meId', me.id)
|
||||
.setParameter('recipientId', recipient.id);
|
||||
|
||||
const messages = await query.take(ps.limit).getMany();
|
||||
|
||||
// Mark all as read
|
||||
if (ps.markAsRead) {
|
||||
this.messagingService.readUserMessagingMessage(me.id, recipient.id, messages.filter(m => m.recipientId === me.id).map(x => x.id));
|
||||
|
||||
// リモートユーザーとのメッセージだったら既読配信
|
||||
if (this.userEntityService.isLocalUser(me) && this.userEntityService.isRemoteUser(recipient)) {
|
||||
this.messagingService.deliverReadActivity(me, recipient, messages);
|
||||
}
|
||||
}
|
||||
|
||||
return await Promise.all(messages.map(message => this.messagingMessageEntityService.pack(message, me, {
|
||||
populateRecipient: false,
|
||||
})));
|
||||
} else if (ps.groupId != null) {
|
||||
// Fetch recipient (group)
|
||||
const recipientGroup = await this.userGroupRepository.findOneBy({ id: ps.groupId });
|
||||
|
||||
if (recipientGroup == null) {
|
||||
throw new ApiError(meta.errors.noSuchGroup);
|
||||
}
|
||||
|
||||
// check joined
|
||||
const joining = await this.userGroupJoiningsRepository.findOneBy({
|
||||
userId: me.id,
|
||||
userGroupId: recipientGroup.id,
|
||||
});
|
||||
|
||||
if (joining == null) {
|
||||
throw new ApiError(meta.errors.groupAccessDenied);
|
||||
}
|
||||
|
||||
const query = this.queryService.makePaginationQuery(this.messagingMessagesRepository.createQueryBuilder('message'), ps.sinceId, ps.untilId)
|
||||
.andWhere('message.groupId = :groupId', { groupId: recipientGroup.id });
|
||||
|
||||
const messages = await query.take(ps.limit).getMany();
|
||||
|
||||
// Mark all as read
|
||||
if (ps.markAsRead) {
|
||||
this.messagingService.readGroupMessagingMessage(me.id, recipientGroup.id, messages.map(x => x.id));
|
||||
}
|
||||
|
||||
return await Promise.all(messages.map(message => this.messagingMessageEntityService.pack(message, me, {
|
||||
populateGroup: false,
|
||||
})));
|
||||
}
|
||||
}
|
||||
|
||||
return await Promise.all(messages.map(message => MessagingMessages.pack(message, user, {
|
||||
populateRecipient: false,
|
||||
})));
|
||||
} else if (ps.groupId != null) {
|
||||
// Fetch recipient (group)
|
||||
const recipientGroup = await UserGroups.findOneBy({ id: ps.groupId });
|
||||
|
||||
if (recipientGroup == null) {
|
||||
throw new ApiError(meta.errors.noSuchGroup);
|
||||
}
|
||||
|
||||
// check joined
|
||||
const joining = await UserGroupJoinings.findOneBy({
|
||||
userId: user.id,
|
||||
userGroupId: recipientGroup.id,
|
||||
});
|
||||
|
||||
if (joining == null) {
|
||||
throw new ApiError(meta.errors.groupAccessDenied);
|
||||
}
|
||||
|
||||
const query = makePaginationQuery(MessagingMessages.createQueryBuilder('message'), ps.sinceId, ps.untilId)
|
||||
.andWhere(`message.groupId = :groupId`, { groupId: recipientGroup.id });
|
||||
|
||||
const messages = await query.take(ps.limit).getMany();
|
||||
|
||||
// Mark all as read
|
||||
if (ps.markAsRead) {
|
||||
readGroupMessagingMessage(user.id, recipientGroup.id, messages.map(x => x.id));
|
||||
}
|
||||
|
||||
return await Promise.all(messages.map(message => MessagingMessages.pack(message, user, {
|
||||
populateGroup: false,
|
||||
})));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user