diff --git a/models/message.js b/models/message.js index f7e5e35..a3dc664 100644 --- a/models/message.js +++ b/models/message.js @@ -10,15 +10,26 @@ Chatter.Message = ChatterMessage = Astro.Class({ fields: { userId: { - type: "string" + type: "string", + validator: [ + reqStrNotNull + ] }, roomId: { - type: "string" + type: "string", + validator: [ + reqStrNotNull + ] }, message: { - type: "string" + type: "string", + validator: [ + Validators.minLength(1, 'The message must not be empty!'), + Validators.maxLength(1000), + reqStrNotNull + ] }, nickname: { @@ -30,22 +41,6 @@ Chatter.Message = ChatterMessage = Astro.Class({ } }, - validators: { - userId: [ - reqStrNotNull - ], - - roomId: [ - reqStrNotNull - ], - - message: [ - Validators.minLength(1, 'The message must not be empty!'), - Validators.maxLength(1000), - reqStrNotNull - ] - }, - events: { beforeSave: function(e) { increaseCounter(this); diff --git a/models/room.js b/models/room.js index 903907a..4a02904 100644 --- a/models/room.js +++ b/models/room.js @@ -3,9 +3,23 @@ Chatter.Room = ChatterRoom = Astro.Class({ collection: new Mongo.Collection("chatterroom"), fields: { - name: "string", + name: { + type: "string", + validator: [ + Validators.required(), + Validators.minLength(1), + Validators.maxLength(30) + ] + }, - description: "string", + description: { + type: "string", + validator: [ + Validators.required(), + Validators.minLength(1), + Validators.maxLength(150) + ] + }, roomType: { type: "string" @@ -18,30 +32,9 @@ Chatter.Room = ChatterRoom = Astro.Class({ } }, - archived: { - type: "boolean", - default: function() { - return false; - } - }, - createdBy: "string" }, - validators: { - name: [ - Validators.required(), - Validators.minLength(1), - Validators.maxLength(30) - ], - - description: [ - Validators.required(), - Validators.minLength(1), - Validators.maxLength(150) - ] - }, - events: { }, diff --git a/models/userroom.js b/models/userroom.js index 1ec5a7c..7d58551 100644 --- a/models/userroom.js +++ b/models/userroom.js @@ -16,7 +16,14 @@ Chatter.UserRoom = ChatterUserRoom = Astro.Class({ default: function() { return 0; } - } + }, + + archived: { + type: "boolean", + default: function() { + return false; + } + }, }, validators: { diff --git a/server-tests/methods.tests.js b/server-tests/methods.tests.js index 0c231c2..62e17c6 100644 --- a/server-tests/methods.tests.js +++ b/server-tests/methods.tests.js @@ -9,6 +9,7 @@ const callbackWrapper = function(fn) { describe("chatter meteor methods", function() { let room; let user; + let userRoom; const assert = chai.assert; const roomAttributes = { @@ -20,7 +21,7 @@ describe("chatter meteor methods", function() { user = Meteor.users.findOne(); const roomId = new Chatter.Room({name: "test_room" }).save(); room = Chatter.Room.findOne(roomId) - const userRoom = new Chatter.UserRoom({userId: user._id, roomId: roomId }).save(); + userRoom = new Chatter.UserRoom({userId: user._id, roomId: roomId }).save(); // Simulate user has been added to chatter and has made admin stubs.findOne.returns({ _id: "meteor_user_one_id", @@ -143,6 +144,74 @@ describe("chatter meteor methods", function() { })); }); }); + }); + + describe("userRoom methods", function() { + + describe("room.join method", function() { + + describe("when parameters are missing or wrong", function() { + + it("returns exception when parameters are missing ", function(done) { + const params = {}; + Meteor.call("room.join", params, callbackWrapper((error, response) => { + assert.isUndefined(response); + assert.equal(error.errorType, "Match.Error"); + })); + + params.roomId = room._id; + + Meteor.call("room.join", params, callbackWrapper((error, response) => { + assert.isUndefined(response); + assert.equal(error.errorType, "Match.Error"); + done(); + })); + }); + + it("returns 'room does not exist' error when roomId does not exist", function(done) { + const params = {}; + params.roomId = "non existent roomId"; + params.invitees = [user._id]; + + Meteor.call("room.join", params, callbackWrapper((error, response) => { + assert.isUndefined(response); + assert.equal(error.error, "non-existing-room"); + done(); + })); + }); + + it("returns 'user does not exist' error when userId does not exist", function(done) { + const params = {}; + params.invitees = ["non existent userId"]; + params.roomId = room._id; + + Meteor.call("room.join", params, callbackWrapper((error, response) => { + assert.isUndefined(response); + assert.equal(error.error, "non-existing-users"); + done(); + })); + }); + }); + + describe("when no parameters are missing or wrong", function() { + + it("creates a userRoom instance", function(done) { + const params = {}; + params.invitees = [user._id]; + params.roomId = room._id; + + Meteor.call("room.join", params, callbackWrapper((error, response) => { + assert.isUndefined(error); + assert.isString(response); + done(); + })); + }); + + it("userRoom is built with archived set to default", function() { + const ur = assert.equal(Chatter.UserRoom.findOne({userId: user._id, roomId: room._id}).archived, false); + }); + }); + }); describe("room.archive", function() { @@ -171,28 +240,27 @@ describe("chatter meteor methods", function() { })); }); - it("throws an error when roomId does not exist", function(done) { + it("throws an error when user is not in room does not exist", function(done) { const params = { roomId: "non-existing-room", + userId: user._id, archived: true }; Meteor.call("room.archive", params, callbackWrapper((error, response) => { assert.isUndefined(response); - assert.equal(error.error, "non-existing-room"); + assert.equal(error.error, "user-not-in-room"); done(); })); }); - it("room is built with archived set to default", function() { - assert.equal(room.archived, false); - }); - it("returns true after succesfully calling method with true parameter", function(done) { const params = { archived: true, - roomId: room._id + roomId: room._id, + userId: user._id }; + Meteor.call("room.archive", params, callbackWrapper((error, response) => { assert.isUndefined(error); assert.equal(response, true); @@ -201,14 +269,16 @@ describe("chatter meteor methods", function() { }); it("actually changed archived status to true", function() { - assert.equal(Chatter.Room.findOne(room._id).archived, true); + assert.equal(Chatter.UserRoom.findOne({userId: user._id, roomId: room._id}).archived, true); }); it("returns false after succesfully calling method with false parameter", function(done) { const params = { archived: false, - roomId: room._id + roomId: room._id, + userId: user._id }; + Meteor.call("room.archive", params, callbackWrapper((error, response) => { assert.isUndefined(error); assert.equal(response, false); @@ -216,72 +286,8 @@ describe("chatter meteor methods", function() { })); }); - it("actually changed archived status to true", function() { - assert.equal(Chatter.Room.findOne(room._id).archived, false); - }); - }); - }); - - describe("userRoom methods", function() { - - describe("room.join method", function() { - - describe("when parameters are missing or wrong", function() { - - it("returns exception when parameters are missing ", function(done) { - const params = {}; - Meteor.call("room.join", params, callbackWrapper((error, response) => { - assert.isUndefined(response); - assert.equal(error.errorType, "Match.Error"); - })); - - params.roomId = room._id; - - Meteor.call("room.join", params, callbackWrapper((error, response) => { - assert.isUndefined(response); - assert.equal(error.errorType, "Match.Error"); - done(); - })); - }); - - it("returns 'room does not exist' error when roomId does not exist", function(done) { - const params = {}; - params.roomId = "non existent roomId"; - params.invitees = [user._id]; - - Meteor.call("room.join", params, callbackWrapper((error, response) => { - assert.isUndefined(response); - assert.equal(error.error, "non-existing-room"); - done(); - })); - }); - - it("returns 'user does not exist' error when userId does not exist", function(done) { - const params = {}; - params.invitees = ["non existent userId"]; - params.roomId = room._id; - - Meteor.call("room.join", params, callbackWrapper((error, response) => { - assert.isUndefined(response); - assert.equal(error.error, "non-existing-users"); - done(); - })); - }); - }); - - describe("when no parameters are missing or wrong", function() { - - it("creates a userRoom instance", function(done) { - const params = {}; - params.invitees = [user._id]; - params.roomId = room._id; - - Meteor.call("room.join", params, callbackWrapper((error, response) => { - assert.isUndefined(error); - assert.isString(response); - done(); - })); - }); + it("actually changed archived status to false", function() { + assert.equal(Chatter.UserRoom.findOne({userId: user._id, roomId: room._id}).archived, false); }); }); @@ -313,7 +319,7 @@ describe("chatter meteor methods", function() { it("room counter returns true when called with right parameters", function(done) { - Meteor.call("room.counter.reset", room._id , callbackWrapper((error, response) => { + Meteor.call("room.unreadMsgCount.reset", room._id , callbackWrapper((error, response) => { assert.isUndefined(error); assert.equal(response, true); done() @@ -326,7 +332,7 @@ describe("chatter meteor methods", function() { it("room counter return error when wrong roomId is passed in", function(done) { - Meteor.call("room.counter.reset", "wrong room id" , callbackWrapper((error, response) => { + Meteor.call("room.unreadMsgCount.reset", "wrong room id" , callbackWrapper((error, response) => { assert.isUndefined(response); assert.equal(error.error, "non-existing-room"); done() @@ -336,7 +342,7 @@ describe("chatter meteor methods", function() { it("room counter returns error when user is not in room", function(done) { Chatter.UserRoom.remove({userId: user._id, roomId: room._id}); - Meteor.call("room.counter.reset", room._id , callbackWrapper((error, response) => { + Meteor.call("room.unreadMsgCount.reset", room._id , callbackWrapper((error, response) => { assert.isUndefined(response); assert.equal(error.error, "user-not-in-room"); done() diff --git a/server-tests/models.tests.js b/server-tests/models.tests.js index 75a8c52..267614d 100644 --- a/server-tests/models.tests.js +++ b/server-tests/models.tests.js @@ -64,7 +64,6 @@ describe("chatter models", function() { }); it("room is inserted with correct defaults", function() { - assert.equal(room.archived, false); const now = new Date(); assert.equal(now.getTime() - room.lastActive.getTime() < 20000, true); }); @@ -91,6 +90,8 @@ describe("chatter models", function() { it("User Room is inserted with correct attributes", function() { assert.equal(userRoom.userId, attributes.userId); assert.equal(userRoom.roomId, attributes.roomId); + assert.equal(userRoom.archived, false); + }); it("User Room is inserted with correct defaults", function() { diff --git a/server/methods.js b/server/methods.js index ed7d4b1..0475548 100644 --- a/server/methods.js +++ b/server/methods.js @@ -13,7 +13,7 @@ const checkIfChatterUser = function(userId) { Meteor.methods({ - "get.room.counts" () { + "get.room.unreadMsgCount" () { const userId = Meteor.userId(); checkIfChatterUser(userId); const userRooms = Chatter.UserRoom.find({userId}).fetch(); @@ -173,18 +173,21 @@ Meteor.methods({ "room.archive" (params) { check(params, { roomId: String, + userId: String, archived: Boolean }); + const userId = Meteor.userId(); const {roomId, archived} = params; - const room = Chatter.Room.findOne(roomId); + const userRoom = Chatter.UserRoom.findOne({roomId, userId}); - if (!room) { - throw new Meteor.Error("non-existing-room", "room does not exist"); + if (!userRoom) { + throw new Meteor.Error("user-not-in-room", "user must be in room"); } - Chatter.Room.update({ - _id : roomId + Chatter.UserRoom.update({ + roomId, + userId }, { $set:{archived: archived} @@ -219,7 +222,7 @@ Meteor.methods({ return nickname; }, - "room.counter.reset" (roomId) { + "room.unreadMsgCount.reset" (roomId) { check(roomId, String); const userId = Meteor.userId(); diff --git a/server/publish.js b/server/publish.js index f4caa6d..726b08f 100644 --- a/server/publish.js +++ b/server/publish.js @@ -37,7 +37,6 @@ Meteor.publish("chatterRooms", function () { description: 1, roomType: 1, lastActive: 1, - archived: 1, createdAt: 1 } }); @@ -54,7 +53,8 @@ Meteor.publish("chatterUserRooms", function () { fields: { unreadMsgCount: 1, userId: 1, - roomId: 1 + roomId: 1, + archived: 1 } }); });