Skip to content
Extraits de code Groupes Projets
posts_controller.rb 2,18 ko
Newer Older
  • Learn to ignore specific revisions
  • #   Copyright (c) 2010-2011, Diaspora Inc.  This file is
    
    maxwell's avatar
    maxwell a validé
    #   licensed under the Affero General Public License version 3 or later.  See
    #   the COPYRIGHT file.
    
    class PostsController < ApplicationController
    
      before_action :authenticate_user!, only: %i(destroy mentionable)
    
      before_action :set_format_if_malformed_from_status_net, only: :show
    
      respond_to :html, :mobile, :json, :xml
    
      rescue_from Diaspora::NonPublic do
    
        authenticate_user!
    
      rescue_from Diaspora::NotMine do
    
        render text: I18n.t("posts.show.forbidden"), status: 403
    
    maxwell's avatar
    maxwell a validé
      def show
    
        post = post_service.find!(params[:id])
    
    Benjamin Neff's avatar
    Benjamin Neff a validé
        post_service.mark_user_notifications(post.id)
    
        presenter = PostPresenter.new(post, current_user)
    
        respond_to do |format|
    
          format.html do
            gon.post = presenter
            render locals: {post: presenter}
          end
    
    Benjamin Neff's avatar
    Benjamin Neff a validé
          format.mobile { render locals: {post: post} }
    
          format.json { render json: presenter }
    
    Maxwell Salzberg's avatar
    Maxwell Salzberg a validé
      def oembed
        post_id = OEmbedPresenter.id_from_url(params.delete(:url))
    
        post = post_service.find!(post_id)
    
    Benjamin Neff's avatar
    Benjamin Neff a validé
        oembed = params.slice(:format, :maxheight, :minheight)
        render json: OEmbedPresenter.new(post, oembed)
      rescue
    
        head :not_found
    
      def interactions
    
        respond_to do |format|
          format.json {
            post = post_service.find!(params[:id])
            render json: PostInteractionPresenter.new(post, current_user)
          }
    
          format.any { head :not_acceptable }
    
      def mentionable
        respond_to do |format|
          format.json {
            if params[:id].present? && params[:q].present?
              render json: post_service.mentionable_in_comment(params[:id], params[:q])
            else
    
              head :no_content
    
          format.any { head :not_acceptable }
    
        end
      rescue ActiveRecord::RecordNotFound
    
        head :not_found
    
      def destroy
    
    Benjamin Neff's avatar
    Benjamin Neff a validé
        post_service.destroy(params[:id])
    
        respond_to do |format|
    
          format.json { head :no_content }
    
    Maxwell Salzberg's avatar
    Maxwell Salzberg a validé
          format.any { redirect_to stream_path }
    
      private
    
    Benjamin Neff's avatar
    Benjamin Neff a validé
      def post_service
        @post_service ||= PostService.new(current_user)
      end
    
    
      def set_format_if_malformed_from_status_net
    
        request.format = :html if request.format == "application/html+xml"
    
    maxwell's avatar
    maxwell a validé
    end