-
Notifications
You must be signed in to change notification settings - Fork 0
/
mangochat.js
executable file
·55 lines (45 loc) · 1.32 KB
/
mangochat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// This code is running on both the client and server.
Messages = new Mongo.Collection("msgs");
//This code below only runs on the server.
if (Meteor.isServer) {
Meteor.publish("messages", function () {
// This code is a standard MongoDB query that limits the amount of messages to 5 for this web app.
return Messages.find({}, {sort: {createdAt: -1}, limit: 5});
});
}
// This code below only runs on the client
if (Meteor.isClient) {
Meteor.subscribe("messages");
}
Meteor.methods({
sendMessage: function (message) {
//The code does not permit anyone to make a message without signing in first.
if (! Meteor.userId()) {
throw new Meteor.Error("not-authorized");
}
Messages.insert({
text: message,
createdAt: new Date(),
username: "anonymous"
});
}
});
if (Meteor.isClient) {
Template.body.helpers({
recentMessages: function () {
return Messages.find({}, {sort: {createdAt: 1}});
}
});
Template.body.events({
"submit .new-message": function (event) {
var text = event.target.text.value;
Meteor.call("sendMessage", text);
event.target.text.value = "";
return false;
}
});
//The code below allows the user to log in with their username and password.
Accounts.ui.config({
passwordSignupFields: "USERNAME_ONLY"
});
}