-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
452 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,7 @@ model Server { | |
members Json[] | ||
channels Json[] | ||
roles Json[] | ||
bannedMembers Json[] | ||
} | ||
|
||
model Channel { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.