Fetch comments, likes and reshares separately

Fixes #7165

closes #7167
This commit is contained in:
Steffen van Bergerem 2016-11-02 13:59:18 +01:00 committed by Benjamin Neff
parent 98b345305e
commit e3b3da404f
11 changed files with 71 additions and 27 deletions

View file

@ -3,6 +3,7 @@
## Refactor ## Refactor
## Bug fixes ## Bug fixes
* Fix fetching comments after fetching likes [#7167](https://github.com/diaspora/diaspora/pull/7167)
## Features ## Features

View file

@ -2,6 +2,9 @@
app.collections.Reshares = Backbone.Collection.extend({ app.collections.Reshares = Backbone.Collection.extend({
model: app.models.Reshare, model: app.models.Reshare,
url : "/reshares"
initialize: function(models, options) {
this.url = "/posts/" + options.post.id + "/reshares";
}
}); });
// @license-end // @license-end

View file

@ -70,6 +70,7 @@ app.models.Post.Interactions = Backbone.Model.extend({
self.post.set({participation: true}); self.post.set({participation: true});
self.trigger("change"); self.trigger("change");
self.set({"likes_count" : self.get("likes_count") + 1}); self.set({"likes_count" : self.get("likes_count") + 1});
self.likes.trigger("change");
}, },
error: function() { error: function() {
app.flashMessages.error(Diaspora.I18n.t("failed_to_like")); app.flashMessages.error(Diaspora.I18n.t("failed_to_like"));
@ -84,6 +85,7 @@ app.models.Post.Interactions = Backbone.Model.extend({
this.userLike().destroy({success : function() { this.userLike().destroy({success : function() {
self.trigger('change'); self.trigger('change');
self.set({"likes_count" : self.get("likes_count") - 1}); self.set({"likes_count" : self.get("likes_count") - 1});
self.likes.trigger("change");
}}); }});
app.instrument("track", "Unlike"); app.instrument("track", "Unlike");
@ -116,6 +118,8 @@ app.models.Post.Interactions = Backbone.Model.extend({
app.stream.addNow(reshare); app.stream.addNow(reshare);
} }
interactions.trigger("change"); interactions.trigger("change");
interactions.set({"reshares_count": interactions.get("reshares_count") + 1});
interactions.reshares.trigger("change");
}) })
.fail(function(){ .fail(function(){
app.flashMessages.error(Diaspora.I18n.t("reshares.duplicate")); app.flashMessages.error(Diaspora.I18n.t("reshares.duplicate"));

View file

@ -11,7 +11,7 @@ app.views.LikesInfo = app.views.Base.extend({
tooltipSelector : ".avatar", tooltipSelector : ".avatar",
initialize : function() { initialize : function() {
this.model.interactions.bind('change', this.render, this); this.model.interactions.likes.on("change", this.render, this);
this.displayAvatars = false; this.displayAvatars = false;
}, },
@ -19,18 +19,16 @@ app.views.LikesInfo = app.views.Base.extend({
return _.extend(this.defaultPresenter(), { return _.extend(this.defaultPresenter(), {
likes : this.model.interactions.likes.toJSON(), likes : this.model.interactions.likes.toJSON(),
likesCount : this.model.interactions.likesCount(), likesCount : this.model.interactions.likesCount(),
displayAvatars : this.model.interactions.get("fetched") && this.displayAvatars displayAvatars: this.displayAvatars
}); });
}, },
showAvatars : function(evt){ showAvatars : function(evt){
if(evt) { evt.preventDefault() } if(evt) { evt.preventDefault() }
this.displayAvatars = true; this.displayAvatars = true;
if(!this.model.interactions.get("fetched")){ this.model.interactions.likes.fetch({success: function() {
this.model.interactions.fetch(); this.model.interactions.likes.trigger("change");
} else { }.bind(this)});
this.model.interactions.trigger("change");
}
} }
}); });
// @license-end // @license-end

View file

@ -11,7 +11,7 @@ app.views.ResharesInfo = app.views.Base.extend({
tooltipSelector : ".avatar", tooltipSelector : ".avatar",
initialize : function() { initialize : function() {
this.model.interactions.bind("change", this.render, this); this.model.interactions.reshares.bind("change", this.render, this);
this.displayAvatars = false; this.displayAvatars = false;
}, },
@ -19,18 +19,16 @@ app.views.ResharesInfo = app.views.Base.extend({
return _.extend(this.defaultPresenter(), { return _.extend(this.defaultPresenter(), {
reshares : this.model.interactions.reshares.toJSON(), reshares : this.model.interactions.reshares.toJSON(),
resharesCount : this.model.interactions.resharesCount(), resharesCount : this.model.interactions.resharesCount(),
displayAvatars : this.model.interactions.get("fetched") && this.displayAvatars displayAvatars: this.displayAvatars
}); });
}, },
showAvatars : function(evt){ showAvatars : function(evt){
if(evt) { evt.preventDefault() } if(evt) { evt.preventDefault() }
this.displayAvatars = true; this.displayAvatars = true;
if(!this.model.interactions.get("fetched")){ this.model.interactions.reshares.fetch({success: function() {
this.model.interactions.fetch(); this.model.interactions.reshares.trigger("change");
} else { }.bind(this)});
this.model.interactions.trigger("change");
}
} }
}); });
// @license-end // @license-end

View file

@ -73,3 +73,16 @@ Feature: commenting
When I follow "less than a minute ago" within ".comments .comment:last-child" When I follow "less than a minute ago" within ".comments .comment:last-child"
Then I should see "I think thats a cat" within ".comments .comment .highlighted" Then I should see "I think thats a cat" within ".comments .comment .highlighted"
And I should have scrolled down And I should have scrolled down
Scenario: Show more comments after loading likes
When "alice@alice.alice" has commented a lot on "Look at this dog"
And "alice@alice.alice" has liked the post "Look at this dog"
And I am on "alice@alice.alice"'s page
Then I should see "Look at this dog"
And I should not see "Comment 2"
When I follow "1 Like"
Then I should not see "1 Like"
When I click on selector ".toggle_post_comments"
Then I should see "Comment 2"

View file

@ -0,0 +1,5 @@
Given /^"([^"]*)" has liked the post "([^"]*)"$/ do |email, post_text|
user = User.find_by(email: email)
post = StatusMessage.find_by(text: post_text)
user.like!(post)
end

View file

@ -98,7 +98,7 @@ describe ResharesController, :type => :controller do
expect(JSON.parse(response.body).map {|h| h["id"] }).to eq(@post.reshares.map(&:id)) expect(JSON.parse(response.body).map {|h| h["id"] }).to eq(@post.reshares.map(&:id))
end end
it "returns an empty array for a post with no likes" do it "returns an empty array for a post with no reshares" do
get :index, post_id: @post.id, format: :json get :index, post_id: @post.id, format: :json
expect(JSON.parse(response.body)).to eq([]) expect(JSON.parse(response.body)).to eq([])
end end

View file

@ -40,6 +40,13 @@ describe("app.models.Post.Interactions", function(){
jasmine.Ajax.requests.mostRecent().respondWith(ajaxSuccess); jasmine.Ajax.requests.mostRecent().respondWith(ajaxSuccess);
expect(this.post.get("participation")).toBeTruthy(); expect(this.post.get("participation")).toBeTruthy();
}); });
it("triggers a change on the likes collection", function() {
spyOn(this.interactions.likes, "trigger");
this.interactions.like();
jasmine.Ajax.requests.mostRecent().respondWith(ajaxSuccess);
expect(this.interactions.likes.trigger).toHaveBeenCalledWith("change");
});
}); });
describe("unlike", function(){ describe("unlike", function(){
@ -56,7 +63,7 @@ describe("app.models.Post.Interactions", function(){
this.reshare = this.interactions.post.reshare(); this.reshare = this.interactions.post.reshare();
}); });
it("triggers a change on the model", function() { it("triggers a change on the interactions model", function() {
spyOn(this.interactions, "trigger"); spyOn(this.interactions, "trigger");
this.interactions.reshare(); this.interactions.reshare();
@ -65,6 +72,13 @@ describe("app.models.Post.Interactions", function(){
expect(this.interactions.trigger).toHaveBeenCalledWith("change"); expect(this.interactions.trigger).toHaveBeenCalledWith("change");
}); });
it("triggers a change on the reshares collection", function() {
spyOn(this.interactions.reshares, "trigger");
this.interactions.reshare();
jasmine.Ajax.requests.mostRecent().respondWith(ajaxSuccess);
expect(this.interactions.reshares.trigger).toHaveBeenCalledWith("change");
});
it("adds the reshare to the default, activity and aspects stream", function() { it("adds the reshare to the default, activity and aspects stream", function() {
app.stream = { addNow: $.noop }; app.stream = { addNow: $.noop };
spyOn(app.stream, "addNow"); spyOn(app.stream, "addNow");

View file

@ -21,19 +21,23 @@ describe("app.views.LikesInfo", function(){
it("fires on a model change", function(){ it("fires on a model change", function(){
spyOn(this.view, "postRenderTemplate"); spyOn(this.view, "postRenderTemplate");
this.view.model.interactions.trigger('change'); this.view.model.interactions.likes.trigger("change");
expect(this.view.postRenderTemplate).toHaveBeenCalled(); expect(this.view.postRenderTemplate).toHaveBeenCalled();
}); });
}); });
describe("showAvatars", function(){ describe("showAvatars", function(){
beforeEach(function(){ it("calls fetch on the model's like collection", function() {
spyOn(this.post.interactions, "fetch").and.callThrough(); spyOn(this.post.interactions.likes, "fetch").and.callThrough();
this.view.showAvatars();
expect(this.post.interactions.likes.fetch).toHaveBeenCalled();
}); });
it("calls fetch on the model's like collection", function(){ it("triggers 'change' on the likes collection", function() {
spyOn(this.post.interactions.likes, "trigger");
this.view.showAvatars(); this.view.showAvatars();
expect(this.post.interactions.fetch).toHaveBeenCalled(); jasmine.Ajax.requests.mostRecent().respondWith({status: 200, responseText: "{\"id\": 1}"});
expect(this.post.interactions.likes.trigger).toHaveBeenCalledWith("change");
}); });
it("sets 'displayAvatars' to true", function(){ it("sets 'displayAvatars' to true", function(){

View file

@ -22,19 +22,23 @@ describe("app.views.ResharesInfo", function(){
it("fires on a model change", function(){ it("fires on a model change", function(){
spyOn(this.view, "postRenderTemplate"); spyOn(this.view, "postRenderTemplate");
this.view.model.interactions.trigger("change"); this.view.model.interactions.reshares.trigger("change");
expect(this.view.postRenderTemplate).toHaveBeenCalled(); expect(this.view.postRenderTemplate).toHaveBeenCalled();
}); });
}); });
describe("showAvatars", function(){ describe("showAvatars", function(){
beforeEach(function(){ it("calls fetch on the model's reshare collection", function() {
spyOn(this.post.interactions, "fetch").and.callThrough(); spyOn(this.post.interactions.reshares, "fetch").and.callThrough();
this.view.showAvatars();
expect(this.post.interactions.reshares.fetch).toHaveBeenCalled();
}); });
it("calls fetch on the model's reshare collection", function(){ it("triggers 'change' on the reshares collection", function() {
spyOn(this.post.interactions.reshares, "trigger");
this.view.showAvatars(); this.view.showAvatars();
expect(this.post.interactions.fetch).toHaveBeenCalled(); jasmine.Ajax.requests.mostRecent().respondWith({status: 200, responseText: "{\"id\": 1}"});
expect(this.post.interactions.reshares.trigger).toHaveBeenCalledWith("change");
}); });
it("sets 'displayAvatars' to true", function(){ it("sets 'displayAvatars' to true", function(){