Files
libr-2/app/controllers/api/v1/posts_controller.rb
T

48 lines
1.1 KiB
Ruby

class Api::V1::PostsController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :authenticate_user_from_token
before_action :permit_params, only: [:create]
after_action :process_post, only: :create
POST_TYPE = {
:ARTICLE => 'article',
:WEB_PAGE => 'web_page'
}
def create
@post = get_post
if @current_user.share_instances.where(sharable: @post).empty?
share_instance = @current_user.share_instances.create sharable: @post, description: params[:description]
render json: share_instance.to_json(include: :sharable)
else
render json: {message: '请勿重复分享同一资源'}, status: 409
end
end
def show
end
def index
render json: @current_user.share_instances.page(params[:page]), each_serializer: ShareInstanceSerializer
end
private
def permit_params
params.permit(:url, :type, :description)
end
def get_post
Post.find_or_create_by!(url: params[:url], type: params[:type])
end
def process_post
unless @post.processed
ProcessPostWorker.perform_async @post.url
end
end
end