Files
libr-2/app/controllers/api/v1/posts_controller.rb
T
2018-06-05 08:04:42 +10:00

108 lines
3.1 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
class Api::V1::PostsController < Api::V1::ApplicationController
before_action :permit_params, :get_post, only: [:create]
before_action :load_post, only: [:add_viewed_times, :show]
skip_before_action :authenticate_user_from_token, only: [:posts, :show, :liked_posts, :bookmarked_posts, :random]
before_action :fetch_current_user
before_action :load_target_user, only: [:posts, :liked_posts, :bookmarked_posts]
# after_action :process_post, only: :create
POST_TYPE = {
:ARTICLE => 'article',
:WEB_PAGE => 'web_page'
}
def create
share_instance = @current_user.share_instances.create! sharable: @post, description: params[:description]
@current_user.timeline_sources.create! post_id: @post.id, action_type: 'share'
@post.images.create permit_params[:images]
#render json: share_instance.to_json(include: :sharable)
render json: @post.to_json(include: :images)
end
def show
render json: @target_post, serializer: PostSerializer
end
def index
post_ids = @current_user.share_instances.order('created_at DESC').page(params[:page]).pluck(:sharable_id)
render json: Post.where(id: post_ids), each_serializer: EachPostSerializer
end
def add_viewed_times
@target_post.increment!(:viewed_times)
render nothing: true
end
def posts
post_ids = @target_user.share_instances.order('created_at DESC').page(params[:page]).pluck(:sharable_id)
render json: Post.where(id: post_ids), each_serializer: EachPostSerializer
end
def liked_posts
ids = Like.where(user_id: @target_user.id).order('created_at DESC').pluck(:likable_id)
render json: Post.where(id: ids).page(params[:page]), each_serializer: EachPostSerializer
end
def bookmarked_posts
ids = Favorite.where(user_id: @target_user.id).order('created_at DESC').pluck(:favoritable_id)
render json: Post.where(id: ids).page(params[:page]), each_serializer: EachPostSerializer
end
def search
params.permit(:keywords)
render json: Post.where('title LIKE ?', "%#{params[:keywords]}%").page(params[:page]), each_serializer: EachPostSerializer
end
def random
render json: Post.order('RANDOM()').limit(1).first, serializer: PostSerializer
end
private
def permit_params
params.permit(:url, :type, :description, :title, :images => [:url, :title, :key])
end
def get_post
begin
@post = Post.find_or_create_by!(title: params[:title], url: 'NONE', type: 'Article')
rescue ActiveRecord::RecordInvalid => e
Rails.logger.warn e
return render json: (msg e), status: 400
end
end
def map_post_type
case params[:type]
when 'article'
@type = 'Article'
when 'page'
@type = 'WebPage'
else
return render json: {msg: 'Post 类型不合法'}, status: 400
end
end
def check_url
begin
uri = URI.parse params[:url]
res = uri.kind_of?(URI::HTTP)
rescue URI::InvalidURIError
res = false
end
return render json: {msg: 'URL不合法, 请重新输入'}, status: 400 unless res
end
def process_post
if !@post.processed
ProcessPostWorker.perform_async @post.url
end
end
end