diff --git a/app/views/templates/comment.handlebars b/app/views/templates/comment.handlebars index c878118e6..02f153b2a 100644 --- a/app/views/templates/comment.handlebars +++ b/app/views/templates/comment.handlebars @@ -1,6 +1,5 @@
- - {{#if ownComment}} + {{#if canRemove}} Deletelabel diff --git a/public/javascripts/app/views/comment_view.js b/public/javascripts/app/views/comment_view.js index 70a8a9753..87d30a3a4 100644 --- a/public/javascripts/app/views/comment_view.js +++ b/public/javascripts/app/views/comment_view.js @@ -12,13 +12,21 @@ app.views.Comment = app.views.Content.extend({ presenter : function() { return _.extend(this.defaultPresenter(), { - ownComment: this.ownComment(), + canRemove: this.canRemove(), text : app.helpers.textFormatter(this.model) }) }, - ownComment: function() { - if(!app.user()){ return false } + ownComment : function() { return this.model.get("author").diaspora_id == app.user().diaspora_id + }, + + postOwner : function() { + return this.model.get("parent").author.diaspora_id == app.user().diaspora_id + }, + + canRemove : function() { + if(!app.user()){ return false } + return this.ownComment() || this.postOwner() } }); diff --git a/public/javascripts/app/views/commment_stream_view.js b/public/javascripts/app/views/commment_stream_view.js index c42f3d585..2badac863 100644 --- a/public/javascripts/app/views/commment_stream_view.js +++ b/public/javascripts/app/views/commment_stream_view.js @@ -41,6 +41,10 @@ app.views.CommentStream = app.views.Base.extend({ }, appendComment: function(comment) { + // Set the post as the comment's parent, so we can check + // on post ownership in the Comment view. + comment.set({parent : this.model.toJSON()}) + this.$("ul.comments").append(new app.views.Comment({ model: comment }).render().el); diff --git a/spec/javascripts/app/views/comment_stream_view_spec.js b/spec/javascripts/app/views/comment_stream_view_spec.js index 5ea005bf0..94d32bd31 100644 --- a/spec/javascripts/app/views/comment_stream_view_spec.js +++ b/spec/javascripts/app/views/comment_stream_view_spec.js @@ -26,4 +26,14 @@ describe("app.views.CommentStream", function(){ expect(this.view.$(".comment_box").val()).toBe("") }) }) + + describe("appendComment", function(){ + it("appends this.model as 'parent' to the comment", function(){ + var comment = new app.models.Comment(factory.comment()) + + spyOn(comment, "set") + this.view.appendComment(comment) + expect(comment.set).toHaveBeenCalled() + }) + }) }) diff --git a/spec/javascripts/app/views/comment_view_spec.js b/spec/javascripts/app/views/comment_view_spec.js index 66f25cb23..2b1676b9d 100644 --- a/spec/javascripts/app/views/comment_view_spec.js +++ b/spec/javascripts/app/views/comment_view_spec.js @@ -1,6 +1,7 @@ describe("app.views.Comment", function(){ beforeEach(function(){ - this.comment = factory.comment() + this.post = factory.post({author : {diaspora_id : "xxx@xxx.xxx"}}) + this.comment = factory.comment({parent : this.post.toJSON()}) this.view = new app.views.Comment({model : this.comment}) }) @@ -31,10 +32,44 @@ describe("app.views.Comment", function(){ loginAs(factory.author({diaspora_id : "notbob@bob.com"})) expect(this.view.ownComment()).toBe(false); }) + }) - it("returns false if the user is not logged in", function(){ - logout() - expect(this.view.ownComment()).toBe(false); + describe("postOwner", function(){ + it("returns true if the author diaspora_id == the current user's diaspora_id", function(){ + loginAs(this.post.get("author")) + expect(this.view.postOwner()).toBe(true) + }) + + it("returns false if the author diaspora_id != the current user's diaspora_id", function(){ + loginAs(factory.author({diaspora_id : "notbob@bob.com"})) + expect(this.view.postOwner()).toBe(false); + }) + }) + + describe("canRemove", function(){ + context("is truthy", function(){ + it("when ownComment is true", function(){ + spyOn(this.view, "ownComment").andReturn(true) + spyOn(this.view, "postOwner").andReturn(false) + + expect(this.view.canRemove()).toBe(true) + }) + + it("when postOwner is true", function(){ + spyOn(this.view, "postOwner").andReturn(true) + spyOn(this.view, "ownComment").andReturn(false) + + expect(this.view.canRemove()).toBe(true) + }) + }) + + context("is falsy", function(){ + it("when postOwner and ownComment are both false", function(){ + spyOn(this.view, "postOwner").andReturn(false) + spyOn(this.view, "ownComment").andReturn(false) + + expect(this.view.canRemove()).toBe(false) + }) }) }) }) diff --git a/spec/javascripts/helpers/factory.js b/spec/javascripts/helpers/factory.js index 4d63f970f..b0e871911 100644 --- a/spec/javascripts/helpers/factory.js +++ b/spec/javascripts/helpers/factory.js @@ -21,6 +21,18 @@ factory = { return _.extend(defaultAttrs, overrides) }, + comment : function(overrides) { + var defaultAttrs = { + "created_at" : "2012-01-04T00:55:30Z", + "author" : this.author(), + "guid" : this.guid(), + "id" : this.id.next(), + "text" : "This is a comment!" + } + + return new app.models.Comment(_.extend(defaultAttrs, overrides)) + }, + userAttrs : function(overrides){ var id = this.id.next() var defaultAttrs = {