fix: improve plugins functionality with server management
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
import { DataSource } from 'typeorm';
|
||||
import { MessageEntity } from '../../../entities';
|
||||
import { ClearRoomMessagesCommand } from '../../types';
|
||||
import { getCurrentUserScope } from '../../current-user-scope';
|
||||
|
||||
export async function handleClearRoomMessages(command: ClearRoomMessagesCommand, dataSource: DataSource): Promise<void> {
|
||||
const repo = dataSource.getRepository(MessageEntity);
|
||||
const currentUserId = await getCurrentUserScope(dataSource);
|
||||
|
||||
await repo.delete({ roomId: command.payload.roomId });
|
||||
if (!currentUserId) {
|
||||
return;
|
||||
}
|
||||
|
||||
await repo.delete({ roomId: command.payload.roomId, ownerUserId: currentUserId });
|
||||
}
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
import { DataSource } from 'typeorm';
|
||||
import { MessageEntity } from '../../../entities';
|
||||
import { DeleteMessageCommand } from '../../types';
|
||||
import { getCurrentUserScope } from '../../current-user-scope';
|
||||
|
||||
export async function handleDeleteMessage(command: DeleteMessageCommand, dataSource: DataSource): Promise<void> {
|
||||
const repo = dataSource.getRepository(MessageEntity);
|
||||
const currentUserId = await getCurrentUserScope(dataSource);
|
||||
|
||||
await repo.delete({ id: command.payload.messageId });
|
||||
if (!currentUserId) {
|
||||
return;
|
||||
}
|
||||
|
||||
await repo.delete({ id: command.payload.messageId, ownerUserId: currentUserId });
|
||||
}
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
import { DataSource } from 'typeorm';
|
||||
import { getCurrentUserScope } from '../../current-user-scope';
|
||||
import { PluginDataEntity } from '../../../entities';
|
||||
import { DeletePluginDataCommand } from '../../types';
|
||||
|
||||
export async function handleDeletePluginData(command: DeletePluginDataCommand, dataSource: DataSource): Promise<void> {
|
||||
const { payload } = command;
|
||||
const ownerUserId = await getCurrentUserScope(dataSource);
|
||||
|
||||
if (!ownerUserId) {
|
||||
return;
|
||||
}
|
||||
|
||||
await dataSource.getRepository(PluginDataEntity).delete({
|
||||
key: payload.key,
|
||||
ownerUserId,
|
||||
pluginId: payload.pluginId,
|
||||
scope: payload.scope,
|
||||
serverId: payload.serverId ?? ''
|
||||
|
||||
@@ -3,23 +3,39 @@ import {
|
||||
RoomChannelPermissionEntity,
|
||||
RoomChannelEntity,
|
||||
RoomEntity,
|
||||
RoomOwnerEntity,
|
||||
RoomMemberEntity,
|
||||
RoomRoleEntity,
|
||||
RoomUserRoleEntity,
|
||||
MessageEntity
|
||||
} from '../../../entities';
|
||||
import { DeleteRoomCommand } from '../../types';
|
||||
import { getCurrentUserScope } from '../../current-user-scope';
|
||||
|
||||
export async function handleDeleteRoom(command: DeleteRoomCommand, dataSource: DataSource): Promise<void> {
|
||||
const { roomId } = command.payload;
|
||||
|
||||
await dataSource.transaction(async (manager) => {
|
||||
const currentUserId = await getCurrentUserScope(manager);
|
||||
|
||||
if (!currentUserId) {
|
||||
return;
|
||||
}
|
||||
|
||||
await manager.getRepository(RoomOwnerEntity).delete({ roomId, userId: currentUserId });
|
||||
await manager.getRepository(MessageEntity).delete({ roomId, ownerUserId: currentUserId });
|
||||
|
||||
const remainingOwners = await manager.getRepository(RoomOwnerEntity).count({ where: { roomId } });
|
||||
|
||||
if (remainingOwners > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
await manager.getRepository(RoomChannelPermissionEntity).delete({ roomId });
|
||||
await manager.getRepository(RoomChannelEntity).delete({ roomId });
|
||||
await manager.getRepository(RoomMemberEntity).delete({ roomId });
|
||||
await manager.getRepository(RoomRoleEntity).delete({ roomId });
|
||||
await manager.getRepository(RoomUserRoleEntity).delete({ roomId });
|
||||
await manager.getRepository(RoomEntity).delete({ id: roomId });
|
||||
await manager.getRepository(MessageEntity).delete({ roomId });
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,15 +2,18 @@ import { DataSource } from 'typeorm';
|
||||
import { MessageEntity } from '../../../entities';
|
||||
import { replaceMessageReactions } from '../../relations';
|
||||
import { SaveMessageCommand } from '../../types';
|
||||
import { getCurrentUserScope } from '../../current-user-scope';
|
||||
|
||||
export async function handleSaveMessage(command: SaveMessageCommand, dataSource: DataSource): Promise<void> {
|
||||
const { message } = command.payload;
|
||||
|
||||
await dataSource.transaction(async (manager) => {
|
||||
const currentUserId = await getCurrentUserScope(manager);
|
||||
const repo = manager.getRepository(MessageEntity);
|
||||
const entity = repo.create({
|
||||
id: message.id,
|
||||
roomId: message.roomId,
|
||||
ownerUserId: currentUserId,
|
||||
channelId: message.channelId ?? null,
|
||||
senderId: message.senderId,
|
||||
senderName: message.senderName,
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
import { DataSource } from 'typeorm';
|
||||
import { getCurrentUserScope } from '../../current-user-scope';
|
||||
import { PluginDataEntity } from '../../../entities';
|
||||
import { SavePluginDataCommand } from '../../types';
|
||||
|
||||
export async function handleSavePluginData(command: SavePluginDataCommand, dataSource: DataSource): Promise<void> {
|
||||
const { payload } = command;
|
||||
const ownerUserId = await getCurrentUserScope(dataSource);
|
||||
|
||||
if (!ownerUserId) {
|
||||
return;
|
||||
}
|
||||
|
||||
await dataSource.getRepository(PluginDataEntity).save({
|
||||
key: payload.key,
|
||||
ownerUserId,
|
||||
pluginId: payload.pluginId,
|
||||
scope: payload.scope,
|
||||
serverId: payload.serverId ?? '',
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { DataSource } from 'typeorm';
|
||||
import { RoomEntity } from '../../../entities';
|
||||
import { RoomEntity, RoomOwnerEntity } from '../../../entities';
|
||||
import { replaceRoomRelations } from '../../relations';
|
||||
import { SaveRoomCommand } from '../../types';
|
||||
import { getCurrentUserScope } from '../../current-user-scope';
|
||||
|
||||
function extractSlowModeInterval(room: SaveRoomCommand['payload']['room']): number {
|
||||
if (typeof room.slowModeInterval === 'number' && Number.isFinite(room.slowModeInterval)) {
|
||||
@@ -21,6 +22,7 @@ export async function handleSaveRoom(command: SaveRoomCommand, dataSource: DataS
|
||||
const { room } = command.payload;
|
||||
|
||||
await dataSource.transaction(async (manager) => {
|
||||
const currentUserId = await getCurrentUserScope(manager);
|
||||
const repo = manager.getRepository(RoomEntity);
|
||||
const entity = repo.create({
|
||||
id: room.id,
|
||||
@@ -43,6 +45,15 @@ export async function handleSaveRoom(command: SaveRoomCommand, dataSource: DataS
|
||||
});
|
||||
|
||||
await repo.save(entity);
|
||||
|
||||
if (currentUserId) {
|
||||
await manager.getRepository(RoomOwnerEntity).save({
|
||||
roomId: room.id,
|
||||
userId: currentUserId,
|
||||
savedAt: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
await replaceRoomRelations(manager, room.id, {
|
||||
channels: room.channels ?? [],
|
||||
members: room.members ?? [],
|
||||
|
||||
@@ -2,13 +2,20 @@ import { DataSource } from 'typeorm';
|
||||
import { MessageEntity } from '../../../entities';
|
||||
import { replaceMessageReactions } from '../../relations';
|
||||
import { UpdateMessageCommand } from '../../types';
|
||||
import { getCurrentUserScope } from '../../current-user-scope';
|
||||
|
||||
export async function handleUpdateMessage(command: UpdateMessageCommand, dataSource: DataSource): Promise<void> {
|
||||
const { messageId, updates } = command.payload;
|
||||
|
||||
await dataSource.transaction(async (manager) => {
|
||||
const currentUserId = await getCurrentUserScope(manager);
|
||||
|
||||
if (!currentUserId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const repo = manager.getRepository(MessageEntity);
|
||||
const existing = await repo.findOne({ where: { id: messageId } });
|
||||
const existing = await repo.findOne({ where: { id: messageId, ownerUserId: currentUserId } });
|
||||
|
||||
if (!existing)
|
||||
return;
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
boolToInt,
|
||||
TransformMap
|
||||
} from './utils/applyUpdates';
|
||||
import { getCurrentUserScope, userOwnsRoom } from '../../current-user-scope';
|
||||
|
||||
const ROOM_TRANSFORMS: TransformMap = {
|
||||
hasPassword: boolToInt,
|
||||
@@ -32,6 +33,12 @@ export async function handleUpdateRoom(command: UpdateRoomCommand, dataSource: D
|
||||
const { roomId, updates } = command.payload;
|
||||
|
||||
await dataSource.transaction(async (manager) => {
|
||||
const currentUserId = await getCurrentUserScope(manager);
|
||||
|
||||
if (!await userOwnsRoom(manager, roomId, currentUserId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const repo = manager.getRepository(RoomEntity);
|
||||
const existing = await repo.findOne({ where: { id: roomId } });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user