Skip to content
Extraits de code Groupes Projets
Valider 19db5519 rédigé par Daniel Grippi's avatar Daniel Grippi Validation de Dennis Collinson
Parcourir les fichiers

DG DH; made acts_as_api play nice with backbonejs. comments are...

DG DH; made acts_as_api play nice with backbonejs.  comments are semi-functional; stream elements use the StreamElement js widget (for now).
parent aa7343d7
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Affichage de avec 104 ajouts et 31 suppressions
...@@ -145,6 +145,12 @@ class ApplicationController < ActionController::Base ...@@ -145,6 +145,12 @@ class ApplicationController < ActionController::Base
end end
end end
# @param stream_klass [Constant]
# @return [String] JSON representation of posts given a [Stream] constant.
def stream_json(stream_klass)
render_for_api :backbone, :json => stream(stream_klass).stream_posts, :root => :posts
end
def stream(stream_klass) def stream(stream_klass)
authenticate_user! authenticate_user!
save_sort_order save_sort_order
......
...@@ -10,10 +10,11 @@ class CommentStreamController < ApplicationController ...@@ -10,10 +10,11 @@ class CommentStreamController < ApplicationController
def index def index
@backbone = true @backbone = true
stream_klass = Stream::Comments
respond_with do |format| respond_with do |format|
format.html{ default_stream_action(Stream::Comments) } format.html{ default_stream_action(stream_klass) }
format.json{ render :json => stream(Stream::Comments).stream_posts.to_json(:include => {:author => {:include => :profile}}) } format.json{ stream_json(stream_klass) }
end end
end end
end end
...@@ -10,10 +10,11 @@ class MentionsController < ApplicationController ...@@ -10,10 +10,11 @@ class MentionsController < ApplicationController
def index def index
@backbone = true @backbone = true
stream_klass = Stream::Mention
respond_with do |format| respond_with do |format|
format.html{ default_stream_action(Stream::Mention) } format.html{ default_stream_action(stream_klass) }
format.json{ render :json => stream(Stream::Mention).stream_posts.to_json(:include => {:author => {:include => :profile}}) } format.json{ stream_json(stream_klass) }
end end
end end
end end
...@@ -10,11 +10,11 @@ class MultisController < ApplicationController ...@@ -10,11 +10,11 @@ class MultisController < ApplicationController
def index def index
@backbone = true @backbone = true
stream_klass = Stream::Multi
respond_with do |format| respond_with do |format|
format.html{ default_stream_action(Stream::Multi) } format.html{ default_stream_action(stream_klass) }
format.json{ render :json => stream(Stream::Multi).stream_posts.to_json(:include => {:author => {:include => :profile}}) } format.json{ stream_json(stream_klass) }
#format.json{ render_for_api :backbone, :json => stream(Stream::Multi).stream_posts }
end end
end end
end end
...@@ -132,7 +132,7 @@ class PeopleController < ApplicationController ...@@ -132,7 +132,7 @@ class PeopleController < ApplicationController
else else
respond_to do |format| respond_to do |format|
format.all { respond_with @person, :locals => {:post_type => :all} } format.all { respond_with @person, :locals => {:post_type => :all} }
format.json{ render :json => @stream.stream_posts.to_json(:include => {:author => {:include => :profile}}) } format.json{ render_for_api :backbone, :json => @stream.stream_posts, :root => :posts }
end end
end end
end end
......
...@@ -11,10 +11,11 @@ class TagFollowingsController < ApplicationController ...@@ -11,10 +11,11 @@ class TagFollowingsController < ApplicationController
def index def index
@backbone = true @backbone = true
stream_klass = Stream::FollowedTag
respond_with do |format| respond_with do |format|
format.html{ default_stream_action(Stream::FollowedTag) } format.html{ default_stream_action(stream_klass) }
format.json{ render :json => stream(Stream::FollowedTag).stream_posts.to_json(:include => {:author => {:include => :profile}}) } format.json{ stream_json(stream_klass) }
end end
end end
......
...@@ -46,7 +46,7 @@ class TagsController < ApplicationController ...@@ -46,7 +46,7 @@ class TagsController < ApplicationController
return return
end end
end end
format.json{ render :json => @stream.stream_posts.to_json(:include => {:author => {:include => :profile}}) } format.json{ render_for_api :backbone, :json => @stream.stream_posts, :root => :posts }
end end
end end
......
...@@ -16,6 +16,16 @@ class Comment < ActiveRecord::Base ...@@ -16,6 +16,16 @@ class Comment < ActiveRecord::Base
extract_tags_from :text extract_tags_from :text
before_create :build_tags before_create :build_tags
# NOTE API V1 to be extracted
acts_as_api
api_accessible :backbone do |t|
t.add :id
t.add :guid
t.add :text
t.add :author, :template => :post
end
xml_attr :text xml_attr :text
xml_attr :diaspora_handle xml_attr :diaspora_handle
...@@ -55,7 +65,7 @@ class Comment < ActiveRecord::Base ...@@ -55,7 +65,7 @@ class Comment < ActiveRecord::Base
def notification_type(user, person) def notification_type(user, person)
if self.post.author == user.person if self.post.author == user.person
return Notifications::CommentOnPost
elsif self.post.comments.where(:author_id => user.person.id) != [] && self.author_id != user.person.id elsif self.post.comments.where(:author_id => user.person.id) != [] && self.author_id != user.person.id
return Notifications::AlsoCommented return Notifications::AlsoCommented
else else
......
...@@ -16,6 +16,9 @@ class Post < ActiveRecord::Base ...@@ -16,6 +16,9 @@ class Post < ActiveRecord::Base
t.add :guid t.add :guid
t.add :text t.add :text
t.add :created_at t.add :created_at
t.add :comments_count
t.add :last_three_comments
t.add :provider_display_name
t.add :author, :template => :post t.add :author, :template => :post
end end
...@@ -33,6 +36,12 @@ class Post < ActiveRecord::Base ...@@ -33,6 +36,12 @@ class Post < ActiveRecord::Base
#scopes #scopes
scope :includes_for_a_stream, includes(:o_embed_cache, {:author => :profile}, :mentions => {:person => :profile}) #note should include root and photos, but i think those are both on status_message scope :includes_for_a_stream, includes(:o_embed_cache, {:author => :profile}, :mentions => {:person => :profile}) #note should include root and photos, but i think those are both on status_message
# gives the last three comments on the post
def last_three_comments
return if self.comments_count == 0
self.comments.last(3)
end
def self.excluding_blocks(user) def self.excluding_blocks(user)
people = user.blocks.includes(:person).map{|b| b.person} people = user.blocks.includes(:person).map{|b| b.person}
......
<% var post = typeof(status_message) === "undefined" ? (typeof(reshare) === "undefined" ? photo : reshare) : status_message; %> <div id="<%= guid %>" class="stream_element">
<div id="<%= post.guid %>" class="stream_element">
<div class="right controls"> <div class="right controls">
<a href="#" class="block_user control_icon"> <a href="#" class="block_user control_icon">
<img src="/images/icons/ignoreuser.png"> <img src="/images/icons/ignoreuser.png">
...@@ -15,37 +13,82 @@ ...@@ -15,37 +13,82 @@
<div class="sm_body"> <div class="sm_body">
<a href="/people/<%= post.author.id %>"> <a href="/people/<%= author.id %>">
<img src="<%= post.author.profile.image_url_small %>" class="avatar" data-person-id="<%= post.author.id %>"/> <img src="<%= author.avatar.small %>" class="avatar" data-person-id="<%= author.id %>"/>
</a> </a>
<div class="content"> <div class="content">
<div class="post_initial_info"> <div class="post_initial_info">
<span class="from"> <span class="from">
<a href="/people/<%= post.author.id %>"> <a href="/people/<%= author.id %>">
<%= post.author.profile.full_name %> <%= author.name %>
</a> </a>
</span> </span>
<span class="details"> <span class="details">
- -
<a href="/posts/<%= post.id %>"> <a href="/posts/<%= id %>">
<time class="timeago" datetime="<%= post.created_at %>"/> <time class="timeago" datetime="<%= created_at %>"/>
</a> </a>
</span> </span>
<p> <p>
<%= post.text %> <%= text %>
</p> </p>
<div class="info"> <div class="info">
<span class="via"> <span class="via">
via <%= post.provider_display_name %> via <%= provider_display_name %>
</span> </span>
</div> </div>
<div class="comments"> <div class="comments">
comments <div class="comment_stream">
<ul class="comments" >
<% _.each(last_three_comments, function(comment) { %>
<li id="<%= comment.guid %>" class="comment">
<a href="/people/<%= author.id %>">
<img src="<%= comment.author.avatar.small %>" class="avatar" data-person-id="<%= comment.author.id %>"/>
</a>
<div class="content">
<span class="from">
<a href="/people/<%= comment.author.id %>">
<%= comment.author.name %>
</a>
</span>
<p>
<%= comment.text %>
</p>
<div class="comment_info">
<time class="timeago" datetime="<%= comment.created_at %>"/>
</div>
</div>
</li>
<% }) %>
</ul>
<div class="new_comment_form_wrapper">
<form accept-charset="UTF-8" action="/posts/<%= id %>/comments" class="new_comment" data-remote="true" id="new_comment_on_<%= id %>" method="post">
<!-- <img alt="Bob Grimm" class="avatar" data-person_id="2" src="http://localhost:3000/images/user/wolf.jpg" title="Bob Grimm"> -->
<p>
<label for="comment_text_on_<%= id %>">Comment</label>
<textarea class="comment_box" id="comment_text_on_<%= id %>" name="text" rows="2" />
</p>
<div class="submit_button">
<input class="comment_submit button creation" data-disable-with="Commenting..." id="comment_submit_<%= id %>" name="commit" type="submit" value="Comment" />
</div>
</form>
</div>
</div>
</div> </div>
</div> </div>
......
...@@ -10,5 +10,9 @@ var BackboneStream = Backbone.Collection.extend({ ...@@ -10,5 +10,9 @@ var BackboneStream = Backbone.Collection.extend({
} }
}, },
model: Post model: Post,
parse: function(resp){
return resp.posts;
}
}); });
var Post = Backbone.Model.extend({ var Post = Backbone.Model.extend({
url: "/posts/:id", url: "/posts/:id",
intTime: function(){ intTime: function(){
return +new Date(this.postAttributes().created_at) / 1000; return +new Date(this.get("created_at")) / 1000;
},
postAttributes: function() {
return this.attributes[_.keys(this.attributes)[0]];
} }
}); });
...@@ -18,7 +18,9 @@ $(function() { ...@@ -18,7 +18,9 @@ $(function() {
}, },
appendPost: function(model) { appendPost: function(model) {
$(this.el).append(this.template(model.toJSON())); var post = $(this.template(model.toJSON()));
$(this.el).append(post);
Diaspora.BaseWidget.instantiate("StreamElement", post);
}, },
collectionFetched: function() { collectionFetched: function() {
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter