Skip to content
Extraits de code Groupes Projets
posts_controller.rb 1,71 ko
Newer Older
  • Learn to ignore specific revisions
  • maxwell's avatar
    maxwell a validé
    #   Copyright (c) 2010, Diaspora Inc.  This file is
    #   licensed under the Affero General Public License version 3 or later.  See
    #   the COPYRIGHT file.
    
    class PostsController < ApplicationController
    
      before_filter :authenticate_user!, :except => :show
    
      respond_to :html,
                 :mobile,
                 :json,
                 :xml
    
    
    maxwell's avatar
    maxwell a validé
      def show
    
        key = params[:id].to_s.length <= 8 ? :id : :guid
    
        if user_signed_in?
          @post = current_user.find_visible_post_by_id(params[:id], :key => key)
        else
          @post = Post.where(key => params[:id], :public => true).includes(:author, :comments => :author).first
        end
    
        if @post
    
          # mark corresponding notification as read
    
          if user_signed_in? && notification = Notification.where(:recipient_id => current_user.id, :target_id => @post.id).first
    
          respond_to do |format|
            format.all{ }
            format.xml{ render :xml => @post.to_diaspora_xml }
          end
    
    
          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
      end
    
      def destroy
        @post = current_user.posts.where(:id => params[:id]).first
        if @post
          current_user.retract(@post)
          respond_to do |format|
            format.js {render 'destroy'}
            format.all {redirect_to root_url}
    
    maxwell's avatar
    maxwell a validé
          end
    
    maxwell's avatar
    maxwell a validé
        else
    
          Rails.logger.info "event=post_destroy status=failure user=#{current_user.diaspora_handle} reason='User does not own post'"
          render :nothing => true, :status => 404
    
    maxwell's avatar
    maxwell a validé
      end
    end