Skip to content
Extraits de code Groupes Projets
Valider 0fc39924 rédigé par Dennis Collinson's avatar Dennis Collinson
Parcourir les fichiers

fix next post and previous post, more efficient

make controller actions
make the presenter reflect that it is instance data ;-p

needs tests lol.
parent 69b9b576
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
app.views.PostViewerNav = app.views.Base.extend({ app.views.PostViewerNav = app.views.Base.extend({
templateName: "post-viewer/nav", templateName: "post-viewer/nav",
events : {
"click a" : "pjax"
},
postRenderTemplate : function() { postRenderTemplate : function() {
var mappings = {"#forward" : "next_post", var mappings = {"#forward" : "next_post",
"#back" : "previous_post"}; "#back" : "previous_post"};
...@@ -17,14 +12,5 @@ app.views.PostViewerNav = app.views.Base.extend({ ...@@ -17,14 +12,5 @@ app.views.PostViewerNav = app.views.Base.extend({
setArrow : function(arrow, loc) { setArrow : function(arrow, loc) {
loc ? arrow.attr('href', loc) : arrow.remove() loc ? arrow.attr('href', loc) : arrow.remove()
},
pjax : function(evt) {
if(evt) { evt.preventDefault(); }
var link;
evt.target.tagName != "A" ? link = $(evt.target).closest("a") : link = $(evt.target)
app.router.navigate(link.attr("href").substring(1), true)
} }
}); });
\ No newline at end of file
...@@ -9,6 +9,7 @@ class PostsController < ApplicationController ...@@ -9,6 +9,7 @@ class PostsController < ApplicationController
before_filter :authenticate_user!, :except => [:show, :iframe, :oembed] before_filter :authenticate_user!, :except => [:show, :iframe, :oembed]
before_filter :set_format_if_malformed_from_status_net, :only => :show before_filter :set_format_if_malformed_from_status_net, :only => :show
before_filter :find_post, :only => [:show, :next, :previous]
layout 'post' layout 'post'
...@@ -24,28 +25,19 @@ class PostsController < ApplicationController ...@@ -24,28 +25,19 @@ class PostsController < ApplicationController
end end
def show def show
@post = find_by_guid_or_id_with_current_user(params[:id]) return log_and_redirect_back unless @post
# @commenting_disabled = can_not_comment_on_post?
if @post # mark corresponding notification as read
# @commenting_disabled = can_not_comment_on_post? if user_signed_in? && notification = Notification.where(:recipient_id => current_user.id, :target_id => @post.id).first
# mark corresponding notification as read notification.unread = false
if user_signed_in? && notification = Notification.where(:recipient_id => current_user.id, :target_id => @post.id).first notification.save
notification.unread = false end
notification.save
end
respond_to do |format|
format.html{ gon.post = postJson; render 'posts/show.html.haml' }
format.xml{ render :xml => @post.to_diaspora_xml }
format.mobile{render 'posts/show.mobile.haml', :layout => "application"}
format.json{ render :json => postJson }
end
else respond_to do |format|
user_id = (user_signed_in? ? current_user : nil) format.html{ gon.post = postJson; render 'posts/show.html.haml' }
Rails.logger.info(":event => :link_to_nonexistent_post, :ref => #{request.env['HTTP_REFERER']}, :user_id => #{user_id}, :post_id => #{params[:id]}") format.xml{ render :xml => @post.to_diaspora_xml }
flash[:error] = I18n.t('posts.show.not_found') format.mobile{render 'posts/show.mobile.haml', :layout => "application"}
redirect_to :back format.json{ render :json => postJson }
end end
end end
...@@ -88,8 +80,31 @@ class PostsController < ApplicationController ...@@ -88,8 +80,31 @@ class PostsController < ApplicationController
end end
end end
def next
redirect_to post_path(post_base.newer(@post))
end
def previous
redirect_to post_path(post_base.older(@post))
end
protected protected
def log_and_redirect_back #preserving old functionality, but this should probably be removed
user_id = (user_signed_in? ? current_user : nil)
Rails.logger.info(":event => :link_to_nonexistent_post, :ref => #{request.env['HTTP_REFERER']}, :user_id => #{user_id}, :post_id => #{params[:id]}")
flash[:error] = I18n.t('posts.show.not_found')
redirect_to :back
end
def find_post
@post = find_by_guid_or_id_with_current_user(params[:id])
end
def post_base
Post.visible_from_author(@post.author, current_user)
end
def postJson def postJson
PostPresenter.new(@post, current_user).to_json PostPresenter.new(@post, current_user).to_json
end end
...@@ -101,7 +116,6 @@ class PostsController < ApplicationController ...@@ -101,7 +116,6 @@ class PostsController < ApplicationController
else else
Post.where(key => id, :public => true).includes(:author, :comments => :author).first Post.where(key => id, :public => true).includes(:author, :comments => :author).first
end end
end end
def set_format_if_malformed_from_status_net def set_format_if_malformed_from_status_net
......
...@@ -4,116 +4,94 @@ class PostPresenter ...@@ -4,116 +4,94 @@ class PostPresenter
attr_accessor :post, :current_user attr_accessor :post, :current_user
def initialize(post, current_user = nil) def initialize(post, current_user = nil)
self.post = post @post = post
self.current_user = current_user @current_user = current_user
end end
def to_json(options = {}) def to_json(options = {})
self.post.as_api_response(:backbone).update( @post.as_api_response(:backbone).update(
{ {
:user_like => self.user_like, :user_like => user_like,
:user_participation => self.user_participation, :user_participation => user_participation,
:likes_count => self.post.likes.count, :likes_count => @post.likes.count,
:participations_count => self.post.participations.count, :participations_count => @post.participations.count,
:reshares_count => self.post.reshares.count, :reshares_count => @post.reshares.count,
:user_reshare => self.user_reshare, :user_reshare => user_reshare,
:next_post => self.next_post_path, :next_post => next_post_path,
:previous_post => self.previous_post_path, :previous_post => previous_post_path,
:likes => self.likes, :likes => likes,
:reshares => self.reshares, :reshares => reshares,
:comments => self.comments, :comments => comments,
:participations => self.participations, :participations => participations,
:frame_name => self.post.frame_name || template_name, :frame_name => @post.frame_name || template_name,
:title => title :title => title
}) })
end end
def next_post_path
Rails.application.routes.url_helpers.next_post_path(@post)
end
def previous_post_path
Rails.application.routes.url_helpers.previous_post_path(@post)
end
def comments def comments
as_api(post.comments) as_api(@post.comments)
end end
def likes def likes
as_api(post.likes) as_api(@post.likes)
end end
def reshares def reshares
as_api(post.reshares) as_api(@post.reshares)
end end
def participations def participations
as_api(post.participations) as_api(@post.participations)
end end
def user_like def user_like
return unless user_signed_in? return unless user_signed_in?
if like = post.likes.where(:author_id => person.id).first @post.likes.where(:author_id => person.id).first.try(:as_api_response, :backbone)
like.as_api_response(:backbone)
end
end end
def user_participation def user_participation
return unless user_signed_in? return unless user_signed_in?
if participation = post.participations.where(:author_id => person.id).first @post.participations.where(:author_id => person.id).first.try(:as_api_response, :backbone)
participation.as_api_response(:backbone)
end
end end
def user_reshare def user_reshare
return unless user_signed_in? return unless user_signed_in?
self.post.reshares.where(:author_id => person.id).first @post.reshares.where(:author_id => person.id).first
end
def next_post_path
if n = next_post
Rails.application.routes.url_helpers.post_path(n)
end
end
def previous_post_path
if p = previous_post
Rails.application.routes.url_helpers.post_path(p)
end
end end
def title def title
if post.text.present? if @post.text.present?
post.text(:plain_text => true) @post.text(:plain_text => true)
else else
I18n.translate('posts.presenter.title', :name => post.author.name) I18n.translate('posts.presenter.title', :name => @post.author.name)
end end
end end
def template_name def template_name #kill me, lol, I should be client side
@template_name ||= TemplatePicker.new(post).template_name @template_name ||= TemplatePicker.new(@post).template_name
end end
protected protected
def next_post
post_base.newer(post)
end
def previous_post
post_base.older(post)
end
def as_api(collection) def as_api(collection)
collection.includes(:author => :profile).all.map do |element| collection.includes(:author => :profile).all.map do |element|
element.as_api_response(:backbone) element.as_api_response(:backbone)
end end
end end
def post_base
Post.visible_from_author(self.post.author, current_user)
end
def person def person
self.current_user.person @current_user.person
end end
def user_signed_in? def user_signed_in?
current_user.present? @current_user.present?
end end
end end
...@@ -12,6 +12,10 @@ Diaspora::Application.routes.draw do ...@@ -12,6 +12,10 @@ Diaspora::Application.routes.draw do
resources :status_messages, :only => [:new, :create] resources :status_messages, :only => [:new, :create]
resources :posts do resources :posts do
member do
get :next
get :previous
end
resources :likes, :only => [:create, :destroy, :index] resources :likes, :only => [:create, :destroy, :index]
resources :participations, :only => [:create, :destroy, :index] resources :participations, :only => [:create, :destroy, :index]
resources :comments, :only => [:new, :create, :destroy, :index] resources :comments, :only => [:new, :create, :destroy, :index]
......
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