なんかもうめっちゃ変えた
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import define from '../../../define.js';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Endpoint } from '@/server/api/endpoint-base.js';
|
||||
import { BlockingsRepository, UserGroupJoiningsRepository, DriveFilesRepository, UserGroupsRepository } from '@/models/index.js';
|
||||
import type { User } from '@/models/entities/User.js';
|
||||
import type { UserGroup } from '@/models/entities/UserGroup.js';
|
||||
import { GetterService } from '@/server/api/common/GetterService.js';
|
||||
import { MessagingService } from '@/core/MessagingService.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import { ApiError } from '../../../error.js';
|
||||
import { getUser } from '../../../common/getters.js';
|
||||
import { MessagingMessages, DriveFiles, UserGroups, UserGroupJoinings, Blockings } from '@/models/index.js';
|
||||
import { User } from '@/models/entities/user.js';
|
||||
import { UserGroup } from '@/models/entities/user-group.js';
|
||||
import { createMessage } from '@/services/messages/create.js';
|
||||
|
||||
export const meta = {
|
||||
tags: ['messaging'],
|
||||
@@ -87,65 +89,85 @@ export const paramDef = {
|
||||
} as const;
|
||||
|
||||
// eslint-disable-next-line import/no-default-export
|
||||
export default define(meta, paramDef, async (ps, user) => {
|
||||
let recipientUser: User | null;
|
||||
let recipientGroup: UserGroup | null;
|
||||
@Injectable()
|
||||
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
||||
constructor(
|
||||
@Inject(DI.userGroupsRepository)
|
||||
private userGroupsRepository: UserGroupsRepository,
|
||||
|
||||
if (ps.userId != null) {
|
||||
// Myself
|
||||
if (ps.userId === user.id) {
|
||||
throw new ApiError(meta.errors.recipientIsYourself);
|
||||
}
|
||||
@Inject(DI.userGroupJoiningsRepository)
|
||||
private userGroupJoiningsRepository: UserGroupJoiningsRepository,
|
||||
|
||||
// Fetch recipient (user)
|
||||
recipientUser = await getUser(ps.userId).catch(e => {
|
||||
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError(meta.errors.noSuchUser);
|
||||
throw e;
|
||||
@Inject(DI.blockingsRepository)
|
||||
private blockingsRepository: BlockingsRepository,
|
||||
|
||||
@Inject(DI.driveFilesRepository)
|
||||
private driveFilesRepository: DriveFilesRepository,
|
||||
|
||||
private getterService: GetterService,
|
||||
private messagingService: MessagingService,
|
||||
) {
|
||||
super(meta, paramDef, async (ps, me) => {
|
||||
let recipientUser: User | null;
|
||||
let recipientGroup: UserGroup | null;
|
||||
|
||||
if (ps.userId != null) {
|
||||
// Myself
|
||||
if (ps.userId === me.id) {
|
||||
throw new ApiError(meta.errors.recipientIsYourself);
|
||||
}
|
||||
|
||||
// Fetch recipient (user)
|
||||
recipientUser = await this.getterService.getUser(ps.userId).catch(err => {
|
||||
if (err.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError(meta.errors.noSuchUser);
|
||||
throw err;
|
||||
});
|
||||
|
||||
// Check blocking
|
||||
const block = await this.blockingsRepository.findOneBy({
|
||||
blockerId: recipientUser.id,
|
||||
blockeeId: me.id,
|
||||
});
|
||||
if (block) {
|
||||
throw new ApiError(meta.errors.youHaveBeenBlocked);
|
||||
}
|
||||
} else if (ps.groupId != null) {
|
||||
// Fetch recipient (group)
|
||||
recipientGroup = await this.userGroupsRepository.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);
|
||||
}
|
||||
}
|
||||
|
||||
let file = null;
|
||||
if (ps.fileId != null) {
|
||||
file = await this.driveFilesRepository.findOneBy({
|
||||
id: ps.fileId,
|
||||
userId: me.id,
|
||||
});
|
||||
|
||||
if (file == null) {
|
||||
throw new ApiError(meta.errors.noSuchFile);
|
||||
}
|
||||
}
|
||||
|
||||
// テキストが無いかつ添付ファイルも無かったらエラー
|
||||
if (ps.text == null && file == null) {
|
||||
throw new ApiError(meta.errors.contentRequired);
|
||||
}
|
||||
|
||||
return await this.messagingService.createMessage(me, recipientUser, recipientGroup, ps.text, file);
|
||||
});
|
||||
|
||||
// Check blocking
|
||||
const block = await Blockings.findOneBy({
|
||||
blockerId: recipientUser.id,
|
||||
blockeeId: user.id,
|
||||
});
|
||||
if (block) {
|
||||
throw new ApiError(meta.errors.youHaveBeenBlocked);
|
||||
}
|
||||
} else if (ps.groupId != null) {
|
||||
// Fetch recipient (group)
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
let file = null;
|
||||
if (ps.fileId != null) {
|
||||
file = await DriveFiles.findOneBy({
|
||||
id: ps.fileId,
|
||||
userId: user.id,
|
||||
});
|
||||
|
||||
if (file == null) {
|
||||
throw new ApiError(meta.errors.noSuchFile);
|
||||
}
|
||||
}
|
||||
|
||||
// テキストが無いかつ添付ファイルも無かったらエラー
|
||||
if (ps.text == null && file == null) {
|
||||
throw new ApiError(meta.errors.contentRequired);
|
||||
}
|
||||
|
||||
return await createMessage(user, recipientUser, recipientGroup, ps.text, file);
|
||||
});
|
||||
}
|
||||
|
@@ -1,8 +1,10 @@
|
||||
import define from '../../../define.js';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import ms from 'ms';
|
||||
import { Endpoint } from '@/server/api/endpoint-base.js';
|
||||
import { MessagingMessagesRepository } from '@/models/index.js';
|
||||
import { MessagingService } from '@/core/MessagingService.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import { ApiError } from '../../../error.js';
|
||||
import { MessagingMessages } from '@/models/index.js';
|
||||
import { deleteMessage } from '@/services/messages/delete.js';
|
||||
|
||||
export const meta = {
|
||||
tags: ['messaging'],
|
||||
@@ -35,15 +37,25 @@ export const paramDef = {
|
||||
} as const;
|
||||
|
||||
// eslint-disable-next-line import/no-default-export
|
||||
export default define(meta, paramDef, async (ps, user) => {
|
||||
const message = await MessagingMessages.findOneBy({
|
||||
id: ps.messageId,
|
||||
userId: user.id,
|
||||
});
|
||||
@Injectable()
|
||||
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
||||
constructor(
|
||||
@Inject(DI.messagingMessagesRepository)
|
||||
private messagingMessagesRepository: MessagingMessagesRepository,
|
||||
|
||||
if (message == null) {
|
||||
throw new ApiError(meta.errors.noSuchMessage);
|
||||
private messagingService: MessagingService,
|
||||
) {
|
||||
super(meta, paramDef, async (ps, me) => {
|
||||
const message = await this.messagingMessagesRepository.findOneBy({
|
||||
id: ps.messageId,
|
||||
userId: me.id,
|
||||
});
|
||||
|
||||
if (message == null) {
|
||||
throw new ApiError(meta.errors.noSuchMessage);
|
||||
}
|
||||
|
||||
await this.messagingService.deleteMessage(message);
|
||||
});
|
||||
}
|
||||
|
||||
await deleteMessage(message);
|
||||
});
|
||||
}
|
||||
|
@@ -1,7 +1,9 @@
|
||||
import define from '../../../define.js';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Endpoint } from '@/server/api/endpoint-base.js';
|
||||
import { MessagingMessagesRepository } from '@/models/index.js';
|
||||
import { MessagingService } from '@/core/MessagingService.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import { ApiError } from '../../../error.js';
|
||||
import { MessagingMessages } from '@/models/index.js';
|
||||
import { readUserMessagingMessage, readGroupMessagingMessage } from '../../../common/read-messaging-message.js';
|
||||
|
||||
export const meta = {
|
||||
tags: ['messaging'],
|
||||
@@ -28,22 +30,32 @@ export const paramDef = {
|
||||
} as const;
|
||||
|
||||
// eslint-disable-next-line import/no-default-export
|
||||
export default define(meta, paramDef, async (ps, user) => {
|
||||
const message = await MessagingMessages.findOneBy({ id: ps.messageId });
|
||||
@Injectable()
|
||||
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
||||
constructor(
|
||||
@Inject(DI.messagingMessagesRepository)
|
||||
private messagingMessagesRepository: MessagingMessagesRepository,
|
||||
|
||||
if (message == null) {
|
||||
throw new ApiError(meta.errors.noSuchMessage);
|
||||
}
|
||||
private messagingService: MessagingService,
|
||||
) {
|
||||
super(meta, paramDef, async (ps, me) => {
|
||||
const message = await this.messagingMessagesRepository.findOneBy({ id: ps.messageId });
|
||||
|
||||
if (message.recipientId) {
|
||||
await readUserMessagingMessage(user.id, message.userId, [message.id]).catch(e => {
|
||||
if (e.id === 'e140a4bf-49ce-4fb6-b67c-b78dadf6b52f') throw new ApiError(meta.errors.noSuchMessage);
|
||||
throw e;
|
||||
});
|
||||
} else if (message.groupId) {
|
||||
await readGroupMessagingMessage(user.id, message.groupId, [message.id]).catch(e => {
|
||||
if (e.id === '930a270c-714a-46b2-b776-ad27276dc569') throw new ApiError(meta.errors.noSuchMessage);
|
||||
throw e;
|
||||
if (message == null) {
|
||||
throw new ApiError(meta.errors.noSuchMessage);
|
||||
}
|
||||
|
||||
if (message.recipientId) {
|
||||
await this.messagingService.readUserMessagingMessage(me.id, message.userId, [message.id]).catch(err => {
|
||||
if (err.id === 'e140a4bf-49ce-4fb6-b67c-b78dadf6b52f') throw new ApiError(meta.errors.noSuchMessage);
|
||||
throw err;
|
||||
});
|
||||
} else if (message.groupId) {
|
||||
await this.messagingService.readGroupMessagingMessage(me.id, message.groupId, [message.id]).catch(err => {
|
||||
if (err.id === '930a270c-714a-46b2-b776-ad27276dc569') throw new ApiError(meta.errors.noSuchMessage);
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user