Compare commits

...

4 Commits

4 changed files with 173 additions and 20 deletions
Split View
  1. +28
    -6
      backend/app/controllers/posts_controller.rb
  2. +1
    -2
      backend/app/controllers/tags_controller.rb
  3. +124
    -7
      backend/spec/requests/posts_spec.rb
  4. +20
    -5
      frontend/src/components/TagSearch.tsx

+ 28
- 6
backend/app/controllers/posts_controller.rb View File

@@ -245,19 +245,41 @@ class PostsController < ApplicationController
end

def filter_posts_by_tags tag_names, match_type
tag_names = TagName.canonicalise(tag_names)
literals = tag_names.map do |raw_name|
{ name: TagName.canonicalise(raw_name.sub(/\Anot:/i, '')).first,
negative: raw_name.downcase.start_with?('not:') }
end

posts = Post.joins(tags: :tag_name)
return Post.all if literals.empty?

if match_type == 'any'
posts.where(tag_names: { name: tag_names }).distinct
literals.reduce(Post.none) do |posts, literal|
posts.or(tag_literal_relation(literal[:name], negative: literal[:negative]))
end
else
literals.reduce(Post.all) do |posts, literal|
ids = tagged_post_ids_for(literal[:name])
if literal[:negative]
posts.where.not(id: ids)
else
posts.where(id: ids)
end
end
end
end

def tag_literal_relation name, negative:
ids = tagged_post_ids_for(name)
if negative
Post.where.not(id: ids)
else
posts.where(tag_names: { name: tag_names })
.group('posts.id')
.having('COUNT(DISTINCT tag_names.id) = ?', tag_names.uniq.size)
Post.where(id: ids)
end
end

def tagged_post_ids_for(name) =
Post.joins(tags: :tag_name).where(tag_names: { name: }).select(:id)

def sync_post_tags! post, desired_tags
desired_tags.each do |t|
t.save! if t.new_record?


+ 1
- 2
backend/app/controllers/tags_controller.rb View File

@@ -17,8 +17,7 @@ class TagsController < ApplicationController
end

def autocomplete
q = params[:q].to_s.strip
return render json: [] if q.blank?
q = params[:q].to_s.strip.sub(/\Anot:/i, '')

with_nico = bool?(:nico, default: true)
present_only = bool?(:present, default: true)


+ 124
- 7
backend/spec/requests/posts_spec.rb View File

@@ -16,7 +16,7 @@ RSpec.describe 'Posts API', type: :request do
end

let!(:tag_name) { TagName.create!(name: 'spec_tag') }
let!(:tag) { Tag.create!(tag_name: tag_name, category: 'general') }
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|
@@ -28,9 +28,9 @@ RSpec.describe 'Posts API', type: :request do
let!(:user) { create_member_user! }

let!(:tag_name) { TagName.create!(name: "spec_tag") }
let!(:tag) { Tag.create!(tag_name:, category: "general") }
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!(:tag2) { Tag.create!(tag_name: tag_name2, category: :deerjikist) }
let!(:alias_tag_name) { TagName.create!(name: 'manko', canonical: tag_name) }

let!(:hit_post) do
@@ -116,6 +116,123 @@ RSpec.describe 'Posts API', type: :request do
end
end

context 'when tags contain not:' do
let!(:foo_tag_name) { TagName.create!(name: 'not_spec_foo') }
let!(:foo_tag) { Tag.create!(tag_name: foo_tag_name, category: :general) }

let!(:bar_tag_name) { TagName.create!(name: 'not_spec_bar') }
let!(:bar_tag) { Tag.create!(tag_name: bar_tag_name, category: :general) }

let!(:baz_tag_name) { TagName.create!(name: 'not_spec_baz') }
let!(:baz_tag) { Tag.create!(tag_name: baz_tag_name, category: :general) }

let!(:foo_alias_tag_name) do
TagName.create!(name: 'not_spec_foo_alias', canonical: foo_tag_name)
end

let!(:foo_only_post) do
Post.create!(uploaded_user: user, title: 'foo only',
url: 'https://example.com/not-spec-foo').tap do |p|
PostTag.create!(post: p, tag: foo_tag)
end
end

let!(:bar_only_post) do
Post.create!(uploaded_user: user, title: 'bar only',
url: 'https://example.com/not-spec-bar').tap do |p|
PostTag.create!(post: p, tag: bar_tag)
end
end

let!(:baz_only_post) do
Post.create!(uploaded_user: user, title: 'baz only',
url: 'https://example.com/not-spec-baz').tap do |p|
PostTag.create!(post: p, tag: baz_tag)
end
end

let!(:foo_bar_post) do
Post.create!(uploaded_user: user, title: 'foo bar',
url: 'https://example.com/not-spec-foo-bar').tap do |p|
PostTag.create!(post: p, tag: foo_tag)
PostTag.create!(post: p, tag: bar_tag)
end
end

