なんかもうめっちゃ変えた

This commit is contained in:
syuilo
2022-09-18 03:27:08 +09:00
committed by GitHub
parent d9ab03f086
commit b75184ec8e
946 changed files with 41219 additions and 28839 deletions

View File

@@ -1,7 +1,10 @@
import define from '../../define.js';
import { MessagingMessage } from '@/models/entities/messaging-message.js';
import { MessagingMessages, Mutings, UserGroupJoinings } from '@/models/index.js';
import { Inject, Injectable } from '@nestjs/common';
import { Brackets } from 'typeorm';
import { Endpoint } from '@/server/api/endpoint-base.js';
import type { MessagingMessage } from '@/models/entities/MessagingMessage.js';
import { MutingsRepository, UserGroupJoiningsRepository, MessagingMessagesRepository } from '@/models/index.js';
import { MessagingMessageEntityService } from '@/core/entities/MessagingMessageEntityService.js';
import { DI } from '@/di-symbols.js';
export const meta = {
tags: ['messaging'],
@@ -31,61 +34,77 @@ export const paramDef = {
} as const;
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => {
const mute = await Mutings.findBy({
muterId: user.id,
});
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> {
constructor(
@Inject(DI.messagingMessagesRepository)
private messagingMessagesRepository: MessagingMessagesRepository,
const groups = ps.group ? await UserGroupJoinings.findBy({
userId: user.id,
}).then(xs => xs.map(x => x.userGroupId)) : [];
@Inject(DI.mutingsRepository)
private mutingsRepository: MutingsRepository,
if (ps.group && groups.length === 0) {
return [];
@Inject(DI.userGroupJoiningsRepository)
private userGroupJoiningsRepository: UserGroupJoiningsRepository,
private messagingMessageEntityService: MessagingMessageEntityService,
) {
super(meta, paramDef, async (ps, me) => {
const mute = await this.mutingsRepository.findBy({
muterId: me.id,
});
const groups = ps.group ? await this.userGroupJoiningsRepository.findBy({
userId: me.id,
}).then(xs => xs.map(x => x.userGroupId)) : [];
if (ps.group && groups.length === 0) {
return [];
}
const history: MessagingMessage[] = [];
for (let i = 0; i < ps.limit; i++) {
const found = ps.group
? history.map(m => m.groupId!)
: history.map(m => (m.userId === me.id) ? m.recipientId! : m.userId!);
const query = this.messagingMessagesRepository.createQueryBuilder('message')
.orderBy('message.createdAt', 'DESC');
if (ps.group) {
query.where('message.groupId IN (:...groups)', { groups: groups });
if (found.length > 0) {
query.andWhere('message.groupId NOT IN (:...found)', { found: found });
}
} else {
query.where(new Brackets(qb => { qb
.where('message.userId = :userId', { userId: me.id })
.orWhere('message.recipientId = :userId', { userId: me.id });
}));
query.andWhere('message.groupId IS NULL');
if (found.length > 0) {
query.andWhere('message.userId NOT IN (:...found)', { found: found });
query.andWhere('message.recipientId NOT IN (:...found)', { found: found });
}
if (mute.length > 0) {
query.andWhere('message.userId NOT IN (:...mute)', { mute: mute.map(m => m.muteeId) });
query.andWhere('message.recipientId NOT IN (:...mute)', { mute: mute.map(m => m.muteeId) });
}
}
const message = await query.getOne();
if (message) {
history.push(message);
} else {
break;
}
}
return await Promise.all(history.map(h => this.messagingMessageEntityService.pack(h.id, me)));
});
}
const history: MessagingMessage[] = [];
for (let i = 0; i < ps.limit; i++) {
const found = ps.group
? history.map(m => m.groupId!)
: history.map(m => (m.userId === user.id) ? m.recipientId! : m.userId!);
const query = MessagingMessages.createQueryBuilder('message')
.orderBy('message.createdAt', 'DESC');
if (ps.group) {
query.where(`message.groupId IN (:...groups)`, { groups: groups });
if (found.length > 0) {
query.andWhere(`message.groupId NOT IN (:...found)`, { found: found });
}
} else {
query.where(new Brackets(qb => { qb
.where(`message.userId = :userId`, { userId: user.id })
.orWhere(`message.recipientId = :userId`, { userId: user.id });
}));
query.andWhere(`message.groupId IS NULL`);
if (found.length > 0) {
query.andWhere(`message.userId NOT IN (:...found)`, { found: found });
query.andWhere(`message.recipientId NOT IN (:...found)`, { found: found });
}
if (mute.length > 0) {
query.andWhere(`message.userId NOT IN (:...mute)`, { mute: mute.map(m => m.muteeId) });
query.andWhere(`message.recipientId NOT IN (:...mute)`, { mute: mute.map(m => m.muteeId) });
}
}
const message = await query.getOne();
if (message) {
history.push(message);
} else {
break;
}
}
return await Promise.all(history.map(h => MessagingMessages.pack(h.id, user)));
});
}

View File

@@ -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,
})));
}
});
}

View File

@@ -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);
});
}

View 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);
});
}

View File

@@ -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;
});
}
});
}
});
}