ぼざクリタグ広場 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.
 
 
 
 
 
 

193 lines
5.4 KiB

  1. class WikiPagesController < ApplicationController
  2. rescue_from Wiki::Commit::Conflict, with: :render_wiki_conflict
  3. def index
  4. title = params[:title].to_s.strip
  5. if title.blank?
  6. return render json: WikiPage.joins(:tag_name)
  7. .includes(:tag_name)
  8. .as_json(methods: [:title])
  9. end
  10. q = WikiPage.joins(:tag_name).includes(:tag_name)
  11. .where('tag_names.name LIKE ?', "%#{ WikiPage.sanitize_sql_like(title) }%")
  12. render json: q.limit(20).as_json(methods: [:title])
  13. end
  14. def show
  15. page = WikiPage.joins(:tag_name)
  16. .includes(:tag_name)
  17. .find_by(id: params[:id])
  18. render_wiki_page_or_404 page
  19. end
  20. def show_by_title
  21. title = params[:title].to_s.strip
  22. page = WikiPage.joins(:tag_name)
  23. .includes(:tag_name)
  24. .find_by(tag_name: { name: title })
  25. render_wiki_page_or_404 page
  26. end
  27. def exists
  28. if WikiPage.exists?(id: params[:id])
  29. head :no_content
  30. else
  31. head :not_found
  32. end
  33. end
  34. def exists_by_title
  35. title = params[:title].to_s.strip
  36. if WikiPage.joins(:tag_name).exists?(tag_names: { name: title })
  37. head :no_content
  38. else
  39. head :not_found
  40. end
  41. end
  42. def diff
  43. id = params[:id]
  44. return head :bad_request if id.blank?
  45. from = params[:from].presence
  46. to = params[:to].presence
  47. page = WikiPage.joins(:tag_name).includes(:tag_name).find(id)
  48. from_rev = from && page.wiki_revisions.find(from)
  49. to_rev = to ? page.wiki_revisions.find(to) : page.current_revision
  50. if ((from_rev && !(from_rev.content?)) || !(to_rev&.content?))
  51. return head :unprocessable_entity
  52. end
  53. diffs = Diff::LCS.sdiff(from_rev&.body&.lines || [], to_rev.body.lines)
  54. diff_json = diffs.map { |change|
  55. case change.action
  56. when ?=
  57. { type: 'context', content: change.old_element }
  58. when ?!
  59. [{ type: 'removed', content: change.old_element },
  60. { type: 'added', content: change.new_element }]
  61. when ?+
  62. { type: 'added', content: change.new_element }
  63. when ?-
  64. { type: 'removed', content: change.old_element }
  65. end
  66. }.flatten.compact
  67. render json: { wiki_page_id: page.id,
  68. title: page.title,
  69. older_revision_id: from_rev&.id,
  70. newer_revision_id: to_rev.id,
  71. diff: diff_json }
  72. end
  73. def create
  74. return head :unauthorized unless current_user
  75. return head :forbidden unless current_user.member?
  76. title = params[:title]&.strip
  77. body = params[:body].to_s
  78. return head :unprocessable_entity if title.blank? || body.blank?
  79. page = WikiPage.new(title:, created_user: current_user, updated_user: current_user)
  80. if page.save
  81. message = params[:message].presence
  82. Wiki::Commit.content!(page:, body:, created_user: current_user, message:)
  83. render json: page.as_json(methods: [:title]), status: :created
  84. else
  85. render json: { errors: page.errors.full_messages },
  86. status: :unprocessable_entity
  87. end
  88. end
  89. def update
  90. return head :unauthorized unless current_user
  91. return head :forbidden unless current_user.member?
  92. title = params[:title]&.strip
  93. body = params[:body].to_s
  94. return head :unprocessable_entity if title.blank? || body.blank?
  95. page = WikiPage.find(params[:id])
  96. base_revision_id = page.current_revision.id
  97. if params[:title].present? && params[:title].strip != page.title
  98. return head :unprocessable_entity
  99. end
  100. message = params[:message].presence
  101. Wiki::Commit.content!(page:,
  102. body:,
  103. created_user: current_user,
  104. message:,
  105. base_revision_id:)
  106. head :ok
  107. end
  108. def search
  109. index
  110. end
  111. def changes
  112. id = params[:id].presence
  113. q = WikiRevision.joins(wiki_page: :tag_name)
  114. .includes(:created_user, wiki_page: :tag_name)
  115. .order(id: :desc)
  116. q = q.where(wiki_page_id: id) if id
  117. render json: q.limit(200).map { |rev|
  118. { revision_id: rev.id,
  119. pred: rev.base_revision_id,
  120. succ: nil,
  121. wiki_page: { id: rev.wiki_page_id, title: rev.wiki_page.title },
  122. user: rev.created_user && { id: rev.created_user.id, name: rev.created_user.name },
  123. kind: rev.kind,
  124. message: rev.message,
  125. timestamp: rev.created_at }
  126. }.compact
  127. end
  128. private
  129. def render_wiki_page_or_404 page
  130. return head :not_found unless page
  131. rev = find_revision(page)
  132. return head :not_found unless rev
  133. if rev.redirect?
  134. return (
  135. redirect_to wiki_page_by_title_path(title: rev.redirect_page.title),
  136. status: :moved_permanently)
  137. end
  138. body = rev.body
  139. revision_id = rev.id
  140. pred = page.pred_revision_id(revision_id)
  141. succ = page.succ_revision_id(revision_id)
  142. updated_at = rev.created_at
  143. render json: page.as_json(methods: [:title])
  144. .merge(body:, revision_id:, pred:, succ:, updated_at:)
  145. end
  146. def find_revision page
  147. if params[:version].present?
  148. page.wiki_revisions.find_by(id: params[:version])
  149. else
  150. page.current_revision
  151. end
  152. end
  153. def render_wiki_conflict err
  154. render json: { error: 'conflict', message: err.message }, status: :conflict
  155. end
  156. end