ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

221 lines
6.2 KiB

  1. class PostsController < ApplicationController
  2. # GET /posts
  3. def index
  4. limit = params[:limit].presence&.to_i
  5. cursor = params[:cursor].presence
  6. created_at = ('COALESCE(posts.original_created_before - INTERVAL 1 SECOND,' +
  7. 'posts.original_created_from,' +
  8. 'posts.created_at)')
  9. q = filtered_posts.order(Arel.sql("#{ created_at } DESC"))
  10. q = q.where("#{ created_at } < ?", Time.iso8601(cursor)) if cursor
  11. posts = limit ? q.limit(limit + 1) : q
  12. next_cursor = nil
  13. if limit && posts.size > limit
  14. next_cursor = posts.last.created_at.iso8601(6)
  15. posts = posts.first(limit)
  16. end
  17. render json: { posts: posts.map { |post|
  18. post.as_json(include: { tags: { only: [:id, :name, :category, :post_count] } }).tap do |json|
  19. json['thumbnail'] =
  20. if post.thumbnail.attached?
  21. rails_storage_proxy_url(post.thumbnail, only_path: false)
  22. else
  23. nil
  24. end
  25. end
  26. }, next_cursor: }
  27. end
  28. def random
  29. post = filtered_posts.order('RAND()').first
  30. return head :not_found unless post
  31. viewed = current_user&.viewed?(post) || false
  32. render json: (post
  33. .as_json(include: { tags: { only: [:id, :name, :category, :post_count] } })
  34. .merge(viewed:))
  35. end
  36. # GET /posts/1
  37. def show
  38. post = Post.includes(:tags).find(params[:id])
  39. return head :not_found unless post
  40. viewed = current_user&.viewed?(post) || false
  41. json = post.as_json
  42. json['tags'] = build_tag_tree_for(post.tags)
  43. json['related'] = post.related(limit: 20)
  44. json['viewed'] = viewed
  45. render json:
  46. end
  47. # POST /posts
  48. def create
  49. return head :unauthorized unless current_user
  50. return head :forbidden unless current_user.member?
  51. # TODO: URL が正規のものがチェック,不正ならエラー
  52. # TODO: URL は必須にする(タイトルは省略可).
  53. # TODO: サイトに応じて thumbnail_base 設定
  54. title = params[:title]
  55. url = params[:url]
  56. thumbnail = params[:thumbnail]
  57. tag_names = params[:tags].to_s.split(' ')
  58. original_created_from = params[:original_created_from]
  59. original_created_before = params[:original_created_before]
  60. post = Post.new(title:, url:, thumbnail_base: '', uploaded_user: current_user,
  61. original_created_from:, original_created_before:)
  62. post.thumbnail.attach(thumbnail)
  63. if post.save
  64. post.resized_thumbnail!
  65. tags = Tag.normalise_tags(tag_names)
  66. tags = Tag.expand_parent_tags(tags)
  67. sync_post_tags!(post, tags)
  68. render json: post.as_json(include: { tags: { only: [:id, :name, :category, :post_count] } }),
  69. status: :created
  70. else
  71. render json: { errors: post.errors.full_messages }, status: :unprocessable_entity
  72. end
  73. end
  74. def viewed
  75. return head :unauthorized unless current_user
  76. current_user.viewed_posts << Post.find(params[:id])
  77. head :no_content
  78. end
  79. def unviewed
  80. return head :unauthorized unless current_user
  81. current_user.viewed_posts.delete(Post.find(params[:id]))
  82. head :no_content
  83. end
  84. # PATCH/PUT /posts/1
  85. def update
  86. return head :unauthorized unless current_user
  87. return head :forbidden unless current_user.member?
  88. title = params[:title]
  89. tag_names = params[:tags].to_s.split(' ')
  90. original_created_from = params[:original_created_from]
  91. original_created_before = params[:original_created_before]
  92. post = Post.find(params[:id].to_i)
  93. if post.update(title:, original_created_from:, original_created_before:)
  94. tags = post.tags.where(category: 'nico').to_a +
  95. Tag.normalise_tags(tag_names, with_tagme: false)
  96. tags = Tag.expand_parent_tags(tags)
  97. sync_post_tags!(post, tags)
  98. json = post.as_json
  99. json['tags'] = build_tag_tree_for(post.tags)
  100. render json:, status: :ok
  101. else
  102. render json: post.errors, status: :unprocessable_entity
  103. end
  104. end
  105. # DELETE /posts/1
  106. def destroy
  107. end
  108. private
  109. def filtered_posts
  110. tag_names = params[:tags]&.split(' ')
  111. match_type = params[:match]
  112. if tag_names.present?
  113. filter_posts_by_tags(tag_names, match_type)
  114. else
  115. Post.all
  116. end
  117. end
  118. def filter_posts_by_tags tag_names, match_type
  119. posts = Post.joins(:tags)
  120. if match_type == 'any'
  121. posts = posts.where(tags: { name: tag_names }).distinct
  122. else
  123. tag_names.each do |tag|
  124. posts = posts.where(id: Post.joins(:tags).where(tags: { name: tag }))
  125. end
  126. end
  127. posts.distinct
  128. end
  129. def sync_post_tags! post, desired_tags
  130. desired_tags.each do |t|
  131. t.save! if t.new_record?
  132. end
  133. desired_ids = desired_tags.map(&:id).to_set
  134. current_ids = post.tags.pluck(:id).to_set
  135. to_add = desired_ids - current_ids
  136. to_remove = current_ids - desired_ids
  137. Tag.where(id: to_add).find_each do |tag|
  138. begin
  139. PostTag.create!(post:, tag:, created_user: current_user)
  140. rescue ActiveRecord::RecordNotUnique
  141. ;
  142. end
  143. end
  144. PostTag.where(post_id: post.id, tag_id: to_remove.to_a).kept.find_each do |pt|
  145. pt.discard_by!(current_user)
  146. end
  147. end
  148. def build_tag_tree_for tags
  149. tags = tags.to_a
  150. tag_ids = tags.map(&:id)
  151. implications = TagImplication.where(parent_tag_id: tag_ids, tag_id: tag_ids)
  152. children_ids_by_parent = Hash.new { |h, k| h[k] = [] }
  153. implications.each do |imp|
  154. children_ids_by_parent[imp.parent_tag_id] << imp.tag_id
  155. end
  156. child_ids = children_ids_by_parent.values.flatten.uniq
  157. root_ids = tag_ids - child_ids
  158. tags_by_id = tags.index_by(&:id)
  159. memo = { }
  160. build_node = -> tag_id, path do
  161. tag = tags_by_id[tag_id]
  162. return nil unless tag
  163. if path.include?(tag_id)
  164. return tag.as_json(only: [:id, :name, :category, :post_count]).merge(children: [])
  165. end
  166. if memo.key?(tag_id)
  167. return memo[tag_id]
  168. end
  169. new_path = path + [tag_id]
  170. child_ids = children_ids_by_parent[tag_id] || []
  171. children = child_ids.filter_map { |cid| build_node.(cid, new_path) }
  172. memo[tag_id] = tag.as_json(only: [:id, :name, :category, :post_count]).merge(children:)
  173. end
  174. root_ids.filter_map { |id| build_node.call(id, []) }
  175. end
  176. end