2022-02-27 04:07:39 +02:00
|
|
|
import define from '../../../define.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';
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-07-16 22:36:44 +03:00
|
|
|
export const meta = {
|
2019-02-23 04:20:58 +02:00
|
|
|
tags: ['messaging'],
|
|
|
|
|
2022-01-18 15:27:10 +02:00
|
|
|
requireCredential: true,
|
2018-07-16 22:36:44 +03:00
|
|
|
|
2019-04-15 06:10:40 +03:00
|
|
|
kind: 'write:messaging',
|
2018-11-01 20:32:24 +02:00
|
|
|
|
2019-02-24 20:43:19 +02:00
|
|
|
res: {
|
2022-01-18 15:27:10 +02:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 16:35:26 +03:00
|
|
|
ref: 'MessagingMessage',
|
2019-02-24 20:43:19 +02:00
|
|
|
},
|
|
|
|
|
2019-02-22 04:46:58 +02:00
|
|
|
errors: {
|
|
|
|
recipientIsYourself: {
|
|
|
|
message: 'You can not send a message to yourself.',
|
|
|
|
code: 'RECIPIENT_IS_YOURSELF',
|
2021-12-09 16:58:30 +02:00
|
|
|
id: '17e2ba79-e22a-4cbc-bf91-d327643f4a7e',
|
2019-02-22 04:46:58 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
noSuchUser: {
|
|
|
|
message: 'No such user.',
|
|
|
|
code: 'NO_SUCH_USER',
|
2021-12-09 16:58:30 +02:00
|
|
|
id: '11795c64-40ea-4198-b06e-3c873ed9039d',
|
2019-02-22 04:46:58 +02:00
|
|
|
},
|
|
|
|
|
2019-05-18 14:36:33 +03:00
|
|
|
noSuchGroup: {
|
|
|
|
message: 'No such group.',
|
|
|
|
code: 'NO_SUCH_GROUP',
|
2021-12-09 16:58:30 +02:00
|
|
|
id: 'c94e2a5d-06aa-4914-8fa6-6a42e73d6537',
|
2019-05-18 14:36:33 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
groupAccessDenied: {
|
|
|
|
message: 'You can not send messages to groups that you have not joined.',
|
|
|
|
code: 'GROUP_ACCESS_DENIED',
|
2021-12-09 16:58:30 +02:00
|
|
|
id: 'd96b3cca-5ad1-438b-ad8b-02f931308fbd',
|
2019-05-18 14:36:33 +03:00
|
|
|
},
|
|
|
|
|
2019-02-22 04:46:58 +02:00
|
|
|
noSuchFile: {
|
|
|
|
message: 'No such file.',
|
|
|
|
code: 'NO_SUCH_FILE',
|
2021-12-09 16:58:30 +02:00
|
|
|
id: '4372b8e2-185d-4146-8749-2f68864a3e5f',
|
2019-02-22 04:46:58 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
contentRequired: {
|
|
|
|
message: 'Content required. You need to set text or fileId.',
|
|
|
|
code: 'CONTENT_REQUIRED',
|
2021-12-09 16:58:30 +02:00
|
|
|
id: '25587321-b0e6-449c-9239-f8925092942c',
|
2021-08-17 15:48:59 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
youHaveBeenBlocked: {
|
|
|
|
message: 'You cannot send a message because you have been blocked by this user.',
|
|
|
|
code: 'YOU_HAVE_BEEN_BLOCKED',
|
2021-12-09 16:58:30 +02:00
|
|
|
id: 'c15a5199-7422-4968-941a-2a462c478f7d',
|
2021-08-17 15:48:59 +03:00
|
|
|
},
|
2021-12-09 16:58:30 +02:00
|
|
|
},
|
2022-01-18 15:27:10 +02:00
|
|
|
} as const;
|
2018-07-16 22:36:44 +03:00
|
|
|
|
2022-02-20 06:15:40 +02:00
|
|
|
export const paramDef = {
|
2022-02-19 07:05:32 +02:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
userId: { type: 'string', format: 'misskey:id' },
|
|
|
|
groupId: { type: 'string', format: 'misskey:id' },
|
|
|
|
text: { type: 'string', nullable: true, maxLength: 3000 },
|
|
|
|
fileId: { type: 'string', format: 'misskey:id' },
|
|
|
|
},
|
|
|
|
required: [],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 19:12:50 +02:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 07:05:32 +02:00
|
|
|
export default define(meta, paramDef, async (ps, user) => {
|
2019-05-18 14:36:33 +03:00
|
|
|
let recipientUser: User | undefined;
|
|
|
|
let recipientGroup: UserGroup | undefined;
|
2017-03-01 07:43:41 +02:00
|
|
|
|
2019-05-18 14:36:33 +03:00
|
|
|
if (ps.userId != null) {
|
|
|
|
// Myself
|
|
|
|
if (ps.userId === user.id) {
|
|
|
|
throw new ApiError(meta.errors.recipientIsYourself);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
});
|
2021-08-17 15:48:59 +03:00
|
|
|
|
|
|
|
// Check blocking
|
|
|
|
const block = await Blockings.findOne({
|
|
|
|
blockerId: recipientUser.id,
|
|
|
|
blockeeId: user.id,
|
|
|
|
});
|
|
|
|
if (block) {
|
|
|
|
throw new ApiError(meta.errors.youHaveBeenBlocked);
|
|
|
|
}
|
2019-05-18 14:36:33 +03:00
|
|
|
} else if (ps.groupId != null) {
|
|
|
|
// Fetch recipient (group)
|
|
|
|
recipientGroup = await UserGroups.findOne(ps.groupId);
|
|
|
|
|
|
|
|
if (recipientGroup == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check joined
|
|
|
|
const joining = await UserGroupJoinings.findOne({
|
|
|
|
userId: user.id,
|
2021-12-09 16:58:30 +02:00
|
|
|
userGroupId: recipientGroup.id,
|
2019-05-18 14:36:33 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (joining == null) {
|
|
|
|
throw new ApiError(meta.errors.groupAccessDenied);
|
|
|
|
}
|
|
|
|
}
|
2017-03-01 07:43:41 +02:00
|
|
|
|
2017-03-03 01:24:48 +02:00
|
|
|
let file = null;
|
2018-11-01 20:32:24 +02:00
|
|
|
if (ps.fileId != null) {
|
2019-04-07 15:50:36 +03:00
|
|
|
file = await DriveFiles.findOne({
|
|
|
|
id: ps.fileId,
|
2021-12-09 16:58:30 +02:00
|
|
|
userId: user.id,
|
2016-12-29 00:49:51 +02:00
|
|
|
});
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
if (file == null) {
|
2019-02-22 04:46:58 +02:00
|
|
|
throw new ApiError(meta.errors.noSuchFile);
|
2016-12-29 00:49:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// テキストが無いかつ添付ファイルも無かったらエラー
|
2018-11-01 20:32:24 +02:00
|
|
|
if (ps.text == null && file == null) {
|
2019-02-22 04:46:58 +02:00
|
|
|
throw new ApiError(meta.errors.contentRequired);
|
2016-12-29 00:49:51 +02:00
|
|
|
}
|
|
|
|
|
2019-10-28 23:01:14 +02:00
|
|
|
return await createMessage(user, recipientUser, recipientGroup, ps.text, file);
|
2019-02-22 04:46:58 +02:00
|
|
|
});
|