Permalink
Browse files

seperate interactions from posts

Lazily load post interactions on show page
hella refactorz
  • Loading branch information...
1 parent 871d3cc commit f6e3c1b88b6b73c3cee799fb82de6f8d5c0e8c06 @denniscollective denniscollective committed May 13, 2012
Showing with 385 additions and 532 deletions.
  1. +13 −0 app/assets/javascripts/app/collections/comments.js
  2. +4 −0 app/assets/javascripts/app/collections/reshares.js
  3. +0 −1 app/assets/javascripts/app/models/participation.js
  4. +10 −53 app/assets/javascripts/app/models/post.js
  5. +116 −0 app/assets/javascripts/app/models/post/interactions.js
  6. +2 −0 app/assets/javascripts/app/models/reshare.js
  7. +3 −1 app/assets/javascripts/app/pages/post-viewer.js
  8. +3 −2 app/assets/javascripts/app/views/comment_stream_view.js
  9. +5 −1 app/assets/javascripts/app/views/comment_view.js
  10. +12 −30 app/assets/javascripts/app/views/feedback_view.js
  11. +5 −9 app/assets/javascripts/app/views/likes_info_view.js
  12. +5 −1 app/assets/javascripts/app/views/post-viewer/feedback.js
  13. +3 −2 app/assets/javascripts/app/views/post-viewer/interactions.js
  14. +1 −2 app/assets/javascripts/app/views/post-viewer/new_comment.js
  15. +15 −5 app/assets/javascripts/app/views/post-viewer/reactions.js
  16. +1 −1 app/assets/templates/comment-stream.jst.hbs
  17. +1 −1 app/assets/templates/feedback.jst.hbs
  18. +4 −5 app/assets/templates/likes-info.jst.hbs
  19. +10 −10 app/assets/templates/post-viewer/feedback.jst.hbs
  20. +3 −3 app/assets/templates/small-frame.jst.hbs
  21. +1 −1 app/controllers/comments_controller.rb
  22. +16 −36 app/controllers/likes_controller.rb
  23. +0 −68 app/controllers/participations_controller.rb
  24. +31 −51 app/controllers/posts_controller.rb
  25. +11 −0 app/models/post.rb
  26. +1 −1 app/presenters/extreme_post_presenter.rb
  27. +3 −1 app/presenters/last_three_comments_decorator.rb
  28. +8 −5 app/presenters/post_presenter.rb
  29. +3 −1 config/routes.rb
  30. +7 −6 features/post_viewer.feature
  31. +1 −1 lib/federated/generator.rb
  32. +6 −9 spec/controllers/likes_controller_spec.rb
  33. +0 −128 spec/controllers/participations_controller_spec.rb
  34. +12 −17 spec/controllers/posts_controller_spec.rb
  35. +45 −0 spec/javascripts/app/models/post/interacations_spec.js
  36. +0 −39 spec/javascripts/app/models/post_spec.js
  37. +0 −22 spec/javascripts/app/views/comment_stream_view_spec.js
  38. +7 −5 spec/javascripts/app/views/feedback_view_spec.js
  39. +5 −7 spec/javascripts/app/views/likes_info_view_spec.js
  40. +8 −4 spec/javascripts/helpers/factory.js
  41. +4 −3 spec/models/user/social_actions_spec.rb
