Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Update some formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
tlaverdure committed May 2, 2020
1 parent e2ecf40 commit 2ff389c
Showing 1 changed file with 83 additions and 68 deletions.
151 changes: 83 additions & 68 deletions src/channels/presence-channel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var _ = require('lodash');
import { Channel } from './channel';
import { Database } from './../database';
import { Log } from './../log';
var _ = require("lodash");

export class PresenceChannel {
/**
Expand All @@ -20,25 +19,32 @@ export class PresenceChannel {
* Get the members of a presence channel.
*/
getMembers(channel: string): Promise<any> {
return this.db.get(channel + ':members');
return this.db.get(channel + ":members");
}

/**
* Check if a user is on a presence channel.
*/
isMember(channel: string, member: any): Promise<boolean> {
return new Promise((resolve, reject) => {
this.getMembers(channel).then(members => {
this.removeInactive(channel, members, member).then((members: any) => {
let search = members.filter(m => m.user_id == member.user_id);

if (search && search.length) {
resolve(true);
}

resolve(false);
});
}, error => Log.error(error));
this.getMembers(channel).then(
(members) => {
this.removeInactive(channel, members, member).then(
(members: any) => {
let search = members.filter(
(m) => m.user_id == member.user_id
);

if (search && search.length) {
resolve(true);
}

resolve(false);
}
);
},
(error) => Log.error(error)
);
});
}

Expand All @@ -47,16 +53,19 @@ export class PresenceChannel {
*/
removeInactive(channel: string, members: any[], member: any): Promise<any> {
return new Promise((resolve, reject) => {
this.io.of('/').in(channel).clients((error, clients) => {
members = members || [];
members = members.filter(member => {
return clients.indexOf(member.socketId) >= 0;
this.io
.of("/")
.in(channel)
.clients((error, clients) => {
members = members || [];
members = members.filter((member) => {
return clients.indexOf(member.socketId) >= 0;
});

this.db.set(channel + ":members", members);

resolve(members);
});

this.db.set(channel + ':members', members);

resolve(members);
});
});
}

Expand All @@ -67,81 +76,87 @@ export class PresenceChannel {
join(socket: any, channel: string, member: any) {
if (!member) {
if (this.options.devMode) {
Log.error('Unable to join channel. Member data for presence channel missing');
Log.error(
"Unable to join channel. Member data for presence channel missing"
);
}

return;
}

this.isMember(channel, member).then(is_member => {
this.getMembers(channel).then(members => {
members = members || [];
member.socketId = socket.id;
members.push(member);

this.db.set(channel + ':members', members);

members = _.uniqBy(members.reverse(), 'user_id');

this.onSubscribed(socket, channel, members);

if (!is_member) {
this.onJoin(socket, channel, member);
}
}, error => Log.error(error));
}, () => {
Log.error('Error retrieving pressence channel members.');
});
this.isMember(channel, member).then(
(is_member) => {
this.getMembers(channel).then(
(members) => {
members = members || [];
member.socketId = socket.id;
members.push(member);

this.db.set(channel + ":members", members);

members = _.uniqBy(members.reverse(), "user_id");

this.onSubscribed(socket, channel, members);

if (!is_member) {
this.onJoin(socket, channel, member);
}
},
(error) => Log.error(error)
);
},
() => {
Log.error("Error retrieving pressence channel members.");
}
);
}

/**
* Remove a member from a presenece channel and broadcast they have left
* only if not other presence channel instances exist.
*/
leave(socket: any, channel: string): void {
this.getMembers(channel).then(members => {
members = members || [];
let member = members.find(member => member.socketId == socket.id);
members = members.filter(m => m.socketId != member.socketId);

this.db.set(channel + ':members', members);

this.isMember(channel, member).then(is_member => {
if (!is_member) {
delete member.socketId;
this.onLeave(channel, member);
}
});
}, error => Log.error(error));
this.getMembers(channel).then(
(members) => {
members = members || [];
let member = members.find(
(member) => member.socketId == socket.id
);
members = members.filter((m) => m.socketId != member.socketId);

this.db.set(channel + ":members", members);

this.isMember(channel, member).then((is_member) => {
if (!is_member) {
delete member.socketId;
this.onLeave(channel, member);
}
});
},
(error) => Log.error(error)
);
}

/**
* On join event handler.
*/
onJoin(socket: any, channel: string, member: any): void {
this.io
.sockets
.connected[socket.id]
.broadcast
this.io.sockets.connected[socket.id].broadcast
.to(channel)
.emit('presence:joining', channel, member);
.emit("presence:joining", channel, member);
}

/**
* On leave emitter.
*/
onLeave(channel: string, member: any): void {
this.io
.to(channel)
.emit('presence:leaving', channel, member);
this.io.to(channel).emit("presence:leaving", channel, member);
}

/**
* On subscribed event emitter.
*/
onSubscribed(socket: any, channel: string, members: any[]) {
this.io
.to(socket.id)
.emit('presence:subscribed', channel, members);
this.io.to(socket.id).emit("presence:subscribed", channel, members);
}
}

0 comments on commit 2ff389c

Please sign in to comment.