post owners can now delete any comments on their own posts. fixes #2652

This commit is contained in:
danielgrippi 2012-01-27 18:24:46 -08:00
parent d773749374
commit f250b79105
6 changed files with 77 additions and 9 deletions

View file

@ -1,6 +1,5 @@
<div class="right controls">
<!-- need access to post -->
{{#if ownComment}}
{{#if canRemove}}
<a href="#" class="delete comment_delete" title="{{t "delete"}}">
<img alt="Deletelabel" src="{{imageUrl "/images/deletelabel.png"}}" />
<a/>

View file

@ -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()
}
});

View file

@ -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);

View file

@ -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()
})
})
})

View file

@ -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)
})
})
})
})

View file

@ -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 = {