From 23d67d21009964c636cd69c06fb99da8a666af08 Mon Sep 17 00:00:00 2001 From: Jakub Macina Date: Mon, 5 Jun 2017 18:00:15 +0200 Subject: [PATCH 1/4] Add includes image choice to advanced search ui. --- .../discourse/components/search-advanced-options.js.es6 | 3 ++- config/locales/client.en.yml | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/discourse/components/search-advanced-options.js.es6 b/app/assets/javascripts/discourse/components/search-advanced-options.js.es6 index 0bc886a8178..1ffd1cdaead 100644 --- a/app/assets/javascripts/discourse/components/search-advanced-options.js.es6 +++ b/app/assets/javascripts/discourse/components/search-advanced-options.js.es6 @@ -14,7 +14,7 @@ const REGEXP_MIN_POST_COUNT_PREFIX = /^min_post_count:/ig; const REGEXP_POST_TIME_PREFIX = /^(before|after):/ig; const REGEXP_TAGS_REPLACE = /(^(tags?:|#(?=[a-z0-9\-]+::tag))|::tag\s?$)/ig; -const REGEXP_IN_MATCH = /^in:(posted|watching|tracking|bookmarks|first|pinned|unpinned|wiki|unseen)/ig; +const REGEXP_IN_MATCH = /^in:(posted|watching|tracking|bookmarks|first|pinned|unpinned|wiki|unseen|image)/ig; const REGEXP_SPECIAL_IN_LIKES_MATCH = /^in:likes/ig; const REGEXP_SPECIAL_IN_PRIVATE_MATCH = /^in:private/ig; const REGEXP_SPECIAL_IN_SEEN_MATCH = /^in:seen/ig; @@ -36,6 +36,7 @@ export default Em.Component.extend({ {name: I18n.t('search.advanced.filters.pinned'), value: "pinned"}, {name: I18n.t('search.advanced.filters.unpinned'), value: "unpinned"}, {name: I18n.t('search.advanced.filters.wiki'), value: "wiki"}, + {name: I18n.t('search.advanced.filters.images'), value: "image"}, ], statusOptions: [ {name: I18n.t('search.advanced.statuses.open'), value: "open"}, diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 52b4e5301f1..71532ab95d8 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1344,6 +1344,7 @@ en: seen: I've read unseen: I've not read wiki: are wiki + images: includes image statuses: label: Where topics open: are open From 8f7d81fde64a756a227e46746af22b99fa922bbc Mon Sep 17 00:00:00 2001 From: Jakub Macina Date: Tue, 6 Jun 2017 14:39:53 +0200 Subject: [PATCH 2/4] Add rspec test for searching posts with images. --- config/locales/client.en.yml | 2 +- spec/components/search_spec.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 71532ab95d8..c2268368f51 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1344,7 +1344,7 @@ en: seen: I've read unseen: I've not read wiki: are wiki - images: includes image + images: includes image(s) statuses: label: Where topics open: are open diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb index d215c8835c3..069f602065d 100644 --- a/spec/components/search_spec.rb +++ b/spec/components/search_spec.rb @@ -628,6 +628,16 @@ describe Search do end + it 'can find posts with images' do + post_uploaded = Fabricate(:post_with_uploaded_image) + post_with_image_urls = Fabricate(:post_with_image_urls) + Fabricate(:post) + TopicLink.extract_from(post_uploaded) + TopicLink.extract_from(post_with_image_urls) + + expect(Search.execute('in:image').posts.map(&:id).sort).to eq([post_uploaded.id, post_with_image_urls.id].sort) + end + it 'can find by latest' do topic1 = Fabricate(:topic, title: 'I do not like that Sam I am') post1 = Fabricate(:post, topic: topic1) From 76712da16697abc24e78bf5748bbb180172f8d3d Mon Sep 17 00:00:00 2001 From: Jakub Macina Date: Wed, 7 Jun 2017 20:13:36 +0200 Subject: [PATCH 3/4] Add backend code for searching posts with images. --- lib/search.rb | 4 ++++ spec/components/search_spec.rb | 5 +++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/search.rb b/lib/search.rb index 543149b18e2..156c30c77a3 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -338,6 +338,10 @@ class Search end end + advanced_filter(/in:image/) do |posts| + posts.where("posts.image_url IS NOT NULL") + end + advanced_filter(/category:(.+)/) do |posts,match| exact = false diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb index 069f602065d..2f9f32b07b8 100644 --- a/spec/components/search_spec.rb +++ b/spec/components/search_spec.rb @@ -632,8 +632,9 @@ describe Search do post_uploaded = Fabricate(:post_with_uploaded_image) post_with_image_urls = Fabricate(:post_with_image_urls) Fabricate(:post) - TopicLink.extract_from(post_uploaded) - TopicLink.extract_from(post_with_image_urls) + + CookedPostProcessor.new(post_uploaded).update_post_image + CookedPostProcessor.new(post_with_image_urls).update_post_image expect(Search.execute('in:image').posts.map(&:id).sort).to eq([post_uploaded.id, post_with_image_urls.id].sort) end From bf002e0873b75ec5a6f252187b61bbaf299caaa7 Mon Sep 17 00:00:00 2001 From: Jakub Macina Date: Fri, 9 Jun 2017 13:16:50 +0200 Subject: [PATCH 4/4] Add extraction of image_url for oneboxed images. Fix search by images filter name. --- .../components/search-advanced-options.js.es6 | 16 +++++++++++----- lib/cooked_post_processor.rb | 8 ++++---- lib/search.rb | 2 +- spec/components/cooked_post_processor_spec.rb | 4 ++-- spec/components/search_spec.rb | 2 +- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/discourse/components/search-advanced-options.js.es6 b/app/assets/javascripts/discourse/components/search-advanced-options.js.es6 index 1ffd1cdaead..bc345a5977a 100644 --- a/app/assets/javascripts/discourse/components/search-advanced-options.js.es6 +++ b/app/assets/javascripts/discourse/components/search-advanced-options.js.es6 @@ -8,13 +8,13 @@ const REGEXP_CATEGORY_PREFIX = /^(category:|#)/ig; const REGEXP_GROUP_PREFIX = /^group:/ig; const REGEXP_BADGE_PREFIX = /^badge:/ig; const REGEXP_TAGS_PREFIX = /^(tags?:|#(?=[a-z0-9\-]+::tag))/ig; -const REGEXP_IN_PREFIX = /^in:/ig; +const REGEXP_IN_PREFIX = /^(in|with):/ig; const REGEXP_STATUS_PREFIX = /^status:/ig; const REGEXP_MIN_POST_COUNT_PREFIX = /^min_post_count:/ig; const REGEXP_POST_TIME_PREFIX = /^(before|after):/ig; const REGEXP_TAGS_REPLACE = /(^(tags?:|#(?=[a-z0-9\-]+::tag))|::tag\s?$)/ig; -const REGEXP_IN_MATCH = /^in:(posted|watching|tracking|bookmarks|first|pinned|unpinned|wiki|unseen|image)/ig; +const REGEXP_IN_MATCH = /^(in|with):(posted|watching|tracking|bookmarks|first|pinned|unpinned|wiki|unseen|image)/ig; const REGEXP_SPECIAL_IN_LIKES_MATCH = /^in:likes/ig; const REGEXP_SPECIAL_IN_PRIVATE_MATCH = /^in:private/ig; const REGEXP_SPECIAL_IN_SEEN_MATCH = /^in:seen/ig; @@ -23,6 +23,8 @@ const REGEXP_CATEGORY_SLUG = /^(\#[a-zA-Z0-9\-:]+)/ig; const REGEXP_CATEGORY_ID = /^(category:[0-9]+)/ig; const REGEXP_POST_TIME_WHEN = /^(before|after)/ig; +const IN_OPTIONS_MAPPING = {'images': 'with'}; + export default Em.Component.extend({ classNames: ['search-advanced-options'], @@ -36,7 +38,7 @@ export default Em.Component.extend({ {name: I18n.t('search.advanced.filters.pinned'), value: "pinned"}, {name: I18n.t('search.advanced.filters.unpinned'), value: "unpinned"}, {name: I18n.t('search.advanced.filters.wiki'), value: "wiki"}, - {name: I18n.t('search.advanced.filters.images'), value: "image"}, + {name: I18n.t('search.advanced.filters.images'), value: "images"}, ], statusOptions: [ {name: I18n.t('search.advanced.statuses.open'), value: "open"}, @@ -392,13 +394,17 @@ export default Em.Component.extend({ updateSearchTermForIn() { const match = this.filterBlocks(REGEXP_IN_MATCH); const inFilter = this.get('searchedTerms.in'); + let keyword = 'in'; + if(inFilter in IN_OPTIONS_MAPPING) { + keyword = IN_OPTIONS_MAPPING[inFilter]; + } let searchTerm = this.get('searchTerm') || ''; if (inFilter) { if (match.length !== 0) { - searchTerm = searchTerm.replace(match[0], `in:${inFilter}`); + searchTerm = searchTerm.replace(match[0], `${keyword}:${inFilter}`); } else { - searchTerm += ` in:${inFilter}`; + searchTerm += ` ${keyword}:${inFilter}`; } this.set('searchTerm', searchTerm.trim()); diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index 48f1d38b494..43bb9567719 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -76,8 +76,6 @@ class CookedPostProcessor limit_size!(img) convert_to_link!(img) end - - update_post_image end def extract_images @@ -98,8 +96,6 @@ class CookedPostProcessor @doc.css("img[src]") - # minus, emojis @doc.css("img.emoji") - - # minus, image inside oneboxes - oneboxed_images - # minus, images inside quotes @doc.css(".quote img") end @@ -283,6 +279,8 @@ class CookedPostProcessor def update_post_image img = extract_images_for_post.first + return if img.blank? + if img["src"].present? @post.update_column(:image_url, img["src"][0...255]) # post @post.topic.update_column(:image_url, img["src"][0...255]) if @post.is_first_post? # topic @@ -301,6 +299,8 @@ class CookedPostProcessor Oneboxer.onebox(url, args) end + update_post_image + # make sure we grab dimensions for oneboxed images oneboxed_images.each { |img| limit_size!(img) } diff --git a/lib/search.rb b/lib/search.rb index 156c30c77a3..b3f886af59b 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -338,7 +338,7 @@ class Search end end - advanced_filter(/in:image/) do |posts| + advanced_filter(/with:images/) do |posts| posts.where("posts.image_url IS NOT NULL") end diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index cc0f5f4467e..dc58e4b8551 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -249,7 +249,7 @@ describe CookedPostProcessor do it "adds a topic image if there's one in the first post" do FastImage.stubs(:size) expect(post.topic.image_url).to eq(nil) - cpp.post_process_images + cpp.update_post_image post.topic.reload expect(post.topic.image_url).to be_present end @@ -262,7 +262,7 @@ describe CookedPostProcessor do it "adds a post image if there's one in the post" do FastImage.stubs(:size) expect(reply.image_url).to eq(nil) - cpp.post_process_images + cpp.update_post_image reply.reload expect(reply.image_url).to be_present end diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb index 2f9f32b07b8..c5357481676 100644 --- a/spec/components/search_spec.rb +++ b/spec/components/search_spec.rb @@ -636,7 +636,7 @@ describe Search do CookedPostProcessor.new(post_uploaded).update_post_image CookedPostProcessor.new(post_with_image_urls).update_post_image - expect(Search.execute('in:image').posts.map(&:id).sort).to eq([post_uploaded.id, post_with_image_urls.id].sort) + expect(Search.execute('with:images').posts.map(&:id)).to contain_exactly(post_uploaded.id, post_with_image_urls.id) end it 'can find by latest' do