forked from dasniko/meteor-chat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
meteor-chat.js
116 lines (103 loc) · 2.49 KB
/
meteor-chat.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
Messages = new Meteor.Collection("messages");
Rooms = new Meteor.Collection("rooms");
if (Meteor.isClient) {
Accounts.ui.config({
password#Fields: 'USERNAME_ONLY'
});
Meteor.subscribe("rooms");
Meteor.subscribe("messages");
Session.setDefault("roomname", "Meteor");
Template.input.events({
'click .sendMsg': function(e) {
_sendMessage();
},
'keyup #msg': function(e) {
if (e.type == "keyup" && e.which == 13) {
_sendMessage();
}
}
});
_sendMessage = function() {
var el = document.getElementById("msg");
Messages.insert({user: Meteor.user().username, msg: el.value, ts: new Date(), room: Session.get("roomname")});
el.value = "";
el.focus();
};
Template.messages.helpers({
messages: function() {
return Messages.find({room: Session.get("roomname")}, {sort: {ts: -1}});
},
roomname: function() {
return Session.get("roomname");
}
});
Template.message.helpers({
timestamp: function() {
return this.ts.toLocaleString();
}
});
Template.rooms.events({
'click li': function(e) {
Session.set("roomname", e.target.innerText);
}
});
Template.rooms.helpers({
rooms: function() {
return Rooms.find();
}
});
Template.room.helpers({
roomstyle: function() {
return Session.equals("roomname", this.roomname) ? "font-weight: bold" : "";
}
});
Template.chat.helpers({
release: function() {
return Meteor.release;
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
Messages.remove({});
Rooms.remove({});
if (Rooms.find().count() === 0) {
["Meteor", "JavaScript", "Reactive", "MongoDB"].forEach(function(r) {
Rooms.insert({roomname: r});
});
}
});
Rooms.deny({
insert: function (userId, doc) {
return true;
},
update: function (userId, doc, fieldNames, modifier) {
return true;
},
remove: function (userId, doc) {
return true;
}
});
Messages.deny({
insert: function (userId, doc) {
return (userId === null);
},
update: function (userId, doc, fieldNames, modifier) {
return true;
},
remove: function (userId, doc) {
return true;
}
});
Messages.allow({
insert: function (userId, doc) {
return (userId !== null);
}
});
Meteor.publish("rooms", function () {
return Rooms.find();
});
Meteor.publish("messages", function () {
return Messages.find({}, {sort: {ts: -1}});
});
}