Skip to content

Commit

Permalink
Merge pull request #1 from hanzydev/main
Browse files Browse the repository at this point in the history
refactor: improvements
  • Loading branch information
hanzydev authored May 3, 2024
2 parents 1647b4d + e6054fb commit 7bb4c07
Show file tree
Hide file tree
Showing 12 changed files with 5 additions and 44 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"main": "dist/index.js",
"type": "module",
"scripts": {
"start": "node scripts/start.js --no-warnings",
"start": "node . --no-warnings",
"dev": "tsup && pnpm start",
"dev:watch": "tsup --watch --onSuccess \"pnpm start\"",
"build": "tsup",
Expand Down
6 changes: 0 additions & 6 deletions scripts/start.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/events/ban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@ import { CoreParticipant, Participant } from './login';
export default class BanOrUnbanParticipant {
async handle({ socket, io, data }: { socket: Socket; io: Server; data: any }) {
const targetUserId = data?.target;

if (!targetUserId) return;

const rooms = Array.from(socket.rooms);

const room = rooms[1];

if (!room) return;

const socketId = socket.id;

const socketRoomParticipants = (await get(`room:${room}:users`)) as Participant[];

if (socketRoomParticipants) {
Expand Down
3 changes: 0 additions & 3 deletions src/events/disconnecting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import { del, delWithPattern, get, set } from '@utils/cache';
export default class Disconnect {
async handle({ socket, io }: { socket: Socket; io: Server }) {
const rooms = Array.from(socket.rooms);

const room = rooms[1];

if (!room) return;

const socketId = socket.id;

const socketRoomParticipants = await get(`room:${room}:users`);

if (socketRoomParticipants) {
Expand All @@ -25,7 +23,6 @@ export default class Disconnect {
}

const remainingParticipants = io.sockets.adapter.rooms.get(room);

if (!remainingParticipants) {
await delWithPattern(`room:${room}:*`);
await del(`sid:${socketId}`);
Expand Down
3 changes: 0 additions & 3 deletions src/events/kick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@ import { Participant } from './login';
export default class KickParticipant {
async handle({ socket, io, data }: { socket: Socket; io: Server; data: any }) {
const targetUserId = data?.target;

if (!targetUserId) return;

const rooms = Array.from(socket.rooms);

const room = rooms[1];

if (!room) return;

const socketId = socket.id;

const socketRoomParticipants = (await get(`room:${room}:users`)) as Participant[];

if (socketRoomParticipants) {
Expand Down
8 changes: 2 additions & 6 deletions src/events/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,21 @@ export default class Login {
password = password.trim();
room = room.trim();

const user = await fetch(`${process.env.API_URL}/user`, {
const user = (await fetch(`${process.env.API_URL}/user`, {
headers: {
Authorization: token,
},
});
})) as any;

const json = await user.json();

if (!json?.id) return callback({ error: 'Kullanıcı verisi alınamadı' });

const roomParticipants = await get(`${prefix}:users`);

if (roomParticipants && roomParticipants.find((user) => user.id == json.id)) {
return callback({ error: 'Zaten bu odadasın' });
}

const getPass = await get(`${prefix}:password`);

if (getPass && getPass != password) {
return callback({ error: 'Yanlış şifre' });
}
Expand All @@ -89,7 +86,6 @@ export default class Login {
[]) as CoreParticipant[];

const banned = bannedParticipants.find((x) => x.id == json.id);

if (banned) {
return callback({ error: 'Bu odadan yasaklandınız' });
}
Expand Down
2 changes: 0 additions & 2 deletions src/events/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ export default class CreateMessage {
data.message.trim().length <= 250
) {
const rooms = Array.from(socket.rooms);

const room = rooms[1];

if (!room) return;

const socketRoomParticipants = await get(`room:${room}:users`);

const author = socketRoomParticipants.find((user) => user.sid == socket.id);

if (!author) return;
Expand Down
3 changes: 0 additions & 3 deletions src/events/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ import { Participant } from './login';
export default class MakeModeratorOrTakeModerator {
async handle({ socket, io, data }: { socket: Socket; io: Server; data: any }) {
const targetUserId = data?.target;

if (!targetUserId) return;

const rooms = Array.from(socket.rooms);

const room = rooms[1];

if (!room) return;

const socketId = socket.id;

const socketRoomParticipants = (await get(`room:${room}:users`)) as Participant[];

if (socketRoomParticipants) {
Expand Down
6 changes: 1 addition & 5 deletions src/events/modControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@ import { Participant } from './login';
export default class ChangeModeratorControl {
async handle({ socket, io, data }: { socket: Socket; io: Server; data: any }) {
const controlledByMods = data?.controlledByMods;

if (!(controlledByMods == true || controlledByMods == false)) return;
if (typeof controlledByMods !== 'boolean') return;

const rooms = Array.from(socket.rooms);

const room = rooms[1];

if (!room) return;

const socketId = socket.id;

const socketRoomParticipants = (await get(`room:${room}:users`)) as Participant[];

const user = socketRoomParticipants.find((user) => user.sid == socketId);

if (user.owner) {
Expand Down
3 changes: 0 additions & 3 deletions src/events/mute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@ import { Participant } from './login';
export default class MuteOrUnmuteParticipant {
async handle({ socket, io, data }: { socket: Socket; io: Server; data: any }) {
const targetUserId = data?.target;

if (!targetUserId) return;

const rooms = Array.from(socket.rooms);

const room = rooms[1];

if (!room) return;

const socketId = socket.id;

const socketRoomParticipants = (await get(`room:${room}:users`)) as Participant[];

if (socketRoomParticipants) {
Expand Down
6 changes: 1 addition & 5 deletions src/events/playerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,17 @@ import { Participant } from './login';
export default class MuteOrUnmuteParticipant {
async handle({ socket, data }: { socket: Socket; data: any }) {
const playing = data?.playing;

if (!(playing == true || playing == false)) return;
if (typeof data?.playing !== 'boolean') return;

const rooms = Array.from(socket.rooms);

const room = rooms[1];

if (!room) return;

const socketId = socket.id;

let pass = false;

const modRequired = (await get(`room:${room}:controlledByMods`)) ?? false;

if (modRequired) {
const socketRoomParticipants = (await get(`room:${room}:users`)) as Participant[];

Expand Down
4 changes: 0 additions & 4 deletions src/events/playerTimestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,17 @@ import { Participant } from './login';
export default class MuteOrUnmuteParticipant {
async handle({ socket, data }: { socket: Socket; data: any }) {
const videoTimestamp = data?.timestamp;

if (isNaN(videoTimestamp)) return;

const rooms = Array.from(socket.rooms);

const room = rooms[1];

if (!room) return;

const socketId = socket.id;

let pass = false;

const modRequired = (await get(`room:${room}:controlledByMods`)) ?? false;

if (modRequired) {
const socketRoomParticipants = (await get(`room:${room}:users`)) as Participant[];

Expand Down

0 comments on commit 7bb4c07

Please sign in to comment.