コミットを比較
68 コミット
| 作成者 | SHA1 | 日付 | |
|---|---|---|---|
| 409498729a | |||
| 9e3cbd2469 | |||
| f7af5efaf9 | |||
| 4235903b49 | |||
| 17ff0e8114 | |||
| 16e9b8ca49 | |||
| 6b1079fc1a | |||
| 7885f6dfb9 | |||
| a66a73e004 | |||
| 98330b00bb | |||
| 95ea097636 | |||
| e0e7a22c38 | |||
| da45bc95eb | |||
| 55dcd4e1f3 | |||
| 1e54fb5be4 | |||
| 1350ae3d99 | |||
| a088503272 | |||
| f8d2d753fe | |||
| 7d554e3950 | |||
| 4b447e952b | |||
| c3aa8bc580 | |||
| 663206c14c | |||
| 3e5eb4687b | |||
| 2a412b589f | |||
| 7d1a87f452 | |||
| 498f215538 | |||
| a0d6aeb91e | |||
| be983e4ad1 | |||
| 82302cd3d1 | |||
| a01c63d972 | |||
| 16bfc6eba2 | |||
| 67c2590f2b | |||
| eb975e5301 | |||
| 1a776e348a | |||
| 9b7fc9059d | |||
| 797e67ac37 | |||
| f3cd108b2e | |||
| 5dd683bd4f | |||
| 5451d9ec9f | |||
| 5a02be0517 | |||
| 39ec57c142 | |||
| 01646ebcb7 | |||
| 200d457d22 | |||
| effde89b07 | |||
| 19c622c5bf | |||
| 9d68367a5a | |||
| 7acdc16c75 | |||
| 0f64ec00f4 | |||
| ef3d428a06 | |||
| f6de272f55 | |||
| 86209dcc84 | |||
| 16ecbd1fc9 | |||
| dd838fdf80 | |||
| c8c262e34d | |||
| fa2030f9a5 | |||
| 74141f2a84 | |||
| f2b774ab1d | |||
| 622f47a856 | |||
| 189175fe84 | |||
| 5160aacc53 | |||
| 5f306b04b8 | |||
| b309263df5 | |||
| 3f2c0a52b9 | |||
| 49888627c9 | |||
| df38835f6e | |||
| 02ea0df683 | |||
| 611455ec55 | |||
| 6cf42e38c6 |
@@ -0,0 +1 @@
|
||||
--require spec_helper
|
||||
@@ -46,6 +46,8 @@ group :development, :test do
|
||||
|
||||
# Omakase Ruby styling [https://github.com/rails/rubocop-rails-omakase/]
|
||||
gem "rubocop-rails-omakase", require: false
|
||||
|
||||
gem 'factory_bot_rails'
|
||||
end
|
||||
|
||||
|
||||
@@ -65,3 +67,5 @@ gem 'dotenv-rails'
|
||||
gem 'whenever', require: false
|
||||
|
||||
gem 'discard'
|
||||
|
||||
gem "rspec-rails", "~> 8.0", :groups => [:development, :test]
|
||||
|
||||
@@ -99,6 +99,11 @@ GEM
|
||||
drb (2.2.1)
|
||||
ed25519 (1.4.0)
|
||||
erubi (1.13.1)
|
||||
factory_bot (6.5.6)
|
||||
activesupport (>= 6.1.0)
|
||||
factory_bot_rails (6.5.1)
|
||||
factory_bot (~> 6.5)
|
||||
railties (>= 6.1.0)
|
||||
ffi (1.17.2-aarch64-linux-gnu)
|
||||
ffi (1.17.2-aarch64-linux-musl)
|
||||
ffi (1.17.2-arm-linux-gnu)
|
||||
@@ -298,6 +303,23 @@ GEM
|
||||
io-console (~> 0.5)
|
||||
rexml (3.4.1)
|
||||
rouge (3.30.0)
|
||||
rspec-core (3.13.6)
|
||||
rspec-support (~> 3.13.0)
|
||||
rspec-expectations (3.13.5)
|
||||
diff-lcs (>= 1.2.0, < 2.0)
|
||||
rspec-support (~> 3.13.0)
|
||||
rspec-mocks (3.13.7)
|
||||
diff-lcs (>= 1.2.0, < 2.0)
|
||||
rspec-support (~> 3.13.0)
|
||||
rspec-rails (8.0.2)
|
||||
actionpack (>= 7.2)
|
||||
activesupport (>= 7.2)
|
||||
railties (>= 7.2)
|
||||
rspec-core (~> 3.13)
|
||||
rspec-expectations (~> 3.13)
|
||||
rspec-mocks (~> 3.13)
|
||||
rspec-support (~> 3.13)
|
||||
rspec-support (3.13.6)
|
||||
rss (0.3.1)
|
||||
rexml
|
||||
rubocop (1.75.6)
|
||||
@@ -424,6 +446,7 @@ DEPENDENCIES
|
||||
diff-lcs
|
||||
discard
|
||||
dotenv-rails
|
||||
factory_bot_rails
|
||||
gollum
|
||||
image_processing (~> 1.14)
|
||||
jwt
|
||||
@@ -433,6 +456,7 @@ DEPENDENCIES
|
||||
puma (>= 5.0)
|
||||
rack-cors
|
||||
rails (~> 8.0.2)
|
||||
rspec-rails (~> 8.0)
|
||||
rubocop-rails-omakase
|
||||
sprockets-rails
|
||||
sqlite3 (>= 2.1)
|
||||
|
||||
@@ -11,4 +11,15 @@ class ApplicationController < ActionController::API
|
||||
code = request.headers['X-Transfer-Code'] || request.headers['HTTP_X_TRANSFER_CODE']
|
||||
@current_user = User.find_by(inheritance_code: code)
|
||||
end
|
||||
|
||||
def bool? key, default: false
|
||||
return default if params[key].nil?
|
||||
|
||||
s = params[key].to_s.strip.downcase
|
||||
if default
|
||||
!(s.in?(['0', 'false', 'off', 'no']))
|
||||
else
|
||||
s.in?(['', '1', 'true', 'on', 'yes'])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
class DeerjikistsController < ApplicationController
|
||||
def show
|
||||
platform = params[:platform].to_s.strip
|
||||
code = params[:code].to_s.strip
|
||||
return head :bad_request if platform.blank? || code.blank?
|
||||
|
||||
deerjikist = Deerjikist
|
||||
.joins(:tag)
|
||||
.includes(tag: :tag_name)
|
||||
.find_by(platform:, code:)
|
||||
if deerjikist
|
||||
render json: DeerjikistRepr.base(deerjikist)
|
||||
else
|
||||
head :not_found
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
return head :unauthorized unless current_user
|
||||
return head :forbidden unless current_user.gte_member?
|
||||
|
||||
platform = params[:platform].to_s.strip
|
||||
code = params[:code].to_s.strip
|
||||
tag_id = params[:tag_id].to_i
|
||||
return head :bad_request if platform.blank? || code.blank? || tag_id <= 0
|
||||
|
||||
deerjikist = Deerjikist.find_or_initialize_by(platform:, code:).tap do |d|
|
||||
d.tag_id = tag_id
|
||||
d.save!
|
||||
end
|
||||
|
||||
render json: DeerjikistRepr.base(deerjikist)
|
||||
end
|
||||
|
||||
def destroy
|
||||
return head :unauthorized unless current_user
|
||||
return head :forbidden unless current_user.gte_member?
|
||||
|
||||
platform = params[:platform].to_s.strip
|
||||
code = params[:code].to_s.strip
|
||||
return head :bad_request if platform.blank? || code.blank?
|
||||
|
||||
Deerjikist.find([platform, code]).destroy!
|
||||
|
||||
head :no_content
|
||||
end
|
||||
end
|
||||
@@ -1,20 +0,0 @@
|
||||
class IpAddressesController < ApplicationController
|
||||
def index
|
||||
@ip_addresses = IpAddress.all
|
||||
|
||||
render json: @ip_addresses
|
||||
end
|
||||
|
||||
def show
|
||||
render json: @ip_address
|
||||
end
|
||||
|
||||
def create
|
||||
end
|
||||
|
||||
def update
|
||||
end
|
||||
|
||||
def destroy
|
||||
end
|
||||
end
|
||||
@@ -1,16 +0,0 @@
|
||||
class NicoTagRelationController < ApplicationController
|
||||
def index
|
||||
end
|
||||
|
||||
def show
|
||||
end
|
||||
|
||||
def create
|
||||
end
|
||||
|
||||
def update
|
||||
end
|
||||
|
||||
def destroy
|
||||
end
|
||||
end
|
||||
@@ -3,10 +3,12 @@ class NicoTagsController < ApplicationController
|
||||
limit = (params[:limit] || 20).to_i
|
||||
cursor = params[:cursor].presence
|
||||
|
||||
q = Tag.nico_tags.includes(:linked_tags).order(updated_at: :desc)
|
||||
q = Tag.nico_tags
|
||||
.includes(:tag_name, tag_name: :wiki_page, linked_tags: { tag_name: :wiki_page })
|
||||
.order(updated_at: :desc)
|
||||
q = q.where('tags.updated_at < ?', Time.iso8601(cursor)) if cursor
|
||||
|
||||
tags = q.limit(limit + 1)
|
||||
tags = q.limit(limit + 1).to_a
|
||||
|
||||
next_cursor = nil
|
||||
if tags.size > limit
|
||||
@@ -15,13 +17,15 @@ class NicoTagsController < ApplicationController
|
||||
end
|
||||
|
||||
render json: { tags: tags.map { |tag|
|
||||
tag.as_json(include: :linked_tags)
|
||||
TagRepr.base(tag).merge(linked_tags: tag.linked_tags.map { |lt|
|
||||
TagRepr.base(lt)
|
||||
})
|
||||
}, next_cursor: }
|
||||
end
|
||||
|
||||
def update
|
||||
return head :unauthorized unless current_user
|
||||
return head :forbidden unless current_user.member?
|
||||
return head :forbidden unless current_user.gte_member?
|
||||
|
||||
id = params[:id].to_i
|
||||
|
||||
@@ -30,12 +34,11 @@ class NicoTagsController < ApplicationController
|
||||
|
||||
linked_tag_names = params[:tags].to_s.split(' ')
|
||||
linked_tags = Tag.normalise_tags(linked_tag_names, with_tagme: false)
|
||||
return head :bad_request if linked_tags.filter { |t| t.category == 'nico' }.present?
|
||||
return head :bad_request if linked_tags.any? { |t| t.category == 'nico' }
|
||||
|
||||
tag.linked_tags = linked_tags
|
||||
tag.updated_at = Time.now
|
||||
tag.save!
|
||||
|
||||
render json: tag.linked_tags, status: :ok
|
||||
render json: tag.linked_tags.map { |t| TagRepr.base(t) }, status: :ok
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
class PostTagsController < ApplicationController
|
||||
def index
|
||||
end
|
||||
|
||||
def show
|
||||
end
|
||||
|
||||
def create
|
||||
end
|
||||
|
||||
def update
|
||||
end
|
||||
|
||||
def destroy
|
||||
end
|
||||
end
|
||||
@@ -1,42 +1,89 @@
|
||||
class PostsController < ApplicationController
|
||||
Event = Struct.new(:post, :tag, :user, :change_type, :timestamp, keyword_init: true)
|
||||
|
||||
# GET /posts
|
||||
def index
|
||||
url = params[:url].presence
|
||||
title = params[:title].presence
|
||||
original_created_from = params[:original_created_from].presence
|
||||
original_created_to = params[:original_created_to].presence
|
||||
created_between = params[:created_from].presence, params[:created_to].presence
|
||||
updated_between = params[:updated_from].presence, params[:updated_to].presence
|
||||
|
||||
order = params[:order].to_s.split(':', 2).map(&:strip)
|
||||
unless order[0].in?(['title', 'url', 'original_created_at', 'created_at', 'updated_at'])
|
||||
order[0] = 'original_created_at'
|
||||
end
|
||||
unless order[1].in?(['asc', 'desc'])
|
||||
order[1] =
|
||||
if order[0].in?(['title', 'url'])
|
||||
'asc'
|
||||
else
|
||||
'desc'
|
||||
end
|
||||
end
|
||||
|
||||
page = (params[:page].presence || 1).to_i
|
||||
limit = (params[:limit].presence || 20).to_i
|
||||
cursor = params[:cursor].presence
|
||||
|
||||
page = 1 if page < 1
|
||||
limit = 1 if limit < 1
|
||||
|
||||
offset = (page - 1) * limit
|
||||
|
||||
sort_sql =
|
||||
'COALESCE(posts.original_created_before - INTERVAL 1 SECOND,' +
|
||||
'posts.original_created_from,' +
|
||||
'posts.created_at)'
|
||||
pt_max_sql =
|
||||
PostTag
|
||||
.select('post_id, MAX(updated_at) AS max_updated_at')
|
||||
.group('post_id')
|
||||
.to_sql
|
||||
|
||||
updated_at_all_sql =
|
||||
'GREATEST(posts.updated_at,' +
|
||||
'COALESCE(pt_max.max_updated_at, posts.updated_at))'
|
||||
|
||||
q =
|
||||
filtered_posts
|
||||
.preload(:tags)
|
||||
.joins("LEFT JOIN (#{ pt_max_sql }) pt_max ON pt_max.post_id = posts.id")
|
||||
.reselect('posts.*', Arel.sql("#{ updated_at_all_sql } AS updated_at_all"))
|
||||
.preload(tags: { tag_name: :wiki_page })
|
||||
.with_attached_thumbnail
|
||||
.select("posts.*, #{ sort_sql } AS sort_ts")
|
||||
.order(Arel.sql("#{ sort_sql } DESC"))
|
||||
posts = (
|
||||
if cursor
|
||||
q.where("#{ sort_sql } < ?", Time.iso8601(cursor)).limit(limit + 1)
|
||||
else
|
||||
q.limit(limit).offset(offset)
|
||||
end).to_a
|
||||
|
||||
next_cursor = nil
|
||||
if cursor && posts.length > limit
|
||||
next_cursor = posts.last.read_attribute('sort_ts').iso8601(6)
|
||||
posts = posts.first(limit)
|
||||
q = q.where('posts.url LIKE ?', "%#{ url }%") if url
|
||||
q = q.where('posts.title LIKE ?', "%#{ title }%") if title
|
||||
if original_created_from
|
||||
q = q.where('posts.original_created_before > ?', original_created_from)
|
||||
end
|
||||
if original_created_to
|
||||
q = q.where('posts.original_created_from <= ?', original_created_to)
|
||||
end
|
||||
q = q.where('posts.created_at >= ?', created_between[0]) if created_between[0]
|
||||
q = q.where('posts.created_at <= ?', created_between[1]) if created_between[1]
|
||||
if updated_between[0]
|
||||
q = q.where("#{ updated_at_all_sql } >= ?", updated_between[0])
|
||||
end
|
||||
if updated_between[1]
|
||||
q = q.where("#{ updated_at_all_sql } <= ?", updated_between[1])
|
||||
end
|
||||
|
||||
sort_sql =
|
||||
case order[0]
|
||||
when 'original_created_at'
|
||||
'COALESCE(posts.original_created_before - INTERVAL 1 MINUTE,' +
|
||||
'posts.original_created_from,' +
|
||||
'posts.created_at) '
|
||||
when 'updated_at'
|
||||
updated_at_all_sql
|
||||
else
|
||||
"posts.#{ order[0] }"
|
||||
end
|
||||
posts = q.order(Arel.sql("#{ sort_sql } #{ order[1] }, id #{ order[1] }"))
|
||||
.limit(limit)
|
||||
.offset(offset)
|
||||
.to_a
|
||||
|
||||
q = q.except(:select, :order)
|
||||
|
||||
render json: { posts: posts.map { |post|
|
||||
post.as_json(include: { tags: { only: [:id, :name, :category, :post_count] } }).tap do |json|
|
||||
PostRepr.base(post).merge(updated_at: post.updated_at_all).tap do |json|
|
||||
json['thumbnail'] =
|
||||
if post.thumbnail.attached?
|
||||
rails_storage_proxy_url(post.thumbnail, only_path: false)
|
||||
@@ -44,23 +91,22 @@ class PostsController < ApplicationController
|
||||
nil
|
||||
end
|
||||
end
|
||||
}, count: filtered_posts.count(:id), next_cursor: }
|
||||
}, count: q.group_values.present? ? q.count.size : q.count }
|
||||
end
|
||||
|
||||
def random
|
||||
post = filtered_posts.order('RAND()').first
|
||||
post = filtered_posts.preload(tags: { tag_name: :wiki_page })
|
||||
.order('RAND()')
|
||||
.first
|
||||
return head :not_found unless post
|
||||
|
||||
viewed = current_user&.viewed?(post) || false
|
||||
|
||||
render json: (post
|
||||
.as_json(include: { tags: { only: [:id, :name, :category, :post_count] } })
|
||||
.merge(viewed:))
|
||||
render json: PostRepr.base(post).merge(viewed:)
|
||||
end
|
||||
|
||||
# GET /posts/1
|
||||
def show
|
||||
post = Post.includes(:tags).find(params[:id])
|
||||
post = Post.includes(tags: { tag_name: :wiki_page }).find_by(id: params[:id])
|
||||
return head :not_found unless post
|
||||
|
||||
viewed = current_user&.viewed?(post) || false
|
||||
@@ -73,22 +119,19 @@ class PostsController < ApplicationController
|
||||
render json:
|
||||
end
|
||||
|
||||
# POST /posts
|
||||
def create
|
||||
return head :unauthorized unless current_user
|
||||
return head :forbidden unless current_user.member?
|
||||
return head :forbidden unless current_user.gte_member?
|
||||
|
||||
# TODO: URL が正規のものがチェック,不正ならエラー
|
||||
# TODO: URL は必須にする(タイトルは省略可).
|
||||
# TODO: サイトに応じて thumbnail_base 設定
|
||||
title = params[:title]
|
||||
title = params[:title].presence
|
||||
url = params[:url]
|
||||
thumbnail = params[:thumbnail]
|
||||
tag_names = params[:tags].to_s.split(' ')
|
||||
tag_names = params[:tags].to_s.split
|
||||
original_created_from = params[:original_created_from]
|
||||
original_created_before = params[:original_created_before]
|
||||
|
||||
post = Post.new(title:, url:, thumbnail_base: '', uploaded_user: current_user,
|
||||
post = Post.new(title:, url:, thumbnail_base: nil, uploaded_user: current_user,
|
||||
original_created_from:, original_created_before:)
|
||||
post.thumbnail.attach(thumbnail)
|
||||
if post.save
|
||||
@@ -96,11 +139,14 @@ class PostsController < ApplicationController
|
||||
tags = Tag.normalise_tags(tag_names)
|
||||
tags = Tag.expand_parent_tags(tags)
|
||||
sync_post_tags!(post, tags)
|
||||
render json: post.as_json(include: { tags: { only: [:id, :name, :category, :post_count] } }),
|
||||
status: :created
|
||||
|
||||
post.reload
|
||||
render json: PostRepr.base(post), status: :created
|
||||
else
|
||||
render json: { errors: post.errors.full_messages }, status: :unprocessable_entity
|
||||
end
|
||||
rescue Tag::NicoTagNormalisationError
|
||||
head :bad_request
|
||||
end
|
||||
|
||||
def viewed
|
||||
@@ -117,13 +163,12 @@ class PostsController < ApplicationController
|
||||
head :no_content
|
||||
end
|
||||
|
||||
# PATCH/PUT /posts/1
|
||||
def update
|
||||
return head :unauthorized unless current_user
|
||||
return head :forbidden unless current_user.member?
|
||||
return head :forbidden unless current_user.gte_member?
|
||||
|
||||
title = params[:title]
|
||||
tag_names = params[:tags].to_s.split(' ')
|
||||
title = params[:title].presence
|
||||
tag_names = params[:tags].to_s.split
|
||||
original_created_from = params[:original_created_from]
|
||||
original_created_before = params[:original_created_before]
|
||||
|
||||
@@ -133,20 +178,20 @@ class PostsController < ApplicationController
|
||||
Tag.normalise_tags(tag_names, with_tagme: false)
|
||||
tags = Tag.expand_parent_tags(tags)
|
||||
sync_post_tags!(post, tags)
|
||||
|
||||
post.reload
|
||||
json = post.as_json
|
||||
json['tags'] = build_tag_tree_for(post.tags)
|
||||
render json:, status: :ok
|
||||
else
|
||||
render json: post.errors, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /posts/1
|
||||
def destroy
|
||||
rescue Tag::NicoTagNormalisationError
|
||||
head :bad_request
|
||||
end
|
||||
|
||||
def changes
|
||||
id = params[:id]
|
||||
id = params[:id].presence
|
||||
page = (params[:page].presence || 1).to_i
|
||||
limit = (params[:limit].presence || 20).to_i
|
||||
|
||||
@@ -157,36 +202,40 @@ class PostsController < ApplicationController
|
||||
|
||||
pts = PostTag.with_discarded
|
||||
pts = pts.where(post_id: id) if id.present?
|
||||
pts = pts.includes(:post, :tag, :created_user, :deleted_user)
|
||||
pts = pts.includes(:post, :created_user, :deleted_user,
|
||||
tag: { tag_name: :wiki_page })
|
||||
|
||||
events = []
|
||||
pts.each do |pt|
|
||||
tag = TagRepr.base(pt.tag)
|
||||
post = pt.post
|
||||
|
||||
events << Event.new(
|
||||
post: pt.post,
|
||||
tag: pt.tag,
|
||||
user: pt.created_user && { id: pt.created_user.id, name: pt.created_user.name },
|
||||
change_type: 'add',
|
||||
timestamp: pt.created_at)
|
||||
post:,
|
||||
tag:,
|
||||
user: pt.created_user && { id: pt.created_user.id, name: pt.created_user.name },
|
||||
change_type: 'add',
|
||||
timestamp: pt.created_at)
|
||||
|
||||
if pt.discarded_at
|
||||
events << Event.new(
|
||||
post: pt.post,
|
||||
tag: pt.tag,
|
||||
user: pt.deleted_user && { id: pt.deleted_user.id, name: pt.deleted_user.name },
|
||||
change_type: 'remove',
|
||||
timestamp: pt.discarded_at)
|
||||
post:,
|
||||
tag:,
|
||||
user: pt.deleted_user && { id: pt.deleted_user.id, name: pt.deleted_user.name },
|
||||
change_type: 'remove',
|
||||
timestamp: pt.discarded_at)
|
||||
end
|
||||
end
|
||||
events.sort_by!(&:timestamp)
|
||||
events.reverse!
|
||||
|
||||
render json: { changes: events.slice(offset, limit).as_json, count: events.size }
|
||||
render json: { changes: (events.slice(offset, limit) || []).as_json, count: events.size }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def filtered_posts
|
||||
tag_names = params[:tags]&.split(' ')
|
||||
tag_names = params[:tags].to_s.split
|
||||
match_type = params[:match]
|
||||
if tag_names.present?
|
||||
filter_posts_by_tags(tag_names, match_type)
|
||||
@@ -196,15 +245,17 @@ class PostsController < ApplicationController
|
||||
end
|
||||
|
||||
def filter_posts_by_tags tag_names, match_type
|
||||
posts = Post.joins(:tags)
|
||||
tag_names = TagName.canonicalise(tag_names)
|
||||
|
||||
posts = Post.joins(tags: :tag_name)
|
||||
|
||||
if match_type == 'any'
|
||||
posts = posts.where(tags: { name: tag_names }).distinct
|
||||
posts.where(tag_names: { name: tag_names }).distinct
|
||||
else
|
||||
tag_names.each do |tag|
|
||||
posts = posts.where(id: Post.joins(:tags).where(tags: { name: tag }))
|
||||
end
|
||||
posts.where(tag_names: { name: tag_names })
|
||||
.group('posts.id')
|
||||
.having('COUNT(DISTINCT tag_names.id) = ?', tag_names.uniq.size)
|
||||
end
|
||||
posts.distinct
|
||||
end
|
||||
|
||||
def sync_post_tags! post, desired_tags
|
||||
@@ -255,7 +306,7 @@ class PostsController < ApplicationController
|
||||
return nil unless tag
|
||||
|
||||
if path.include?(tag_id)
|
||||
return tag.as_json(only: [:id, :name, :category, :post_count]).merge(children: [])
|
||||
return TagRepr.base(tag).merge(children: [])
|
||||
end
|
||||
|
||||
if memo.key?(tag_id)
|
||||
@@ -267,7 +318,7 @@ class PostsController < ApplicationController
|
||||
|
||||
children = child_ids.filter_map { |cid| build_node.(cid, new_path) }
|
||||
|
||||
memo[tag_id] = tag.as_json(only: [:id, :name, :category, :post_count]).merge(children:)
|
||||
memo[tag_id] = TagRepr.base(tag).merge(children:)
|
||||
end
|
||||
|
||||
root_ids.filter_map { |id| build_node.call(id, []) }
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
class SettingsController < ApplicationController
|
||||
def index
|
||||
end
|
||||
|
||||
def show
|
||||
end
|
||||
|
||||
def create
|
||||
end
|
||||
|
||||
def update
|
||||
end
|
||||
|
||||
def destroy
|
||||
end
|
||||
end
|
||||
@@ -1,16 +0,0 @@
|
||||
class TagAliasesController < ApplicationController
|
||||
def index
|
||||
end
|
||||
|
||||
def show
|
||||
end
|
||||
|
||||
def create
|
||||
end
|
||||
|
||||
def update
|
||||
end
|
||||
|
||||
def destroy
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,27 @@
|
||||
class TagChildrenController < ApplicationController
|
||||
def create
|
||||
return head :unauthorized unless current_user
|
||||
return head :forbidden unless current_user.admin?
|
||||
|
||||
parent_id = params[:parent_id]
|
||||
child_id = params[:child_id]
|
||||
return head :bad_request if parent_id.blank? || child_id.blank?
|
||||
|
||||
Tag.find(parent_id).children << Tag.find(child_id) rescue nil
|
||||
|
||||
head :no_content
|
||||
end
|
||||
|
||||
def destroy
|
||||
return head :unauthorized unless current_user
|
||||
return head :forbidden unless current_user.admin?
|
||||
|
||||
parent_id = params[:parent_id]
|
||||
child_id = params[:child_id]
|
||||
return head :bad_request if parent_id.blank? || child_id.blank?
|
||||
|
||||
Tag.find(parent_id).children.delete(Tag.find(child_id)) rescue nil
|
||||
|
||||
head :no_content
|
||||
end
|
||||
end
|
||||
@@ -1,46 +1,129 @@
|
||||
class TagsController < ApplicationController
|
||||
def index
|
||||
post_id = params[:post]
|
||||
tags = if post_id.present?
|
||||
Tag.joins(:posts).where(posts: { id: post_id })
|
||||
else
|
||||
Tag.all
|
||||
end
|
||||
render json: tags
|
||||
|
||||
tags =
|
||||
if post_id.present?
|
||||
Tag.joins(:posts, :tag_name)
|
||||
else
|
||||
Tag.joins(:tag_name)
|
||||
end
|
||||
.includes(:tag_name, tag_name: :wiki_page)
|
||||
if post_id.present?
|
||||
tags = tags.where(posts: { id: post_id })
|
||||
end
|
||||
|
||||
render json: TagRepr.base(tags)
|
||||
end
|
||||
|
||||
def autocomplete
|
||||
q = params[:q].to_s.strip
|
||||
return render json: [] if q.blank?
|
||||
|
||||
tags = (Tag
|
||||
.where('(category = ? AND name LIKE ?) OR name LIKE ?',
|
||||
'nico', "nico:#{ q }%", "#{ q }%")
|
||||
.order('post_count DESC, name ASC')
|
||||
.limit(20))
|
||||
render json: tags
|
||||
with_nico = bool?(:nico, default: true)
|
||||
present_only = bool?(:present, default: true)
|
||||
|
||||
alias_rows =
|
||||
TagName
|
||||
.where('name LIKE ?', "#{ q }%")
|
||||
.where.not(canonical_id: nil)
|
||||
.pluck(:canonical_id, :name)
|
||||
|
||||
matched_alias_by_tag_name_id = { }
|
||||
canonical_ids = []
|
||||
|
||||
alias_rows.each do |canonical_id, alias_name|
|
||||
canonical_ids << canonical_id
|
||||
matched_alias_by_tag_name_id[canonical_id] ||= alias_name
|
||||
end
|
||||
|
||||
base = Tag.joins(:tag_name)
|
||||
.includes(:tag_name, tag_name: :wiki_page)
|
||||
base = base.where('tags.post_count > 0') if present_only
|
||||
|
||||
canonical_hit =
|
||||
base
|
||||
.where(((with_nico ? '(tags.category = ? AND tag_names.name LIKE ?) OR ' : '') +
|
||||
'tag_names.name LIKE ?'),
|
||||
*(with_nico ? ['nico', "nico:#{ q }%"] : []), "#{ q }%")
|
||||
|
||||
tags =
|
||||
if canonical_ids.present?
|
||||
canonical_hit.or(base.where(tag_name_id: canonical_ids.uniq))
|
||||
else
|
||||
canonical_hit
|
||||
end
|
||||
|
||||
tags = tags.order(Arel.sql('post_count DESC, tag_names.name')).limit(20).to_a
|
||||
|
||||
render json: tags.map { |tag|
|
||||
TagRepr.base(tag).merge(matched_alias: matched_alias_by_tag_name_id[tag.tag_name_id])
|
||||
}
|
||||
end
|
||||
|
||||
def show
|
||||
tag = Tag.find(params[:id])
|
||||
render json: tag
|
||||
end
|
||||
|
||||
def show_by_name
|
||||
tag = Tag.find_by(name: params[:name])
|
||||
tag = Tag.joins(:tag_name)
|
||||
.includes(:tag_name, tag_name: :wiki_page)
|
||||
.find_by(id: params[:id])
|
||||
if tag
|
||||
render json: tag
|
||||
render json: TagRepr.base(tag)
|
||||
else
|
||||
head :not_found
|
||||
end
|
||||
end
|
||||
|
||||
def create
|
||||
def show_by_name
|
||||
name = params[:name].to_s.strip
|
||||
return head :bad_request if name.blank?
|
||||
|
||||
tag = Tag.joins(:tag_name)
|
||||
.includes(:tag_name, tag_name: :wiki_page)
|
||||
.find_by(tag_names: { name: })
|
||||
if tag
|
||||
render json: TagRepr.base(tag)
|
||||
else
|
||||
head :not_found
|
||||
end
|
||||
end
|
||||
|
||||
def deerjikists
|
||||
tag = Tag.joins(:tag_name)
|
||||
.includes(:tag_name, tag_name: :wiki_page)
|
||||
.find_by(id: params[:id])
|
||||
return head :not_found unless tag
|
||||
|
||||
render json: DeerjikistRepr.many(tag.deerjikists)
|
||||
end
|
||||
|
||||
def deerjikists_by_name
|
||||
name = params[:name].to_s.strip
|
||||
return head :bad_request if name.blank?
|
||||
|
||||
tag = Tag.joins(:tag_name)
|
||||
.includes(:tag_name, tag_name: :wiki_page)
|
||||
.find_by(tag_names: { name: })
|
||||
return head :not_found unless tag
|
||||
|
||||
render json: DeerjikistRepr.many(tag.deerjikists)
|
||||
end
|
||||
|
||||
def update
|
||||
end
|
||||
return head :unauthorized unless current_user
|
||||
return head :forbidden unless current_user.gte_member?
|
||||
|
||||
def destroy
|
||||
name = params[:name].presence
|
||||
category = params[:category].presence
|
||||
|
||||
tag = Tag.find(params[:id])
|
||||
|
||||
if name.present?
|
||||
tag.tag_name.update!(name:)
|
||||
end
|
||||
|
||||
if category.present?
|
||||
tag.update!(category:)
|
||||
end
|
||||
|
||||
render json: TagRepr.base(tag)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
class UserIpsController < ApplicationController
|
||||
def index
|
||||
end
|
||||
|
||||
def show
|
||||
end
|
||||
|
||||
def create
|
||||
end
|
||||
|
||||
def update
|
||||
end
|
||||
|
||||
def destroy
|
||||
end
|
||||
end
|
||||
@@ -1,16 +0,0 @@
|
||||
class UserPostViewsController < ApplicationController
|
||||
def index
|
||||
end
|
||||
|
||||
def show
|
||||
end
|
||||
|
||||
def create
|
||||
end
|
||||
|
||||
def update
|
||||
end
|
||||
|
||||
def destroy
|
||||
end
|
||||
end
|
||||
@@ -18,6 +18,8 @@ class UsersController < ApplicationController
|
||||
end
|
||||
|
||||
def renew
|
||||
return head :unauthorized unless current_user
|
||||
|
||||
user = current_user
|
||||
user.inheritance_code = SecureRandom.uuid
|
||||
user.save!
|
||||
|
||||
@@ -1,20 +1,34 @@
|
||||
class WikiPagesController < ApplicationController
|
||||
def index
|
||||
wiki_pages = WikiPage.all
|
||||
rescue_from Wiki::Commit::Conflict, with: :render_wiki_conflict
|
||||
|
||||
render json: wiki_pages
|
||||
def index
|
||||
title = params[:title].to_s.strip
|
||||
if title.blank?
|
||||
return render json: WikiPageRepr.base(WikiPage.joins(:tag_name).includes(:tag_name))
|
||||
end
|
||||
|
||||
q = WikiPage.joins(:tag_name).includes(:tag_name)
|
||||
.where('tag_names.name LIKE ?', "%#{ WikiPage.sanitize_sql_like(title) }%")
|
||||
render json: WikiPageRepr.base(q.limit(20))
|
||||
end
|
||||
|
||||
def show
|
||||
render_wiki_page_or_404 WikiPage.find(params[:id])
|
||||
page = WikiPage.joins(:tag_name)
|
||||
.includes(:tag_name)
|
||||
.find_by(id: params[:id])
|
||||
render_wiki_page_or_404 page
|
||||
end
|
||||
|
||||
def show_by_title
|
||||
render_wiki_page_or_404 WikiPage.find_by(title: params[:title])
|
||||
title = params[:title].to_s.strip
|
||||
page = WikiPage.joins(:tag_name)
|
||||
.includes(:tag_name)
|
||||
.find_by(tag_name: { name: title })
|
||||
render_wiki_page_or_404 page
|
||||
end
|
||||
|
||||
def exists
|
||||
if WikiPage.exists?(params[:id])
|
||||
if WikiPage.exists?(id: params[:id])
|
||||
head :no_content
|
||||
else
|
||||
head :not_found
|
||||
@@ -22,7 +36,8 @@ class WikiPagesController < ApplicationController
|
||||
end
|
||||
|
||||
def exists_by_title
|
||||
if WikiPage.exists?(title: params[:title])
|
||||
title = params[:title].to_s.strip
|
||||
if WikiPage.joins(:tag_name).exists?(tag_names: { name: title })
|
||||
head :no_content
|
||||
else
|
||||
head :not_found
|
||||
@@ -31,21 +46,25 @@ class WikiPagesController < ApplicationController
|
||||
|
||||
def diff
|
||||
id = params[:id]
|
||||
from = params[:from]
|
||||
return head :bad_request if id.blank?
|
||||
|
||||
from = params[:from].presence
|
||||
to = params[:to].presence
|
||||
return head :bad_request if id.blank? || from.blank?
|
||||
|
||||
wiki_page_from = WikiPage.find(id)
|
||||
wiki_page_to = WikiPage.find(id)
|
||||
wiki_page_from.sha = from
|
||||
wiki_page_to.sha = to
|
||||
page = WikiPage.joins(:tag_name).includes(:tag_name).find(id)
|
||||
|
||||
diffs = Diff::LCS.sdiff(wiki_page_from.body, wiki_page_to.body)
|
||||
from_rev = from && page.wiki_revisions.find(from)
|
||||
to_rev = to ? page.wiki_revisions.find(to) : page.current_revision
|
||||
if ((from_rev && !(from_rev.content?)) || !(to_rev&.content?))
|
||||
return head :unprocessable_entity
|
||||
end
|
||||
|
||||
diffs = Diff::LCS.sdiff(from_rev&.body&.lines || [], to_rev.body.lines)
|
||||
diff_json = diffs.map { |change|
|
||||
case change.action
|
||||
when ?=
|
||||
{ type: 'context', content: change.old_element }
|
||||
when ?|
|
||||
when ?!
|
||||
[{ type: 'removed', content: change.old_element },
|
||||
{ type: 'added', content: change.new_element }]
|
||||
when ?+
|
||||
@@ -55,97 +74,116 @@ class WikiPagesController < ApplicationController
|
||||
end
|
||||
}.flatten.compact
|
||||
|
||||
render json: { wiki_page_id: wiki_page_from.id,
|
||||
title: wiki_page_from.title,
|
||||
older_sha: wiki_page_from.sha,
|
||||
newer_sha: wiki_page_to.sha,
|
||||
diff: diff_json }
|
||||
render json: { wiki_page_id: page.id,
|
||||
title: page.title,
|
||||
older_revision_id: from_rev&.id,
|
||||
newer_revision_id: to_rev.id,
|
||||
diff: diff_json }
|
||||
end
|
||||
|
||||
def create
|
||||
return head :unauthorized unless current_user
|
||||
return head :forbidden unless ['admin', 'member'].include?(current_user.role)
|
||||
return head :forbidden unless current_user.gte_member?
|
||||
|
||||
wiki_page = WikiPage.new(title: params[:title], created_user: current_user, updated_user: current_user)
|
||||
if wiki_page.save
|
||||
wiki_page.set_body params[:body], user: current_user
|
||||
render json: wiki_page, status: :created
|
||||
name = params[:title]&.strip
|
||||
body = params[:body].to_s
|
||||
|
||||
return head :unprocessable_entity if name.blank? || body.blank?
|
||||
|
||||
tag_name = TagName.find_or_create_by!(name:)
|
||||
page = WikiPage.new(tag_name:, created_user: current_user, updated_user: current_user)
|
||||
if page.save
|
||||
message = params[:message].presence
|
||||
Wiki::Commit.content!(page:, body:, created_user: current_user, message:)
|
||||
|
||||
render json: WikiPageRepr.base(page), status: :created
|
||||
else
|
||||
render json: { errors: wiki_page.errors.full_messages }, status: :unprocessable_entity
|
||||
render json: { errors: page.errors.full_messages },
|
||||
status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
return head :unauthorized unless current_user
|
||||
return head :forbidden unless current_user.member?
|
||||
return head :forbidden unless current_user.gte_member?
|
||||
|
||||
title = params[:title]
|
||||
body = params[:body]
|
||||
title = params[:title]&.strip
|
||||
body = params[:body].to_s
|
||||
|
||||
return head :unprocessable_entity if title.blank? || body.blank?
|
||||
|
||||
wiki_page = WikiPage.find(params[:id])
|
||||
wiki_page.title = title
|
||||
wiki_page.updated_user = current_user
|
||||
wiki_page.set_body(body, user: current_user)
|
||||
wiki_page.save!
|
||||
page = WikiPage.find(params[:id])
|
||||
base_revision_id = page.current_revision.id
|
||||
|
||||
if params[:title].present? && params[:title].strip != page.title
|
||||
return head :unprocessable_entity
|
||||
end
|
||||
|
||||
message = params[:message].presence
|
||||
Wiki::Commit.content!(page:,
|
||||
body:,
|
||||
created_user: current_user,
|
||||
message:,
|
||||
base_revision_id:)
|
||||
|
||||
head :ok
|
||||
end
|
||||
|
||||
def search
|
||||
title = params[:title]&.strip
|
||||
|
||||
q = WikiPage.all
|
||||
q = q.where('title LIKE ?', "%#{ WikiPage.sanitize_sql_like(title) }%") if title.present?
|
||||
|
||||
render json: q.limit(20)
|
||||
index
|
||||
end
|
||||
|
||||
def changes
|
||||
id = params[:id]
|
||||
log = if id.present?
|
||||
wiki.page("#{ id }.md")&.versions
|
||||
else
|
||||
wiki.repo.log('main', nil)
|
||||
end
|
||||
return render json: [] unless log
|
||||
id = params[:id].presence
|
||||
q = WikiRevision.joins(wiki_page: :tag_name)
|
||||
.includes(:created_user, wiki_page: :tag_name)
|
||||
.order(id: :desc)
|
||||
q = q.where(wiki_page_id: id) if id
|
||||
|
||||
render json: log.map { |commit|
|
||||
wiki_page = WikiPage.find(commit.message.split(' ')[1].to_i)
|
||||
wiki_page.sha = commit.id
|
||||
|
||||
next nil if wiki_page.sha.blank?
|
||||
|
||||
user = User.find(commit.author.name.to_i)
|
||||
|
||||
{ sha: wiki_page.sha,
|
||||
pred: wiki_page.pred,
|
||||
succ: wiki_page.succ,
|
||||
wiki_page: wiki_page && { id: wiki_page.id, title: wiki_page.title },
|
||||
user: user && { id: user.id, name: user.name },
|
||||
change_type: commit.message.split(' ')[0].downcase[0...(-1)],
|
||||
timestamp: commit.authored_date }
|
||||
render json: q.limit(200).map { |rev|
|
||||
{ revision_id: rev.id,
|
||||
pred: rev.base_revision_id,
|
||||
succ: nil,
|
||||
wiki_page: { id: rev.wiki_page_id, title: rev.wiki_page.title },
|
||||
user: rev.created_user && { id: rev.created_user.id, name: rev.created_user.name },
|
||||
kind: rev.kind,
|
||||
message: rev.message,
|
||||
timestamp: rev.created_at }
|
||||
}.compact
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
WIKI_PATH = Rails.root.join('wiki').to_s
|
||||
def render_wiki_page_or_404 page
|
||||
return head :not_found unless page
|
||||
|
||||
def wiki
|
||||
@wiki ||= Gollum::Wiki.new(WIKI_PATH)
|
||||
rev = find_revision(page)
|
||||
return head :not_found unless rev
|
||||
|
||||
if rev.redirect?
|
||||
return (
|
||||
redirect_to wiki_page_by_title_path(title: rev.redirect_page.title),
|
||||
status: :moved_permanently)
|
||||
end
|
||||
|
||||
body = rev.body
|
||||
revision_id = rev.id
|
||||
pred = page.pred_revision_id(revision_id)
|
||||
succ = page.succ_revision_id(revision_id)
|
||||
updated_at = rev.created_at
|
||||
|
||||
render json: WikiPageRepr.base(page).merge(body:, revision_id:, pred:, succ:, updated_at:)
|
||||
end
|
||||
|
||||
def render_wiki_page_or_404 wiki_page
|
||||
return head :not_found unless wiki_page
|
||||
def find_revision page
|
||||
if params[:version].present?
|
||||
page.wiki_revisions.find_by(id: params[:version])
|
||||
else
|
||||
page.current_revision
|
||||
end
|
||||
end
|
||||
|
||||
wiki_page.sha = params[:version].presence
|
||||
|
||||
body = wiki_page.body
|
||||
sha = wiki_page.sha
|
||||
pred = wiki_page.pred
|
||||
succ = wiki_page.succ
|
||||
render json: wiki_page.as_json.merge(body:, sha:, pred:, succ:)
|
||||
def render_wiki_conflict err
|
||||
render json: { error: 'conflict', message: err.message }, status: :conflict
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
class Deerjikist < ApplicationRecord
|
||||
self.primary_key = :platform, :code
|
||||
|
||||
belongs_to :tag
|
||||
|
||||
validates :platform, presence: true
|
||||
validates :code, presence: true
|
||||
validates :tag_id, presence: true
|
||||
|
||||
validate :tag_must_be_deerjikist
|
||||
|
||||
enum :platform, nico: 'nico', youtube: 'youtube'
|
||||
|
||||
private
|
||||
|
||||
def tag_must_be_deerjikist
|
||||
if tag && !(tag.deerjikist?)
|
||||
errors.add :tag, 'タグはニジラー・カテゴリである必要があります.'
|
||||
end
|
||||
end
|
||||
end
|
||||
+46
-19
@@ -8,16 +8,18 @@ class Post < ApplicationRecord
|
||||
has_many :active_post_tags, -> { kept }, class_name: 'PostTag', inverse_of: :post
|
||||
has_many :post_tags_with_discarded, -> { with_discarded }, class_name: 'PostTag'
|
||||
has_many :tags, through: :active_post_tags
|
||||
has_many :user_post_views, dependent: :destroy
|
||||
has_many :post_similarities_as_post,
|
||||
class_name: 'PostSimilarity',
|
||||
foreign_key: :post_id
|
||||
has_many :post_similarities_as_target_post,
|
||||
class_name: 'PostSimilarity',
|
||||
foreign_key: :target_post_id
|
||||
|
||||
has_many :user_post_views, dependent: :delete_all
|
||||
has_many :post_similarities, dependent: :delete_all
|
||||
|
||||
has_one_attached :thumbnail
|
||||
|
||||
before_validation :normalise_url
|
||||
|
||||
validates :url, presence: true, uniqueness: true
|
||||
|
||||
validate :validate_original_created_range
|
||||
validate :url_must_be_http_url
|
||||
|
||||
def as_json options = { }
|
||||
super(options).merge({ thumbnail: thumbnail.attached? ?
|
||||
@@ -28,19 +30,15 @@ class Post < ApplicationRecord
|
||||
super(options).merge(thumbnail: nil)
|
||||
end
|
||||
|
||||
def related(limit: nil)
|
||||
ids_with_cos =
|
||||
post_similarities_as_post.select(:target_post_id, :cos)
|
||||
.map { |ps| [ps.target_post_id, ps.cos] } +
|
||||
post_similarities_as_target_post.select(:post_id, :cos)
|
||||
.map { |ps| [ps.post_id, ps.cos] }
|
||||
def related limit: nil
|
||||
ids = post_similarities.order(cos: :desc)
|
||||
ids = ids.limit(limit) if limit
|
||||
ids = ids.pluck(:target_post_id)
|
||||
return Post.none if ids.empty?
|
||||
|
||||
sorted = ids_with_cos.sort_by { |_, cos| -cos }
|
||||
|
||||
ids = sorted.map(&:first)
|
||||
ids = ids.first(limit) if limit
|
||||
|
||||
Post.where(id: ids).index_by(&:id).values_at(*ids)
|
||||
Post.where(id: ids)
|
||||
.with_attached_thumbnail
|
||||
.order(Arel.sql("FIELD(posts.id, #{ ids.join(',') })"))
|
||||
end
|
||||
|
||||
def resized_thumbnail!
|
||||
@@ -69,4 +67,33 @@ class Post < ApplicationRecord
|
||||
errors.add :original_created_before, 'オリジナルの作成日時の順番がをかしぃです.'
|
||||
end
|
||||
end
|
||||
|
||||
def url_must_be_http_url
|
||||
begin
|
||||
u = URI.parse(url)
|
||||
rescue URI::InvalidURIError
|
||||
errors.add(:url, 'URL が不正です.')
|
||||
return
|
||||
end
|
||||
|
||||
if !(u in URI::HTTP) || u.host.blank?
|
||||
errors.add(:url, 'URL が不正です.')
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
def normalise_url
|
||||
return if url.blank?
|
||||
|
||||
self.url = url.strip
|
||||
|
||||
u = URI.parse(url)
|
||||
return unless u in URI::HTTP
|
||||
|
||||
u.host = u.host.downcase if u.host
|
||||
u.path = u.path.sub(/\/\Z/, '') if u.path.present?
|
||||
self.url = u.to_s
|
||||
rescue URI::InvalidURIError
|
||||
;
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
class PostSimilarity < ApplicationRecord
|
||||
belongs_to :post, class_name: 'Post', foreign_key: 'post_id'
|
||||
belongs_to :target_post, class_name: 'Post', foreign_key: 'target_post_id'
|
||||
self.primary_key = :post_id, :target_post_id
|
||||
|
||||
belongs_to :post
|
||||
belongs_to :target_post, class_name: 'Post'
|
||||
end
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
class PostTag < ApplicationRecord
|
||||
include Discard::Model
|
||||
|
||||
before_destroy do
|
||||
raise ActiveRecord::ReadOnlyRecord, '消さないでください.'
|
||||
end
|
||||
|
||||
belongs_to :post
|
||||
belongs_to :tag, counter_cache: :post_count
|
||||
belongs_to :created_user, class_name: 'User', optional: true
|
||||
|
||||
+128
-44
@@ -1,81 +1,109 @@
|
||||
class Tag < ApplicationRecord
|
||||
has_many :post_tags, dependent: :delete_all, inverse_of: :tag
|
||||
class NicoTagNormalisationError < ArgumentError
|
||||
;
|
||||
end
|
||||
|
||||
has_many :post_tags, inverse_of: :tag
|
||||
has_many :active_post_tags, -> { kept }, class_name: 'PostTag', inverse_of: :tag
|
||||
has_many :post_tags_with_discarded, -> { with_discarded }, class_name: 'PostTag'
|
||||
has_many :posts, through: :active_post_tags
|
||||
has_many :tag_aliases, dependent: :destroy
|
||||
|
||||
has_many :nico_tag_relations, foreign_key: :nico_tag_id, dependent: :destroy
|
||||
has_many :linked_tags, through: :nico_tag_relations, source: :tag
|
||||
|
||||
has_many :reversed_nico_tag_relations, class_name: 'NicoTagRelation',
|
||||
foreign_key: :tag_id,
|
||||
dependent: :destroy
|
||||
has_many :reversed_nico_tag_relations,
|
||||
class_name: 'NicoTagRelation', foreign_key: :tag_id, dependent: :destroy
|
||||
has_many :linked_nico_tags, through: :reversed_nico_tag_relations, source: :nico_tag
|
||||
|
||||
has_many :tag_implications, foreign_key: :parent_tag_id, dependent: :destroy
|
||||
has_many :children, through: :tag_implications, source: :tag
|
||||
|
||||
has_many :reversed_tag_implications, class_name: 'TagImplication',
|
||||
foreign_key: :tag_id,
|
||||
dependent: :destroy
|
||||
has_many :reversed_tag_implications,
|
||||
class_name: 'TagImplication', foreign_key: :tag_id, dependent: :destroy
|
||||
has_many :parents, through: :reversed_tag_implications, source: :parent_tag
|
||||
|
||||
enum :category, { deerjikist: 'deerjikist',
|
||||
meme: 'meme',
|
||||
character: 'character',
|
||||
general: 'general',
|
||||
material: 'material',
|
||||
nico: 'nico',
|
||||
meta: 'meta' }
|
||||
has_many :tag_similarities, dependent: :delete_all
|
||||
has_many :tag_similarities_as_target,
|
||||
class_name: 'TagSimilarity', foreign_key: :target_tag_id, dependent: :delete_all
|
||||
|
||||
has_many :deerjikists, dependent: :delete_all
|
||||
|
||||
belongs_to :tag_name
|
||||
delegate :wiki_page, to: :tag_name
|
||||
|
||||
delegate :name, to: :tag_name, allow_nil: true
|
||||
validates :tag_name, presence: true
|
||||
|
||||
enum :category, deerjikist: 'deerjikist',
|
||||
meme: 'meme',
|
||||
character: 'character',
|
||||
general: 'general',
|
||||
material: 'material',
|
||||
nico: 'nico',
|
||||
meta: 'meta'
|
||||
|
||||
validates :name, presence: true, length: { maximum: 255 }
|
||||
validates :category, presence: true, inclusion: { in: Tag.categories.keys }
|
||||
|
||||
validate :nico_tag_name_must_start_with_nico
|
||||
validate :tag_name_must_be_canonical
|
||||
validate :category_must_be_deerjikist_with_deerjikists
|
||||
|
||||
scope :nico_tags, -> { where(category: :nico) }
|
||||
scope :nico_tags, -> { nico }
|
||||
|
||||
CATEGORY_PREFIXES = {
|
||||
'gen:' => 'general',
|
||||
'djk:' => 'deerjikist',
|
||||
'meme:' => 'meme',
|
||||
'chr:' => 'character',
|
||||
'mtr:' => 'material',
|
||||
'meta:' => 'meta' }.freeze
|
||||
'general:' => :general,
|
||||
'gen:' => :general,
|
||||
'deerjikist:' => :deerjikist,
|
||||
'djk:' => :deerjikist,
|
||||
'meme:' => :meme,
|
||||
'character:' => :character,
|
||||
'chr:' => :character,
|
||||
'material:' => :material,
|
||||
'mtr:' => :material,
|
||||
'meta:' => :meta }.freeze
|
||||
|
||||
def name= val
|
||||
(self.tag_name ||= build_tag_name).name = val
|
||||
end
|
||||
|
||||
def has_wiki = wiki_page.present?
|
||||
|
||||
def self.tagme
|
||||
@tagme ||= Tag.find_or_create_by!(name: 'タグ希望') do |tag|
|
||||
tag.category = 'meta'
|
||||
end
|
||||
@tagme ||= find_or_create_by_tag_name!('タグ希望', category: :meta)
|
||||
end
|
||||
|
||||
def self.bot
|
||||
@bot ||= Tag.find_or_create_by!(name: 'bot操作') do |tag|
|
||||
tag.category = 'meta'
|
||||
end
|
||||
@bot ||= find_or_create_by_tag_name!('bot操作', category: :meta)
|
||||
end
|
||||
|
||||
def self.no_deerjikist
|
||||
@no_deerjikist ||= Tag.find_or_initialize_by(name: 'ニジラー情報不詳') do |tag|
|
||||
tag.category = 'meta'
|
||||
end
|
||||
@no_deerjikist ||= find_or_create_by_tag_name!('ニジラー情報不詳', category: :meta)
|
||||
end
|
||||
|
||||
def self.normalise_tags tag_names, with_tagme: true
|
||||
def self.video
|
||||
@video ||= find_or_create_by_tag_name!('動画', category: :meta)
|
||||
end
|
||||
|
||||
def self.niconico
|
||||
@niconico ||= find_or_create_by_tag_name!('ニコニコ', category: :meta)
|
||||
end
|
||||
|
||||
def self.normalise_tags tag_names, with_tagme: true, deny_nico: true
|
||||
if deny_nico && tag_names.any? { |n| n.downcase.start_with?('nico:') }
|
||||
raise NicoTagNormalisationError
|
||||
end
|
||||
|
||||
tags = tag_names.map do |name|
|
||||
pf, cat = CATEGORY_PREFIXES.find { |p, _| name.start_with?(p) } || ['', nil]
|
||||
name.delete_prefix!(pf)
|
||||
Tag.find_or_initialize_by(name:).tap do |tag|
|
||||
if cat && tag.category != cat
|
||||
tag.category = cat
|
||||
tag.save!
|
||||
end
|
||||
pf, cat = CATEGORY_PREFIXES.find { |p, _| name.downcase.start_with?(p) } || ['', nil]
|
||||
name = TagName.canonicalise(name.sub(/\A#{ pf }/i, '')).first
|
||||
find_or_create_by_tag_name!(name, category: (cat || :general)).tap do |tag|
|
||||
tag.update!(category: cat) if cat && tag.category != cat
|
||||
end
|
||||
end
|
||||
|
||||
tags << Tag.tagme if with_tagme && tags.size < 10 && tags.none?(Tag.tagme)
|
||||
tags << Tag.no_deerjikist if tags.all? { |t| t.category != 'deerjikist' }
|
||||
tags.uniq
|
||||
tags << Tag.no_deerjikist if tags.all? { |t| !(t.deerjikist?) }
|
||||
tags.uniq(&:id)
|
||||
end
|
||||
|
||||
def self.expand_parent_tags tags
|
||||
@@ -101,12 +129,68 @@ class Tag < ApplicationRecord
|
||||
(result + tags).uniq { |t| t.id }
|
||||
end
|
||||
|
||||
def self.find_or_create_by_tag_name! name, category:
|
||||
tn = TagName.find_or_create_by!(name: name.to_s.strip)
|
||||
tn = tn.canonical if tn.canonical_id?
|
||||
|
||||
Tag.find_or_create_by!(tag_name_id: tn.id) do |t|
|
||||
t.category = category
|
||||
end
|
||||
rescue ActiveRecord::RecordNotUnique
|
||||
retry
|
||||
end
|
||||
|
||||
def self.merge_tags! target_tag, source_tags
|
||||
target_tag => Tag
|
||||
|
||||
Tag.transaction do
|
||||
Array(source_tags).compact.uniq.each do |st|
|
||||
st => Tag
|
||||
|
||||
next if st == target_tag
|
||||
|
||||
st.post_tags.find_each do |pt|
|
||||
if PostTag.kept.exists?(post_id: pt.post_id, tag_id: target_tag.id)
|
||||
pt.discard_by!(nil)
|
||||
# discard 後の update! は禁止なので DB を直に更新
|
||||
pt.update_columns(tag_id: target_tag.id, updated_at: Time.current)
|
||||
else
|
||||
pt.update!(tag: target_tag)
|
||||
end
|
||||
end
|
||||
|
||||
tag_name = st.tag_name
|
||||
st.destroy!
|
||||
tag_name.reload
|
||||
tag_name.update!(canonical: target_tag.tag_name)
|
||||
end
|
||||
|
||||
# 投稿件数を再集計
|
||||
target_tag.update_columns(post_count: PostTag.kept.where(tag: target_tag).count)
|
||||
end
|
||||
|
||||
target_tag.reload
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def nico_tag_name_must_start_with_nico
|
||||
if ((category == 'nico' && !(name.start_with?('nico:'))) ||
|
||||
(category != 'nico' && name.start_with?('nico:')))
|
||||
n = name.to_s
|
||||
if ((nico? && !(n.downcase.start_with?('nico:'))) ||
|
||||
(!(nico?) && n.downcase.start_with?('nico:')))
|
||||
errors.add :name, 'ニコニコ・タグの命名規則に反してゐます.'
|
||||
end
|
||||
end
|
||||
|
||||
def tag_name_must_be_canonical
|
||||
if tag_name&.canonical_id?
|
||||
errors.add :tag_name, 'tag_names へは実体を示す必要があります.'
|
||||
end
|
||||
end
|
||||
|
||||
def category_must_be_deerjikist_with_deerjikists
|
||||
if !(deerjikist?) && deerjikists.exists?
|
||||
errors.add :category, 'ニジラーと紐づいてゐるタグはニジラー・カテゴリである必要があります.'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
class TagAlias < ApplicationRecord
|
||||
belongs_to :tag
|
||||
|
||||
validates :tag_id, presence: true
|
||||
validates :name, presence: true, length: { maximum: 255 }, uniqueness: true
|
||||
end
|
||||
@@ -0,0 +1,42 @@
|
||||
class TagName < ApplicationRecord
|
||||
has_one :tag
|
||||
has_one :wiki_page
|
||||
|
||||
belongs_to :canonical, class_name: 'TagName', optional: true
|
||||
has_many :aliases, class_name: 'TagName', foreign_key: :canonical_id
|
||||
|
||||
validates :name, presence: true, length: { maximum: 255 }, uniqueness: true
|
||||
|
||||
validate :canonical_must_be_canonical
|
||||
validate :alias_name_must_not_have_prefix
|
||||
validate :canonical_must_not_be_present_with_tag_or_wiki_page
|
||||
|
||||
def self.canonicalise names
|
||||
names = Array(names).map { |n| n.to_s.strip }.reject(&:blank?)
|
||||
return [] if names.blank?
|
||||
|
||||
tns = TagName.includes(:canonical).where(name: names).index_by(&:name)
|
||||
|
||||
names.map { |name| tns[name]&.canonical&.name || name }.uniq
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def canonical_must_be_canonical
|
||||
if canonical&.canonical_id?
|
||||
errors.add :canonical, 'canonical は実体を示す必要があります.'
|
||||
end
|
||||
end
|
||||
|
||||
def alias_name_must_not_have_prefix
|
||||
if canonical_id? && name.to_s.include?(':')
|
||||
errors.add :name, 'エーリアス名にプレフィクスを含むことはできません.'
|
||||
end
|
||||
end
|
||||
|
||||
def canonical_must_not_be_present_with_tag_or_wiki_page
|
||||
if canonical_id? && (tag || wiki_page)
|
||||
errors.add :canonical, 'タグもしくは Wiki の参照がある名前はエーリアスになれません.'
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,6 @@
|
||||
class TagSimilarity < ApplicationRecord
|
||||
self.primary_key = :tag_id, :target_tag_id
|
||||
|
||||
belongs_to :tag
|
||||
belongs_to :target_tag, class_name: 'Tag'
|
||||
end
|
||||
@@ -1,29 +1,23 @@
|
||||
class User < ApplicationRecord
|
||||
enum :role, { guest: 'guest', member: 'member', admin: 'admin' }
|
||||
enum :role, guest: 'guest', member: 'member', admin: 'admin'
|
||||
|
||||
validates :name, length: { maximum: 255 }
|
||||
validates :inheritance_code, presence: true, length: { maximum: 64 }
|
||||
validates :role, presence: true, inclusion: { in: roles.keys }
|
||||
validates :banned, inclusion: { in: [true, false] }
|
||||
|
||||
has_many :posts
|
||||
has_many :created_posts,
|
||||
class_name: 'Post', foreign_key: :uploaded_user_id, dependent: :nullify
|
||||
has_many :settings
|
||||
has_many :user_ips, dependent: :destroy
|
||||
has_many :ip_addresses, through: :user_ips
|
||||
has_many :user_post_views, dependent: :destroy
|
||||
has_many :viewed_posts, through: :user_post_views, source: :post
|
||||
has_many :created_wiki_pages, class_name: 'WikiPage', foreign_key: 'created_user_id', dependent: :nullify
|
||||
has_many :updated_wiki_pages, class_name: 'WikiPage', foreign_key: 'updated_user_id', dependent: :nullify
|
||||
has_many :created_wiki_pages,
|
||||
class_name: 'WikiPage', foreign_key: :created_user_id, dependent: :nullify
|
||||
has_many :updated_wiki_pages,
|
||||
class_name: 'WikiPage', foreign_key: :updated_user_id, dependent: :nullify
|
||||
|
||||
def viewed? post
|
||||
user_post_views.exists? post_id: post.id
|
||||
end
|
||||
|
||||
def member?
|
||||
['member', 'admin'].include?(role)
|
||||
end
|
||||
|
||||
def admin?
|
||||
role == 'admin'
|
||||
end
|
||||
def viewed?(post) = user_post_views.exists?(post_id: post.id)
|
||||
def gte_member? = member? || admin?
|
||||
end
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
class UserIp < ApplicationRecord
|
||||
self.primary_key = :user_id, :ip_address_id
|
||||
|
||||
belongs_to :user
|
||||
belongs_to :ip_address
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
class UserPostView < ApplicationRecord
|
||||
self.primary_key = :user_id, :post_id
|
||||
|
||||
belongs_to :user
|
||||
belongs_to :post
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
class WikiLine < ApplicationRecord
|
||||
has_many :wiki_revision_lines, dependent: :restrict_with_exception
|
||||
|
||||
validates :sha256, presence: true, uniqueness: true, length: { is: 64 }
|
||||
validates :body, presence: true
|
||||
|
||||
def self.upsert_by_body! body
|
||||
sha = Digest::SHA256.hexdigest(body)
|
||||
now = Time.current
|
||||
|
||||
upsert(sha256: sha, body:, created_at: now, updated_at: now)
|
||||
|
||||
find_by!(sha256: sha)
|
||||
end
|
||||
end
|
||||
@@ -1,80 +1,52 @@
|
||||
require 'gollum-lib'
|
||||
require 'set'
|
||||
|
||||
|
||||
class WikiPage < ApplicationRecord
|
||||
belongs_to :tag, optional: true
|
||||
belongs_to :created_user, class_name: 'User', foreign_key: 'created_user_id'
|
||||
belongs_to :updated_user, class_name: 'User', foreign_key: 'updated_user_id'
|
||||
has_many :wiki_revisions, dependent: :destroy
|
||||
belongs_to :created_user, class_name: 'User'
|
||||
belongs_to :updated_user, class_name: 'User'
|
||||
|
||||
validates :title, presence: true, length: { maximum: 255 }, uniqueness: true
|
||||
has_many :redirected_from_revisions,
|
||||
class_name: 'WikiRevision',
|
||||
foreign_key: :redirect_page_id,
|
||||
dependent: :nullify
|
||||
|
||||
def as_json options = { }
|
||||
self.sha = nil
|
||||
super options
|
||||
belongs_to :tag_name
|
||||
validates :tag_name, presence: true
|
||||
|
||||
def title = tag_name.name
|
||||
|
||||
def title= val
|
||||
(self.tag_name ||= build_tag_name).name = val
|
||||
end
|
||||
|
||||
def sha= val
|
||||
if val.present?
|
||||
@sha = val
|
||||
@page = wiki.page("#{ id }.md", @sha)
|
||||
else
|
||||
@page = wiki.page("#{ id }.md")
|
||||
@sha = @page.versions.first.id
|
||||
end
|
||||
vers = @page.versions
|
||||
idx = vers.find_index { |ver| ver.id == @sha }
|
||||
if idx
|
||||
@pred = vers[idx + 1]&.id
|
||||
@succ = idx.positive? ? vers[idx - 1].id : nil
|
||||
@updated_at = vers[idx].authored_date
|
||||
else
|
||||
@sha = nil
|
||||
@pred = nil
|
||||
@succ = nil
|
||||
@updated_at = nil
|
||||
end
|
||||
@sha
|
||||
end
|
||||
|
||||
def sha
|
||||
@sha
|
||||
end
|
||||
|
||||
def pred
|
||||
@pred
|
||||
end
|
||||
|
||||
def succ
|
||||
@succ
|
||||
end
|
||||
|
||||
def updated_at
|
||||
@updated_at
|
||||
end
|
||||
def current_revision = wiki_revisions.order(id: :desc).first
|
||||
|
||||
def body
|
||||
sha = nil unless @page
|
||||
@page&.raw_data&.force_encoding('UTF-8')
|
||||
rev = current_revision
|
||||
rev.body if rev&.content?
|
||||
end
|
||||
|
||||
def set_body content, user:
|
||||
commit_info = { name: user.id.to_s,
|
||||
email: 'dummy@example.com' }
|
||||
page = wiki.page("#{ id }.md")
|
||||
if page
|
||||
commit_info[:message] = "Updated #{ id }"
|
||||
wiki.update_page(page, id.to_s, :markdown, content, commit_info)
|
||||
else
|
||||
commit_info[:message] = "Created #{ id }"
|
||||
wiki.write_page(id.to_s, :markdown, content, commit_info)
|
||||
def resolve_redirect limit: 10
|
||||
page = self
|
||||
visited = Set.new
|
||||
|
||||
limit.times do
|
||||
return page if visited.include?(page.id)
|
||||
|
||||
visited.add(page.id)
|
||||
|
||||
rev = page.current_revision
|
||||
return page if !(rev&.redirect?) || !(rev.redirect_page)
|
||||
|
||||
page = rev.redirect_page
|
||||
end
|
||||
|
||||
page
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
WIKI_PATH = Rails.root.join('wiki').to_s
|
||||
|
||||
def wiki
|
||||
@wiki ||= Gollum::Wiki.new(WIKI_PATH)
|
||||
end
|
||||
def pred_revision_id(revision_id) =
|
||||
wiki_revisions.where('id < ?', revision_id).order(id: :desc).limit(1).pick(:id)
|
||||
def succ_revision_id(revision_id) =
|
||||
wiki_revisions.where('id > ?', revision_id).order(id: :asc).limit(1).pick(:id)
|
||||
end
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
class WikiRevision < ApplicationRecord
|
||||
belongs_to :wiki_page
|
||||
belongs_to :base_revision, class_name: 'WikiRevision', optional: true
|
||||
belongs_to :created_user, class_name: 'User'
|
||||
belongs_to :redirect_page, class_name: 'WikiPage', optional: true
|
||||
|
||||
has_many :wiki_revision_lines, dependent: :delete_all
|
||||
has_many :wiki_lines, through: :wiki_revision_lines
|
||||
|
||||
enum :kind, content: 0, redirect: 1
|
||||
|
||||
validates :kind, presence: true
|
||||
validates :lines_count, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
||||
validates :tree_sha256, length: { is: 64 }, allow_nil: true
|
||||
|
||||
validate :kind_consistency
|
||||
|
||||
def body
|
||||
return unless content?
|
||||
|
||||
wiki_revision_lines
|
||||
.includes(:wiki_line)
|
||||
.order(:position)
|
||||
.map { |rev| rev.wiki_line.body }
|
||||
.join("\n")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def kind_consistency
|
||||
if content?
|
||||
if tree_sha256.blank?
|
||||
errors.add(:tree_sha256, '種類がページの場合は必須です.')
|
||||
end
|
||||
|
||||
if redirect_page_id.present?
|
||||
errors.add(:redirect_page_id, '種類がページの場合は空である必要があります.')
|
||||
end
|
||||
end
|
||||
|
||||
if redirect?
|
||||
if redirect_page_id.blank?
|
||||
errors.add(:redirect_page_id, '種類がリダイレクトの場合は必須です.')
|
||||
end
|
||||
|
||||
if tree_sha256.present?
|
||||
errors.add(:tree_sha256, '種類がリダイレクトの場合は空である必要があります.')
|
||||
end
|
||||
|
||||
if lines_count.to_i > 0
|
||||
errors.add(:lines_count, '種類がリダイレクトの場合は 0 である必要があります.')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,8 @@
|
||||
class WikiRevisionLine < ApplicationRecord
|
||||
belongs_to :wiki_revision
|
||||
belongs_to :wiki_line
|
||||
|
||||
validates :position, presence: true,
|
||||
numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
||||
validates :position, uniqueness: { scope: :wiki_revision_id }
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
|
||||
module DeerjikistRepr
|
||||
BASE = { only: [:platform, :code], include: { tag: TagRepr::BASE } }.freeze
|
||||
|
||||
module_function
|
||||
|
||||
def base deerjikist
|
||||
deerjikist.as_json(BASE)
|
||||
end
|
||||
|
||||
def many deerjikists
|
||||
deerjikists.map { |d| base(d) }
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
|
||||
module PostRepr
|
||||
BASE = { include: { tags: TagRepr::BASE } }.freeze
|
||||
|
||||
module_function
|
||||
|
||||
def base post
|
||||
post.as_json(BASE)
|
||||
end
|
||||
|
||||
def many posts
|
||||
posts.map { |p| base(p) }
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
|
||||
module TagRepr
|
||||
BASE = { only: [:id, :category, :post_count], methods: [:name, :has_wiki] }.freeze
|
||||
|
||||
module_function
|
||||
|
||||
def base tag
|
||||
tag.as_json(BASE)
|
||||
end
|
||||
|
||||
def many tags
|
||||
tags.map { |t| base(t) }
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
|
||||
module WikiPageRepr
|
||||
BASE = { methods: [:title] }.freeze
|
||||
|
||||
module_function
|
||||
|
||||
def base wiki_page
|
||||
wiki_page.as_json(BASE)
|
||||
end
|
||||
|
||||
def many wiki_pages
|
||||
wiki_pages.map { |p| base(p) }
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,106 @@
|
||||
module Similarity
|
||||
class Calc
|
||||
def self.call model, tgt
|
||||
similarity_model = "#{ model.name }Similarity".constantize
|
||||
|
||||
# 最大保存件数
|
||||
n = 20
|
||||
|
||||
similarity_model.delete_all
|
||||
|
||||
posts = model.includes(tgt).select(:id).to_a
|
||||
|
||||
tag_ids = { }
|
||||
tag_cnts = { }
|
||||
|
||||
posts.each do |p|
|
||||
arr = p.public_send(tgt).map(&:id).sort
|
||||
tag_ids[p.id] = arr
|
||||
tag_cnts[p.id] = arr.size
|
||||
end
|
||||
|
||||
intersection_size = -> a, b do
|
||||
i = 0
|
||||
j = 0
|
||||
cnt = 0
|
||||
while i < a.size && j < b.size
|
||||
a_i = a[i]
|
||||
b_j = b[j]
|
||||
if a_i == b_j
|
||||
cnt += 1
|
||||
i += 1
|
||||
j += 1
|
||||
elsif a_i < b_j
|
||||
i += 1
|
||||
else
|
||||
j += 1
|
||||
end
|
||||
end
|
||||
cnt
|
||||
end
|
||||
|
||||
push_topk = -> list, cos, target_id do
|
||||
return if list.size >= n && cos <= list[-1][0]
|
||||
|
||||
idx = nil
|
||||
list.each_with_index do |(c, tid), i|
|
||||
if tid == target_id
|
||||
idx = i
|
||||
break
|
||||
end
|
||||
end
|
||||
if idx
|
||||
return if cos <= list[idx][0]
|
||||
list.delete_at(idx)
|
||||
end
|
||||
|
||||
insert_at = list.size
|
||||
list.each_with_index do |(c, _), i|
|
||||
if cos > c
|
||||
insert_at = i
|
||||
break
|
||||
end
|
||||
end
|
||||
list.insert(insert_at, [cos, target_id])
|
||||
list.pop if list.size > n
|
||||
end
|
||||
|
||||
top = Hash.new { |h, key| h[key] = [] }
|
||||
|
||||
ids = posts.map(&:id)
|
||||
ids.each_with_index do |post_id, i|
|
||||
a = tag_ids[post_id]
|
||||
a_cnt = tag_cnts[post_id]
|
||||
|
||||
((i + 1)...ids.size).each do |j|
|
||||
target_id = ids[j]
|
||||
b = tag_ids[target_id]
|
||||
b_cnt = tag_cnts[target_id]
|
||||
|
||||
norm = Math.sqrt(a_cnt * b_cnt)
|
||||
cos = norm.zero? ? 0.0 : intersection_size.(a, b).fdiv(norm)
|
||||
|
||||
push_topk.(top[post_id], cos, target_id)
|
||||
push_topk.(top[target_id], cos, post_id)
|
||||
end
|
||||
end
|
||||
|
||||
buf = []
|
||||
flush = -> do
|
||||
return if buf.empty?
|
||||
similarity_model.insert_all!(buf)
|
||||
buf.clear
|
||||
end
|
||||
|
||||
top.each do |post_id, list|
|
||||
list.each do |cos, target_post_id|
|
||||
buf << { "#{ model.name.underscore }_id".to_sym => post_id,
|
||||
"target_#{ model.name.underscore }_id".to_sym => target_post_id,
|
||||
cos: }
|
||||
flush.call if buf.size >= 1_000
|
||||
end
|
||||
end
|
||||
flush.call
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,122 @@
|
||||
require 'digest'
|
||||
|
||||
|
||||
module Wiki
|
||||
class Commit
|
||||
class Conflict < StandardError
|
||||
;
|
||||
end
|
||||
|
||||
def self.content! page:, body:, created_user:, message: nil, base_revision_id: nil
|
||||
new(page:, created_user:).content!(body:, message:, base_revision_id:)
|
||||
end
|
||||
|
||||
def self.redirect! page:, redirect_page:, created_user:, message: nil, base_revision_id: nil
|
||||
new(page:, created_user:).redirect!(redirect_page:, message:, base_revision_id:)
|
||||
end
|
||||
|
||||
def initialize page:, created_user:
|
||||
@page = page
|
||||
@created_user = created_user
|
||||
end
|
||||
|
||||
def content! body:, message:, base_revision_id:
|
||||
normalised = normalise_body(body)
|
||||
lines = split_lines(normalised)
|
||||
|
||||
line_shas = lines.map { |line| Digest::SHA256.hexdigest(line) }
|
||||
tree_sha = Digest::SHA256.hexdigest(line_shas.join(','))
|
||||
|
||||
line_id_by_sha = upsert_lines!(lines, line_shas)
|
||||
line_ids = line_shas.map { |sha| line_id_by_sha.fetch(sha) }
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
@page.lock!
|
||||
|
||||
if base_revision_id.present?
|
||||
current_id = @page.wiki_revisions.maximum(:id)
|
||||
if current_id && current_id != base_revision_id.to_i
|
||||
raise Conflict,
|
||||
"競合が発生してゐます(現在の Id.:#{ current_id },ベース Id.:#{ base_revision_id })."
|
||||
end
|
||||
end
|
||||
|
||||
rev = WikiRevision.create!(
|
||||
wiki_page: @page,
|
||||
base_revision_id:,
|
||||
created_user: @created_user,
|
||||
kind: :content,
|
||||
redirect_page_id: nil,
|
||||
message:,
|
||||
lines_count: lines.length,
|
||||
tree_sha256: tree_sha)
|
||||
|
||||
rows = line_ids.each_with_index.map do |line_id, pos|
|
||||
{ wiki_revision_id: rev.id, wiki_line_id: line_id, position: pos }
|
||||
end
|
||||
WikiRevisionLine.insert_all!(rows)
|
||||
|
||||
rev
|
||||
end
|
||||
end
|
||||
|
||||
def redirect! redirect_page:, message:, base_revision_id:
|
||||
ActiveRecord::Base.transaction do
|
||||
@page.lock!
|
||||
|
||||
if base_revision_id.present?
|
||||
current_id = @page.wiki_revisions.maximum(:id)
|
||||
if current_id && current_id != base_revision_id.to_i
|
||||
raise Conflict,
|
||||
"競合が発生してゐます(現在の Id.:#{ current_id },ベース Id.:#{ base_revision_id })."
|
||||
end
|
||||
end
|
||||
|
||||
WikiRevision.create!(
|
||||
wiki_page: @page,
|
||||
base_revision_id:,
|
||||
created_user: @created_user,
|
||||
kind: :redirect,
|
||||
redirect_page:,
|
||||
message:,
|
||||
lines_count: 0,
|
||||
tree_sha256: nil)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def normalise_body body
|
||||
s = body.to_s
|
||||
s.gsub!("\r\n", "\n")
|
||||
s.encode('UTF-8', invalid: :replace, undef: :replace, replace: '🖕')
|
||||
end
|
||||
|
||||
def split_lines body
|
||||
body.split("\n")
|
||||
end
|
||||
|
||||
def upsert_lines! lines, line_shas
|
||||
now = Time.current
|
||||
|
||||
id_by_sha = WikiLine.where(sha256: line_shas).pluck(:sha256, :id).to_h
|
||||
|
||||
missing_rows = []
|
||||
line_shas.each_with_index do |sha, i|
|
||||
next if id_by_sha.key?(sha)
|
||||
|
||||
missing_rows << { sha256: sha,
|
||||
body: lines[i],
|
||||
created_at: now,
|
||||
updated_at: now }
|
||||
end
|
||||
|
||||
if missing_rows.any?
|
||||
WikiLine.upsert_all(missing_rows)
|
||||
id_by_sha = WikiLine.where(sha256: line_shas).pluck(:sha256, :id).to_h
|
||||
end
|
||||
|
||||
id_by_sha
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -11,6 +11,9 @@ module Backend
|
||||
# Initialize configuration defaults for originally generated Rails version.
|
||||
config.load_defaults 8.0
|
||||
|
||||
config.i18n.available_locales = [:ja, :en]
|
||||
config.i18n.default_locale = :ja
|
||||
|
||||
# Please, add to the `ignore` list any other `lib` subdirectories that do
|
||||
# not contain `.rb` files, or that should not be reloaded or eager loaded.
|
||||
# Common ones are `templates`, `generators`, or `middleware`, for example.
|
||||
|
||||
@@ -28,4 +28,8 @@
|
||||
# enabled: "ON"
|
||||
|
||||
en:
|
||||
hello: "Hello world"
|
||||
activerecord:
|
||||
errors:
|
||||
messages:
|
||||
record_invalid: "Validation failed: %{errors}"
|
||||
taken: 'イキスギ!'
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
ja:
|
||||
hello: 'ぬ゛〜゛ん゛'
|
||||
+25
-10
@@ -1,10 +1,23 @@
|
||||
Rails.application.routes.draw do
|
||||
resources :nico_tags, path: 'tags/nico', only: [:index, :update]
|
||||
|
||||
resources :tags do
|
||||
scope 'tags/:parent_id/children', controller: :tag_children do
|
||||
post ':child_id', action: :create
|
||||
delete ':child_id', action: :destroy
|
||||
end
|
||||
|
||||
resources :tags, only: [:index, :show, :update] do
|
||||
collection do
|
||||
get :autocomplete
|
||||
get 'name/:name', action: :show_by_name
|
||||
|
||||
scope :name do
|
||||
get ':name/deerjikists', action: :deerjikists_by_name
|
||||
get ':name', action: :show_by_name
|
||||
end
|
||||
end
|
||||
|
||||
member do
|
||||
get :deerjikists
|
||||
end
|
||||
end
|
||||
|
||||
@@ -30,7 +43,7 @@ Rails.application.routes.draw do
|
||||
end
|
||||
end
|
||||
|
||||
resources :posts do
|
||||
resources :posts, only: [:index, :show, :create, :update] do
|
||||
collection do
|
||||
get :random
|
||||
get :changes
|
||||
@@ -50,11 +63,13 @@ Rails.application.routes.draw do
|
||||
end
|
||||
end
|
||||
|
||||
resources :ip_addresses
|
||||
resources :nico_tag_relations
|
||||
resources :post_tags
|
||||
resources :settings
|
||||
resources :tag_aliases
|
||||
resources :user_ips
|
||||
resources :user_post_views
|
||||
resources :deerjikists, only: [] do
|
||||
collection do
|
||||
scope ':platform/:code' do
|
||||
get '', action: :show
|
||||
put '', action: :update
|
||||
delete '', action: :destroy
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
class RemoveTagFromWikiPages < ActiveRecord::Migration[7.0]
|
||||
def change
|
||||
remove_reference :wiki_pages, :tag, if_exists: true
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,11 @@
|
||||
class CreateWikiLines < ActiveRecord::Migration[7.0]
|
||||
def change
|
||||
create_table :wiki_lines do |t|
|
||||
t.string :sha256, null: false, limit: 64
|
||||
t.text :body, null: false
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
add_index :wiki_lines, :sha256, unique: true
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,19 @@
|
||||
class CreateWikiRevisions < ActiveRecord::Migration[7.0]
|
||||
def change
|
||||
create_table :wiki_revisions do |t|
|
||||
t.references :wiki_page, null: false, foreign_key: true
|
||||
t.references :base_revision, foreign_key: { to_table: :wiki_revisions }
|
||||
t.references :created_user, null: false, foreign_key: { to_table: :users }
|
||||
t.integer :kind, null: false, default: 0 # 0: content, 1: redirect
|
||||
t.references :redirect_page, foreign_key: { to_table: :wiki_pages }
|
||||
t.string :message
|
||||
t.integer :lines_count, null: false, default: 0
|
||||
t.string :tree_sha256, limit: 64
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
add_index :wiki_revisions, :tree_sha256
|
||||
add_index :wiki_revisions, [:wiki_page_id, :id]
|
||||
add_index :wiki_revisions, :kind
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,12 @@
|
||||
class CreateWikiRevisionLines < ActiveRecord::Migration[7.0]
|
||||
def change
|
||||
create_table :wiki_revision_lines do |t|
|
||||
t.references :wiki_revision, null: false, foreign_key: true
|
||||
t.integer :position, null: false
|
||||
t.references :wiki_line, null: false, foreign_key: true
|
||||
end
|
||||
|
||||
add_index :wiki_revision_lines, [:wiki_revision_id, :position], unique: true
|
||||
add_index :wiki_revision_lines, [:wiki_revision_id, :wiki_line_id]
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,27 @@
|
||||
class MakeThumbnailBaseNullableInPosts < ActiveRecord::Migration[7.0]
|
||||
def up
|
||||
change_column_null :posts, :thumbnail_base, true
|
||||
|
||||
execute <<~SQL
|
||||
UPDATE
|
||||
posts
|
||||
SET
|
||||
thumbnail_base = NULL
|
||||
WHERE
|
||||
thumbnail_base = ''
|
||||
SQL
|
||||
end
|
||||
|
||||
def down
|
||||
execute <<~SQL
|
||||
UPDATE
|
||||
posts
|
||||
SET
|
||||
thumbnail_base = ''
|
||||
WHERE
|
||||
thumbnail_base IS NULL
|
||||
SQL
|
||||
|
||||
change_column_null :posts, :thumbnail_base, false
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,28 @@
|
||||
class MakeTitleNullableInPosts < ActiveRecord::Migration[7.0]
|
||||
def up
|
||||
change_column_null :posts, :title, true
|
||||
|
||||
execute <<~SQL
|
||||
UPDATE
|
||||
posts
|
||||
SET
|
||||
title = NULL
|
||||
WHERE
|
||||
title = ''
|
||||
SQL
|
||||
end
|
||||
|
||||
def down
|
||||
execute <<~SQL
|
||||
UPDATE
|
||||
posts
|
||||
SET
|
||||
title = ''
|
||||
WHERE
|
||||
title IS NULL
|
||||
SQL
|
||||
|
||||
change_column_null :posts, :title, false
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
class CreateTagNames < ActiveRecord::Migration[7.0]
|
||||
def change
|
||||
create_table :tag_names do |t|
|
||||
t.string :name, limit: 255, null: false
|
||||
t.references :canonical, null: true, foreign_key: { to_table: :tag_names }, index: true
|
||||
t.timestamps
|
||||
end
|
||||
add_index :tag_names, :name, unique: true
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,42 @@
|
||||
class AddTagNameToTags < ActiveRecord::Migration[7.0]
|
||||
class Tag < ApplicationRecord
|
||||
self.table_name = 'tags'
|
||||
end
|
||||
|
||||
class TagName < ApplicationRecord
|
||||
self.table_name = 'tag_names'
|
||||
end
|
||||
|
||||
def up
|
||||
add_reference :tags, :tag_name, null: true, foreign_key: true, index: false, after: :id
|
||||
add_index :tags, :tag_name_id, unique: true
|
||||
|
||||
Tag.find_each do |tag|
|
||||
name = tag.read_attribute(:name)
|
||||
tn = TagName.find_or_create_by!(name:) do |r|
|
||||
r.canonical_id = nil
|
||||
end
|
||||
tag.update_columns(tag_name_id: tn.id)
|
||||
end
|
||||
|
||||
change_column_null :tags, :tag_name_id, false
|
||||
|
||||
remove_column :tags, :name
|
||||
end
|
||||
|
||||
def down
|
||||
add_column :tags, :name, :string, limit: 255, null: true, after: :id
|
||||
|
||||
Tag.find_each do |tag|
|
||||
tag_name_id = tag.read_attribute(:tag_name_id)
|
||||
name = TagName.find(tag_name_id).read_attribute(:name)
|
||||
tag.update_columns(name:)
|
||||
end
|
||||
|
||||
change_column_null :tags, :name, false
|
||||
|
||||
remove_foreign_key :tags, column: :tag_name_id
|
||||
remove_index :tags, :tag_name_id
|
||||
remove_column :tags, :tag_name_id
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,42 @@
|
||||
class AddTagNameToWikiPages < ActiveRecord::Migration[7.0]
|
||||
class WikiPage < ApplicationRecord
|
||||
self.table_name = 'wiki_pages'
|
||||
end
|
||||
|
||||
class TagName < ApplicationRecord
|
||||
self.table_name = 'tag_names'
|
||||
end
|
||||
|
||||
def up
|
||||
add_reference :wiki_pages, :tag_name, null: true, foreign_key: true, index: false, after: :id
|
||||
add_index :wiki_pages, :tag_name_id, unique: true
|
||||
|
||||
WikiPage.find_each do |page|
|
||||
name = page.read_attribute(:title)
|
||||
tn = TagName.find_or_create_by!(name:) do |r|
|
||||
r.canonical_id = nil
|
||||
end
|
||||
page.update_columns(tag_name_id: tn.id)
|
||||
end
|
||||
|
||||
change_column_null :wiki_pages, :tag_name_id, false
|
||||
|
||||
remove_column :wiki_pages, :title
|
||||
end
|
||||
|
||||
def down
|
||||
add_column :wiki_pages, :title, :string, limit: 255, null: true, after: :id
|
||||
|
||||
WikiPage.find_each do |page|
|
||||
tag_name_id = page.read_attribute(:tag_name_id)
|
||||
title = TagName.find(tag_name_id).read_attribute(:name)
|
||||
page.update_columns(title:)
|
||||
end
|
||||
|
||||
change_column_null :wiki_pages, :title, false
|
||||
|
||||
remove_foreign_key :wiki_pages, column: :tag_name_id
|
||||
remove_index :wiki_pages, :tag_name_id
|
||||
remove_column :wiki_pages, :tag_name_id
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,9 @@
|
||||
class DropTagAliases < ActiveRecord::Migration[7.0]
|
||||
def up
|
||||
drop_table :tag_aliases
|
||||
end
|
||||
|
||||
def down
|
||||
raise ActiveRecord::IrreversibleMigration, '戻せません.'
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,6 @@
|
||||
class AddUniqueIndexToUrlInPosts < ActiveRecord::Migration[7.1]
|
||||
def change
|
||||
change_column :posts, :url, :string, limit: 768
|
||||
add_index :posts, :url, unique: true, name: 'index_posts_on_url'
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,43 @@
|
||||
class ChangePostSimilaritiesToCompositePk < ActiveRecord::Migration[8.0]
|
||||
def up
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
post_similarities
|
||||
MODIFY COLUMN id BIGINT NOT NULL
|
||||
;
|
||||
SQL
|
||||
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
post_similarities
|
||||
DROP PRIMARY KEY
|
||||
;
|
||||
SQL
|
||||
|
||||
remove_column :post_similarities, :id
|
||||
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
post_similarities
|
||||
ADD PRIMARY KEY (post_id, target_post_id)
|
||||
;
|
||||
SQL
|
||||
end
|
||||
|
||||
def down
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
post_similarities
|
||||
DROP PRIMARY KEY
|
||||
;
|
||||
SQL
|
||||
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
post_similarities
|
||||
ADD COLUMN id BIGINT NOT NULL AUTO_INCREMENT FIRST
|
||||
, ADD PRIMARY KEY (id)
|
||||
;
|
||||
SQL
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,43 @@
|
||||
class ChangeTagSimilaritiesToCompositePk < ActiveRecord::Migration[8.0]
|
||||
def up
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
tag_similarities
|
||||
MODIFY COLUMN id BIGINT NOT NULL
|
||||
;
|
||||
SQL
|
||||
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
tag_similarities
|
||||
DROP PRIMARY KEY
|
||||
;
|
||||
SQL
|
||||
|
||||
remove_column :tag_similarities, :id
|
||||
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
tag_similarities
|
||||
ADD PRIMARY KEY (tag_id, target_tag_id)
|
||||
;
|
||||
SQL
|
||||
end
|
||||
|
||||
def down
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
tag_similarities
|
||||
DROP PRIMARY KEY
|
||||
;
|
||||
SQL
|
||||
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
tag_similarities
|
||||
ADD COLUMN id BIGINT NOT NULL AUTO_INCREMENT FIRST
|
||||
, ADD PRIMARY KEY (id)
|
||||
;
|
||||
SQL
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,9 @@
|
||||
class AddIndexToPostSimilarities < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
remove_index :post_similarities, name: 'index_post_similarities_on_post_id'
|
||||
|
||||
add_index :post_similarities, [:post_id, :cos],
|
||||
order: { cos: :desc },
|
||||
name: 'index_post_similarities_on_post_id_and_cos'
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,9 @@
|
||||
class AddIndexToTagSimilarities < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
remove_index :tag_similarities, name: 'index_tag_similarities_on_tag_id'
|
||||
|
||||
add_index :tag_similarities, [:tag_id, :cos],
|
||||
order: { cos: :desc },
|
||||
name: 'index_tag_similarities_on_tag_id_and_cos'
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,5 @@
|
||||
class AddUniqueIndexToIpAddresses < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
add_index :ip_addresses, :ip_address, unique: true, name: 'index_ip_addresses_on_ip_address'
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,47 @@
|
||||
class ChangeUserIpsToCompositePk < ActiveRecord::Migration[8.0]
|
||||
def up
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
user_ips
|
||||
MODIFY COLUMN id BIGINT NOT NULL
|
||||
;
|
||||
SQL
|
||||
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
user_ips
|
||||
DROP PRIMARY KEY
|
||||
;
|
||||
SQL
|
||||
|
||||
remove_column :user_ips, :id
|
||||
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
user_ips
|
||||
ADD PRIMARY KEY (user_id, ip_address_id)
|
||||
;
|
||||
SQL
|
||||
|
||||
remove_index :user_ips, name: 'index_user_ips_on_user_id'
|
||||
end
|
||||
|
||||
def down
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
user_ips
|
||||
DROP PRIMARY KEY
|
||||
;
|
||||
SQL
|
||||
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
user_ips
|
||||
ADD COLUMN id BIGINT NOT NULL AUTO_INCREMENT FIRST
|
||||
, ADD PRIMARY KEY (id)
|
||||
;
|
||||
SQL
|
||||
|
||||
add_index :user_ips, :user_id, name: 'index_user_ips_on_user_id'
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,47 @@
|
||||
class ChangeUserPostViewsToCompositePk < ActiveRecord::Migration[8.0]
|
||||
def up
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
user_post_views
|
||||
MODIFY COLUMN id BIGINT NOT NULL
|
||||
;
|
||||
SQL
|
||||
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
user_post_views
|
||||
DROP PRIMARY KEY
|
||||
;
|
||||
SQL
|
||||
|
||||
remove_column :user_post_views, :id
|
||||
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
user_post_views
|
||||
ADD PRIMARY KEY (user_id, post_id)
|
||||
;
|
||||
SQL
|
||||
|
||||
remove_index :user_post_views, name: 'index_user_post_views_on_user_id'
|
||||
end
|
||||
|
||||
def down
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
user_post_views
|
||||
DROP PRIMARY KEY
|
||||
;
|
||||
SQL
|
||||
|
||||
execute <<~SQL
|
||||
ALTER TABLE
|
||||
user_post_views
|
||||
ADD COLUMN id BIGINT NOT NULL AUTO_INCREMENT FIRST
|
||||
, ADD PRIMARY KEY (id)
|
||||
;
|
||||
SQL
|
||||
|
||||
add_index :user_post_views, :user_id, name: 'index_user_post_views_on_user_id'
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,10 @@
|
||||
class CreateDeerjikists < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
create_table :deerjikists, primary_key: [:platform, :code] do |t|
|
||||
t.string :platform, null: false, limit: 16
|
||||
t.string :code, null: false
|
||||
t.references :tag, null: false
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
生成ファイル
+81
-26
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[8.0].define(version: 2025_12_10_123200) do
|
||||
ActiveRecord::Schema[8.0].define(version: 2026_03_03_122700) do
|
||||
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.string "record_type", null: false
|
||||
@@ -39,11 +39,21 @@ ActiveRecord::Schema[8.0].define(version: 2025_12_10_123200) do
|
||||
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
|
||||
end
|
||||
|
||||
create_table "deerjikists", primary_key: ["platform", "code"], charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.string "platform", limit: 16, null: false
|
||||
t.string "code", null: false
|
||||
t.bigint "tag_id", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["tag_id"], name: "index_deerjikists_on_tag_id"
|
||||
end
|
||||
|
||||
create_table "ip_addresses", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.binary "ip_address", limit: 16, null: false
|
||||
t.boolean "banned", default: false, null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["ip_address"], name: "index_ip_addresses_on_ip_address", unique: true
|
||||
end
|
||||
|
||||
create_table "nico_tag_relations", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
@@ -55,11 +65,11 @@ ActiveRecord::Schema[8.0].define(version: 2025_12_10_123200) do
|
||||
t.index ["tag_id"], name: "index_nico_tag_relations_on_tag_id"
|
||||
end
|
||||
|
||||
create_table "post_similarities", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
create_table "post_similarities", primary_key: ["post_id", "target_post_id"], charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.bigint "post_id", null: false
|
||||
t.bigint "target_post_id", null: false
|
||||
t.float "cos", null: false
|
||||
t.index ["post_id"], name: "index_post_similarities_on_post_id"
|
||||
t.index ["post_id", "cos"], name: "index_post_similarities_on_post_id_and_cos", order: { cos: :desc }
|
||||
t.index ["target_post_id"], name: "index_post_similarities_on_target_post_id"
|
||||
end
|
||||
|
||||
@@ -84,9 +94,9 @@ ActiveRecord::Schema[8.0].define(version: 2025_12_10_123200) do
|
||||
end
|
||||
|
||||
create_table "posts", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.string "title", null: false
|
||||
t.string "url", limit: 2000, null: false
|
||||
t.string "thumbnail_base", limit: 2000, null: false
|
||||
t.string "title"
|
||||
t.string "url", limit: 768, null: false
|
||||
t.string "thumbnail_base", limit: 2000
|
||||
t.bigint "parent_id"
|
||||
t.bigint "uploaded_user_id"
|
||||
t.datetime "created_at", null: false
|
||||
@@ -95,6 +105,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_12_10_123200) do
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["parent_id"], name: "index_posts_on_parent_id"
|
||||
t.index ["uploaded_user_id"], name: "index_posts_on_uploaded_user_id"
|
||||
t.index ["url"], name: "index_posts_on_url", unique: true
|
||||
end
|
||||
|
||||
create_table "settings", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
@@ -106,14 +117,6 @@ ActiveRecord::Schema[8.0].define(version: 2025_12_10_123200) do
|
||||
t.index ["user_id"], name: "index_settings_on_user_id"
|
||||
end
|
||||
|
||||
create_table "tag_aliases", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.bigint "tag_id", null: false
|
||||
t.string "name", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["tag_id"], name: "index_tag_aliases_on_tag_id"
|
||||
end
|
||||
|
||||
create_table "tag_implications", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.bigint "tag_id", null: false
|
||||
t.bigint "parent_tag_id", null: false
|
||||
@@ -124,38 +127,46 @@ ActiveRecord::Schema[8.0].define(version: 2025_12_10_123200) do
|
||||
t.index ["tag_id"], name: "index_tag_implications_on_tag_id"
|
||||
end
|
||||
|
||||
create_table "tag_similarities", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
create_table "tag_names", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.bigint "canonical_id"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["canonical_id"], name: "index_tag_names_on_canonical_id"
|
||||
t.index ["name"], name: "index_tag_names_on_name", unique: true
|
||||
end
|
||||
|
||||
create_table "tag_similarities", primary_key: ["tag_id", "target_tag_id"], charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.bigint "tag_id", null: false
|
||||
t.bigint "target_tag_id", null: false
|
||||
t.float "cos", null: false
|
||||
t.index ["tag_id"], name: "index_tag_similarities_on_tag_id"
|
||||
t.index ["tag_id", "cos"], name: "index_tag_similarities_on_tag_id_and_cos", order: { cos: :desc }
|
||||
t.index ["target_tag_id"], name: "index_tag_similarities_on_target_tag_id"
|
||||
end
|
||||
|
||||
create_table "tags", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.bigint "tag_name_id", null: false
|
||||
t.string "category", default: "general", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "post_count", default: 0, null: false
|
||||
t.index ["tag_name_id"], name: "index_tags_on_tag_name_id", unique: true
|
||||
end
|
||||
|
||||
create_table "user_ips", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
create_table "user_ips", primary_key: ["user_id", "ip_address_id"], charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.bigint "user_id", null: false
|
||||
t.bigint "ip_address_id", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["ip_address_id"], name: "index_user_ips_on_ip_address_id"
|
||||
t.index ["user_id"], name: "index_user_ips_on_user_id"
|
||||
end
|
||||
|
||||
create_table "user_post_views", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
create_table "user_post_views", primary_key: ["user_id", "post_id"], charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.bigint "user_id", null: false
|
||||
t.bigint "post_id", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["post_id"], name: "index_user_post_views_on_post_id"
|
||||
t.index ["user_id"], name: "index_user_post_views_on_user_id"
|
||||
end
|
||||
|
||||
create_table "users", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
@@ -167,18 +178,55 @@ ActiveRecord::Schema[8.0].define(version: 2025_12_10_123200) do
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
create_table "wiki_lines", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.string "sha256", limit: 64, null: false
|
||||
t.text "body", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["sha256"], name: "index_wiki_lines_on_sha256", unique: true
|
||||
end
|
||||
|
||||
create_table "wiki_pages", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.string "title", null: false
|
||||
t.bigint "tag_id"
|
||||
t.bigint "tag_name_id", null: false
|
||||
t.bigint "created_user_id", null: false
|
||||
t.bigint "updated_user_id", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["created_user_id"], name: "index_wiki_pages_on_created_user_id"
|
||||
t.index ["tag_id"], name: "index_wiki_pages_on_tag_id"
|
||||
t.index ["tag_name_id"], name: "index_wiki_pages_on_tag_name_id", unique: true
|
||||
t.index ["updated_user_id"], name: "index_wiki_pages_on_updated_user_id"
|
||||
end
|
||||
|
||||
create_table "wiki_revision_lines", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.bigint "wiki_revision_id", null: false
|
||||
t.integer "position", null: false
|
||||
t.bigint "wiki_line_id", null: false
|
||||
t.index ["wiki_line_id"], name: "index_wiki_revision_lines_on_wiki_line_id"
|
||||
t.index ["wiki_revision_id", "position"], name: "index_wiki_revision_lines_on_wiki_revision_id_and_position", unique: true
|
||||
t.index ["wiki_revision_id", "wiki_line_id"], name: "index_wiki_revision_lines_on_wiki_revision_id_and_wiki_line_id"
|
||||
t.index ["wiki_revision_id"], name: "index_wiki_revision_lines_on_wiki_revision_id"
|
||||
end
|
||||
|
||||
create_table "wiki_revisions", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.bigint "wiki_page_id", null: false
|
||||
t.bigint "base_revision_id"
|
||||
t.bigint "created_user_id", null: false
|
||||
t.integer "kind", default: 0, null: false
|
||||
t.bigint "redirect_page_id"
|
||||
t.string "message"
|
||||
t.integer "lines_count", default: 0, null: false
|
||||
t.string "tree_sha256", limit: 64
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["base_revision_id"], name: "index_wiki_revisions_on_base_revision_id"
|
||||
t.index ["created_user_id"], name: "index_wiki_revisions_on_created_user_id"
|
||||
t.index ["kind"], name: "index_wiki_revisions_on_kind"
|
||||
t.index ["redirect_page_id"], name: "index_wiki_revisions_on_redirect_page_id"
|
||||
t.index ["tree_sha256"], name: "index_wiki_revisions_on_tree_sha256"
|
||||
t.index ["wiki_page_id", "id"], name: "index_wiki_revisions_on_wiki_page_id_and_id"
|
||||
t.index ["wiki_page_id"], name: "index_wiki_revisions_on_wiki_page_id"
|
||||
end
|
||||
|
||||
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
|
||||
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
|
||||
add_foreign_key "nico_tag_relations", "tags"
|
||||
@@ -192,16 +240,23 @@ ActiveRecord::Schema[8.0].define(version: 2025_12_10_123200) do
|
||||
add_foreign_key "posts", "posts", column: "parent_id"
|
||||
add_foreign_key "posts", "users", column: "uploaded_user_id"
|
||||
add_foreign_key "settings", "users"
|
||||
add_foreign_key "tag_aliases", "tags"
|
||||
add_foreign_key "tag_implications", "tags"
|
||||
add_foreign_key "tag_implications", "tags", column: "parent_tag_id"
|
||||
add_foreign_key "tag_names", "tag_names", column: "canonical_id"
|
||||
add_foreign_key "tag_similarities", "tags"
|
||||
add_foreign_key "tag_similarities", "tags", column: "target_tag_id"
|
||||
add_foreign_key "tags", "tag_names"
|
||||
add_foreign_key "user_ips", "ip_addresses"
|
||||
add_foreign_key "user_ips", "users"
|
||||
add_foreign_key "user_post_views", "posts"
|
||||
add_foreign_key "user_post_views", "users"
|
||||
add_foreign_key "wiki_pages", "tags"
|
||||
add_foreign_key "wiki_pages", "tag_names"
|
||||
add_foreign_key "wiki_pages", "users", column: "created_user_id"
|
||||
add_foreign_key "wiki_pages", "users", column: "updated_user_id"
|
||||
add_foreign_key "wiki_revision_lines", "wiki_lines"
|
||||
add_foreign_key "wiki_revision_lines", "wiki_revisions"
|
||||
add_foreign_key "wiki_revisions", "users", column: "created_user_id"
|
||||
add_foreign_key "wiki_revisions", "wiki_pages"
|
||||
add_foreign_key "wiki_revisions", "wiki_pages", column: "redirect_page_id"
|
||||
add_foreign_key "wiki_revisions", "wiki_revisions", column: "base_revision_id"
|
||||
end
|
||||
|
||||
@@ -1,28 +1,6 @@
|
||||
namespace :post_similarity do
|
||||
desc '関聯投稿テーブル作成'
|
||||
task calc: :environment do
|
||||
dot = -> a, b { (a.keys & b.keys).sum { |k| a[k] * b[k] } }
|
||||
norm = -> v { Math.sqrt(v.values.sum { |e| e * e }) }
|
||||
cos = -> a, b do
|
||||
na = norm.(a)
|
||||
nb = norm.(b)
|
||||
if na.zero? || nb.zero?
|
||||
0.0
|
||||
else
|
||||
dot.(a, b) / na / nb
|
||||
end
|
||||
end
|
||||
|
||||
posts = Post.includes(:tags).to_a
|
||||
posts.each_with_index do |post, i|
|
||||
existence_of_tags = post.tags.index_with(1)
|
||||
((i + 1)...posts.size).each do |j|
|
||||
target_post = posts[j]
|
||||
existence_of_target_tags = target_post.tags.index_with(1)
|
||||
PostSimilarity.find_or_initialize_by(post:, target_post:).tap { |ps|
|
||||
ps.cos = cos.(existence_of_tags, existence_of_target_tags)
|
||||
}.save!
|
||||
end
|
||||
end
|
||||
Similarity::Calc.call(Post, :tags)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace :tag_similarity do
|
||||
desc '関聯タグ・テーブル作成'
|
||||
task calc: :environment do
|
||||
Similarity::Calc.call(Tag, :posts)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,74 @@
|
||||
namespace :wiki do
|
||||
desc 'Wiki 移行'
|
||||
task migrate: :environment do
|
||||
require 'digest'
|
||||
require 'gollum-lib'
|
||||
|
||||
wiki = Gollum::Wiki.new(Rails.root.join('wiki').to_s)
|
||||
|
||||
WikiPage.where.missing(:wiki_revisions).find_each do |wiki_page|
|
||||
page = wiki.page("#{ wiki_page.id }.md")
|
||||
next unless page
|
||||
|
||||
versions = page.versions
|
||||
next if versions.blank?
|
||||
|
||||
base_revision_id = nil
|
||||
versions.reverse_each do |version|
|
||||
pg = wiki.page("#{ wiki_page.id }.md", version.id)
|
||||
raw = pg&.raw_data
|
||||
next unless raw
|
||||
|
||||
lines = raw.force_encoding('UTF-8').split("\n")
|
||||
|
||||
line_shas = lines.map { |l| Digest::SHA256.hexdigest(l) }
|
||||
tree_sha = Digest::SHA256.hexdigest(line_shas.join(','))
|
||||
|
||||
at = version.authored_date
|
||||
|
||||
line_id_by_sha = WikiLine.where(sha256: line_shas).pluck(:sha256, :id).to_h
|
||||
|
||||
missing_rows = []
|
||||
line_shas.each_with_index do |sha, i|
|
||||
next if line_id_by_sha.key?(sha)
|
||||
|
||||
missing_rows << { sha256: sha,
|
||||
body: lines[i],
|
||||
created_at: at,
|
||||
updated_at: at }
|
||||
end
|
||||
|
||||
if missing_rows.any?
|
||||
WikiLine.upsert_all(missing_rows)
|
||||
line_id_by_sha = WikiLine.where(sha256: line_shas).pluck(:sha256, :id).to_h
|
||||
end
|
||||
line_ids = line_shas.map { |sha| line_id_by_sha.fetch(sha) }
|
||||
|
||||
rev = nil
|
||||
ActiveRecord::Base.transaction do
|
||||
wiki_page.lock!
|
||||
|
||||
rev = WikiRevision.create!(
|
||||
wiki_page:,
|
||||
base_revision_id:,
|
||||
created_user_id: (Integer(version.author.name) rescue 2),
|
||||
kind: :content,
|
||||
redirect_page_id: nil,
|
||||
message: nil,
|
||||
lines_count: lines.length,
|
||||
tree_sha256: tree_sha,
|
||||
created_at: at,
|
||||
updated_at: at)
|
||||
|
||||
rows = line_ids.each_with_index.map do |line_id, pos|
|
||||
{ wiki_revision_id: rev.id,
|
||||
wiki_line_id: line_id,
|
||||
position: pos }
|
||||
end
|
||||
WikiRevisionLine.insert_all!(rows)
|
||||
end
|
||||
base_revision_id = rev.id
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -3,9 +3,10 @@ namespace :nico do
|
||||
task sync: :environment do
|
||||
require 'json'
|
||||
require 'nokogiri'
|
||||
require 'open3'
|
||||
require 'open-uri'
|
||||
require 'open3'
|
||||
require 'set'
|
||||
require 'time'
|
||||
|
||||
fetch_thumbnail = -> url do
|
||||
html = URI.open(url, read_timeout: 60, 'User-Agent' => 'Mozilla/5.0').read
|
||||
@@ -14,12 +15,12 @@ namespace :nico do
|
||||
doc.at('meta[name="thumbnail"]')&.[]('content').presence
|
||||
end
|
||||
|
||||
def sync_post_tags! post, desired_tag_ids
|
||||
desired_ids = desired_tag_ids.compact.to_set
|
||||
current_ids = post.tags.pluck(:id).to_set
|
||||
def sync_post_tags! post, desired_tag_ids, current_tag_ids: nil
|
||||
current_tag_ids ||= PostTag.kept.where(post_id: post.id).pluck(:tag_id).to_set
|
||||
desired_tag_ids = desired_tag_ids.compact.to_set
|
||||
|
||||
to_add = desired_ids - current_ids
|
||||
to_remove = current_ids - desired_ids
|
||||
to_add = desired_tag_ids - current_tag_ids
|
||||
to_remove = current_tag_ids - desired_tag_ids
|
||||
|
||||
Tag.where(id: to_add.to_a).find_each do |tag|
|
||||
begin
|
||||
@@ -41,18 +42,49 @@ namespace :nico do
|
||||
{ 'MYSQL_USER' => mysql_user, 'MYSQL_PASS' => mysql_pass },
|
||||
'python3', "#{ nizika_nico_path }/get_videos.py")
|
||||
|
||||
abort unless status.success?
|
||||
unless status.success?
|
||||
warn stderr
|
||||
abort
|
||||
end
|
||||
|
||||
data = JSON.parse(stdout)
|
||||
data.each do |datum|
|
||||
post = Post.where('url LIKE ?', '%nicovideo.jp%').find { |post|
|
||||
post.url =~ %r{#{ Regexp.escape(datum['code']) }(?!\d)}
|
||||
}
|
||||
unless post
|
||||
title = datum['title']
|
||||
url = "https://www.nicovideo.jp/watch/#{ datum['code'] }"
|
||||
thumbnail_base = fetch_thumbnail.(url) || '' rescue ''
|
||||
post = Post.new(title:, url:, thumbnail_base:, uploaded_user: nil)
|
||||
code = datum['code']
|
||||
post =
|
||||
Post
|
||||
.where('url REGEXP ?', "nicovideo\\.jp/watch/#{ Regexp.escape(code) }([^0-9]|$)")
|
||||
.first
|
||||
|
||||
title = datum['title']
|
||||
original_created_at = datum['uploaded_at'] &&
|
||||
Time.strptime(datum['uploaded_at'], '%Y-%m-%d %H:%M:%S')
|
||||
original_created_from = original_created_at&.change(sec: 0)
|
||||
original_created_before = original_created_from&.+(1.minute)
|
||||
|
||||
if post
|
||||
attrs = { title:, original_created_from:, original_created_before: }
|
||||
|
||||
unless post.thumbnail.attached?
|
||||
thumbnail_base = fetch_thumbnail.(post.url) rescue nil
|
||||
if thumbnail_base.present?
|
||||
post.thumbnail.attach(
|
||||
io: URI.open(thumbnail_base),
|
||||
filename: File.basename(URI.parse(thumbnail_base).path),
|
||||
content_type: 'image/jpeg')
|
||||
attrs[:thumbnail_base] = thumbnail_base
|
||||
end
|
||||
end
|
||||
|
||||
post.assign_attributes(attrs)
|
||||
if post.changed?
|
||||
post.save!
|
||||
post.resized_thumbnail! if post.thumbnail.attached?
|
||||
end
|
||||
else
|
||||
url = "https://www.nicovideo.jp/watch/#{ code }"
|
||||
thumbnail_base = fetch_thumbnail.(url) rescue nil
|
||||
post = Post.new(title:, url:, thumbnail_base:, uploaded_user: nil,
|
||||
original_created_from:, original_created_before:)
|
||||
if thumbnail_base.present?
|
||||
post.thumbnail.attach(
|
||||
io: URI.open(thumbnail_base),
|
||||
@@ -61,40 +93,53 @@ namespace :nico do
|
||||
end
|
||||
post.save!
|
||||
post.resized_thumbnail!
|
||||
sync_post_tags!(post, [Tag.tagme.id])
|
||||
sync_post_tags!(post, [Tag.tagme.id, Tag.bot.id, Tag.niconico.id, Tag.video.id])
|
||||
end
|
||||
|
||||
kept_tags = post.tags.reload
|
||||
kept_non_nico_ids = kept_tags.where.not(category: 'nico').pluck(:id).to_set
|
||||
tags = post.tags
|
||||
# 既存のタグ Id. 集合
|
||||
kept_tag_ids = tags.pluck(:id).to_set
|
||||
# うち内部タグ Id. 集合
|
||||
kept_non_nico_tag_ids = tags.not_nico.pluck(:id).to_set
|
||||
|
||||
# 記載すべき外部タグ Id. および連携される内部タグ Id. のリスト
|
||||
desired_nico_tag_based_ids = []
|
||||
# 記載すべき内部タグ Id. のリスト
|
||||
desired_non_nico_tag_ids = []
|
||||
|
||||
desired_nico_ids = []
|
||||
desired_non_nico_ids = []
|
||||
datum['tags'].each do |raw|
|
||||
name = "nico:#{ raw }"
|
||||
tag = Tag.find_or_initialize_by(name:) do |t|
|
||||
t.category = 'nico'
|
||||
end
|
||||
tag.save! if tag.new_record?
|
||||
desired_nico_ids << tag.id
|
||||
unless tag.in?(kept_tags)
|
||||
desired_non_nico_ids.concat(tag.linked_tags.pluck(:id))
|
||||
desired_nico_ids.concat(tag.linked_tags.pluck(:id))
|
||||
tag = Tag.find_or_create_by_tag_name!(name, category: :nico)
|
||||
desired_nico_tag_based_ids << tag.id
|
||||
|
||||
# 新たに記載される外部タグと連携される内部タグを記載
|
||||
unless tag.id.in?(kept_tag_ids)
|
||||
linked_ids = tag.linked_tags.pluck(:id)
|
||||
desired_non_nico_tag_ids.concat(linked_ids)
|
||||
desired_nico_tag_based_ids.concat(linked_ids)
|
||||
end
|
||||
end
|
||||
desired_nico_ids.uniq!
|
||||
|
||||
desired_all_ids = kept_non_nico_ids.to_a + desired_nico_ids
|
||||
desired_non_nico_ids.concat(kept_non_nico_ids.to_a)
|
||||
desired_non_nico_ids.uniq!
|
||||
if kept_non_nico_ids.to_set != desired_non_nico_ids.to_set
|
||||
desired_all_ids << Tag.bot.id
|
||||
deerjikist = Deerjikist.find_by(platform: :nico, code: datum['user'])
|
||||
if deerjikist
|
||||
desired_non_nico_tag_ids << deerjikist.tag_id
|
||||
desired_nico_tag_based_ids << deerjikist.tag_id
|
||||
elsif !(Tag.where(id: kept_non_nico_tag_ids).where(category: :deerjikist).exists?)
|
||||
desired_non_nico_tag_ids << Tag.no_deerjikist.id
|
||||
desired_nico_tag_based_ids << Tag.no_deerjikist.id
|
||||
end
|
||||
unless Tag.where(id: desired_all_ids).where(category: 'deerjikist').exists?
|
||||
desired_all_ids << Tag.no_deerjikist.id
|
||||
end
|
||||
desired_all_ids.uniq!
|
||||
|
||||
sync_post_tags!(post, desired_all_ids)
|
||||
desired_nico_tag_based_ids.uniq!
|
||||
|
||||
desired_all_tag_ids = kept_non_nico_tag_ids.to_a + desired_nico_tag_based_ids
|
||||
desired_non_nico_tag_ids.concat(kept_non_nico_tag_ids.to_a)
|
||||
desired_non_nico_tag_ids.uniq!
|
||||
if kept_non_nico_tag_ids != desired_non_nico_tag_ids.to_set
|
||||
desired_all_tag_ids << Tag.bot.id
|
||||
end
|
||||
desired_all_tag_ids.uniq!
|
||||
|
||||
sync_post_tags!(post, desired_all_tag_ids, current_tag_ids: kept_tag_ids)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
FactoryBot.define do
|
||||
factory :tag_name do
|
||||
name { "tag-#{SecureRandom.hex(4)}" }
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,12 @@
|
||||
FactoryBot.define do
|
||||
factory :tag do
|
||||
category { :general }
|
||||
post_count { 0 }
|
||||
association :tag_name
|
||||
|
||||
trait :nico do
|
||||
category { :nico }
|
||||
tag_name { association(:tag_name, name: "nico:#{ SecureRandom.hex(4) }") }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,15 @@
|
||||
FactoryBot.define do
|
||||
factory :user do
|
||||
name { "test-user" }
|
||||
inheritance_code { SecureRandom.uuid }
|
||||
role { "guest" }
|
||||
|
||||
trait :member do
|
||||
role { "member" }
|
||||
end
|
||||
|
||||
trait :admin do
|
||||
role { 'admin' }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,7 @@
|
||||
FactoryBot.define do
|
||||
factory :wiki_page do
|
||||
title { "TestPage" }
|
||||
association :created_user, factory: :user
|
||||
association :updated_user, factory: :user
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,71 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Tag, type: :model do
|
||||
describe '.merge_tags!' do
|
||||
let!(:target_tag) { create(:tag) }
|
||||
let!(:source_tag) { create(:tag) }
|
||||
|
||||
let!(:post_record) { Post.create!(url: 'https://example.com/posts/1', title: 'test post') }
|
||||
|
||||
context 'when merging a simple source tag' do
|
||||
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
|
||||
|
||||
it 'moves the post_tag, deletes the source tag, and aliases the source tag_name' do
|
||||
described_class.merge_tags!(target_tag, [source_tag])
|
||||
|
||||
expect(source_post_tag.reload.tag_id).to eq(target_tag.id)
|
||||
expect(Tag.exists?(source_tag.id)).to be(false)
|
||||
expect(source_tag.tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the target already has the same post_tag' do
|
||||
let!(:target_post_tag) { PostTag.create!(post: post_record, tag: target_tag) }
|
||||
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
|
||||
|
||||
it 'discards the duplicate source post_tag and keeps one active target post_tag' do
|
||||
described_class.merge_tags!(target_tag, [source_tag])
|
||||
|
||||
active = PostTag.kept.where(post_id: post_record.id, tag_id: target_tag.id)
|
||||
discarded_source = PostTag.with_discarded.find(source_post_tag.id)
|
||||
|
||||
expect(active.count).to eq(1)
|
||||
expect(discarded_source.discarded_at).to be_present
|
||||
expect(Tag.exists?(source_tag.id)).to be(false)
|
||||
expect(source_tag.tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when source_tags includes the target itself' do
|
||||
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
|
||||
|
||||
it 'ignores the target tag in source_tags' do
|
||||
described_class.merge_tags!(target_tag, [source_tag, target_tag])
|
||||
|
||||
expect(Tag.exists?(target_tag.id)).to be(true)
|
||||
expect(Tag.exists?(source_tag.id)).to be(false)
|
||||
expect(source_post_tag.reload.tag_id).to eq(target_tag.id)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when aliasing the source tag_name is invalid' do
|
||||
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
|
||||
let!(:wiki_page) do
|
||||
WikiPage.create!(
|
||||
tag_name: source_tag.tag_name,
|
||||
created_user: create_admin_user!,
|
||||
updated_user: create_admin_user!)
|
||||
end
|
||||
|
||||
it 'rolls back the transaction' do
|
||||
expect {
|
||||
described_class.merge_tags!(target_tag, [source_tag])
|
||||
}.to raise_error(ActiveRecord::RecordInvalid)
|
||||
|
||||
expect(Tag.exists?(source_tag.id)).to be(true)
|
||||
expect(source_post_tag.reload.tag_id).to eq(source_tag.id)
|
||||
expect(source_tag.tag_name.reload.canonical_id).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,90 @@
|
||||
# This file is copied to spec/ when you run 'rails generate rspec:install'
|
||||
require 'spec_helper'
|
||||
ENV['RAILS_ENV'] ||= 'test'
|
||||
require_relative '../config/environment'
|
||||
# Prevent database truncation if the environment is production
|
||||
abort("The Rails environment is running in production mode!") if Rails.env.production?
|
||||
# Uncomment the line below in case you have `--require rails_helper` in the `.rspec` file
|
||||
# that will avoid rails generators crashing because migrations haven't been run yet
|
||||
# return unless Rails.env.test?
|
||||
require 'rspec/rails'
|
||||
Dir[Rails.root.join('spec/support/**/*.rb')].each do |f|
|
||||
require f
|
||||
end
|
||||
|
||||
# Add additional requires below this line. Rails is not loaded until this point!
|
||||
|
||||
# Requires supporting ruby files with custom matchers and macros, etc, in
|
||||
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
|
||||
# run as spec files by default. This means that files in spec/support that end
|
||||
# in _spec.rb will both be required and run as specs, causing the specs to be
|
||||
# run twice. It is recommended that you do not name files matching this glob to
|
||||
# end with _spec.rb. You can configure this pattern with the --pattern
|
||||
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
|
||||
#
|
||||
# The following line is provided for convenience purposes. It has the downside
|
||||
# of increasing the boot-up time by auto-requiring all files in the support
|
||||
# directory. Alternatively, in the individual `*_spec.rb` files, manually
|
||||
# require only the support files necessary.
|
||||
#
|
||||
# Rails.root.glob('spec/support/**/*.rb').sort_by(&:to_s).each { |f| require f }
|
||||
|
||||
# Ensures that the test database schema matches the current schema file.
|
||||
# If there are pending migrations it will invoke `db:test:prepare` to
|
||||
# recreate the test database by loading the schema.
|
||||
# If you are not using ActiveRecord, you can remove these lines.
|
||||
begin
|
||||
ActiveRecord::Migration.maintain_test_schema!
|
||||
rescue ActiveRecord::PendingMigrationError => e
|
||||
abort e.to_s.strip
|
||||
end
|
||||
RSpec.configure do |config|
|
||||
config.before do
|
||||
I18n.locale = :en
|
||||
end
|
||||
|
||||
config.include TestRecords
|
||||
config.include RakeTaskHelper
|
||||
|
||||
# FactoryBot の create / create_list を使へるやぅにする
|
||||
config.include FactoryBot::Syntax::Methods
|
||||
|
||||
# request spec で helper 使へるやぅにする
|
||||
config.include AuthHelper, type: :request
|
||||
config.include JsonHelper, type: :request
|
||||
|
||||
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
|
||||
config.fixture_paths = [
|
||||
Rails.root.join('spec/fixtures')
|
||||
]
|
||||
|
||||
# If you're not using ActiveRecord, or you'd prefer not to run each of your
|
||||
# examples within a transaction, remove the following line or assign false
|
||||
# instead of true.
|
||||
config.use_transactional_fixtures = true
|
||||
|
||||
# You can uncomment this line to turn off ActiveRecord support entirely.
|
||||
# config.use_active_record = false
|
||||
|
||||
# RSpec Rails uses metadata to mix in different behaviours to your tests,
|
||||
# for example enabling you to call `get` and `post` in request specs. e.g.:
|
||||
#
|
||||
# RSpec.describe UsersController, type: :request do
|
||||
# # ...
|
||||
# end
|
||||
#
|
||||
# The different available types are documented in the features, such as in
|
||||
# https://rspec.info/features/8-0/rspec-rails
|
||||
#
|
||||
# You can also this infer these behaviours automatically by location, e.g.
|
||||
# /spec/models would pull in the same behaviour as `type: :model` but this
|
||||
# behaviour is considered legacy and will be removed in a future version.
|
||||
#
|
||||
# To enable this behaviour uncomment the line below.
|
||||
# config.infer_spec_type_from_file_location!
|
||||
|
||||
# Filter lines from Rails gems in backtraces.
|
||||
config.filter_rails_from_backtrace!
|
||||
# arbitrary gems may also be filtered via:
|
||||
# config.filter_gems_from_backtrace("gem name")
|
||||
end
|
||||
@@ -0,0 +1,181 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Deerjikists API', type: :request do
|
||||
let(:platform) { 'nico' }
|
||||
let(:code) { 'deerjika-bot' }
|
||||
|
||||
let!(:tag1) { create(:tag, category: :deerjikist) }
|
||||
let!(:tag2) { create(:tag, category: :deerjikist) }
|
||||
|
||||
let(:member) { create(:user, :member) }
|
||||
let(:guest) { create(:user, role: :guest) }
|
||||
|
||||
describe 'GET /deerjikists/:platform/:code' do
|
||||
subject(:do_request) do
|
||||
get "/deerjikists/#{ platform }/#{ code }"
|
||||
end
|
||||
|
||||
context 'when deerjikist exists' do
|
||||
before do
|
||||
Deerjikist.create!(platform:, code:, tag: tag1)
|
||||
end
|
||||
|
||||
it 'returns 200 and deerjikist json' do
|
||||
do_request
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
expect(json).to be_a(Hash)
|
||||
expect(json['platform']).to eq(platform)
|
||||
expect(json['code']).to eq(code)
|
||||
|
||||
expect(json['tag']).to be_a(Hash)
|
||||
expect(json['tag']['id']).to eq(tag1.id)
|
||||
expect(json['tag']['name']).to eq(tag1.name)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when deerjikist does not exist' do
|
||||
it 'returns 404' do
|
||||
do_request
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when platform or code become blank after strip' do
|
||||
it 'returns 400' do
|
||||
get '/deerjikists/%20/%20'
|
||||
expect(response).to have_http_status(:bad_request)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PUT /deerjikists/:platform/:code' do
|
||||
subject(:do_request) do
|
||||
put "/deerjikists/#{ platform }/#{ code }", params: payload
|
||||
end
|
||||
|
||||
let(:payload) { { tag_id: tag1.id } }
|
||||
|
||||
context 'when not legged in' do
|
||||
it 'returns 401' do
|
||||
sign_out
|
||||
do_request
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when logged in but not member' do
|
||||
it 'returns 403' do
|
||||
sign_in_as guest
|
||||
do_request
|
||||
expect(response).to have_http_status(:forbidden)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when member' do
|
||||
before do
|
||||
sign_in_as member
|
||||
end
|
||||
|
||||
context 'when params invalid' do
|
||||
it 'returns 400 when tag_id is missing or invalid' do
|
||||
put "/deerjikists/#{ platform }/#{ code }", params: { tag_id: 0 }
|
||||
expect(response).to have_http_status(:bad_request)
|
||||
end
|
||||
|
||||
it 'returns 400 when platform or code blank after strip' do
|
||||
put '/deerjikists/%20/%20', params: { tag_id: tag1.id }
|
||||
expect(response).to have_http_status(:bad_request)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when creating new deerjikist' do
|
||||
it 'creates and returns 200 with json' do
|
||||
expect { do_request }.to change { Deerjikist.count }.by(1)
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
d = Deerjikist.find_by(platform:, code:)
|
||||
expect(d).to be_present
|
||||
expect(d.tag_id).to eq(tag1.id)
|
||||
|
||||
expect(json['platform']).to eq(platform)
|
||||
expect(json['code']).to eq(code)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when updating existing deerjikist' do
|
||||
before do
|
||||
Deerjikist.create!(platform:, code:, tag: tag1)
|
||||
end
|
||||
|
||||
let(:payload) { { tag_id: tag2.id } }
|
||||
|
||||
it 'updates tag_id and returns 200' do
|
||||
expect { do_request }.not_to change { Deerjikist.count }
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
d = Deerjikist.find_by(platform:, code:)
|
||||
expect(d.tag_id).to eq(tag2.id)
|
||||
|
||||
expect(json['platform']).to eq(platform)
|
||||
expect(json['code']).to eq(code)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'DELETE /deerjikists/:platform/:code' do
|
||||
subject(:do_request) do
|
||||
delete "/deerjikists/#{ platform }/#{ code }"
|
||||
end
|
||||
|
||||
context 'when not logged in' do
|
||||
it 'returns 401' do
|
||||
sign_out
|
||||
do_request
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when logged in but not member' do
|
||||
it 'returns 403' do
|
||||
sign_in_as guest
|
||||
do_request
|
||||
expect(response).to have_http_status(:forbidden)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when member' do
|
||||
before do
|
||||
sign_in_as member
|
||||
end
|
||||
|
||||
it 'returns 400 when platform/code blank after strip' do
|
||||
delete '/deerjikists/%20/%20'
|
||||
expect(response).to have_http_status(:bad_request)
|
||||
end
|
||||
|
||||
context 'when deerjikist exists' do
|
||||
before do
|
||||
Deerjikist.create!(platform: platform, code: code, tag: tag1)
|
||||
end
|
||||
|
||||
it 'destroys and returns 204' do
|
||||
expect {
|
||||
do_request
|
||||
}.to change { Deerjikist.exists?(platform: platform, code: code) }
|
||||
.from(true).to(false)
|
||||
|
||||
expect(response).to have_http_status(:no_content)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when deerjikist does not exist' do
|
||||
it 'returns 404' do
|
||||
do_request
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,38 @@
|
||||
require 'rails_helper'
|
||||
|
||||
|
||||
RSpec.describe 'NicoTags', type: :request do
|
||||
describe 'GET /tags/nico' do
|
||||
it 'returns tags and next_cursor when overflowing limit' do
|
||||
create_list(:tag, 21, :nico)
|
||||
get '/tags/nico', params: { limit: 20 }
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json['tags'].size).to eq(20)
|
||||
expect(json['next_cursor']).to be_present
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PATCH /tags/nico/:id' do
|
||||
let(:member) { create(:user, :member) }
|
||||
let(:nico_tag) { create(:tag, :nico) }
|
||||
|
||||
it '401 when not logged in' do
|
||||
sign_out
|
||||
patch "/tags/nico/#{nico_tag.id}", params: { tags: 'a b' }
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
|
||||
it '403 when not member' do
|
||||
sign_in_as(create(:user, role: 'guest'))
|
||||
patch "/tags/nico/#{nico_tag.id}", params: { tags: 'a b' }
|
||||
expect(response).to have_http_status(:forbidden)
|
||||
end
|
||||
|
||||
it '400 when target is not nico category' do
|
||||
sign_in_as(member)
|
||||
non_nico = create(:tag, :general)
|
||||
patch "/tags/nico/#{non_nico.id}", params: { tags: 'a b' }
|
||||
expect(response).to have_http_status(:bad_request)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,592 @@
|
||||
include ActiveSupport::Testing::TimeHelpers
|
||||
|
||||
require 'rails_helper'
|
||||
require 'set'
|
||||
|
||||
RSpec.describe 'Posts API', type: :request do
|
||||
# create / update で thumbnail.attach は走るが、
|
||||
# resized_thumbnail! が MiniMagick 依存でコケやすいので request spec ではスタブしとくのが無難。
|
||||
before do
|
||||
allow_any_instance_of(Post).to receive(:resized_thumbnail!).and_return(true)
|
||||
end
|
||||
|
||||
def dummy_upload
|
||||
# 中身は何でもいい(加工処理はスタブしてる)
|
||||
Rack::Test::UploadedFile.new(StringIO.new('dummy'), 'image/jpeg', original_filename: 'dummy.jpg')
|
||||
end
|
||||
|
||||
let!(:tag_name) { TagName.create!(name: 'spec_tag') }
|
||||
let!(:tag) { Tag.create!(tag_name: tag_name, category: 'general') }
|
||||
|
||||
let!(:post_record) do
|
||||
Post.create!(title: 'spec post', url: 'https://example.com/spec').tap do |p|
|
||||
PostTag.create!(post: p, tag: tag)
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /posts" do
|
||||
let!(:user) { create_member_user! }
|
||||
|
||||
let!(:tag_name) { TagName.create!(name: "spec_tag") }
|
||||
let!(:tag) { Tag.create!(tag_name:, category: "general") }
|
||||
let!(:tag_name2) { TagName.create!(name: 'unko') }
|
||||
let!(:tag2) { Tag.create!(tag_name: tag_name2, category: 'deerjikist') }
|
||||
let!(:alias_tag_name) { TagName.create!(name: 'manko', canonical: tag_name) }
|
||||
|
||||
let!(:hit_post) do
|
||||
Post.create!(uploaded_user: user, title: "hello spec world",
|
||||
url: 'https://example.com/spec2').tap do |p|
|
||||
PostTag.create!(post: p, tag:)
|
||||
end
|
||||
end
|
||||
|
||||
let!(:miss_post) do
|
||||
Post.create!(uploaded_user: user, title: "unrelated title",
|
||||
url: 'https://example.com/spec3').tap do |p|
|
||||
PostTag.create!(post: p, tag: tag2)
|
||||
end
|
||||
end
|
||||
|
||||
it "returns posts with tag name in JSON" do
|
||||
get "/posts"
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
posts = json.fetch("posts")
|
||||
|
||||
# 全postの全tagが name を含むこと
|
||||
expect(posts).not_to be_empty
|
||||
posts.each do |p|
|
||||
expect(p['tags']).to be_an(Array)
|
||||
p['tags'].each do |t|
|
||||
expect(t).to include('name', 'category', 'has_wiki')
|
||||
end
|
||||
end
|
||||
expect(json['count']).to be_an(Integer)
|
||||
|
||||
# spec_tag を含む投稿が存在すること
|
||||
all_tag_names = posts.flat_map { |p| p["tags"].map { |t| t["name"] } }
|
||||
expect(all_tag_names).to include("spec_tag")
|
||||
end
|
||||
|
||||
context "when q is provided" do
|
||||
it "filters posts by q (hit case)" do
|
||||
get "/posts", params: { tags: "spec_tag" }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
posts = json.fetch('posts')
|
||||
ids = posts.map { |p| p['id'] }
|
||||
|
||||
expect(ids).to include(hit_post.id)
|
||||
expect(ids).not_to include(miss_post.id)
|
||||
expect(json['count']).to be_an(Integer)
|
||||
|
||||
posts.each do |p|
|
||||
expect(p['tags']).to be_an(Array)
|
||||
p['tags'].each do |t|
|
||||
expect(t).to include('name', 'category', 'has_wiki')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "filters posts by q (hit case by alias)" do
|
||||
get "/posts", params: { tags: "manko" }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
posts = json.fetch('posts')
|
||||
ids = posts.map { |p| p['id'] }
|
||||
|
||||
expect(ids).to include(hit_post.id)
|
||||
expect(ids).not_to include(miss_post.id)
|
||||
expect(json['count']).to be_an(Integer)
|
||||
|
||||
posts.each do |p|
|
||||
expect(p['tags']).to be_an(Array)
|
||||
p['tags'].each do |t|
|
||||
expect(t).to include('name', 'category', 'has_wiki')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "returns empty posts when nothing matches" do
|
||||
get "/posts", params: { tags: "no_such_keyword_12345" }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json.fetch("posts")).to eq([])
|
||||
expect(json.fetch('count')).to eq(0)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when url is provided' do
|
||||
let!(:url_hit_post) do
|
||||
Post.create!(uploaded_user: user, title: 'url hit',
|
||||
url: 'https://example.com/needle-url-xyz').tap do |p|
|
||||
PostTag.create!(post: p, tag:)
|
||||
end
|
||||
end
|
||||
|
||||
let!(:url_miss_post) do
|
||||
Post.create!(uploaded_user: user, title: 'url miss',
|
||||
url: 'https://example.com/other-url').tap do |p|
|
||||
PostTag.create!(post: p, tag:)
|
||||
end
|
||||
end
|
||||
|
||||
it 'filters posts by url substring' do
|
||||
get '/posts', params: { url: 'needle-url-xyz' }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
ids = json.fetch('posts').map { |p| p['id'] }
|
||||
|
||||
expect(ids).to include(url_hit_post.id)
|
||||
expect(ids).not_to include(url_miss_post.id)
|
||||
expect(json.fetch('count')).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when title is provided' do
|
||||
let!(:title_hit_post) do
|
||||
Post.create!(uploaded_user: user, title: 'needle-title-xyz',
|
||||
url: 'https://example.com/title-hit').tap do |p|
|
||||
PostTag.create!(post: p, tag:)
|
||||
end
|
||||
end
|
||||
|
||||
let!(:title_miss_post) do
|
||||
Post.create!(uploaded_user: user, title: 'other title',
|
||||
url: 'https://example.com/title-miss').tap do |p|
|
||||
PostTag.create!(post: p, tag:)
|
||||
end
|
||||
end
|
||||
|
||||
it 'filters posts by title substring' do
|
||||
get '/posts', params: { title: 'needle-title-xyz' }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
ids = json.fetch('posts').map { |p| p['id'] }
|
||||
|
||||
expect(ids).to include(title_hit_post.id)
|
||||
expect(ids).not_to include(title_miss_post.id)
|
||||
expect(json.fetch('count')).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when created_from/created_to are provided' do
|
||||
let(:t_created_hit) { Time.zone.local(2010, 1, 5, 12, 0, 0) }
|
||||
let(:t_created_miss) { Time.zone.local(2012, 1, 5, 12, 0, 0) }
|
||||
|
||||
let!(:created_hit_post) do
|
||||
travel_to(t_created_hit) do
|
||||
Post.create!(uploaded_user: user, title: 'created hit',
|
||||
url: 'https://example.com/created-hit').tap do |p|
|
||||
PostTag.create!(post: p, tag:)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
let!(:created_miss_post) do
|
||||
travel_to(t_created_miss) do
|
||||
Post.create!(uploaded_user: user, title: 'created miss',
|
||||
url: 'https://example.com/created-miss').tap do |p|
|
||||
PostTag.create!(post: p, tag:)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it 'filters posts by created_at range' do
|
||||
get '/posts', params: {
|
||||
created_from: Time.zone.local(2010, 1, 1, 0, 0, 0).iso8601,
|
||||
created_to: Time.zone.local(2010, 12, 31, 23, 59, 59).iso8601
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
ids = json.fetch('posts').map { |p| p['id'] }
|
||||
|
||||
expect(ids).to include(created_hit_post.id)
|
||||
expect(ids).not_to include(created_miss_post.id)
|
||||
expect(json.fetch('count')).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when updated_from/updated_to are provided' do
|
||||
let(:t0) { Time.zone.local(2011, 2, 1, 12, 0, 0) }
|
||||
let(:t1) { Time.zone.local(2011, 2, 10, 12, 0, 0) }
|
||||
|
||||
let!(:updated_hit_post) do
|
||||
p = nil
|
||||
travel_to(t0) do
|
||||
p = Post.create!(uploaded_user: user, title: 'updated hit',
|
||||
url: 'https://example.com/updated-hit').tap do |pp|
|
||||
PostTag.create!(post: pp, tag:)
|
||||
end
|
||||
end
|
||||
travel_to(t1) do
|
||||
p.update!(title: 'updated hit v2')
|
||||
end
|
||||
p
|
||||
end
|
||||
|
||||
let!(:updated_miss_post) do
|
||||
travel_to(Time.zone.local(2013, 1, 1, 12, 0, 0)) do
|
||||
Post.create!(uploaded_user: user, title: 'updated miss',
|
||||
url: 'https://example.com/updated-miss').tap do |p|
|
||||
PostTag.create!(post: p, tag:)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it 'filters posts by updated_at range' do
|
||||
get '/posts', params: {
|
||||
updated_from: Time.zone.local(2011, 2, 5, 0, 0, 0).iso8601,
|
||||
updated_to: Time.zone.local(2011, 2, 20, 23, 59, 59).iso8601
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
ids = json.fetch('posts').map { |p| p['id'] }
|
||||
|
||||
expect(ids).to include(updated_hit_post.id)
|
||||
expect(ids).not_to include(updated_miss_post.id)
|
||||
expect(json.fetch('count')).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when original_created_from/original_created_to are provided' do
|
||||
# 注意: controller の現状ロジックに合わせてる
|
||||
# original_created_from は `original_created_before > ?`
|
||||
# original_created_to は `original_created_from <= ?`
|
||||
|
||||
let!(:oc_hit_post) do
|
||||
Post.create!(uploaded_user: user, title: 'oc hit',
|
||||
url: 'https://example.com/oc-hit',
|
||||
original_created_from: Time.zone.local(2015, 1, 1, 0, 0, 0),
|
||||
original_created_before: Time.zone.local(2015, 1, 10, 0, 0, 0)).tap do |p|
|
||||
PostTag.create!(post: p, tag:)
|
||||
end
|
||||
end
|
||||
|
||||
# original_created_from の条件は「original_created_before > param」なので、
|
||||
# before が param 以下になるようにする(ただし before >= from は守る)
|
||||
let!(:oc_miss_post_for_from) do
|
||||
Post.create!(
|
||||
uploaded_user: user,
|
||||
title: 'oc miss for from',
|
||||
url: 'https://example.com/oc-miss-from',
|
||||
original_created_from: Time.zone.local(2014, 12, 1, 0, 0, 0),
|
||||
original_created_before: Time.zone.local(2015, 1, 1, 0, 0, 0)
|
||||
).tap { |p| PostTag.create!(post: p, tag:) }
|
||||
end
|
||||
|
||||
# original_created_to の条件は「original_created_from <= param」なので、
|
||||
# from が param より後になるようにする(before >= from は守る)
|
||||
let!(:oc_miss_post_for_to) do
|
||||
Post.create!(
|
||||
uploaded_user: user,
|
||||
title: 'oc miss for to',
|
||||
url: 'https://example.com/oc-miss-to',
|
||||
original_created_from: Time.zone.local(2015, 2, 1, 0, 0, 0),
|
||||
original_created_before: Time.zone.local(2015, 2, 10, 0, 0, 0)
|
||||
).tap { |p| PostTag.create!(post: p, tag:) }
|
||||
end
|
||||
|
||||
it 'filters posts by original_created_from (current controller behavior)' do
|
||||
get '/posts', params: {
|
||||
original_created_from: Time.zone.local(2015, 1, 5, 0, 0, 0).iso8601
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
ids = json.fetch('posts').map { |p| p['id'] }
|
||||
|
||||
expect(ids).to include(oc_hit_post.id)
|
||||
expect(ids).not_to include(oc_miss_post_for_from.id)
|
||||
expect(json.fetch('count')).to eq(2)
|
||||
end
|
||||
|
||||
it 'filters posts by original_created_to (current controller behavior)' do
|
||||
get '/posts', params: {
|
||||
original_created_to: Time.zone.local(2015, 1, 15, 0, 0, 0).iso8601
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
ids = json.fetch('posts').map { |p| p['id'] }
|
||||
|
||||
expect(ids).to include(oc_hit_post.id)
|
||||
expect(ids).not_to include(oc_miss_post_for_to.id)
|
||||
expect(json.fetch('count')).to eq(2)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /posts/:id' do
|
||||
subject(:request) { get "/posts/#{post_id}" }
|
||||
|
||||
context 'when post exists' do
|
||||
let(:post_id) { post_record.id }
|
||||
|
||||
it 'returns post with tag tree + related + viewed' do
|
||||
request
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
expect(json).to include('id' => post_record.id)
|
||||
expect(json).to have_key('tags')
|
||||
expect(json['tags']).to be_an(Array)
|
||||
|
||||
# show は build_tag_tree_for を使うので、tags はツリー形式(children 付き)
|
||||
node = json['tags'][0]
|
||||
expect(node).to include('id', 'name', 'category', 'post_count', 'children', 'has_wiki')
|
||||
expect(node['name']).to eq('spec_tag')
|
||||
|
||||
expect(json).to have_key('related')
|
||||
expect(json['related']).to be_an(Array)
|
||||
|
||||
expect(json).to have_key('viewed')
|
||||
expect([true, false]).to include(json['viewed'])
|
||||
end
|
||||
end
|
||||
|
||||
context 'when post does not exist' do
|
||||
let(:post_id) { 999_999_999 }
|
||||
|
||||
it 'returns 404' do
|
||||
request
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST /posts' do
|
||||
let(:member) { create(:user, :member) }
|
||||
let!(:alias_tag_name) { TagName.create!(name: 'manko', canonical: tag_name) }
|
||||
|
||||
it '401 when not logged in' do
|
||||
sign_out
|
||||
post '/posts', params: { title: 't', url: 'https://example.com/x', tags: 'a', thumbnail: dummy_upload }
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
|
||||
it '403 when not member' do
|
||||
sign_in_as(create(:user, role: 'guest'))
|
||||
post '/posts', params: { title: 't', url: 'https://example.com/x', tags: 'a', thumbnail: dummy_upload }
|
||||
expect(response).to have_http_status(:forbidden)
|
||||
end
|
||||
|
||||
it '201 and creates post + tags when member' do
|
||||
sign_in_as(member)
|
||||
|
||||
post '/posts', params: {
|
||||
title: 'new post',
|
||||
url: 'https://example.com/new',
|
||||
tags: 'spec_tag', # 既存タグ名を投げる
|
||||
thumbnail: dummy_upload
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:created)
|
||||
expect(json).to include('id', 'title', 'url')
|
||||
|
||||
# tags が name を含むこと(API 側の serialization が正しいこと)
|
||||
expect(json).to have_key('tags')
|
||||
expect(json['tags']).to be_an(Array)
|
||||
expect(json['tags'][0]).to have_key('name')
|
||||
end
|
||||
|
||||
it '201 and creates post + tags when member and tags have aliases' do
|
||||
sign_in_as(member)
|
||||
|
||||
post '/posts', params: {
|
||||
title: 'new post',
|
||||
url: 'https://example.com/new',
|
||||
tags: 'manko', # 既存タグ名を投げる
|
||||
thumbnail: dummy_upload
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:created)
|
||||
expect(json).to include('id', 'title', 'url')
|
||||
|
||||
# tags が name を含むこと(API 側の serialization が正しいこと)
|
||||
names = json.fetch('tags').map { |t| t['name'] }
|
||||
expect(names).to include('spec_tag')
|
||||
expect(names).not_to include('manko')
|
||||
end
|
||||
|
||||
context "when nico tag already exists in tags" do
|
||||
before do
|
||||
Tag.find_or_create_by!(tag_name: TagName.find_or_create_by!(name: 'nico:nico_tag'),
|
||||
category: 'nico')
|
||||
end
|
||||
|
||||
it 'return 400' do
|
||||
sign_in_as(member)
|
||||
|
||||
post '/posts', params: {
|
||||
title: 'new post',
|
||||
url: 'https://example.com/nico_tag',
|
||||
tags: 'nico:nico_tag',
|
||||
thumbnail: dummy_upload }
|
||||
|
||||
expect(response).to have_http_status(:bad_request)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when url is blank' do
|
||||
it 'returns 422' do
|
||||
sign_in_as(member)
|
||||
|
||||
post '/posts', params: {
|
||||
title: 'new post',
|
||||
url: ' ',
|
||||
tags: 'spec_tag', # 既存タグ名を投げる
|
||||
thumbnail: dummy_upload }
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when url is invalid' do
|
||||
it 'returns 422' do
|
||||
sign_in_as(member)
|
||||
|
||||
post '/posts', params: {
|
||||
title: 'new post',
|
||||
url: 'ぼざクリタグ広場',
|
||||
tags: 'spec_tag', # 既存タグ名を投げる
|
||||
thumbnail: dummy_upload
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PUT /posts/:id' do
|
||||
let(:member) { create(:user, :member) }
|
||||
|
||||
it '401 when not logged in' do
|
||||
sign_out
|
||||
put "/posts/#{post_record.id}", params: { title: 'updated', tags: 'spec_tag' }
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
|
||||
it '403 when not member' do
|
||||
sign_in_as(create(:user, role: 'guest'))
|
||||
put "/posts/#{post_record.id}", params: { title: 'updated', tags: 'spec_tag' }
|
||||
expect(response).to have_http_status(:forbidden)
|
||||
end
|
||||
|
||||
it '200 and updates title + resync tags when member' do
|
||||
sign_in_as(member)
|
||||
|
||||
# 追加で別タグも作って、更新時に入れ替わることを見る
|
||||
tn2 = TagName.create!(name: 'spec_tag_2')
|
||||
Tag.create!(tag_name: tn2, category: 'general')
|
||||
|
||||
put "/posts/#{post_record.id}", params: {
|
||||
title: 'updated title',
|
||||
tags: 'spec_tag_2'
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json).to have_key('tags')
|
||||
expect(json['tags']).to be_an(Array)
|
||||
|
||||
# show と同様、update 後レスポンスもツリー形式
|
||||
names = json['tags'].map { |n| n['name'] }
|
||||
expect(names).to include('spec_tag_2')
|
||||
end
|
||||
|
||||
context "when nico tag already exists in tags" do
|
||||
before do
|
||||
Tag.find_or_create_by!(tag_name: TagName.find_or_create_by!(name: 'nico:nico_tag'),
|
||||
category: 'nico')
|
||||
end
|
||||
|
||||
it 'return 400' do
|
||||
sign_in_as(member)
|
||||
|
||||
put "/posts/#{ post_record.id }", params: {
|
||||
title: 'updated title',
|
||||
tags: 'nico:nico_tag' }
|
||||
|
||||
expect(response).to have_http_status(:bad_request)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /posts/random' do
|
||||
it '404 when no posts' do
|
||||
PostTag.delete_all
|
||||
Post.delete_all
|
||||
get '/posts/random'
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
|
||||
it '200 and returns viewed boolean' do
|
||||
get '/posts/random'
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json).to have_key('viewed')
|
||||
expect([true, false]).to include(json['viewed'])
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /posts/changes' do
|
||||
let(:member) { create(:user, :member) }
|
||||
|
||||
it 'returns add/remove events (history) for a post' do
|
||||
# add
|
||||
tn2 = TagName.create!(name: 'spec_tag2')
|
||||
tag2 = Tag.create!(tag_name: tn2, category: 'general')
|
||||
pt = PostTag.create!(post: post_record, tag: tag2, created_user: member)
|
||||
|
||||
# remove (discard)
|
||||
pt.discard_by!(member)
|
||||
|
||||
get '/posts/changes', params: { id: post_record.id }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json).to include('changes', 'count')
|
||||
expect(json['changes']).to be_an(Array)
|
||||
expect(json['count']).to be >= 2
|
||||
|
||||
types = json['changes'].map { |e| e['change_type'] }.uniq
|
||||
expect(types).to include('add')
|
||||
expect(types).to include('remove')
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST /posts/:id/viewed' do
|
||||
let(:user) { create(:user) }
|
||||
|
||||
it '401 when not logged in' do
|
||||
sign_out
|
||||
post "/posts/#{ post_record.id }/viewed"
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
|
||||
it '204 and marks viewed when logged in' do
|
||||
sign_in_as(user)
|
||||
post "/posts/#{ post_record.id }/viewed"
|
||||
expect(response).to have_http_status(:no_content)
|
||||
|
||||
expect(user.reload.viewed?(post_record)).to be(true)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'DELETE /posts/:id/viewed' do
|
||||
let(:user) { create(:user) }
|
||||
|
||||
it '401 when not logged in' do
|
||||
sign_out
|
||||
delete "/posts/#{ post_record.id }/viewed"
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
|
||||
it '204 and unmarks viewed when logged in' do
|
||||
sign_in_as(user)
|
||||
|
||||
# 先に viewed 付けてから外す
|
||||
user.viewed_posts << post_record
|
||||
|
||||
delete "/posts/#{ post_record.id }/viewed"
|
||||
expect(response).to have_http_status(:no_content)
|
||||
|
||||
expect(user.reload.viewed?(post_record)).to be(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,28 @@
|
||||
require "rails_helper"
|
||||
|
||||
|
||||
RSpec.describe "Preview", type: :request do
|
||||
describe "GET /preview/title" do
|
||||
it "401 unless logged in" do
|
||||
sign_out
|
||||
get "/preview/title", params: { url: "example.com" }
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
|
||||
it "400 when url blank" do
|
||||
sign_in_as(create(:user))
|
||||
get "/preview/title", params: { url: "" }
|
||||
expect(response).to have_http_status(:bad_request)
|
||||
end
|
||||
|
||||
it "returns parsed title (stubbing URI.open)" do
|
||||
sign_in_as(create(:user))
|
||||
fake_html = "<html><head><title> Hello </title></head></html>"
|
||||
allow(URI).to receive(:open).and_return(StringIO.new(fake_html))
|
||||
|
||||
get "/preview/title", params: { url: "example.com" }
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json["title"]).to eq("Hello")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,133 @@
|
||||
require "rails_helper"
|
||||
|
||||
RSpec.describe "TagChildren", type: :request do
|
||||
let!(:parent) { create(:tag) }
|
||||
let!(:child) { create(:tag) }
|
||||
|
||||
# ここは君のUser factoryに合わせて調整
|
||||
let(:user) { create_member_user! }
|
||||
let(:admin) { create_admin_user! }
|
||||
|
||||
# current_user を ApplicationController でスタブ
|
||||
def stub_current_user(user_or_nil)
|
||||
allow_any_instance_of(ApplicationController)
|
||||
.to receive(:current_user)
|
||||
.and_return(user_or_nil)
|
||||
end
|
||||
|
||||
describe "POST /tag_children" do
|
||||
subject(:do_request) do
|
||||
post "/tags/#{ parent_id }/children/#{ child_id }"
|
||||
end
|
||||
|
||||
context "when not logged in" do
|
||||
let(:parent_id) { parent.id }
|
||||
let(:child_id) { child.id }
|
||||
|
||||
it "returns 401" do
|
||||
stub_current_user(nil)
|
||||
do_request
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context "when logged in but not admin" do
|
||||
let(:parent_id) { parent.id }
|
||||
let(:child_id) { child.id }
|
||||
|
||||
it "returns 403" do
|
||||
stub_current_user(user)
|
||||
do_request
|
||||
expect(response).to have_http_status(:forbidden)
|
||||
end
|
||||
end
|
||||
|
||||
context "when admin and params are present" do
|
||||
before { stub_current_user(admin) }
|
||||
let(:parent_id) { parent.id }
|
||||
let(:child_id) { child.id }
|
||||
|
||||
it "returns 204 and adds child to parent.children" do
|
||||
expect(parent.children).not_to include(child)
|
||||
|
||||
expect { do_request }
|
||||
.to change { parent.reload.children.ids.include?(child.id) }
|
||||
.from(false).to(true)
|
||||
|
||||
expect(response).to have_http_status(:no_content)
|
||||
end
|
||||
end
|
||||
|
||||
context "when Tag.find raises (invalid ids) it still returns 204" do
|
||||
before { stub_current_user(admin) }
|
||||
|
||||
let(:parent_id) { -1 }
|
||||
let(:child_id) { -1 }
|
||||
|
||||
it "returns 204 (rescue nil)" do
|
||||
do_request
|
||||
expect(response).to have_http_status(:no_content)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "DELETE /tag_children" do
|
||||
subject(:do_request) do
|
||||
delete "/tags/#{ parent_id }/children/#{ child_id }"
|
||||
end
|
||||
|
||||
context "when not logged in" do
|
||||
let(:parent_id) { parent.id }
|
||||
let(:child_id) { child.id }
|
||||
|
||||
it "returns 401" do
|
||||
stub_current_user(nil)
|
||||
do_request
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context "when logged in but not admin" do
|
||||
let(:parent_id) { parent.id }
|
||||
let(:child_id) { child.id }
|
||||
|
||||
it "returns 403" do
|
||||
stub_current_user(user)
|
||||
do_request
|
||||
expect(response).to have_http_status(:forbidden)
|
||||
end
|
||||
end
|
||||
|
||||
context "when admin and params are present" do
|
||||
before do
|
||||
stub_current_user(admin)
|
||||
parent.children << child
|
||||
end
|
||||
|
||||
let(:parent_id) { parent.id }
|
||||
let(:child_id) { child.id }
|
||||
|
||||
it "returns 204 and removes child from parent.children" do
|
||||
expect(parent.reload.children).to include(child)
|
||||
|
||||
expect { do_request }
|
||||
.to change { parent.reload.children.ids.include?(child.id) }
|
||||
.from(true).to(false)
|
||||
|
||||
expect(response).to have_http_status(:no_content)
|
||||
end
|
||||
end
|
||||
|
||||
context "when Tag.find raises (invalid ids) it still returns 204" do
|
||||
before { stub_current_user(admin) }
|
||||
|
||||
let(:parent_id) { -1 }
|
||||
let(:child_id) { -1 }
|
||||
|
||||
it "returns 204 (rescue nil)" do
|
||||
do_request
|
||||
expect(response).to have_http_status(:no_content)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,102 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Tags deerjikists API', type: :request do
|
||||
let(:platform1) { 'nico' }
|
||||
let(:code1) { 'deerjika-bot' }
|
||||
let(:platform2) { 'youtube' }
|
||||
let(:code2) { 'deerjika-bot.bsky.social' }
|
||||
|
||||
let!(:tag) { create(:tag, category: :deerjikist) }
|
||||
|
||||
before do
|
||||
# show_by_name / deerjikists_by_name 用に名前を固定
|
||||
tag.tag_name.update!(name: 'deerjika')
|
||||
end
|
||||
|
||||
describe 'GET /tags/:id/deerjikists' do
|
||||
subject(:do_request) do
|
||||
get "/tags/#{ tag_id }/deerjikists"
|
||||
end
|
||||
|
||||
let(:tag_id) { tag.id }
|
||||
|
||||
context 'when tag exists and has no deerjikists' do
|
||||
it 'returns 200 and empty array' do
|
||||
do_request
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json).to eq([])
|
||||
end
|
||||
end
|
||||
|
||||
context 'when tag exists and has deerjikists' do
|
||||
before do
|
||||
Deerjikist.create!(platform: platform1, code: code1, tag: tag)
|
||||
Deerjikist.create!(platform: platform2, code: code2, tag: tag)
|
||||
end
|
||||
|
||||
it 'returns 200 and deerjikists array' do
|
||||
do_request
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
expect(json).to be_a(Array)
|
||||
expect(json.size).to eq(2)
|
||||
|
||||
expect(json.map { |h| [h['platform'], h['code']] }).to contain_exactly(
|
||||
[platform1, code1],
|
||||
[platform2, code2],
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when tag does not exist' do
|
||||
let(:tag_id) { 9_999_999 }
|
||||
|
||||
it 'returns 404' do
|
||||
do_request
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /tags/name/:name/deerjikists' do
|
||||
subject(:do_request) do
|
||||
get "/tags/name/#{ name }/deerjikists"
|
||||
end
|
||||
|
||||
let(:name) { 'deerjika' }
|
||||
|
||||
context 'when name is blank after strip' do
|
||||
let(:name) { '%20' }
|
||||
|
||||
it 'returns 400' do
|
||||
do_request
|
||||
expect(response).to have_http_status(:bad_request)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when tag does not exist for name' do
|
||||
let(:name) { 'no-such-tag' }
|
||||
|
||||
it 'returns 404' do
|
||||
do_request
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when tag exists and has deerjikists' do
|
||||
before do
|
||||
Deerjikist.create!(platform: platform1, code: code1, tag: tag)
|
||||
end
|
||||
|
||||
it 'returns 200 and deerjikists array' do
|
||||
do_request
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
expect(json).to be_a(Array)
|
||||
expect(json.size).to eq(1)
|
||||
expect(json[0]['platform']).to eq(platform1)
|
||||
expect(json[0]['code']).to eq(code1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,198 @@
|
||||
require 'cgi'
|
||||
require 'rails_helper'
|
||||
|
||||
|
||||
RSpec.describe 'Tags API', type: :request do
|
||||
let!(:tn) { TagName.create!(name: 'spec_tag') }
|
||||
let!(:tag) { Tag.create!(tag_name: tn, category: :general) }
|
||||
let!(:alias_tn) { TagName.create!(name: 'unko', canonical: tn) }
|
||||
let!(:post) { Post.create!(url: 'https://example.com/unkounkounko') }
|
||||
let!(:post_tag) { PostTag.create!(post:, tag:) }
|
||||
let!(:tn2) { TagName.create!(name: 'unknown') }
|
||||
let!(:tag2) { Tag.create!(tag_name: tn2, category: :general) }
|
||||
|
||||
describe 'GET /tags' do
|
||||
it 'returns tags with name' do
|
||||
get '/tags'
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
expect(json).to be_an(Array)
|
||||
expect(json).not_to be_empty
|
||||
expect(json[0]).to have_key('name')
|
||||
expect(json.map { |t| t['name'] }).to include('spec_tag')
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /tags/:id' do
|
||||
subject(:request) do
|
||||
get "/tags/#{ tag_id }"
|
||||
end
|
||||
|
||||
let(:tag_id) { tag.id }
|
||||
|
||||
context 'when tag exists' do
|
||||
it 'returns tag with name' do
|
||||
request
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
expect(json).to include(
|
||||
'id' => tag.id,
|
||||
'name' => 'spec_tag',
|
||||
'category' => 'general')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when tag does not exist' do
|
||||
let(:tag_id) { 9_999_999 }
|
||||
|
||||
it 'returns 404' do
|
||||
request
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /tags/autocomplete' do
|
||||
it 'returns matching tags by q' do
|
||||
get '/tags/autocomplete', params: { q: 'spec' }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
expect(json).to be_an(Array)
|
||||
expect(json.map { |t| t['name'] }).to include('spec_tag')
|
||||
t = json.find { |t| t['name'] == 'spec_tag' }
|
||||
expect(t).to have_key('matched_alias')
|
||||
expect(t['matched_alias']).to be(nil)
|
||||
end
|
||||
|
||||
it 'returns matching canonical tags by q with aliases' do
|
||||
get '/tags/autocomplete', params: { q: 'unk' }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
expect(json).to be_an(Array)
|
||||
expect(json.map { |t| t['name'] }).to include('spec_tag')
|
||||
t = json.find { |t| t['name'] == 'spec_tag' }
|
||||
expect(t['matched_alias']).to eq('unko')
|
||||
expect(json.map { |t| t['name'] }).not_to include('unknown')
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /tags/name/:name' do
|
||||
it 'returns tag by name' do
|
||||
get "/tags/name/#{ CGI.escape('spec_tag') }"
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
expect(json).to have_key('id')
|
||||
expect(json).to have_key('name')
|
||||
expect(json['id']).to eq(tag.id)
|
||||
expect(json['name']).to eq('spec_tag')
|
||||
end
|
||||
|
||||
it 'returns 404 when not found' do
|
||||
get "/tags/name/#{ CGI.escape('nope') }"
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
end
|
||||
|
||||
# member? を持つ user を想定(Factory 側で trait 作ってもOK)
|
||||
let(:member_user) { create(:user) }
|
||||
let(:non_member_user) { create(:user) }
|
||||
|
||||
def stub_current_user(user)
|
||||
allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user)
|
||||
end
|
||||
|
||||
before do
|
||||
allow(member_user).to receive(:gte_member?).and_return(true)
|
||||
allow(non_member_user).to receive(:gte_member?).and_return(false)
|
||||
end
|
||||
|
||||
describe "PATCH /tags/:id" do
|
||||
context "未ログイン" do
|
||||
before { stub_current_user(nil) }
|
||||
|
||||
it "401 を返す" do
|
||||
patch "/tags/#{tag.id}", params: { name: "new" }
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context "ログインしてゐるが member でない" do
|
||||
before { stub_current_user(non_member_user) }
|
||||
|
||||
it "403 を返す" do
|
||||
patch "/tags/#{tag.id}", params: { name: "new" }
|
||||
expect(response).to have_http_status(:forbidden)
|
||||
end
|
||||
end
|
||||
|
||||
context "member" do
|
||||
before { stub_current_user(member_user) }
|
||||
|
||||
it "name だけ更新できる" do
|
||||
patch "/tags/#{tag.id}", params: { name: "new" }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
tag.reload
|
||||
expect(tag.name).to eq("new")
|
||||
expect(tag.category).to eq("general")
|
||||
|
||||
json = JSON.parse(response.body)
|
||||
expect(json["id"]).to eq(tag.id)
|
||||
expect(json["name"]).to eq("new")
|
||||
expect(json["category"]).to eq("general")
|
||||
end
|
||||
|
||||
it "category だけ更新できる" do
|
||||
patch "/tags/#{tag.id}", params: { category: "meme" }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
tag.reload
|
||||
expect(tag.name).to eq("spec_tag")
|
||||
expect(tag.category).to eq("meme")
|
||||
end
|
||||
|
||||
it "空文字は presence により無視され、更新は走らない(値が変わらない)" do
|
||||
patch "/tags/#{tag.id}", params: { name: "", category: " " }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
tag.reload
|
||||
expect(tag.name).to eq("spec_tag")
|
||||
expect(tag.category).to eq("general")
|
||||
end
|
||||
|
||||
it "両方更新できる" do
|
||||
patch "/tags/#{tag.id}", params: { name: "n", category: "meta" }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
tag.reload
|
||||
expect(tag.name).to eq("n")
|
||||
expect(tag.category).to eq("meta")
|
||||
end
|
||||
|
||||
it "存在しない id だと RecordNotFound になる(通常は 404)" do
|
||||
# Rails 設定次第で例外がそのまま上がる/404になる
|
||||
# APIなら rescue_from で 404 にしてることが多いので、その場合は 404 を期待。
|
||||
patch "/tags/999999999", params: { name: "x" }
|
||||
|
||||
expect(response.status).to be_in([404, 500])
|
||||
end
|
||||
|
||||
it "バリデーションで update! が失敗したら(通常は 422 か 500)" do
|
||||
patch "/tags/#{tag.id}", params: { name: 'new', category: 'nico' }
|
||||
|
||||
# rescue_from の実装次第で変はる:
|
||||
# - RecordInvalid を 422 にしてるなら 422
|
||||
# - 未処理なら 500
|
||||
expect(response.status).to be_in([422, 500])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,110 @@
|
||||
require "rails_helper"
|
||||
|
||||
|
||||
RSpec.describe "Users", type: :request do
|
||||
describe "POST /users" do
|
||||
it "creates guest user and returns code" do
|
||||
post "/users"
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json["code"]).to be_present
|
||||
expect(json["user"]["role"]).to eq("guest")
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST /users/code/renew" do
|
||||
it "returns 401 when not logged in" do
|
||||
sign_out
|
||||
post "/users/code/renew"
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
describe "PUT /users/:id" do
|
||||
let(:user) { create(:user, name: "old-name", role: "guest") }
|
||||
|
||||
it "returns 401 when current_user id mismatch" do
|
||||
sign_in_as(create(:user))
|
||||
put "/users/#{user.id}", params: { name: "new-name" }
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
|
||||
it "returns 400 when name is blank" do
|
||||
sign_in_as(user)
|
||||
put "/users/#{user.id}", params: { name: " " }
|
||||
expect(response).to have_http_status(:bad_request)
|
||||
end
|
||||
|
||||
it "updates name and returns 201 with user slice" do
|
||||
sign_in_as(user)
|
||||
put "/users/#{user.id}", params: { name: "new-name" }
|
||||
|
||||
expect(response).to have_http_status(:created)
|
||||
expect(json["id"]).to eq(user.id)
|
||||
expect(json["name"]).to eq("new-name")
|
||||
|
||||
user.reload
|
||||
expect(user.name).to eq("new-name")
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST /users/verify" do
|
||||
it "returns valid:false when code not found" do
|
||||
post "/users/verify", params: { code: "nope" }
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json["valid"]).to eq(false)
|
||||
end
|
||||
|
||||
it "creates IpAddress and UserIp, and returns valid:true with user slice" do
|
||||
user = create(:user, inheritance_code: SecureRandom.uuid, role: "guest")
|
||||
|
||||
# request.remote_ip を固定
|
||||
allow_any_instance_of(ActionDispatch::Request).to receive(:remote_ip).and_return("203.0.113.10")
|
||||
|
||||
expect {
|
||||
post "/users/verify", params: { code: user.inheritance_code }
|
||||
}.to change(UserIp, :count).by(1)
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json["valid"]).to eq(true)
|
||||
expect(json["user"]["id"]).to eq(user.id)
|
||||
expect(json["user"]["inheritance_code"]).to eq(user.inheritance_code)
|
||||
expect(json["user"]["role"]).to eq("guest")
|
||||
|
||||
# ついでに IpAddress もできてることを確認(ipの保存形式がバイナリでも count で見れる)
|
||||
expect(IpAddress.count).to be >= 1
|
||||
end
|
||||
|
||||
it "is idempotent for same user+ip (does not create duplicate UserIp)" do
|
||||
user = create(:user, inheritance_code: SecureRandom.uuid, role: "guest")
|
||||
allow_any_instance_of(ActionDispatch::Request).to receive(:remote_ip).and_return("203.0.113.10")
|
||||
|
||||
post "/users/verify", params: { code: user.inheritance_code }
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
expect {
|
||||
post "/users/verify", params: { code: user.inheritance_code }
|
||||
}.not_to change(UserIp, :count)
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json["valid"]).to eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /users/me" do
|
||||
it "returns 404 when code not found" do
|
||||
get "/users/me", params: { code: "nope" }
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
|
||||
it "returns user slice when found" do
|
||||
user = create(:user, inheritance_code: SecureRandom.uuid, name: "me", role: "guest")
|
||||
get "/users/me", params: { code: user.inheritance_code }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json["id"]).to eq(user.id)
|
||||
expect(json["name"]).to eq("me")
|
||||
expect(json["inheritance_code"]).to eq(user.inheritance_code)
|
||||
expect(json["role"]).to eq("guest")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,426 @@
|
||||
require 'cgi'
|
||||
require 'rails_helper'
|
||||
require 'securerandom'
|
||||
|
||||
|
||||
RSpec.describe 'Wiki API', type: :request do
|
||||
let!(:user) { create_member_user! }
|
||||
|
||||
let!(:tn) { TagName.create!(name: 'spec_wiki_title') }
|
||||
let!(:page) do
|
||||
WikiPage.create!(tag_name: tn, created_user: user, updated_user: user).tap do |p|
|
||||
Wiki::Commit.content!(page: p, body: 'init', created_user: user, message: 'init')
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /wiki' do
|
||||
it 'returns wiki pages with title' do
|
||||
get '/wiki'
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
expect(json).to be_an(Array)
|
||||
expect(json).not_to be_empty
|
||||
|
||||
expect(json[0]).to have_key('title')
|
||||
expect(json.map { |p| p['title'] }).to include('spec_wiki_title')
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /wiki/:id' do
|
||||
subject(:request) do
|
||||
get "/wiki/#{ page_id }"
|
||||
end
|
||||
|
||||
let(:page_id) { page.id }
|
||||
|
||||
context 'when wiki page exists' do
|
||||
it 'returns wiki page with title' do
|
||||
request
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
expect(json).to include(
|
||||
'id' => page.id,
|
||||
'title' => 'spec_wiki_title')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when wiki page does not exist' do
|
||||
let(:page_id) { 9_999_999 }
|
||||
|
||||
it 'returns 404' do
|
||||
request
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST /wiki' do
|
||||
let(:endpoint) { '/wiki' }
|
||||
|
||||
let(:member) { create(:user, role: 'member') }
|
||||
let(:guest) { create(:user, role: 'guest') }
|
||||
|
||||
def auth_headers(user)
|
||||
{ 'X-Transfer-Code' => user.inheritance_code }
|
||||
end
|
||||
|
||||
context 'when not logged in' do
|
||||
it 'returns 401' do
|
||||
post endpoint, params: { title: 'Test', body: 'Hello' }
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when logged in but not member' do
|
||||
it 'returns 403' do
|
||||
post endpoint, params: { title: 'Test', body: 'Hello' }, headers: auth_headers(guest)
|
||||
expect(response).to have_http_status(:forbidden)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when params invalid' do
|
||||
it 'returns 422 when title blank' do
|
||||
post endpoint, params: { title: '', body: 'Hello' }, headers: auth_headers(member)
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
end
|
||||
|
||||
it 'returns 422 when body blank' do
|
||||
post endpoint, params: { title: 'Test', body: '' }, headers: auth_headers(member)
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when success' do
|
||||
it 'creates wiki_page and first content revision' do
|
||||
expect do
|
||||
post endpoint, params: { title: 'TestPage', body: "a\nb\nc", message: 'init' },
|
||||
headers: auth_headers(member)
|
||||
end
|
||||
.to change(WikiPage, :count).by(1)
|
||||
.and change(WikiRevision, :count).by(1)
|
||||
|
||||
expect(response).to have_http_status(:created)
|
||||
|
||||
page_id = json.fetch('id')
|
||||
expect(json.fetch('title')).to eq('TestPage')
|
||||
|
||||
page = WikiPage.find(page_id)
|
||||
rev = page.current_revision
|
||||
expect(rev).to be_present
|
||||
expect(rev).to be_content
|
||||
expect(rev.message).to eq('init')
|
||||
|
||||
# body が復元できること
|
||||
expect(page.body).to eq("a\nb\nc")
|
||||
|
||||
# 行数とリレーションの整合
|
||||
expect(rev.lines_count).to eq(3)
|
||||
expect(rev.wiki_revision_lines.order(:position).pluck(:position)).to eq([0, 1, 2])
|
||||
expect(rev.wiki_lines.pluck(:body)).to match_array(%w[a b c])
|
||||
end
|
||||
|
||||
it 'reuses existing WikiLine rows by sha256' do
|
||||
# 先に同じ行を作っておく
|
||||
WikiLine.create!(sha256: Digest::SHA256.hexdigest('a'), body: 'a', created_at: Time.current, updated_at: Time.current)
|
||||
|
||||
post endpoint,
|
||||
params: { title: 'Reuse', body: "a\na" },
|
||||
headers: auth_headers(member)
|
||||
|
||||
page = WikiPage.find(JSON.parse(response.body).fetch('id'))
|
||||
rev = page.current_revision
|
||||
expect(rev.lines_count).to eq(2)
|
||||
|
||||
# "a" の WikiLine が増殖しない(1行のはず)
|
||||
expect(WikiLine.where(body: 'a').count).to eq(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PUT /wiki/:id' do
|
||||
let(:member) { create(:user, role: 'member', inheritance_code: SecureRandom.hex(16)) }
|
||||
let(:guest) { create(:user, role: 'guest', inheritance_code: SecureRandom.hex(16)) }
|
||||
|
||||
def auth_headers(user)
|
||||
{ 'X-Transfer-Code' => user.inheritance_code }
|
||||
end
|
||||
|
||||
#let!(:page) { create(:wiki_page, title: 'TestPage') }
|
||||
let!(:page) do
|
||||
build(:wiki_page, title: 'TestPage').tap do |p|
|
||||
puts p.errors.full_messages unless p.valid?
|
||||
p.save!
|
||||
end
|
||||
end
|
||||
|
||||
before do
|
||||
# 初期版を 1 つ作っておく(更新が“2版目”になるように)
|
||||
Wiki::Commit.content!(page: page, body: "a\nb", created_user: member, message: 'init')
|
||||
end
|
||||
|
||||
context 'when not logged in' do
|
||||
it 'returns 401' do
|
||||
put "/wiki/#{page.id}", params: { title: 'TestPage', body: 'x' }
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when logged in but not member' do
|
||||
it 'returns 403' do
|
||||
put "/wiki/#{page.id}",
|
||||
params: { title: 'TestPage', body: 'x' },
|
||||
headers: auth_headers(guest)
|
||||
expect(response).to have_http_status(:forbidden)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when params invalid' do
|
||||
it 'returns 422 when body blank' do
|
||||
put "/wiki/#{page.id}",
|
||||
params: { title: 'TestPage', body: '' },
|
||||
headers: auth_headers(member)
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
end
|
||||
|
||||
it 'returns 422 when title mismatched (if you forbid rename here)' do
|
||||
put "/wiki/#{page.id}",
|
||||
params: { title: 'OtherTitle', body: 'x' },
|
||||
headers: auth_headers(member)
|
||||
# 君の controller 例だと title 変更は 422 にしてた
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when success' do
|
||||
it 'creates new revision and returns 200' do
|
||||
current_id = page.wiki_revisions.maximum(:id)
|
||||
|
||||
expect do
|
||||
put "/wiki/#{page.id}",
|
||||
params: { title: 'TestPage', body: "x\ny", message: 'edit', base_revision_id: current_id },
|
||||
headers: auth_headers(member)
|
||||
end.to change(WikiRevision, :count).by(1)
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
page.reload
|
||||
rev = page.current_revision
|
||||
expect(rev).to be_content
|
||||
expect(rev.message).to eq('edit')
|
||||
expect(page.body).to eq("x\ny")
|
||||
expect(rev.base_revision_id).to eq(current_id)
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: コンフリクト未実装のため,実装したらコメント外す.
|
||||
# context 'when conflict' do
|
||||
# it 'returns 409 when base_revision_id mismatches' do
|
||||
# # 先に別ユーザ(同じ member でもOK)が 1 回更新して先頭を進める
|
||||
# Wiki::Commit.content!(page: page, body: "zzz", created_user: member, message: 'other edit')
|
||||
# page.reload
|
||||
|
||||
# stale_id = page.wiki_revisions.order(:id).first.id # わざと古い id
|
||||
# put "/wiki/#{page.id}",
|
||||
# params: { title: 'TestPage', body: 'x', base_revision_id: stale_id },
|
||||
# headers: auth_headers(member)
|
||||
|
||||
# expect(response).to have_http_status(:conflict)
|
||||
# json = JSON.parse(response.body)
|
||||
# expect(json['error']).to eq('conflict')
|
||||
# end
|
||||
# end
|
||||
|
||||
context 'when page not found' do
|
||||
it 'returns 404' do
|
||||
put "/wiki/99999999",
|
||||
params: { title: 'X', body: 'x' },
|
||||
headers: auth_headers(member)
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /wiki/title/:title' do
|
||||
it 'returns wiki page by title' do
|
||||
get "/wiki/title/#{CGI.escape('spec_wiki_title')}"
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
expect(json).to have_key('id')
|
||||
expect(json).to have_key('title')
|
||||
expect(json['id']).to eq(page.id)
|
||||
expect(json['title']).to eq('spec_wiki_title')
|
||||
end
|
||||
|
||||
it 'returns 404 when not found' do
|
||||
get "/wiki/title/#{ CGI.escape('nope') }"
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /wiki/search' do
|
||||
before do
|
||||
# 追加で検索ヒット用
|
||||
TagName.create!(name: 'spec_wiki_title_2')
|
||||
WikiPage.create!(tag_name: TagName.find_by!(name: 'spec_wiki_title_2'),
|
||||
created_user: user, updated_user: user)
|
||||
|
||||
TagName.create!(name: 'unrelated_title')
|
||||
WikiPage.create!(tag_name: TagName.find_by!(name: 'unrelated_title'),
|
||||
created_user: user, updated_user: user)
|
||||
end
|
||||
|
||||
it 'returns up to 20 pages filtered by title like' do
|
||||
get "/wiki/search?title=#{CGI.escape('spec_wiki')}"
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json).to be_an(Array)
|
||||
|
||||
titles = json.map { |p| p['title'] }
|
||||
expect(titles).to include('spec_wiki_title', 'spec_wiki_title_2')
|
||||
expect(titles).not_to include('unrelated_title')
|
||||
end
|
||||
|
||||
it 'returns all when title param is blank' do
|
||||
get "/wiki/search?title=#{CGI.escape('')}"
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json).to be_an(Array)
|
||||
expect(json.map { |p| p['title'] }).to include('spec_wiki_title')
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /wiki/changes' do
|
||||
let!(:rev1) do
|
||||
Wiki::Commit.content!(page: page, body: "a\nb", created_user: user, message: 'r1')
|
||||
page.current_revision
|
||||
end
|
||||
|
||||
let!(:rev2) do
|
||||
Wiki::Commit.content!(page: page, body: "a\nc", created_user: user, message: 'r2')
|
||||
page.current_revision
|
||||
end
|
||||
|
||||
it 'returns latest revisions (optionally filtered by page id)' do
|
||||
get "/wiki/changes?id=#{page.id}"
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json).to be_an(Array)
|
||||
expect(json).not_to be_empty
|
||||
|
||||
top = json.first
|
||||
expect(top).to include(
|
||||
'revision_id' => rev2.id,
|
||||
'pred' => rev2.base_revision_id,
|
||||
'succ' => nil,
|
||||
'kind' => 'content',
|
||||
'message' => 'r2'
|
||||
)
|
||||
expect(top['wiki_page']).to include('id' => page.id, 'title' => 'spec_wiki_title')
|
||||
expect(top['user']).to include('id' => user.id, 'name' => user.name)
|
||||
expect(top).to have_key('timestamp')
|
||||
|
||||
# order desc をざっくり担保
|
||||
ids = json.map { |x| x['revision_id'] }
|
||||
expect(ids).to eq(ids.sort.reverse)
|
||||
end
|
||||
|
||||
it 'returns empty array when page has no revisions and filtered by id' do
|
||||
# 別ページを作って revision 無し
|
||||
tn2 = TagName.create!(name: 'spec_no_rev')
|
||||
p2 = WikiPage.create!(tag_name: tn2, created_user: user, updated_user: user)
|
||||
|
||||
get "/wiki/changes?id=#{p2.id}"
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json).to eq([])
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /wiki/title/:title/exists' do
|
||||
it 'returns 204 when exists' do
|
||||
get "/wiki/title/#{CGI.escape('spec_wiki_title')}/exists"
|
||||
expect(response).to have_http_status(:no_content)
|
||||
expect(response.body).to be_empty
|
||||
end
|
||||
|
||||
it 'returns 404 when not exists' do
|
||||
get "/wiki/title/#{CGI.escape('nope')}/exists"
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /wiki/:id/exists' do
|
||||
it 'returns 204 when exists' do
|
||||
get "/wiki/#{page.id}/exists"
|
||||
expect(response).to have_http_status(:no_content)
|
||||
expect(response.body).to be_empty
|
||||
end
|
||||
|
||||
it 'returns 404 when not exists' do
|
||||
get "/wiki/99999999/exists"
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /wiki/:id/diff' do
|
||||
let!(:rev_a) do
|
||||
Wiki::Commit.content!(page: page, body: "a\nb\nc", created_user: user, message: 'A')
|
||||
page.current_revision
|
||||
end
|
||||
|
||||
let!(:rev_b) do
|
||||
Wiki::Commit.content!(page: page, body: "a\nx\nc", created_user: user, message: 'B')
|
||||
page.current_revision
|
||||
end
|
||||
|
||||
it 'returns diff json between revisions' do
|
||||
get "/wiki/#{page.id}/diff?from=#{rev_a.id}&to=#{rev_b.id}"
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
expect(json).to include(
|
||||
'wiki_page_id' => page.id,
|
||||
'title' => 'spec_wiki_title',
|
||||
'older_revision_id' => rev_a.id,
|
||||
'newer_revision_id' => rev_b.id
|
||||
)
|
||||
expect(json['diff']).to be_an(Array)
|
||||
# ざっくり「b が消えて x が増えた」が含まれることを確認
|
||||
types = json['diff'].map { |x| x['type'] }
|
||||
expect(types).to include('removed', 'added').or include('removed').and include('added')
|
||||
end
|
||||
|
||||
it 'uses latest as "to" when to is omitted' do
|
||||
get "/wiki/#{page.id}/diff?from=#{rev_a.id}"
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json['older_revision_id']).to eq(rev_a.id)
|
||||
expect(json['newer_revision_id']).to eq(page.current_revision.id)
|
||||
end
|
||||
|
||||
it 'returns 422 when "to" is redirect revision' do
|
||||
# redirect revision を作る
|
||||
tn2 = TagName.create!(name: 'redirect_target')
|
||||
target = WikiPage.create!(tag_name: tn2, created_user: user, updated_user: user)
|
||||
|
||||
Wiki::Commit.redirect!(page: page, redirect_page: target, created_user: user, message: 'redir')
|
||||
redirect_rev = page.current_revision
|
||||
expect(redirect_rev).to be_redirect
|
||||
|
||||
get "/wiki/#{page.id}/diff?from=#{rev_a.id}&to=#{redirect_rev.id}"
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
end
|
||||
|
||||
it 'returns 422 when "from" is redirect revision' do
|
||||
tn2 = TagName.create!(name: 'redirect_target2')
|
||||
target = WikiPage.create!(tag_name: tn2, created_user: user, updated_user: user)
|
||||
|
||||
Wiki::Commit.redirect!(page: page, redirect_page: target, created_user: user, message: 'redir2')
|
||||
redirect_rev = page.current_revision
|
||||
|
||||
get "/wiki/#{page.id}/diff?from=#{redirect_rev.id}&to=#{rev_b.id}"
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,94 @@
|
||||
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
|
||||
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
||||
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
||||
# this file to always be loaded, without a need to explicitly require it in any
|
||||
# files.
|
||||
#
|
||||
# Given that it is always loaded, you are encouraged to keep this file as
|
||||
# light-weight as possible. Requiring heavyweight dependencies from this file
|
||||
# will add to the boot time of your test suite on EVERY test run, even for an
|
||||
# individual file that may not need all of that loaded. Instead, consider making
|
||||
# a separate helper file that requires the additional dependencies and performs
|
||||
# the additional setup, and require it from the spec files that actually need
|
||||
# it.
|
||||
#
|
||||
# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
||||
RSpec.configure do |config|
|
||||
# rspec-expectations config goes here. You can use an alternate
|
||||
# assertion/expectation library such as wrong or the stdlib/minitest
|
||||
# assertions if you prefer.
|
||||
config.expect_with :rspec do |expectations|
|
||||
# This option will default to `true` in RSpec 4. It makes the `description`
|
||||
# and `failure_message` of custom matchers include text for helper methods
|
||||
# defined using `chain`, e.g.:
|
||||
# be_bigger_than(2).and_smaller_than(4).description
|
||||
# # => "be bigger than 2 and smaller than 4"
|
||||
# ...rather than:
|
||||
# # => "be bigger than 2"
|
||||
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
||||
end
|
||||
|
||||
# rspec-mocks config goes here. You can use an alternate test double
|
||||
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
||||
config.mock_with :rspec do |mocks|
|
||||
# Prevents you from mocking or stubbing a method that does not exist on
|
||||
# a real object. This is generally recommended, and will default to
|
||||
# `true` in RSpec 4.
|
||||
mocks.verify_partial_doubles = true
|
||||
end
|
||||
|
||||
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
||||
# have no way to turn it off -- the option exists only for backwards
|
||||
# compatibility in RSpec 3). It causes shared context metadata to be
|
||||
# inherited by the metadata hash of host groups and examples, rather than
|
||||
# triggering implicit auto-inclusion in groups with matching metadata.
|
||||
config.shared_context_metadata_behavior = :apply_to_host_groups
|
||||
|
||||
# The settings below are suggested to provide a good initial experience
|
||||
# with RSpec, but feel free to customize to your heart's content.
|
||||
=begin
|
||||
# This allows you to limit a spec run to individual examples or groups
|
||||
# you care about by tagging them with `:focus` metadata. When nothing
|
||||
# is tagged with `:focus`, all examples get run. RSpec also provides
|
||||
# aliases for `it`, `describe`, and `context` that include `:focus`
|
||||
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
||||
config.filter_run_when_matching :focus
|
||||
|
||||
# Allows RSpec to persist some state between runs in order to support
|
||||
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
||||
# you configure your source control system to ignore this file.
|
||||
config.example_status_persistence_file_path = "spec/examples.txt"
|
||||
|
||||
# Limits the available syntax to the non-monkey patched syntax that is
|
||||
# recommended. For more details, see:
|
||||
# https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/
|
||||
config.disable_monkey_patching!
|
||||
|
||||
# Many RSpec users commonly either run the entire suite or an individual
|
||||
# file, and it's useful to allow more verbose output when running an
|
||||
# individual spec file.
|
||||
if config.files_to_run.one?
|
||||
# Use the documentation formatter for detailed output,
|
||||
# unless a formatter has already been configured
|
||||
# (e.g. via a command-line flag).
|
||||
config.default_formatter = "doc"
|
||||
end
|
||||
|
||||
# Print the 10 slowest examples and example groups at the
|
||||
# end of the spec run, to help surface which specs are running
|
||||
# particularly slow.
|
||||
config.profile_examples = 10
|
||||
|
||||
# Run specs in random order to surface order dependencies. If you find an
|
||||
# order dependency and want to debug it, you can fix the order by providing
|
||||
# the seed, which is printed after each run.
|
||||
# --seed 1234
|
||||
config.order = :random
|
||||
|
||||
# Seed global randomization in this process using the `--seed` CLI option.
|
||||
# Setting this allows you to use `--seed` to deterministically reproduce
|
||||
# test failures related to randomization by passing the same `--seed` value
|
||||
# as the one that triggered the failure.
|
||||
Kernel.srand config.seed
|
||||
=end
|
||||
end
|
||||
@@ -0,0 +1,11 @@
|
||||
module AuthHelper
|
||||
def sign_in_as(user)
|
||||
allow_any_instance_of(ApplicationController)
|
||||
.to receive(:current_user).and_return(user)
|
||||
end
|
||||
|
||||
def sign_out
|
||||
allow_any_instance_of(ApplicationController)
|
||||
.to receive(:current_user).and_return(nil)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,5 @@
|
||||
module JsonHelper
|
||||
def json
|
||||
JSON.parse(response.body)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,21 @@
|
||||
require "rake"
|
||||
|
||||
|
||||
module RakeTaskHelper
|
||||
# Railsの rake task を一度だけロードする
|
||||
def load_rails_tasks!
|
||||
return if defined?(@rails_tasks_loaded) && @rails_tasks_loaded
|
||||
@rails_tasks_loaded = true
|
||||
|
||||
Rake.application = Rake::Application.new
|
||||
Rails.application.load_tasks
|
||||
end
|
||||
|
||||
def run_rake_task(full_name)
|
||||
load_rails_tasks!
|
||||
|
||||
task = Rake::Task[full_name] # ここは rake[...] じゃなくて良い
|
||||
task.reenable
|
||||
task.invoke
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,15 @@
|
||||
module TestRecords
|
||||
def create_member_user!
|
||||
User.create!(name: 'spec user',
|
||||
inheritance_code: SecureRandom.hex(16),
|
||||
role: 'member',
|
||||
banned: false)
|
||||
end
|
||||
|
||||
def create_admin_user!
|
||||
User.create!(name: 'spec admin',
|
||||
inheritance_code: SecureRandom.hex(16),
|
||||
role: 'admin',
|
||||
banned: false)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,93 @@
|
||||
require "rails_helper"
|
||||
|
||||
|
||||
RSpec.describe "nico:sync" do
|
||||
def stub_python(json_array)
|
||||
status = instance_double(Process::Status, success?: true)
|
||||
allow(Open3).to receive(:capture3).and_return([json_array.to_json, "", status])
|
||||
end
|
||||
|
||||
def create_tag!(name, category:)
|
||||
tn = TagName.find_or_create_by!(name: name.to_s.strip)
|
||||
Tag.find_or_create_by!(tag_name_id: tn.id) { |t| t.category = category }
|
||||
end
|
||||
|
||||
def link_nico_to_tag!(nico_tag, tag)
|
||||
NicoTagRelation.create!(nico_tag_id: nico_tag.id, tag_id: tag.id)
|
||||
end
|
||||
|
||||
it "既存 post を見つけて、nico tag と linked tag を追加し、差分が出たら bot を付ける" do
|
||||
# 既存 post(正規表現で拾われるURL)
|
||||
post = Post.create!(title: "old", url: "https://www.nicovideo.jp/watch/sm9", uploaded_user: nil)
|
||||
|
||||
# 既存の非nicoタグ(kept_non_nico_ids)
|
||||
kept_general = create_tag!("spec_kept", category: "general")
|
||||
PostTag.create!(post: post, tag: kept_general)
|
||||
|
||||
# 追加される linked tag を準備(nico tag に紐付く一般タグ)
|
||||
linked = create_tag!("spec_linked", category: "general")
|
||||
nico = create_tag!("nico:AAA", category: "nico")
|
||||
link_nico_to_tag!(nico, linked)
|
||||
|
||||
# bot / tagme は task 内で使うので作っておく(Tag.bot/tagme がある前提)
|
||||
Tag.bot
|
||||
Tag.tagme
|
||||
|
||||
# pythonの出力(AAA が付く)
|
||||
stub_python([{
|
||||
'code' => 'sm9',
|
||||
'title' => 't',
|
||||
'tags' => ['AAA'],
|
||||
'uploaded_at' => '2026-01-01 12:34:56',
|
||||
'deleted_at' => '2026-01-31 00:00:00' }])
|
||||
|
||||
# 外部HTTPは今回「既存 post なので呼ばれない」はずだが、念のため塞ぐ
|
||||
allow(URI).to receive(:open).and_return(StringIO.new("<html></html>"))
|
||||
|
||||
run_rake_task("nico:sync")
|
||||
|
||||
post.reload
|
||||
active_tag_names = post.tags.joins(:tag_name).pluck("tag_names.name")
|
||||
|
||||
expect(active_tag_names).to include("spec_kept")
|
||||
expect(active_tag_names).to include("nico:AAA")
|
||||
expect(active_tag_names).to include("spec_linked")
|
||||
|
||||
expect(post.original_created_from).to eq(Time.iso8601('2026-01-01T03:34:00Z'))
|
||||
expect(post.original_created_before).to eq(Time.iso8601('2026-01-01T03:35:00Z'))
|
||||
|
||||
# 差分が出るので bot が付く(kept_non_nico_ids != desired_non_nico_ids)
|
||||
expect(active_tag_names).to include("bot操作")
|
||||
end
|
||||
|
||||
it "既存 post にあった古い nico tag は active から外され、履歴として discard される" do
|
||||
post = Post.create!(title: "old", url: "https://www.nicovideo.jp/watch/sm9", uploaded_user: nil)
|
||||
|
||||
# 旧nicoタグ(今回の同期結果に含まれない)
|
||||
old_nico = create_tag!("nico:OLD", category: "nico")
|
||||
old_pt = PostTag.create!(post: post, tag: old_nico)
|
||||
expect(old_pt.discarded_at).to be_nil
|
||||
|
||||
# 今回は NEW のみ欲しい
|
||||
new_nico = create_tag!("nico:NEW", category: "nico")
|
||||
|
||||
# bot/tagme 念のため
|
||||
Tag.bot
|
||||
Tag.tagme
|
||||
|
||||
stub_python([{ "code" => "sm9", "title" => "t", "tags" => ["NEW"] }])
|
||||
allow(URI).to receive(:open).and_return(StringIO.new("<html></html>"))
|
||||
|
||||
run_rake_task("nico:sync")
|
||||
|
||||
# OLD は active から外れる(discarded_at が入る)
|
||||
old_pts = PostTag.where(post_id: post.id, tag_id: old_nico.id).order(:id).to_a
|
||||
expect(old_pts.last.discarded_at).to be_present
|
||||
|
||||
# NEW は active にいる
|
||||
post.reload
|
||||
active_names = post.tags.joins(:tag_name).pluck("tag_names.name")
|
||||
expect(active_names).to include("nico:NEW")
|
||||
expect(active_names).not_to include("nico:OLD")
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,34 @@
|
||||
require 'rails_helper'
|
||||
|
||||
|
||||
RSpec.describe 'post_similarity:calc' do
|
||||
include RakeTaskHelper
|
||||
|
||||
it 'calls Similarity::Calc with Post and :tags' do
|
||||
# 必要最低限のデータ
|
||||
t1 = Tag.create!(name: "t1")
|
||||
t2 = Tag.create!(name: "t2")
|
||||
t3 = Tag.create!(name: "t3")
|
||||
|
||||
p1 = Post.create!(url: "https://example.com/1")
|
||||
p2 = Post.create!(url: "https://example.com/2")
|
||||
p3 = Post.create!(url: "https://example.com/3")
|
||||
|
||||
# kept スコープが絡むなら、PostTag がデフォで kept になる前提
|
||||
PostTag.create!(post: p1, tag: t1)
|
||||
PostTag.create!(post: p1, tag: t2)
|
||||
|
||||
PostTag.create!(post: p2, tag: t1)
|
||||
PostTag.create!(post: p2, tag: t3)
|
||||
|
||||
PostTag.create!(post: p3, tag: t3)
|
||||
|
||||
expect { run_rake_task("post_similarity:calc") }
|
||||
.to change { PostSimilarity.count }.from(0)
|
||||
|
||||
ps = PostSimilarity.find_by!(post_id: p1.id, target_post_id: p2.id)
|
||||
ps_rev = PostSimilarity.find_by!(post_id: p2.id, target_post_id: p1.id)
|
||||
expect(ps_rev.cos).to eq(ps.cos)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
require 'rails_helper'
|
||||
|
||||
|
||||
RSpec.describe 'tag_similarity:calc' do
|
||||
include RakeTaskHelper
|
||||
|
||||
it 'calls Similarity::Calc with Tag and :posts' do
|
||||
# 必要最低限のデータ
|
||||
t1 = Tag.create!(name: "t1")
|
||||
t2 = Tag.create!(name: "t2")
|
||||
t3 = Tag.create!(name: "t3")
|
||||
|
||||
p1 = Post.create!(url: "https://example.com/1")
|
||||
p2 = Post.create!(url: "https://example.com/2")
|
||||
p3 = Post.create!(url: "https://example.com/3")
|
||||
|
||||
# kept スコープが絡むなら、PostTag がデフォで kept になる前提
|
||||
PostTag.create!(post: p1, tag: t1)
|
||||
PostTag.create!(post: p1, tag: t2)
|
||||
|
||||
PostTag.create!(post: p2, tag: t1)
|
||||
PostTag.create!(post: p2, tag: t3)
|
||||
|
||||
PostTag.create!(post: p3, tag: t3)
|
||||
|
||||
expect { run_rake_task("tag_similarity:calc") }
|
||||
.to change { TagSimilarity.count }.from(0)
|
||||
|
||||
ps = TagSimilarity.find_by!(tag_id: t1.id, target_tag_id: t2.id)
|
||||
ps_rev = TagSimilarity.find_by!(tag_id: t2.id, target_tag_id: t1.id)
|
||||
expect(ps_rev.cos).to eq(ps.cos)
|
||||
end
|
||||
end
|
||||
|
||||
生成ファイル
+134
-9
@@ -9,10 +9,14 @@
|
||||
"version": "0.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@dnd-kit/core": "^6.3.1",
|
||||
"@dnd-kit/modifiers": "^9.0.0",
|
||||
"@dnd-kit/utilities": "^3.2.2",
|
||||
"@fontsource-variable/noto-sans-jp": "^5.2.9",
|
||||
"@radix-ui/react-dialog": "^1.1.14",
|
||||
"@radix-ui/react-switch": "^1.2.5",
|
||||
"@radix-ui/react-toast": "^1.2.14",
|
||||
"@tanstack/react-query": "^5.90.2",
|
||||
"axios": "^1.10.0",
|
||||
"camelcase-keys": "^9.1.3",
|
||||
"class-variance-authority": "^0.7.1",
|
||||
@@ -21,15 +25,18 @@
|
||||
"humps": "^2.0.1",
|
||||
"lucide-react": "^0.511.0",
|
||||
"markdown-it": "^14.1.0",
|
||||
"path-to-regexp": "^8.3.0",
|
||||
"react": "^19.1.0",
|
||||
"react-dom": "^19.1.0",
|
||||
"react-helmet-async": "^2.0.5",
|
||||
"react-markdown": "^10.1.0",
|
||||
"react-markdown-editor-lite": "^1.3.4",
|
||||
"react-router-dom": "^6.30.0",
|
||||
"react-router-dom": "^6.30.1",
|
||||
"react-youtube": "^10.1.0",
|
||||
"remark-gfm": "^4.0.1",
|
||||
"tailwind-merge": "^3.3.0"
|
||||
"tailwind-merge": "^3.3.0",
|
||||
"unist-util-visit-parents": "^6.0.1",
|
||||
"zustand": "^5.0.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.25.0",
|
||||
@@ -370,6 +377,59 @@
|
||||
"node": ">=6.9.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@dnd-kit/accessibility": {
|
||||
"version": "3.1.1",
|
||||
"resolved": "https://registry.npmjs.org/@dnd-kit/accessibility/-/accessibility-3.1.1.tgz",
|
||||
"integrity": "sha512-2P+YgaXF+gRsIihwwY1gCsQSYnu9Zyj2py8kY5fFvUM1qm2WA2u639R6YNVfU4GWr+ZM5mqEsfHZZLoRONbemw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"tslib": "^2.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": ">=16.8.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@dnd-kit/core": {
|
||||
"version": "6.3.1",
|
||||
"resolved": "https://registry.npmjs.org/@dnd-kit/core/-/core-6.3.1.tgz",
|
||||
"integrity": "sha512-xkGBRQQab4RLwgXxoqETICr6S5JlogafbhNsidmrkVv2YRs5MLwpjoF2qpiGjQt8S9AoxtIV603s0GIUpY5eYQ==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@dnd-kit/accessibility": "^3.1.1",
|
||||
"@dnd-kit/utilities": "^3.2.2",
|
||||
"tslib": "^2.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": ">=16.8.0",
|
||||
"react-dom": ">=16.8.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@dnd-kit/modifiers": {
|
||||
"version": "9.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@dnd-kit/modifiers/-/modifiers-9.0.0.tgz",
|
||||
"integrity": "sha512-ybiLc66qRGuZoC20wdSSG6pDXFikui/dCNGthxv4Ndy8ylErY0N3KVxY2bgo7AWwIbxDmXDg3ylAFmnrjcbVvw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@dnd-kit/utilities": "^3.2.2",
|
||||
"tslib": "^2.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@dnd-kit/core": "^6.3.0",
|
||||
"react": ">=16.8.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@dnd-kit/utilities": {
|
||||
"version": "3.2.2",
|
||||
"resolved": "https://registry.npmjs.org/@dnd-kit/utilities/-/utilities-3.2.2.tgz",
|
||||
"integrity": "sha512-+MKAJEOfaBe5SmV6t34p80MMKhjvUz0vRrvVJbPT0WElzaOJ/1xs+D+KDv+tD/NE5ujfrChEcshd4fLn0wpiqg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"tslib": "^2.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": ">=16.8.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/aix-ppc64": {
|
||||
"version": "0.25.4",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.25.4.tgz",
|
||||
@@ -1909,6 +1969,32 @@
|
||||
"win32"
|
||||
]
|
||||
},
|
||||
"node_modules/@tanstack/query-core": {
|
||||
"version": "5.90.2",
|
||||
"resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.90.2.tgz",
|
||||
"integrity": "sha512-k/TcR3YalnzibscALLwxeiLUub6jN5EDLwKDiO7q5f4ICEoptJ+n9+7vcEFy5/x/i6Q+Lb/tXrsKCggf5uQJXQ==",
|
||||
"license": "MIT",
|
||||
"funding": {
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/tannerlinsley"
|
||||
}
|
||||
},
|
||||
"node_modules/@tanstack/react-query": {
|
||||
"version": "5.90.2",
|
||||
"resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.90.2.tgz",
|
||||
"integrity": "sha512-CLABiR+h5PYfOWr/z+vWFt5VsOA2ekQeRQBFSKlcoW6Ndx/f8rfyVmq4LbgOM4GG2qtxAxjLYLOpCNTYm4uKzw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@tanstack/query-core": "5.90.2"
|
||||
},
|
||||
"funding": {
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/tannerlinsley"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": "^18 || ^19"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/axios": {
|
||||
"version": "0.14.4",
|
||||
"resolved": "https://registry.npmjs.org/@types/axios/-/axios-0.14.4.tgz",
|
||||
@@ -5495,6 +5581,16 @@
|
||||
"dev": true,
|
||||
"license": "ISC"
|
||||
},
|
||||
"node_modules/path-to-regexp": {
|
||||
"version": "8.3.0",
|
||||
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-8.3.0.tgz",
|
||||
"integrity": "sha512-7jdwVIRtsP8MYpdXSwOS0YdD0Du+qOoF/AEPIt88PcCFrZCzx41oxku1jD88hZBwbNUIEfpqvuhjFaMAqMTWnA==",
|
||||
"license": "MIT",
|
||||
"funding": {
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/express"
|
||||
}
|
||||
},
|
||||
"node_modules/picocolors": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz",
|
||||
@@ -5934,9 +6030,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/react-router": {
|
||||
"version": "6.30.0",
|
||||
"resolved": "https://registry.npmjs.org/react-router/-/react-router-6.30.0.tgz",
|
||||
"integrity": "sha512-D3X8FyH9nBcTSHGdEKurK7r8OYE1kKFn3d/CF+CoxbSHkxU7o37+Uh7eAHRXr6k2tSExXYO++07PeXJtA/dEhQ==",
|
||||
"version": "6.30.1",
|
||||
"resolved": "https://registry.npmjs.org/react-router/-/react-router-6.30.1.tgz",
|
||||
"integrity": "sha512-X1m21aEmxGXqENEPG3T6u0Th7g0aS4ZmoNynhbs+Cn+q+QGTLt+d5IQ2bHAXKzKcxGJjxACpVbnYQSCRcfxHlQ==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@remix-run/router": "1.23.0"
|
||||
@@ -5949,13 +6045,13 @@
|
||||
}
|
||||
},
|
||||
"node_modules/react-router-dom": {
|
||||
"version": "6.30.0",
|
||||
"resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-6.30.0.tgz",
|
||||
"integrity": "sha512-x30B78HV5tFk8ex0ITwzC9TTZMua4jGyA9IUlH1JLQYQTFyxr/ZxwOJq7evg1JX1qGVUcvhsmQSKdPncQrjTgA==",
|
||||
"version": "6.30.1",
|
||||
"resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-6.30.1.tgz",
|
||||
"integrity": "sha512-llKsgOkZdbPU1Eg3zK8lCn+sjD9wMRZZPuzmdWWX5SUs8OFkN5HnFVC0u5KMeMaC9aoancFI/KoLuKPqN+hxHw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@remix-run/router": "1.23.0",
|
||||
"react-router": "6.30.0"
|
||||
"react-router": "6.30.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
@@ -7229,6 +7325,35 @@
|
||||
"integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/zustand": {
|
||||
"version": "5.0.8",
|
||||
"resolved": "https://registry.npmjs.org/zustand/-/zustand-5.0.8.tgz",
|
||||
"integrity": "sha512-gyPKpIaxY9XcO2vSMrLbiER7QMAMGOQZVRdJ6Zi782jkbzZygq5GI9nG8g+sMgitRtndwaBSl7uiqC49o1SSiw==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=12.20.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": ">=18.0.0",
|
||||
"immer": ">=9.0.6",
|
||||
"react": ">=18.0.0",
|
||||
"use-sync-external-store": ">=1.2.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
},
|
||||
"immer": {
|
||||
"optional": true
|
||||
},
|
||||
"react": {
|
||||
"optional": true
|
||||
},
|
||||
"use-sync-external-store": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/zwitch": {
|
||||
"version": "2.0.4",
|
||||
"resolved": "https://registry.npmjs.org/zwitch/-/zwitch-2.0.4.tgz",
|
||||
|
||||
+9
-2
@@ -11,10 +11,14 @@
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"@dnd-kit/core": "^6.3.1",
|
||||
"@dnd-kit/modifiers": "^9.0.0",
|
||||
"@dnd-kit/utilities": "^3.2.2",
|
||||
"@fontsource-variable/noto-sans-jp": "^5.2.9",
|
||||
"@radix-ui/react-dialog": "^1.1.14",
|
||||
"@radix-ui/react-switch": "^1.2.5",
|
||||
"@radix-ui/react-toast": "^1.2.14",
|
||||
"@tanstack/react-query": "^5.90.2",
|
||||
"axios": "^1.10.0",
|
||||
"camelcase-keys": "^9.1.3",
|
||||
"class-variance-authority": "^0.7.1",
|
||||
@@ -23,15 +27,18 @@
|
||||
"humps": "^2.0.1",
|
||||
"lucide-react": "^0.511.0",
|
||||
"markdown-it": "^14.1.0",
|
||||
"path-to-regexp": "^8.3.0",
|
||||
"react": "^19.1.0",
|
||||
"react-dom": "^19.1.0",
|
||||
"react-helmet-async": "^2.0.5",
|
||||
"react-markdown": "^10.1.0",
|
||||
"react-markdown-editor-lite": "^1.3.4",
|
||||
"react-router-dom": "^6.30.0",
|
||||
"react-router-dom": "^6.30.1",
|
||||
"react-youtube": "^10.1.0",
|
||||
"remark-gfm": "^4.0.1",
|
||||
"tailwind-merge": "^3.3.0"
|
||||
"tailwind-merge": "^3.3.0",
|
||||
"zustand": "^5.0.8",
|
||||
"unist-util-visit-parents": "^6.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.25.0",
|
||||
|
||||
+64
-36
@@ -1,17 +1,22 @@
|
||||
import axios from 'axios'
|
||||
import toCamel from 'camelcase-keys'
|
||||
import { AnimatePresence, LayoutGroup } from 'framer-motion'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { BrowserRouter, Navigate, Route, Routes } from 'react-router-dom'
|
||||
import { BrowserRouter,
|
||||
Navigate,
|
||||
Route,
|
||||
Routes,
|
||||
useLocation } from 'react-router-dom'
|
||||
|
||||
import RouteBlockerOverlay from '@/components/RouteBlockerOverlay'
|
||||
import TopNav from '@/components/TopNav'
|
||||
import { Toaster } from '@/components/ui/toaster'
|
||||
import { API_BASE_URL } from '@/config'
|
||||
import { apiPost, isApiError } from '@/lib/api'
|
||||
import NicoTagListPage from '@/pages/tags/NicoTagListPage'
|
||||
import NotFound from '@/pages/NotFound'
|
||||
import PostDetailPage from '@/pages/posts/PostDetailPage'
|
||||
import PostHistoryPage from '@/pages/posts/PostHistoryPage'
|
||||
import PostListPage from '@/pages/posts/PostListPage'
|
||||
import PostNewPage from '@/pages/posts/PostNewPage'
|
||||
import PostSearchPage from '@/pages/posts/PostSearchPage'
|
||||
import ServiceUnavailable from '@/pages/ServiceUnavailable'
|
||||
import SettingPage from '@/pages/users/SettingPage'
|
||||
import WikiDetailPage from '@/pages/wiki/WikiDetailPage'
|
||||
@@ -21,23 +26,60 @@ import WikiHistoryPage from '@/pages/wiki/WikiHistoryPage'
|
||||
import WikiNewPage from '@/pages/wiki/WikiNewPage'
|
||||
import WikiSearchPage from '@/pages/wiki/WikiSearchPage'
|
||||
|
||||
import type { FC } from 'react'
|
||||
import type { Dispatch, FC, SetStateAction } from 'react'
|
||||
|
||||
import type { User } from '@/types'
|
||||
|
||||
|
||||
const RouteTransitionWrapper = ({ user, setUser }: {
|
||||
user: User | null
|
||||
setUser: Dispatch<SetStateAction<User | null>> }) => {
|
||||
const location = useLocation ()
|
||||
|
||||
return (
|
||||
<LayoutGroup id="gallery-shared">
|
||||
<AnimatePresence mode="wait">
|
||||
<Routes location={location} key={location.pathname}>
|
||||
<Route path="/" element={<Navigate to="/posts" replace/>}/>
|
||||
<Route path="/posts" element={<PostListPage/>}/>
|
||||
<Route path="/posts/new" element={<PostNewPage user={user}/>}/>
|
||||
<Route path="/posts/search" element={<PostSearchPage/>}/>
|
||||
<Route path="/posts/:id" element={<PostDetailRoute user={user}/>}/>
|
||||
<Route path="/posts/changes" element={<PostHistoryPage/>}/>
|
||||
<Route path="/tags/nico" element={<NicoTagListPage user={user}/>}/>
|
||||
<Route path="/wiki" element={<WikiSearchPage/>}/>
|
||||
<Route path="/wiki/:title" element={<WikiDetailPage/>}/>
|
||||
<Route path="/wiki/new" element={<WikiNewPage user={user}/>}/>
|
||||
<Route path="/wiki/:id/edit" element={<WikiEditPage user={user}/>}/>
|
||||
<Route path="/wiki/:id/diff" element={<WikiDiffPage/>}/>
|
||||
<Route path="/wiki/changes" element={<WikiHistoryPage/>}/>
|
||||
<Route path="/users/settings" element={<SettingPage user={user} setUser={setUser}/>}/>
|
||||
<Route path="/settings" element={<Navigate to="/users/settings" replace/>}/>
|
||||
<Route path="*" element={<NotFound/>}/>
|
||||
</Routes>
|
||||
</AnimatePresence>
|
||||
</LayoutGroup>)
|
||||
}
|
||||
|
||||
|
||||
const PostDetailRoute = ({ user }: { user: User | null }) => {
|
||||
const location = useLocation ()
|
||||
const key = location.pathname
|
||||
return <PostDetailPage key={key} user={user}/>
|
||||
}
|
||||
|
||||
|
||||
export default (() => {
|
||||
const [user, setUser] = useState<User | null> (null)
|
||||
const [status, setStatus] = useState (200)
|
||||
|
||||
useEffect (() => {
|
||||
const createUser = async () => {
|
||||
const res = await axios.post (`${ API_BASE_URL }/users`)
|
||||
const data = res.data as { code: string; user: any }
|
||||
const data = await apiPost<{ code: string; user: User }> ('/users')
|
||||
if (data.code)
|
||||
{
|
||||
localStorage.setItem ('user_code', data.code)
|
||||
setUser (toCamel (data.user, { deep: true }) as User)
|
||||
setUser (data.user)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,17 +89,16 @@ export default (() => {
|
||||
void (async () => {
|
||||
try
|
||||
{
|
||||
const res = await axios.post (`${ API_BASE_URL }/users/verify`, { code })
|
||||
const data = res.data as { valid: boolean, user: any }
|
||||
const data = await apiPost<{ valid: boolean; user: User }> ('/users/verify', { code })
|
||||
if (data.valid)
|
||||
setUser (toCamel (data.user, { deep: true }))
|
||||
setUser (data.user)
|
||||
else
|
||||
await createUser ()
|
||||
}
|
||||
catch (err)
|
||||
{
|
||||
if (axios.isAxiosError (err))
|
||||
setStatus (err.status ?? 200)
|
||||
if (isApiError (err))
|
||||
setStatus (err.response?.status ?? 200)
|
||||
}
|
||||
}) ()
|
||||
}
|
||||
@@ -72,27 +113,14 @@ export default (() => {
|
||||
}
|
||||
|
||||
return (
|
||||
<BrowserRouter>
|
||||
<div className="flex flex-col h-screen w-screen">
|
||||
<TopNav user={user}/>
|
||||
<Routes>
|
||||
<Route path="/" element={<Navigate to="/posts" replace/>}/>
|
||||
<Route path="/posts" element={<PostListPage/>}/>
|
||||
<Route path="/posts/new" element={<PostNewPage user={user}/>}/>
|
||||
<Route path="/posts/:id" element={<PostDetailPage user={user}/>}/>
|
||||
<Route path="/posts/changes" element={<PostHistoryPage/>}/>
|
||||
<Route path="/tags/nico" element={<NicoTagListPage user={user}/>}/>
|
||||
<Route path="/wiki" element={<WikiSearchPage/>}/>
|
||||
<Route path="/wiki/:title" element={<WikiDetailPage/>}/>
|
||||
<Route path="/wiki/new" element={<WikiNewPage user={user}/>}/>
|
||||
<Route path="/wiki/:id/edit" element={<WikiEditPage user={user}/>}/>
|
||||
<Route path="/wiki/:id/diff" element={<WikiDiffPage/>}/>
|
||||
<Route path="/wiki/changes" element={<WikiHistoryPage/>}/>
|
||||
<Route path="/users/settings" element={<SettingPage user={user} setUser={setUser}/>}/>
|
||||
<Route path="/settings" element={<Navigate to="/users/settings" replace/>}/>
|
||||
<Route path="*" element={<NotFound/>}/>
|
||||
</Routes>
|
||||
</div>
|
||||
<Toaster/>
|
||||
</BrowserRouter>)
|
||||
<>
|
||||
<RouteBlockerOverlay/>
|
||||
<BrowserRouter>
|
||||
<div className="flex flex-col h-screen w-screen">
|
||||
<TopNav user={user}/>
|
||||
<RouteTransitionWrapper user={user} setUser={setUser}/>
|
||||
</div>
|
||||
<Toaster/>
|
||||
</BrowserRouter>
|
||||
</>)
|
||||
}) satisfies FC
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
import { useDraggable, useDroppable } from '@dnd-kit/core'
|
||||
import { CSS } from '@dnd-kit/utilities'
|
||||
import { useRef } from 'react'
|
||||
|
||||
import TagLink from '@/components/TagLink'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
import type { CSSProperties, FC, MutableRefObject } from 'react'
|
||||
|
||||
import type { Tag } from '@/types'
|
||||
|
||||
type Props = {
|
||||
tag: Tag
|
||||
nestLevel: number
|
||||
pathKey: string
|
||||
parentTagId?: number
|
||||
suppressClickRef: MutableRefObject<boolean> }
|
||||
|
||||
|
||||
export default (({ tag, nestLevel, pathKey, parentTagId, suppressClickRef }: Props) => {
|
||||
const dndId = `tag-node:${ pathKey }`
|
||||
|
||||
const downPosRef = useRef<{ x: number; y: number } | null> (null)
|
||||
const armedRef = useRef (false)
|
||||
|
||||
const armEatNextClick = () => {
|
||||
if (armedRef.current)
|
||||
return
|
||||
|
||||
armedRef.current = true
|
||||
suppressClickRef.current = true
|
||||
|
||||
const handler = (ev: MouseEvent) => {
|
||||
ev.preventDefault ()
|
||||
ev.stopPropagation ()
|
||||
armedRef.current = false
|
||||
suppressClickRef.current = false
|
||||
}
|
||||
|
||||
addEventListener ('click', handler, { capture: true, once: true })
|
||||
}
|
||||
|
||||
const { attributes,
|
||||
listeners,
|
||||
setNodeRef: setDragRef,
|
||||
transform,
|
||||
isDragging: dragging } = useDraggable ({ id: dndId,
|
||||
data: { kind: 'tag',
|
||||
tagId: tag.id,
|
||||
parentTagId } })
|
||||
|
||||
const { setNodeRef: setDropRef, isOver: over } = useDroppable ({
|
||||
id: dndId,
|
||||
data: { kind: 'tag', tagId: tag.id } })
|
||||
|
||||
const style: CSSProperties = { transform: CSS.Translate.toString (transform),
|
||||
visibility: dragging ? 'hidden' : 'visible' }
|
||||
|
||||
return (
|
||||
<div
|
||||
onPointerDownCapture={e => {
|
||||
downPosRef.current = { x: e.clientX, y: e.clientY }
|
||||
}}
|
||||
onPointerMoveCapture={e => {
|
||||
const p = downPosRef.current
|
||||
if (!(p))
|
||||
return
|
||||
const dx = e.clientX - p.x
|
||||
const dy = e.clientY - p.y
|
||||
if (dx * dx + dy * dy >= 9)
|
||||
armEatNextClick ()
|
||||
}}
|
||||
onPointerUpCapture={() => {
|
||||
downPosRef.current = null
|
||||
}}
|
||||
onClickCapture={e => {
|
||||
if (suppressClickRef.current)
|
||||
{
|
||||
e.preventDefault ()
|
||||
e.stopPropagation ()
|
||||
}
|
||||
}}
|
||||
ref={node => {
|
||||
setDragRef (node)
|
||||
setDropRef (node)
|
||||
}}
|
||||
style={style}
|
||||
className={cn ('rounded select-none', over && 'ring-2 ring-offset-2')}
|
||||
{...attributes}
|
||||
{...listeners}>
|
||||
<TagLink tag={tag} nestLevel={nestLevel}/>
|
||||
</div>)
|
||||
}) satisfies FC<Props>
|
||||
@@ -1,12 +1,10 @@
|
||||
import axios from 'axios'
|
||||
import toCamel from 'camelcase-keys'
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
import PostFormTagsArea from '@/components/PostFormTagsArea'
|
||||
import PostOriginalCreatedTimeField from '@/components/PostOriginalCreatedTimeField'
|
||||
import Label from '@/components/common/Label'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { API_BASE_URL } from '@/config'
|
||||
import { apiPut } from '@/lib/api'
|
||||
|
||||
import type { FC } from 'react'
|
||||
|
||||
@@ -41,14 +39,11 @@ export default (({ post, onSave }: Props) => {
|
||||
const [tags, setTags] = useState<string> ('')
|
||||
|
||||
const handleSubmit = async () => {
|
||||
const res = await axios.put (
|
||||
`${ API_BASE_URL }/posts/${ post.id }`,
|
||||
{ title, tags,
|
||||
original_created_from: originalCreatedFrom,
|
||||
const data = await apiPut<Post> (
|
||||
`/posts/${ post.id }`,
|
||||
{ title, tags, original_created_from: originalCreatedFrom,
|
||||
original_created_before: originalCreatedBefore },
|
||||
{ headers: { 'Content-Type': 'multipart/form-data',
|
||||
'X-Transfer-Code': localStorage.getItem ('user_code') ?? '' } })
|
||||
const data = toCamel (res.data as any, { deep: true }) as Post
|
||||
{ headers: { 'Content-Type': 'multipart/form-data' } })
|
||||
onSave ({ ...post,
|
||||
title: data.title,
|
||||
tags: data.tags,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { useState } from 'react'
|
||||
import YoutubeEmbed from 'react-youtube'
|
||||
|
||||
import NicoViewer from '@/components/NicoViewer'
|
||||
@@ -17,17 +18,35 @@ export default (({ post }: Props) => {
|
||||
{
|
||||
case 'nicovideo.jp':
|
||||
{
|
||||
const [videoId] = url.pathname.match (/(?<=\/watch\/)[a-zA-Z0-9]+?(?=\/|$)/)!
|
||||
const mVideoId = url.pathname.match (/(?<=\/watch\/)[a-zA-Z0-9]+?(?=\/|$)/)
|
||||
if (!(mVideoId))
|
||||
break
|
||||
|
||||
const [videoId] = mVideoId
|
||||
|
||||
return <NicoViewer id={videoId} width={640} height={360}/>
|
||||
}
|
||||
|
||||
case 'twitter.com':
|
||||
case 'x.com':
|
||||
const [userId] = url.pathname.match (/(?<=\/)[^\/]+?(?=\/|$|\?)/)!
|
||||
const [statusId] = url.pathname.match (/(?<=\/status\/)\d+?(?=\/|$|\?)/)!
|
||||
return <TwitterEmbed userId={userId} statusId={statusId}/>
|
||||
{
|
||||
const mUserId = url.pathname.match (/(?<=\/)[^\/]+?(?=\/|$|\?)/)
|
||||
const mStatusId = url.pathname.match (/(?<=\/status\/)\d+?(?=\/|$|\?)/)
|
||||
if (!(mUserId) || !(mStatusId))
|
||||
break
|
||||
|
||||
const [userId] = mUserId
|
||||
const [statusId] = mStatusId
|
||||
|
||||
return <TwitterEmbed userId={userId} statusId={statusId}/>
|
||||
}
|
||||
|
||||
case 'youtube.com':
|
||||
{
|
||||
const videoId = url.searchParams.get ('v')!
|
||||
const videoId = url.searchParams.get ('v')
|
||||
if (!(videoId))
|
||||
break
|
||||
|
||||
return (
|
||||
<YoutubeEmbed videoId={videoId} opts={{ playerVars: {
|
||||
playsinline: 1,
|
||||
@@ -39,10 +58,28 @@ export default (({ post }: Props) => {
|
||||
}
|
||||
}
|
||||
|
||||
const [framed, setFramed] = useState (false)
|
||||
|
||||
return (
|
||||
<a href={post.url} target="_blank">
|
||||
<img src={post.thumbnailBase || post.thumbnail}
|
||||
alt={post.url}
|
||||
className="mb-4 w-full"/>
|
||||
</a>)
|
||||
<>
|
||||
{framed
|
||||
? (
|
||||
<iframe
|
||||
src={post.url}
|
||||
title={post.title || post.url}
|
||||
width={640}
|
||||
height={360}/>)
|
||||
: (
|
||||
<div>
|
||||
<a href="#" onClick={e => {
|
||||
e.preventDefault ()
|
||||
setFramed (confirm ('未確認の外部ページを表示します。\n'
|
||||
+ '悪意のあるスクリプトが実行される可能性があります。\n'
|
||||
+ '表示しますか?'))
|
||||
return
|
||||
}}>
|
||||
外部ページを表示
|
||||
</a>
|
||||
</div>)}
|
||||
</>)
|
||||
}) satisfies FC<Props>
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import axios from 'axios'
|
||||
import toCamel from 'camelcase-keys'
|
||||
// TODO: TagSearch と共通化する.
|
||||
|
||||
import { useRef, useState } from 'react'
|
||||
|
||||
import TagSearchBox from '@/components/TagSearchBox'
|
||||
import Label from '@/components/common/Label'
|
||||
import TextArea from '@/components/common/TextArea'
|
||||
import { API_BASE_URL } from '@/config'
|
||||
import { apiGet } from '@/lib/api'
|
||||
|
||||
import type { FC, SyntheticEvent } from 'react'
|
||||
|
||||
@@ -27,8 +27,8 @@ const getTokenAt = (value: string, pos: number) => {
|
||||
}
|
||||
|
||||
|
||||
const replaceToken = (value: string, start: number, end: number, text: string) => (
|
||||
`${ value.slice (0, start) }${ text }${ value.slice (end) }`)
|
||||
const replaceToken = (value: string, start: number, end: number, text: string) =>
|
||||
`${ value.slice (0, start) }${ text }${ value.slice (end) }`
|
||||
|
||||
|
||||
type Props = {
|
||||
@@ -40,16 +40,17 @@ export default (({ tags, setTags }: Props) => {
|
||||
const ref = useRef<HTMLTextAreaElement> (null)
|
||||
|
||||
const [bounds, setBounds] = useState<{ start: number; end: number }> ({ start: 0, end: 0 })
|
||||
const [focused, setFocused] = useState (false)
|
||||
const [suggestions, setSuggestions] = useState<Tag[]> ([])
|
||||
const [suggestionsVsbl, setSuggestionsVsbl] = useState (false)
|
||||
|
||||
const handleTagSelect = (tag: Tag) => {
|
||||
setSuggestionsVsbl (false)
|
||||
const textarea = ref.current!
|
||||
const newValue = replaceToken (tags, bounds.start, bounds.end, tag.name)
|
||||
const newValue = replaceToken (tags, bounds.start, bounds.end, tag.name + ' ')
|
||||
setTags (newValue)
|
||||
requestAnimationFrame (async () => {
|
||||
const p = bounds.start + tag.name.length
|
||||
const p = bounds.start + tag.name.length + 1
|
||||
textarea.selectionStart = textarea.selectionEnd = p
|
||||
textarea.focus ()
|
||||
await recompute (p, newValue)
|
||||
@@ -58,14 +59,21 @@ export default (({ tags, setTags }: Props) => {
|
||||
|
||||
const recompute = async (pos: number, v: string = tags) => {
|
||||
const { start, end, token } = getTokenAt (v, pos)
|
||||
if (!(token.trim ()))
|
||||
{
|
||||
setSuggestionsVsbl (false)
|
||||
return
|
||||
}
|
||||
|
||||
setBounds ({ start, end })
|
||||
const res = await axios.get (`${ API_BASE_URL }/tags/autocomplete`, { params: { q: token } })
|
||||
setSuggestions (toCamel (res.data as any, { deep: true }) as Tag[])
|
||||
|
||||
const data = await apiGet<Tag[]> ('/tags/autocomplete', { params: { q: token, nico: '0' } })
|
||||
setSuggestions (data.filter (t => t.postCount > 0))
|
||||
setSuggestionsVsbl (suggestions.length > 0)
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="relative w-full">
|
||||
<Label>タグ</Label>
|
||||
<TextArea
|
||||
ref={ref}
|
||||
@@ -74,11 +82,18 @@ export default (({ tags, setTags }: Props) => {
|
||||
onSelect={async (ev: SyntheticEvent<HTMLTextAreaElement>) => {
|
||||
const pos = (ev.target as HTMLTextAreaElement).selectionStart
|
||||
await recompute (pos)
|
||||
}}
|
||||
onFocus={() => setFocused (true)}
|
||||
onBlur={() => {
|
||||
setFocused (false)
|
||||
setSuggestionsVsbl (false)
|
||||
}}/>
|
||||
<TagSearchBox suggestions={suggestionsVsbl && suggestions.length
|
||||
? suggestions
|
||||
: [] as Tag[]}
|
||||
activeIndex={-1}
|
||||
onSelect={handleTagSelect}/>
|
||||
{focused && (
|
||||
<TagSearchBox
|
||||
suggestions={suggestionsVsbl && suggestions.length > 0
|
||||
? suggestions
|
||||
: [] as Tag[]}
|
||||
activeIndex={-1}
|
||||
onSelect={handleTagSelect}/>)}
|
||||
</div>)
|
||||
}) satisfies FC<Props>
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
import { Link } from 'react-router-dom'
|
||||
import { motion } from 'framer-motion'
|
||||
import { useRef } from 'react'
|
||||
import { useLocation } from 'react-router-dom'
|
||||
|
||||
import PrefetchLink from '@/components/PrefetchLink'
|
||||
import { useSharedTransitionStore } from '@/stores/sharedTransitionStore'
|
||||
|
||||
import type { FC, MouseEvent } from 'react'
|
||||
|
||||
@@ -8,18 +13,58 @@ type Props = { posts: Post[]
|
||||
onClick?: (event: MouseEvent<HTMLElement>) => void }
|
||||
|
||||
|
||||
export default (({ posts, onClick }: Props) => (
|
||||
<div className="flex flex-wrap gap-6 p-4">
|
||||
{posts.map ((post, i) => (
|
||||
<Link to={`/posts/${ post.id }`}
|
||||
key={post.id}
|
||||
className="w-40 h-40 overflow-hidden rounded-lg shadow-md hover:shadow-lg"
|
||||
onClick={onClick}>
|
||||
<img src={post.thumbnail || post.thumbnailBase || undefined}
|
||||
alt={post.title || post.url}
|
||||
title={post.title || post.url || undefined}
|
||||
loading={i < 12 ? 'eager' : 'lazy'}
|
||||
decoding="async"
|
||||
className="object-cover w-full h-full"/>
|
||||
</Link>))}
|
||||
</div>)) satisfies FC<Props>
|
||||
export default (({ posts, onClick }: Props) => {
|
||||
const location = useLocation ()
|
||||
|
||||
const setForLocationKey = useSharedTransitionStore (s => s.setForLocationKey)
|
||||
|
||||
const cardRef = useRef<HTMLDivElement> (null)
|
||||
|
||||
return (
|
||||
<div className="flex flex-wrap gap-6 p-4">
|
||||
{posts.map ((post, i) => {
|
||||
const sharedId = `page-${ post.id }`
|
||||
const layoutId = sharedId
|
||||
|
||||
return (
|
||||
<PrefetchLink
|
||||
to={`/posts/${ post.id }`}
|
||||
key={post.id}
|
||||
className="w-40 h-40"
|
||||
state={{ sharedId }}
|
||||
onClick={e => {
|
||||
setForLocationKey (location.key, sharedId)
|
||||
onClick?.(e)
|
||||
}}>
|
||||
<motion.div
|
||||
ref={cardRef}
|
||||
layoutId={layoutId}
|
||||
className="w-full h-full overflow-hidden rounded-xl shadow
|
||||
transform-gpu will-change-transform"
|
||||
whileHover={{ scale: 1.02 }}
|
||||
onLayoutAnimationStart={() => {
|
||||
if (!(cardRef.current))
|
||||
return
|
||||
|
||||
cardRef.current.style.position = 'relative'
|
||||
cardRef.current.style.zIndex = '9999'
|
||||
}}
|
||||
onLayoutAnimationComplete={() => {
|
||||
if (!(cardRef.current))
|
||||
return
|
||||
|
||||
cardRef.current.style.zIndex = ''
|
||||
cardRef.current.style.position = ''
|
||||
}}
|
||||
transition={{ type: 'spring', stiffness: 500, damping: 40, mass: .5 }}>
|
||||
<img src={post.thumbnail || post.thumbnailBase || undefined}
|
||||
alt={post.title || post.url}
|
||||
title={post.title || post.url || undefined}
|
||||
loading={i < 12 ? 'eager' : 'lazy'}
|
||||
decoding="async"
|
||||
className="object-cover w-full h-full"/>
|
||||
</motion.div>
|
||||
</PrefetchLink>)
|
||||
})}
|
||||
</div>)
|
||||
}) satisfies FC<Props>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import DateTimeField from '@/components/common/DateTimeField'
|
||||
import Label from '@/components/common/Label'
|
||||
import { Button } from '@/components/ui/button'
|
||||
|
||||
import type { FC } from 'react'
|
||||
|
||||
@@ -16,34 +17,52 @@ export default (({ originalCreatedFrom,
|
||||
setOriginalCreatedBefore }: Props) => (
|
||||
<div>
|
||||
<Label>オリジナルの作成日時</Label>
|
||||
<div className="my-1">
|
||||
<DateTimeField
|
||||
className="mr-2"
|
||||
value={originalCreatedFrom ?? undefined}
|
||||
onChange={setOriginalCreatedFrom}
|
||||
onBlur={ev => {
|
||||
const v = ev.target.value
|
||||
if (!(v))
|
||||
return
|
||||
const d = new Date (v)
|
||||
if (d.getSeconds () === 0)
|
||||
{
|
||||
if (d.getMinutes () === 0 && d.getHours () === 0)
|
||||
d.setDate (d.getDate () + 1)
|
||||
else
|
||||
d.setMinutes (d.getMinutes () + 1)
|
||||
}
|
||||
else
|
||||
d.setSeconds (d.getSeconds () + 1)
|
||||
setOriginalCreatedBefore (d.toISOString ())
|
||||
}}/>
|
||||
以降
|
||||
<div className="my-1 flex">
|
||||
<div className="w-80">
|
||||
<DateTimeField
|
||||
className="mr-2"
|
||||
value={originalCreatedFrom ?? undefined}
|
||||
onChange={setOriginalCreatedFrom}
|
||||
onBlur={ev => {
|
||||
const v = ev.target.value
|
||||
if (!(v))
|
||||
return
|
||||
|
||||
const d = new Date (v)
|
||||
if (d.getMinutes () === 0 && d.getHours () === 0)
|
||||
d.setDate (d.getDate () + 1)
|
||||
else
|
||||
d.setMinutes (d.getMinutes () + 1)
|
||||
setOriginalCreatedBefore (d.toISOString ())
|
||||
}}/>
|
||||
以降
|
||||
</div>
|
||||
<div>
|
||||
<Button
|
||||
className="bg-gray-600 text-white rounded"
|
||||
onClick={() => {
|
||||
setOriginalCreatedFrom (null)
|
||||
}}>
|
||||
リセット
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<div className="my-1">
|
||||
<DateTimeField
|
||||
className="mr-2"
|
||||
value={originalCreatedBefore ?? undefined}
|
||||
onChange={setOriginalCreatedBefore}/>
|
||||
より前
|
||||
<div className="my-1 flex">
|
||||
<div className="w-80">
|
||||
<DateTimeField
|
||||
className="mr-2"
|
||||
value={originalCreatedBefore ?? undefined}
|
||||
onChange={setOriginalCreatedBefore}/>
|
||||
より前
|
||||
</div>
|
||||
<div>
|
||||
<Button
|
||||
className="bg-gray-600 text-white rounded"
|
||||
onClick={() => {
|
||||
setOriginalCreatedBefore (null)
|
||||
}}>
|
||||
リセット
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>)) satisfies FC<Props>
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
import { useQueryClient } from '@tanstack/react-query'
|
||||
import { forwardRef, useMemo } from 'react'
|
||||
import { flushSync } from 'react-dom'
|
||||
import { createPath, useNavigate } from 'react-router-dom'
|
||||
|
||||
import { useOverlayStore } from '@/components/RouteBlockerOverlay'
|
||||
import { prefetchForURL } from '@/lib/prefetchers'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
import type { AnchorHTMLAttributes, MouseEvent, TouchEvent } from 'react'
|
||||
import type { To } from 'react-router-dom'
|
||||
|
||||
type Props = AnchorHTMLAttributes<HTMLAnchorElement> & {
|
||||
to: To
|
||||
state?: Record<string, string>
|
||||
replace?: boolean
|
||||
className?: string
|
||||
cancelOnError?: boolean }
|
||||
|
||||
|
||||
export default forwardRef<HTMLAnchorElement, Props> (({
|
||||
to,
|
||||
replace,
|
||||
className,
|
||||
state,
|
||||
onMouseEnter,
|
||||
onTouchStart,
|
||||
onClick,
|
||||
cancelOnError = false,
|
||||
...rest }, ref) => {
|
||||
if ('onClick' in rest)
|
||||
delete rest['onClick']
|
||||
|
||||
const navigate = useNavigate ()
|
||||
const qc = useQueryClient ()
|
||||
const url = useMemo (() => {
|
||||
const path = (typeof to === 'string') ? to : createPath (to)
|
||||
return (new URL (path, location.origin)).toString ()
|
||||
}, [to])
|
||||
const setOverlay = useOverlayStore (s => s.setActive)
|
||||
|
||||
const doPrefetch = async () => {
|
||||
try
|
||||
{
|
||||
await prefetchForURL (qc, url)
|
||||
return true
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
console.error ('データ取得エラー', e)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
const handleMouseEnter = async (ev: MouseEvent<HTMLAnchorElement>) => {
|
||||
onMouseEnter?.(ev)
|
||||
await doPrefetch ()
|
||||
}
|
||||
|
||||
const handleTouchStart = async (ev: TouchEvent<HTMLAnchorElement>) => {
|
||||
onTouchStart?.(ev)
|
||||
await doPrefetch ()
|
||||
}
|
||||
|
||||
const handleClick = async (ev: MouseEvent<HTMLAnchorElement>) => {
|
||||
try
|
||||
{
|
||||
onClick?.(ev)
|
||||
|
||||
if (ev.defaultPrevented
|
||||
|| ev.metaKey
|
||||
|| ev.ctrlKey
|
||||
|| ev.shiftKey
|
||||
|| ev.altKey)
|
||||
return
|
||||
|
||||
ev.preventDefault ()
|
||||
|
||||
flushSync (() => {
|
||||
setOverlay (true)
|
||||
})
|
||||
const ok = await doPrefetch ()
|
||||
flushSync (() => {
|
||||
setOverlay (false)
|
||||
})
|
||||
|
||||
if (!(ok) && cancelOnError)
|
||||
return
|
||||
|
||||
navigate (to, { replace, ...(state && { state }) })
|
||||
}
|
||||
catch (ex)
|
||||
{
|
||||
console.log (ex)
|
||||
ev.preventDefault ()
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<a ref={ref}
|
||||
href={typeof to === 'string' ? to : createPath (to)}
|
||||
onMouseEnter={handleMouseEnter}
|
||||
onTouchStart={handleTouchStart}
|
||||
onClick={handleClick}
|
||||
className={cn ('cursor-pointer', className)}
|
||||
{...rest}/>)
|
||||
})
|
||||
変更されたファイルが多すぎるため,一部のファイルは表示されません さらに表示
新しい課題から参照
ユーザをブロックする