@@ -7,5 +7,18 @@ app.collections.Comments = Backbone.Collection.extend({
initialize : function(models, options) {
this.post = options.post
+ },
+
+ make : function(text){
+ var self = this
+
+ var comment = new app.models.Comment({text: text })
+ , deferred = comment.save({}, {url : self.url()})
+
+ comment.set({author: app.currentUser.toJSON(), parent: self.post })
+
+ this.add(comment)
+
+ return deferred
}
});
@@ -0,0 +1,4 @@
+app.collections.Reshares = Backbone.Collection.extend({
+ model: app.models.Reshare,
+ url : "/reshares"
+});
@@ -1 +0,0 @@
-app.models.Participation = Backbone.Model.extend({ });
@@ -2,27 +2,27 @@ app.models.Post = Backbone.Model.extend(_.extend({}, app.models.formatDateMixin,
urlRoot : "/posts",
initialize : function() {
- this.setupCollections();
- this.bind("change", this.setupCollections, this)
+ this.interactions = new app.models.Post.Interactions(_.extend({post : this}, this.get("interactions")))
+ this.delegateToInteractions()
},
- setupCollections: function() {
- this.comments = new app.collections.Comments(this.get("comments") || this.get("last_three_comments"), {post : this});
- this.likes = this.likes || new app.collections.Likes([], {post : this}); // load in the user like initially
- this.participations = this.participations || new app.collections.Participations([], {post : this}); // load in the user like initially
+ delegateToInteractions : function(){
+ this.comments = this.interactions.comments
+ this.likes = this.interactions.likes
+
+ this.comment = function(){
+ this.interactions.comment.apply(this.interactions, arguments)
+ }
},
setFrameName : function(){
- var templatePicker = new app.models.Post.TemplatePicker(this)
- this.set({frame_name : templatePicker.getFrameName()})
+ this.set({frame_name : new app.models.Post.TemplatePicker(this).getFrameName()})
},
interactedAt : function() {
return this.timeOf("interacted_at");
},
- createReshareUrl : "/reshares",
-
reshare : function(){
return this._reshare = this._reshare || new app.models.Reshare({root_guid : this.get("guid")});
},
@@ -31,56 +31,13 @@ app.models.Post = Backbone.Model.extend(_.extend({}, app.models.formatDateMixin,
return this.get("author")
},
- toggleLike : function() {
- var userLike = this.get("user_like")
- if(userLike) {
- this.unlike()
- } else {
- this.like()
- }
- },
-
toggleFavorite : function(options){
this.set({favorite : !this.get("favorite")})
/* guard against attempting to save a model that a user doesn't own */
if(options.save){ this.save() }
},
- like : function() {
- var self = this;
- this.likes.create({}, {success : function(resp){
- self.set(resp)
- self.trigger('interacted', self)
- }});
-
- },
-
- unlike : function() {
- var self = this;
- var likeModel = new app.models.Like(this.get("user_like"));
- likeModel.url = this.likes.url + "/" + likeModel.id;
-
- likeModel.destroy({success : function(model, resp) {
- self.set(resp);
- self.trigger('interacted', this)
- }});
- },
-
- comment : function (text) {
-
- var self = this
- , postComments = this.comments;
-
- postComments.create({"text": text}, {
- url : postComments.url(),
- wait:true, // added a wait for the time being. 0.5.3 was not optimistic, but 0.9.2 is.
- error:function () {
- alert(Diaspora.I18n.t("failed_to_post_message"));
- }
- });
- },
-
headline : function() {
var headline = this.get("text").trim()
, newlineIdx = headline.indexOf("\n")
@@ -0,0 +1,116 @@
+//require ../post
+
+app.models.Post.Interactions = Backbone.Model.extend({
+ url : function(){
+ return this.post.url() + "/interactions"
+ },
+
+ initialize : function(options){
+ this.post = options.post
+ this.comments = new app.collections.Comments(this.get("comments"), {post : this.post})
+ this.likes = new app.collections.Likes(this.get("likes"), {post : this.post});
+ this.reshares = new app.collections.Reshares(this.get("reshares"), {post : this.post});
+ },
+
+ parse : function(resp){
+ this.comments.reset(resp.comments)
+ this.likes.reset(resp.likes)
+ this.reshares.reset(resp.reshares)
+
+ var comments = this.comments
+ , likes = this.likes
+ , reshares = this.reshares
+
+ return {
+ comments : comments,
+ likes : likes,
+ reshares : reshares,
+ fetched : true
+ }
+ },
+
+ likesCount : function(){
+ return (this.get("fetched") ? this.likes.models.length : this.get("likes_count") )
+ },
+
+ resharesCount : function(){
+ return this.get("fetched") ? this.reshares.models.length : this.get("reshares_count")
+ },
+
+ commentsCount : function(){
+ return this.get("fetched") ? this.comments.models.length : this.get("comments_count")
+ },
+
+ userLike : function(){
+ return this.likes.select(function(like){ return like.get("author").guid == app.currentUser.get("guid")})[0]
+ },
+
+ userReshare : function(){
+ return this.reshares.select(function(reshare){ return reshare.get("author").guid == app.currentUser.get("guid")})[0]
@Raven24
Raven24 May 17, 2012 Member

That reshare in there doesn't have an "author" attached to it anymore. Therefore this results in a Javascript error: reshare.get("author") is undefined
The only thing that could work is author_id, if the guid isn't necessary ...

@maxwell
maxwell May 17, 2012 Member

yeah, i am not sure exactly what is happening. I got it to happen when i like rreshared a reshare i think?

@maxwell
maxwell May 17, 2012 Member

i guess if author is nil, the author did not reshare this?

+ },
+
+ toggleLike : function() {
+ if(this.userLike()) {
+ this.unlike()
+ } else {
+ this.like()
+ }
+ },
+
+ like : function() {
+ var self = this;
+ this.likes.create({}, {success : function(){
+ self.trigger("change")
+ self.set({"likes_count" : self.get("likes_count") + 1})
+ }})
+ },
+
+ unlike : function() {
+ var self = this;
+ this.userLike().destroy({success : function(model, resp) {
+ self.trigger('change')
+ self.set({"likes_count" : self.get("likes_count") - 1})
+ }});
+ },
+
+ comment : function (text) {
+ var self = this;
+
+ this.comments.make(text).fail(function () {
+ alert(Diaspora.I18n.t("failed_to_post_message"));
+ }).done(function() {
+ self.trigger('change') //updates after sync
+ });
+
+ this.trigger("change") //updates count in an eager manner
+ },
+
+ reshare : function(){
+ var interactions = this
+ , reshare = this.post.reshare()
+
+ reshare.save({}, {
+ success : function(resp){
+ var flash = new Diaspora.Widgets.FlashMessages;
+ flash.render({
+ success: true,
+ notice: Diaspora.I18n.t("reshares.successful")
+ });
+ }
+ }).done(function(){
+ interactions.reshares.add(reshare)
+ }).done(function(){
+ interactions.trigger("change")
+ });
+ },
+
+ userCanReshare : function(){
+ var isReshare = this.post.get("post_type") == "Reshare"
+ , rootExists = (isReshare ? this.post.get("root") : true)
+ , publicPost = this.post.get("public")
+ , userIsNotAuthor = this.post.get("author").diaspora_id != app.currentUser.get("diaspora_id")
+ , userIsNotRootAuthor = rootExists && (isReshare ? this.post.get("root").author.diaspora_id != app.currentUser.get("diaspora_id") : true)
+
+ return publicPost && app.currentUser.authenticated() && userIsNotAuthor && userIsNotRootAuthor;
+ }
+});
@@ -1,4 +1,6 @@
app.models.Reshare = app.models.Post.extend({
+ urlRoot : "/reshares",
+
rootPost : function(){
this._rootPost = this._rootPost || new app.models.Post(this.get("root"));
return this._rootPost
@@ -9,8 +9,10 @@ app.pages.PostViewer = app.views.Base.extend({
},
initialize : function(options) {
- this.model = new app.models.Post({ id : options.id });
+ var post = this.model = new app.models.Post({ id : options.id });
this.model.preloadOrFetch().done(_.bind(this.initViews, this));
+ this.model.interactions.fetch() //async, yo, might want to throttle this later.
+
this.bindEvents()
},
@@ -31,8 +31,9 @@ app.views.CommentStream = app.views.Base.extend({
presenter: function(){
return _.extend(this.defaultPresenter(), {
- moreCommentsCount : (this.model.get("comments_count") - 3),
- showExpandCommentsLink : (this.model.get("comments_count") > 3)
+ moreCommentsCount : (this.model.interactions.commentsCount() - 3),
+ showExpandCommentsLink : (this.model.interactions.commentsCount() > 3),
+ commentsCount : this.model.interactions.commentsCount()
})
},
@@ -4,11 +4,15 @@ app.views.Comment = app.views.Content.extend({
className : "comment media",
events : function() {
- return _.extend(app.views.Content.prototype.events, {
+ return _.extend({}, app.views.Content.prototype.events, {
"click .comment_delete": "destroyModel"
});
},
+ initialize : function(){
+ this.model.on("change", this.render, this)
+ },
+
presenter : function() {
return _.extend(this.defaultPresenter(), {
canRemove: this.canRemove(),
@@ -1,5 +1,4 @@
app.views.Feedback = app.views.Base.extend({
-
templateName: "feedback",
className : "info",
@@ -10,47 +9,30 @@ app.views.Feedback = app.views.Base.extend({
},
initialize : function() {
- this.model.bind('interacted', this.render, this);
+ this.model.interactions.on('change', this.render, this);
},
presenter : function() {
- return _.extend(this.defaultPresenter(), {
- userCanReshare : this.userCanReshare()
+ var interactions = this.model.interactions
+
+ return _.extend(this.defaultPresenter(),{
+ commentsCount : interactions.commentsCount(),
+ likesCount : interactions.likesCount(),
+ resharesCount : interactions.resharesCount(),
+ userCanReshare : interactions.userCanReshare(),
+ userLike : interactions.userLike(),
+ userReshare : interactions.userReshare(),
})
},
toggleLike: function(evt) {
if(evt) { evt.preventDefault(); }
- this.model.toggleLike();
+ this.model.interactions.toggleLike();
},
resharePost : function(evt) {
if(evt) { evt.preventDefault(); }
if(!window.confirm(Diaspora.I18n.t("reshares.post", {name: this.model.reshareAuthor().name}))) { return }
- var reshare = this.model.reshare()
- var model = this.model
-
- reshare.save({}, {
- url: this.model.createReshareUrl,
- success : function(resp){
- var flash = new Diaspora.Widgets.FlashMessages;
- flash.render({
- success: true,
- notice: Diaspora.I18n.t("reshares.successful")
- });
- model.trigger("interacted")
- }
- });
- },
-
- userCanReshare : function() {
- var isReshare = this.model.get("post_type") == "Reshare"
- var rootExists = (isReshare ? this.model.get("root") : true)
-
- var publicPost = this.model.get("public");
- var userIsNotAuthor = this.model.get("author").diaspora_id != app.currentUser.get("diaspora_id");
- var userIsNotRootAuthor = rootExists && (isReshare ? this.model.get("root").author.diaspora_id != app.currentUser.get("diaspora_id") : true)
-
- return publicPost && app.currentUser.authenticated() && userIsNotAuthor && userIsNotRootAuthor;
+ this.model.interactions.reshare();
}
});
@@ -10,23 +10,19 @@ app.views.LikesInfo = app.views.StreamObject.extend({
tooltipSelector : ".avatar",
initialize : function() {
- this.model.bind('expandedLikes', this.render, this)
+ this.model.interactions.bind('change', this.render, this)
},
presenter : function() {
return _.extend(this.defaultPresenter(), {
- likes : this.model.likes.models
+ likes : this.model.interactions.likes.toJSON(),
+ likesCount : this.model.interactions.likesCount(),
+ likes_fetched : this.model.interactions.get("fetched"),
})
},
showAvatars : function(evt){
if(evt) { evt.preventDefault() }
- var self = this;
- this.model.likes.fetch()
- .done(function(resp){
- // set like attribute and like collection
- self.model.set({likes : self.model.likes.reset(resp)})
- self.model.trigger("expandedLikes")
- })
+ this.model.interactions.fetch()
}
});
Oops, something went wrong.

0 comments on commit f6e3c1b

Please sign in to comment.