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

152 lines
3.9 KiB

  1. class WikiPagesController < ApplicationController
  2. def index
  3. wiki_pages = WikiPage.all
  4. render json: wiki_pages
  5. end
  6. def show
  7. render_wiki_page_or_404 WikiPage.find(params[:id])
  8. end
  9. def show_by_title
  10. render_wiki_page_or_404 WikiPage.find_by(title: params[:title])
  11. end
  12. def exists
  13. if WikiPage.exists?(params[:id])
  14. head :no_content
  15. else
  16. head :not_found
  17. end
  18. end
  19. def exists_by_title
  20. if WikiPage.exists?(title: params[:title])
  21. head :no_content
  22. else
  23. head :not_found
  24. end
  25. end
  26. def diff
  27. id = params[:id]
  28. from = params[:from]
  29. to = params[:to].presence
  30. return head :bad_request if id.blank? || from.blank?
  31. wiki_page_from = WikiPage.find(id)
  32. wiki_page_to = WikiPage.find(id)
  33. wiki_page_from.sha = from
  34. wiki_page_to.sha = to
  35. diffs = Diff::LCS.sdiff(wiki_page_from.body, wiki_page_to.body)
  36. diff_json = diffs.map { |change|
  37. case change.action
  38. when ?=
  39. { type: 'context', content: change.old_element }
  40. when ?|
  41. [{ type: 'removed', content: change.old_element },
  42. { type: 'added', content: change.new_element }]
  43. when ?+
  44. { type: 'added', content: change.new_element }
  45. when ?-
  46. { type: 'removed', content: change.old_element }
  47. end
  48. }.flatten.compact
  49. render json: { wiki_page_id: wiki_page_from.id,
  50. title: wiki_page_from.title,
  51. older_sha: wiki_page_from.sha,
  52. newer_sha: wiki_page_to.sha,
  53. diff: diff_json }
  54. end
  55. def create
  56. return head :unauthorized unless current_user
  57. return head :forbidden unless ['admin', 'member'].include?(current_user.role)
  58. wiki_page = WikiPage.new(title: params[:title], created_user: current_user, updated_user: current_user)
  59. if wiki_page.save
  60. wiki_page.set_body params[:body], user: current_user
  61. render json: wiki_page, status: :created
  62. else
  63. render json: { errors: wiki_page.errors.full_messages }, status: :unprocessable_entity
  64. end
  65. end
  66. def update
  67. return head :unauthorized unless current_user
  68. return head :forbidden unless current_user.member?
  69. title = params[:title]
  70. body = params[:body]
  71. return head :unprocessable_entity if title.blank? || body.blank?
  72. wiki_page = WikiPage.find(params[:id])
  73. wiki_page.title = title
  74. wiki_page.updated_user = current_user
  75. wiki_page.set_body(body, user: current_user)
  76. wiki_page.save!
  77. head :ok
  78. end
  79. def search
  80. title = params[:title]&.strip
  81. q = WikiPage.all
  82. q = q.where('title LIKE ?', "%#{ WikiPage.sanitize_sql_like(title) }%") if title.present?
  83. render json: q.limit(20)
  84. end
  85. def changes
  86. id = params[:id]
  87. log = if id.present?
  88. wiki.page("#{ id }.md")&.versions
  89. else
  90. wiki.repo.log('main', nil)
  91. end
  92. return render json: [] unless log
  93. render json: log.map { |commit|
  94. wiki_page = WikiPage.find(commit.message.split(' ')[1].to_i)
  95. wiki_page.sha = commit.id
  96. next nil if wiki_page.sha.blank?
  97. user = User.find(commit.author.name.to_i)
  98. { sha: wiki_page.sha,
  99. pred: wiki_page.pred,
  100. succ: wiki_page.succ,
  101. wiki_page: wiki_page && { id: wiki_page.id, title: wiki_page.title },
  102. user: user && { id: user.id, name: user.name },
  103. change_type: commit.message.split(' ')[0].downcase[0...(-1)],
  104. timestamp: commit.authored_date }
  105. }.compact
  106. end
  107. private
  108. WIKI_PATH = Rails.root.join('wiki').to_s
  109. def wiki
  110. @wiki ||= Gollum::Wiki.new(WIKI_PATH)
  111. end
  112. def render_wiki_page_or_404 wiki_page
  113. return head :not_found unless wiki_page
  114. wiki_page.sha = params[:version].presence
  115. body = wiki_page.body
  116. sha = wiki_page.sha
  117. pred = wiki_page.pred
  118. succ = wiki_page.succ
  119. render json: wiki_page.as_json.merge(body:, sha:, pred:, succ:)
  120. end
  121. end