mirror of
https://github.com/ENSL/ensl_gathers.git
synced 2025-01-19 08:01:07 +00:00
Basic events implementation
This commit is contained in:
parent
739ad07559
commit
787c663b5f
6 changed files with 80 additions and 3 deletions
|
@ -7,6 +7,7 @@ var EnslClient = require("../lib/ensl/client");
|
|||
var chatController = require("../lib/chat/controller");
|
||||
var gatherController = require("../lib/gather/controller");
|
||||
var userController = require("../lib/user/controller");
|
||||
var eventController = require("../lib/event/controller");
|
||||
var usersHelper = require("../lib/user/helper");
|
||||
var env = process.env.NODE_ENV || "development";
|
||||
var parseCookies = EnslClient.parseCookies;
|
||||
|
@ -65,4 +66,5 @@ module.exports = io => {
|
|||
userController(rootNamespace);
|
||||
chatController(rootNamespace);
|
||||
gatherController(rootNamespace);
|
||||
eventController(rootNamespace);
|
||||
};
|
||||
|
|
21
lib/event/controller.js
Normal file
21
lib/event/controller.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
/*
|
||||
* Events Controller
|
||||
*
|
||||
* Server API
|
||||
* event:append - New event to be added to history
|
||||
*
|
||||
*/
|
||||
|
||||
const winston = require("winston");
|
||||
const pubsub = require("./pubsub.js");
|
||||
|
||||
module.exports = namespace => {
|
||||
pubsub.on("newEvent", event => {
|
||||
if (!event.public) return;
|
||||
namespace.emit("event:append", {
|
||||
type: event.type,
|
||||
description: event.description,
|
||||
createdAt: event.createdAt
|
||||
})
|
||||
});
|
||||
};
|
3
lib/event/pubsub.js
Normal file
3
lib/event/pubsub.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
const EventEmitter = require("events").EventEmitter;
|
||||
|
||||
module.exports = new EventEmitter();
|
|
@ -22,6 +22,7 @@ var Server = require("./server");
|
|||
var mongoose = require("mongoose");
|
||||
var Gather = require("./gather_singleton");
|
||||
var ArchivedGather = mongoose.model("ArchivedGather");
|
||||
var Event = mongoose.model("Event");
|
||||
var _ = require("lodash");
|
||||
var winston = require("winston");
|
||||
|
||||
|
@ -83,7 +84,7 @@ module.exports = function (namespace) {
|
|||
socket.on("gather:join", function (data) {
|
||||
let gather = Gather.current;
|
||||
if (gather.can("addGatherer")) gather.addGatherer(socket._user);
|
||||
winston.info("Gather Joiner", JSON.stringify(socket._user));
|
||||
Event.joiner(socket._user);
|
||||
refreshGather();
|
||||
});
|
||||
|
||||
|
@ -99,7 +100,7 @@ module.exports = function (namespace) {
|
|||
let gather = Gather.current;
|
||||
if (gather.can("removeGatherer")) gather.removeGatherer(user);
|
||||
if (user.cooldown) gather.applyCooldown(user);
|
||||
winston.info("Gather Leaver", JSON.stringify(user));
|
||||
Event.leaver(socket._user);
|
||||
refreshGather();
|
||||
}
|
||||
|
||||
|
|
38
lib/react/event.jsx
Normal file
38
lib/react/event.jsx
Normal file
|
@ -0,0 +1,38 @@
|
|||
const Events = React.createClass({
|
||||
propTypes: {
|
||||
events: React.PropTypes.array.isRequired
|
||||
},
|
||||
|
||||
getTime(timeString) {
|
||||
return (new Date(timeString)).toTimeString().match(/^[\d:]*/)[0];
|
||||
},
|
||||
|
||||
render() {
|
||||
let events;
|
||||
if (this.props.events.length) {
|
||||
events = this.props.events.map(event => {
|
||||
return (
|
||||
<tr key={event._id}>
|
||||
<td className="col-xs-2">{this.getTime(event.createdAt)}</td>
|
||||
<td className="col-xs-10">{event.description}</td>
|
||||
</tr>
|
||||
);
|
||||
});
|
||||
} else {
|
||||
events = <tr><td>Listening for new events...</td></tr>
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="panel panel-primary">
|
||||
<div className="panel-heading">
|
||||
Recent Events
|
||||
</div>
|
||||
<table className="table table-condensed">
|
||||
<tbody>
|
||||
{events}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
});
|
|
@ -10,7 +10,8 @@ var App = React.createClass({
|
|||
}
|
||||
|
||||
return {
|
||||
updateTitle: updateTitle
|
||||
updateTitle: updateTitle,
|
||||
events: []
|
||||
};
|
||||
},
|
||||
|
||||
|
@ -83,6 +84,15 @@ var App = React.createClass({
|
|||
}
|
||||
});
|
||||
|
||||
socket.on('event:append', data => {
|
||||
console.log(data)
|
||||
let events = self.state.events;
|
||||
events.unshift(data);
|
||||
self.setState({
|
||||
events: events.slice(0, 20)
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('users:update',
|
||||
data => self.setProps({
|
||||
users: data.users,
|
||||
|
@ -185,6 +195,8 @@ var App = React.createClass({
|
|||
<Chatroom
|
||||
messages={this.props.messages}
|
||||
user={this.props.user} />
|
||||
<Events
|
||||
events={this.state.events} />
|
||||
</div>
|
||||
<div className="col-md-6" id="gathers">
|
||||
<Gather
|
||||
|
|
Loading…
Reference in a new issue