From 3155d0ee40d51a000400f52d0c882669c41b559d Mon Sep 17 00:00:00 2001 From: miteruzo Date: Sun, 8 Mar 2026 18:05:38 +0900 Subject: [PATCH 1/3] =?UTF-8?q?#68=20=E3=81=BE=E3=81=A5=E3=81=AF=20NOT=20?= =?UTF-8?q?=E3=81=AB=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/controllers/posts_controller.rb | 40 ++++++++++++++++----- backend/app/controllers/tags_controller.rb | 3 +- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/backend/app/controllers/posts_controller.rb b/backend/app/controllers/posts_controller.rb index f9f68f5..75b380b 100644 --- a/backend/app/controllers/posts_controller.rb +++ b/backend/app/controllers/posts_controller.rb @@ -84,7 +84,7 @@ class PostsController < ApplicationController 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] @@ -125,7 +125,7 @@ class PostsController < ApplicationController return head :forbidden unless current_user.gte_member? title = params[:title].presence - 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] @@ -192,7 +192,7 @@ class PostsController < ApplicationController private def filtered_posts - tag_names = params[:tags].to_s.split(' ') + tag_names = params[:tags].to_s.split match_type = params[:match] if tag_names.present? filter_posts_by_tags(tag_names, match_type) @@ -202,19 +202,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? diff --git a/backend/app/controllers/tags_controller.rb b/backend/app/controllers/tags_controller.rb index bf4c821..b97fcaa 100644 --- a/backend/app/controllers/tags_controller.rb +++ b/backend/app/controllers/tags_controller.rb @@ -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) -- 2.34.1 From 6b519381b58384ef6d13df960f9613d6b75141fa Mon Sep 17 00:00:00 2001 From: miteruzo Date: Sun, 8 Mar 2026 19:23:15 +0900 Subject: [PATCH 2/3] #68 --- frontend/src/components/TagSearch.tsx | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/frontend/src/components/TagSearch.tsx b/frontend/src/components/TagSearch.tsx index 6e7a8bd..c46905b 100644 --- a/frontend/src/components/TagSearch.tsx +++ b/frontend/src/components/TagSearch.tsx @@ -54,8 +54,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': @@ -65,14 +68,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) @@ -80,7 +94,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]) -- 2.34.1 From 3a13e1b5b2ff2e722cbe5ccac011928214b3ac1d Mon Sep 17 00:00:00 2001 From: miteruzo Date: Sun, 8 Mar 2026 22:40:33 +0900 Subject: [PATCH 3/3] =?UTF-8?q?#68=20=E3=83=86=E3=82=B9=E3=83=88=E3=83=BB?= =?UTF-8?q?=E3=82=B1=E3=83=BC=E3=82=B9=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/spec/requests/posts_spec.rb | 131 ++++++++++++++++++++++++++-- 1 file changed, 124 insertions(+), 7 deletions(-) diff --git a/backend/spec/requests/posts_spec.rb b/backend/spec/requests/posts_spec.rb index 7ccdb52..71973d4 100644 --- a/backend/spec/requests/posts_spec.rb +++ b/backend/spec/requests/posts_spec.rb @@ -15,7 +15,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| @@ -27,9 +27,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 @@ -114,6 +114,123 @@ RSpec.describe 'Posts API', type: :request do expect(json.fetch('count')).to eq(0) 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 end describe 'GET /posts/:id' do @@ -210,7 +327,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 @@ -276,7 +393,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', @@ -295,7 +412,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 @@ -332,7 +449,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) -- 2.34.1