Skip to content

Commit

Permalink
Add kick/banMember, bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
StamTsag committed Feb 20, 2024
1 parent 8d8bbcf commit f469fb9
Show file tree
Hide file tree
Showing 17 changed files with 452 additions and 4 deletions.
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ model Server {
members Json[]
channels Json[]
roles Json[]
bannedMembers Json[]
}

model Channel {
Expand Down
6 changes: 6 additions & 0 deletions src/events/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ import renameChannel from 'events/account/renameChannel';
import sendPost from 'events/account/sendPost';
import fetchPost from 'events/account/fetchPost';
import leaveServer from 'events/account/leaveServer';
import kickMember from 'events/account/kickMember';
import banMember from 'events/account/banMember';
import unbanMember from 'events/account/unbanMember';

import { EventExportTemplate } from 'interfaces/all';

Expand Down Expand Up @@ -100,6 +103,9 @@ const accountTemplate: EventExportTemplate = {
sendPost,
fetchPost,
leaveServer,
kickMember,
banMember,
unbanMember,
};

export default accountTemplate;
123 changes: 123 additions & 0 deletions src/events/account/banMember.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// ******************** //
// The banMember account event file.
// ******************** //

import { StringSchema } from '@ezier/validate';
import { profileIdSchema, serverIdSchema } from 'events/shared';
import {
BanMemberResult,
BanMemberServerParams,
} from 'interfaces/account/banMember';
import { EventTemplate, FronvoError } from 'interfaces/all';
import { generateError, getAccountSocketId } from 'utilities/global';
import { prismaClient } from 'variables/global';

async function banMember({
io,
socket,
account,
serverId,
profileId,
}: BanMemberServerParams): Promise<BanMemberResult | FronvoError> {
const server = await prismaClient.server.findFirst({
where: {
serverId,
},
});

if (account.profileId != server.ownerId) {
return generateError('NOT_OWNER');
}

if (!server) {
return generateError('SERVER_404');
}

if (!server.members.includes(account.profileId)) {
return generateError('NOT_IN_SERVER');
}

const targetAccount = await prismaClient.account.findFirst({
where: {
profileId,
},
});

if (!targetAccount) {
return generateError('ACCOUNT_404');
}

// TODO: Moderators
const targetSocket = io.sockets.sockets.get(getAccountSocketId(profileId));

// Get target account socket if it's online
targetSocket.join(account.profileId);

const newMembers = server.members;
newMembers.splice(newMembers.indexOf(profileId));

try {
await prismaClient.server.update({
where: {
serverId,
},

data: {
members: {
set: newMembers,
},
},
});
} catch (e) {
return generateError('UNKNOWN');
}

io.to(targetSocket.id).emit('serverDeleted', {
serverId,
});

io.to(server.serverId).emit('memberLeft', {
roomId: server.serverId,
profileId: profileId,
});

// Update bans
try {
const bans = server.bannedMembers;
bans.push(profileId);

await prismaClient.server.update({
where: {
serverId,
},

data: {
bannedMembers: {
set: bans,
},
},
});

// Only emit to owner
// TODO: Moderators aswell
io.to(socket.id).emit('memberBanned', {
serverId,
profileId,
});
} catch (e) {
return generateError('UNKNOWN');
}

return {};
}

const banMemberTemplate: EventTemplate = {
func: banMember,
template: ['serverId', 'profileId'],
schema: new StringSchema({
...serverIdSchema,
...profileIdSchema,
}),
};

export default banMemberTemplate;
4 changes: 4 additions & 0 deletions src/events/account/fetchServers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ async function fetchServers({
name: server.name,
channels: channels as [],
roles: server.roles,
bannedMembers:
account.profileId == server.ownerId
? server.bannedMembers
: [],
};

checkLoadingDone();
Expand Down
18 changes: 18 additions & 0 deletions src/events/account/joinServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ async function joinServer({
members: true,
channels: true,
roles: true,
bannedMembers: true,
},
});

Expand All @@ -49,6 +50,9 @@ async function joinServer({
return generateError('SERVER_INVITES_DISABLED');
}

if (server.bannedMembers.includes(account.profileId)) {
return generateError('SERVER_BANNED');
}
// Add to server
const server2 = await prismaClient.server.update({
where: {
Expand All @@ -60,6 +64,20 @@ async function joinServer({
push: account.profileId,
},
},

select: {
serverId: true,
ownerId: true,
name: true,
icon: true,
invite: true,
invitesDisabled: true,
creationDate: true,
members: true,
// TODO: Channel permission check when roles are done
channels: true,
roles: true,
},
});

// Channel info on the fly
Expand Down
99 changes: 99 additions & 0 deletions src/events/account/kickMember.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// ******************** //
// The kickMember account event file.
// ******************** //

import { StringSchema } from '@ezier/validate';
import { profileIdSchema, serverIdSchema } from 'events/shared';
import {
KickMemberResult,
KickMemberServerParams,
} from 'interfaces/account/kickMember';
import { EventTemplate, FronvoError } from 'interfaces/all';
import { generateError, getAccountSocketId } from 'utilities/global';
import { prismaClient } from 'variables/global';

async function kickMember({
io,
account,
serverId,
profileId,
}: KickMemberServerParams): Promise<KickMemberResult | FronvoError> {
const server = await prismaClient.server.findFirst({
where: {
serverId,
},
});

if (account.profileId != server.ownerId) {
return generateError('NOT_OWNER');
}

if (!server) {
return generateError('SERVER_404');
}

if (!server.members.includes(account.profileId)) {
return generateError('NOT_IN_SERVER');
}

const targetAccount = await prismaClient.account.findFirst({
where: {
profileId,
},
});

if (!targetAccount) {
return generateError('ACCOUNT_404');
}

// TODO: Moderators
if (server.ownerId == account.profileId) {
const targetSocket = io.sockets.sockets.get(
getAccountSocketId(profileId)
);

// Get target account socket if it's online
targetSocket.join(account.profileId);

const newMembers = server.members;
newMembers.splice(newMembers.indexOf(profileId));

try {
await prismaClient.server.update({
where: {
serverId,
},

data: {
members: {
set: newMembers,
},
},
});
} catch (e) {
return generateError('UNKNOWN');
}

io.to(targetSocket.id).emit('serverDeleted', {
serverId,
});

io.to(server.serverId).emit('memberLeft', {
roomId: server.serverId,
profileId: profileId,
});
}

return {};
}

const kickMemberTemplate: EventTemplate = {
func: kickMember,
template: ['serverId', 'profileId'],
schema: new StringSchema({
...serverIdSchema,
...profileIdSchema,
}),
};

export default kickMemberTemplate;
93 changes: 93 additions & 0 deletions src/events/account/unbanMember.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// ******************** //
// The unbanMember account event file.
// ******************** //

import { StringSchema } from '@ezier/validate';
import { profileIdSchema, serverIdSchema } from 'events/shared';
import {
UnbanMemberResult,
UnbanMemberServerParams,
} from 'interfaces/account/unbanMember';
import { EventTemplate, FronvoError } from 'interfaces/all';
import { generateError } from 'utilities/global';
import { prismaClient } from 'variables/global';

async function unbanMember({
io,
socket,
account,
serverId,
profileId,
}: UnbanMemberServerParams): Promise<UnbanMemberResult | FronvoError> {
const server = await prismaClient.server.findFirst({
where: {
serverId,
},
});

if (account.profileId != server.ownerId) {
return generateError('NOT_OWNER');
}

if (!server) {
return generateError('SERVER_404');
}

if (!server.members.includes(account.profileId)) {
return generateError('NOT_IN_SERVER');
}

const targetAccount = await prismaClient.account.findFirst({
where: {
profileId,
},
});

if (!targetAccount) {
return generateError('ACCOUNT_404');
}

if (
server.members.includes(profileId) ||
!server.bannedMembers.includes(profileId)
) {
return generateError('NOT_BANNED');
}

try {
const newBans = server.bannedMembers;
newBans.splice(newBans.indexOf(profileId), 1);

await prismaClient.server.update({
where: {
serverId,
},

data: {
bannedMembers: {
set: newBans,
},
},
});

io.to(socket.id).emit('memberUnbanned', {
serverId,
profileId,
});
} catch (e) {
return generateError('UNKNOWN');
}

return {};
}

const unbanMemberTemplate: EventTemplate = {
func: unbanMember,
template: ['serverId', 'profileId'],
schema: new StringSchema({
...serverIdSchema,
...profileIdSchema,
}),
};

export default unbanMemberTemplate;
Loading

0 comments on commit f469fb9

Please sign in to comment.