let!(:foo_baz_post) do
Post.create!(uploaded_user: user, title: 'foo baz',
url: 'https://example.com/not-spec-foo-baz').tap do |p|
PostTag.create!(post: p, tag: foo_tag)
PostTag.create!(post: p, tag: baz_tag)
end
end

let(:controlled_ids) do
[foo_only_post.id, bar_only_post.id, baz_only_post.id,
foo_bar_post.id, foo_baz_post.id]
end

it 'supports not search' do
get '/posts', params: { tags: 'not:not_spec_foo' }

expect(response).to have_http_status(:ok)

expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
[bar_only_post.id, baz_only_post.id]
)
end

it 'supports alias in not search' do
get '/posts', params: { tags: 'not:not_spec_foo_alias' }

expect(response).to have_http_status(:ok)

expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
[bar_only_post.id, baz_only_post.id]
)
end

it 'treats multiple not terms as AND when match is omitted' do
get '/posts', params: { tags: 'not:not_spec_foo not:not_spec_bar' }

expect(response).to have_http_status(:ok)

expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
[baz_only_post.id]
)
end

it 'treats multiple not terms as OR when match=any' do
get '/posts', params: { tags: 'not:not_spec_foo not:not_spec_bar', match: 'any' }

expect(response).to have_http_status(:ok)

expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
[foo_only_post.id, bar_only_post.id, baz_only_post.id, foo_baz_post.id]
)
end

it 'supports mixed positive and negative search with AND' do
get '/posts', params: { tags: 'not_spec_foo not:not_spec_bar' }

expect(response).to have_http_status(:ok)

expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
[foo_only_post.id, foo_baz_post.id]
)
end

it 'supports mixed positive and negative search with OR when match=any' do
get '/posts', params: { tags: 'not_spec_foo not:not_spec_bar', match: 'any' }

expect(response).to have_http_status(:ok)

expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
[foo_only_post.id, baz_only_post.id, foo_bar_post.id, foo_baz_post.id]
)
end
end

context 'when url is provided' do
let!(:url_hit_post) do
Post.create!(uploaded_user: user, title: 'url hit',
@@ -409,7 +526,7 @@ RSpec.describe 'Posts API', type: :request do
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')
category: :nico)
end

it 'return 400' do
@@ -475,7 +592,7 @@ RSpec.describe 'Posts API', type: :request do

# 追加で別タグも作って、更新時に入れ替わることを見る
tn2 = TagName.create!(name: 'spec_tag_2')
Tag.create!(tag_name: tn2, category: 'general')
Tag.create!(tag_name: tn2, category: :general)

put "/posts/#{post_record.id}", params: {
title: 'updated title',
@@ -494,7 +611,7 @@ RSpec.describe 'Posts API', type: :request do
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')
category: :nico)
end

it 'return 400' do
@@ -531,7 +648,7 @@ RSpec.describe 'Posts API', type: :request do
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')
tag2 = Tag.create!(tag_name: tn2, category: :general)
pt = PostTag.create!(post: post_record, tag: tag2, created_user: member)

# remove (discard)


+ 20
- 5
frontend/src/components/TagSearch.tsx View File

@@ -56,8 +56,11 @@ export default (() => {
if (activeIndex < 0)
break
ev.preventDefault ()
const selected = suggestions[activeIndex]
selected && handleTagSelect (selected)
{
const selected = suggestions[activeIndex]
if (selected)
handleTagSelect (selected)
}
break

case 'Escape':
@@ -67,14 +70,25 @@ export default (() => {
}
if (ev.key === 'Enter' && (!(suggestionsVsbl) || activeIndex < 0))
{
navigate (`/posts?${ (new URLSearchParams ({ tags: search })).toString () }`)
const parts = search.split (' ')
const match = parts.map (t => t.toLowerCase ()).includes ('type:or') ? 'any' : 'all'
navigate (`/posts?${
(new URLSearchParams ({
tags: parts.map (t => t.toLowerCase ())
.filter (t => t !== 'type:or')
.join (' '),
match }))
.toString () }`)
setSuggestionsVsbl (false)
}
}

const handleTagSelect = (tag: Tag) => {
const parts = search.split (' ')
parts[parts.length - 1] = tag.name
parts[parts.length - 1] = (
(parts[parts.length - 1].slice(0, 4).toLowerCase () === 'not:')
? ('not:' + tag.name)
: tag.name)
setSearch (parts.join (' ') + ' ')
setSuggestions ([])
setActiveIndex (-1)
@@ -82,7 +96,8 @@ export default (() => {

useEffect (() => {
const query = new URLSearchParams (location.search)
const tagsQuery = query.get ('tags') ?? ''
const anyFlg = query.get ('match') === 'any'
const tagsQuery = `${ query.get ('tags') ?? '' }${ anyFlg ? ' type:or' : '' }`
setSearch (tagsQuery)
}, [location.search])



Loading…
Cancel
Save