From 51eb1115035499a47bb03670c03afbd03ce7c5ac Mon Sep 17 00:00:00 2001 From: ThibG Date: Wed, 8 Jan 2020 22:42:05 +0100 Subject: Allow blocking TLDs, and fix TLD blocks not being editable (#12805) Fixes #12795 It was already possible to create domain blocks for TLDs, but those weren't enforced, nor editable. This commit changes it so that they are enforced and editable. --- spec/models/domain_block_spec.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'spec') diff --git a/spec/models/domain_block_spec.rb b/spec/models/domain_block_spec.rb index d98c5e118..28647dc89 100644 --- a/spec/models/domain_block_spec.rb +++ b/spec/models/domain_block_spec.rb @@ -52,6 +52,16 @@ RSpec.describe DomainBlock, type: :model do block = Fabricate(:domain_block, domain: 'sub.example.com') expect(DomainBlock.rule_for('sub.example.com')).to eq block end + + it 'returns a rule matching a blocked TLD' do + block = Fabricate(:domain_block, domain: 'google') + expect(DomainBlock.rule_for('google')).to eq block + end + + it 'returns a rule matching a subdomain of a blocked TLD' do + block = Fabricate(:domain_block, domain: 'google') + expect(DomainBlock.rule_for('maps.google')).to eq block + end end describe '#stricter_than?' do -- cgit From 05756c9a14864655ae6777505a4ee5cfa9b0ee93 Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 10 Jan 2020 22:58:16 +0100 Subject: improve status title (#8596) * improve shown status title, useful for atom/rss * use single quotes to satisfy codeclimate * fix tests, make message more pretty * fix tests * fix codestyle * fix codestyle * remove atom_serializer_spec Co-authored-by: Yamagishi Kazutoshi --- app/models/status.rb | 6 +++++- spec/models/status_spec.rb | 12 ++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) (limited to 'spec') diff --git a/app/models/status.rb b/app/models/status.rb index 1cb381400..670109762 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -194,8 +194,12 @@ class Status < ApplicationRecord def title if destroyed? "#{account.acct} deleted status" + elsif reblog? + preview = sensitive ? '' : text.slice(0, 10).split("\n")[0] + "#{account.acct} shared #{reblog.account.acct}'s: #{preview}" else - reblog? ? "#{account.acct} shared a status by #{reblog.account.acct}" : "New status by #{account.acct}" + preview = sensitive ? '' : text.slice(0, 20).split("\n")[0] + "#{account.acct}: #{preview}" end end diff --git a/spec/models/status_spec.rb b/spec/models/status_spec.rb index 51a10cd17..b238691a8 100644 --- a/spec/models/status_spec.rb +++ b/spec/models/status_spec.rb @@ -96,16 +96,20 @@ RSpec.describe Status, type: :model do context 'unless destroyed?' do context 'if reblog?' do - it 'returns "#{account.acct} shared a status by #{reblog.account.acct}"' do + it 'returns "#{account.acct} shared #{reblog.account.acct}\'s: #{preview}"' do reblog = subject.reblog = other - expect(subject.title).to eq "#{account.acct} shared a status by #{reblog.account.acct}" + preview = subject.text.slice(0, 10).split("\n")[0] + expect(subject.title).to( + eq "#{account.acct} shared #{reblog.account.acct}'s: #{preview}" + ) end end context 'unless reblog?' do - it 'returns "New status by #{account.acct}"' do + it 'returns "#{account.acct}: #{preview}"' do subject.reblog = nil - expect(subject.title).to eq "New status by #{account.acct}" + preview = subject.text.slice(0, 20).split("\n")[0] + expect(subject.title).to eq "#{account.acct}: #{preview}" end end end -- cgit From ea436b355bd844c86a4f4ddfd204b9bf15a1db6c Mon Sep 17 00:00:00 2001 From: ThibG Date: Sat, 11 Jan 2020 02:15:25 +0100 Subject: Add support for linking XMPP URIs in toots (#12709) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix wrong grouping in Twitter valid_url regex * Add support for xmpp URIs Fixes #9776 The difficult part is autolinking, because Twitter-text's extractor does some pretty ad-hoc stuff to find things that “look like” URLs, and XMPP URIs do not really match the assumptions of that lib, so it doesn't sound wise to try to shoehorn it into the existing regex. This is why I used a specific regex (very close, although slightly more permissive than the RFC), and a specific scan function (a simplified version of the generalized one from Twitter). * Remove leading “xmpp:” from auto-linked text --- app/lib/formatter.rb | 5 ++-- app/lib/sanitize_config.rb | 2 +- config/initializers/twitter_regex.rb | 51 +++++++++++++++++++++++++++++++++++- spec/lib/formatter_spec.rb | 16 +++++++++++ 4 files changed, 70 insertions(+), 4 deletions(-) (limited to 'spec') diff --git a/app/lib/formatter.rb b/app/lib/formatter.rb index 6ba327614..c771dcaaa 100644 --- a/app/lib/formatter.rb +++ b/app/lib/formatter.rb @@ -245,8 +245,9 @@ class Formatter end standard = Extractor.extract_entities_with_indices(text, options) + xmpp = Extractor.extract_xmpp_uris_with_indices(text, options) - Extractor.remove_overlapping_entities(special + standard) + Extractor.remove_overlapping_entities(special + standard + xmpp) end def link_to_url(entity, options = {}) @@ -284,7 +285,7 @@ class Formatter def link_html(url) url = Addressable::URI.parse(url).to_s - prefix = url.match(/\Ahttps?:\/\/(www\.)?/).to_s + prefix = url.match(/\A(https?:\/\/(www\.)?|xmpp:)/).to_s text = url[prefix.length, 30] suffix = url[prefix.length + 30..-1] cutoff = url[prefix.length..-1].length > 30 diff --git a/app/lib/sanitize_config.rb b/app/lib/sanitize_config.rb index 77045155e..e2480376e 100644 --- a/app/lib/sanitize_config.rb +++ b/app/lib/sanitize_config.rb @@ -2,7 +2,7 @@ class Sanitize module Config - HTTP_PROTOCOLS ||= ['http', 'https', 'dat', 'dweb', 'ipfs', 'ipns', 'ssb', 'gopher', :relative].freeze + HTTP_PROTOCOLS ||= ['http', 'https', 'dat', 'dweb', 'ipfs', 'ipns', 'ssb', 'gopher', 'xmpp', :relative].freeze CLASS_WHITELIST_TRANSFORMER = lambda do |env| node = env[:node] diff --git a/config/initializers/twitter_regex.rb b/config/initializers/twitter_regex.rb index 0ddbbee98..87815d458 100644 --- a/config/initializers/twitter_regex.rb +++ b/config/initializers/twitter_regex.rb @@ -29,7 +29,7 @@ module Twitter ( # $1 total match (#{REGEXEN[:valid_url_preceding_chars]}) # $2 Preceding character ( # $3 URL - ((https?|dat|dweb|ipfs|ipns|ssb|gopher):\/\/)? # $4 Protocol (optional) + ((?:https?|dat|dweb|ipfs|ipns|ssb|gopher):\/\/)? # $4 Protocol (optional) (#{REGEXEN[:valid_domain]}) # $5 Domain(s) (?::(#{REGEXEN[:valid_port_number]}))? # $6 Port number (optional) (/#{REGEXEN[:valid_url_path]}*)? # $7 URL Path and anchor @@ -37,5 +37,54 @@ module Twitter ) ) }iox + REGEXEN[:validate_nodeid] = /(?: + #{REGEXEN[:validate_url_unreserved]}| + #{REGEXEN[:validate_url_pct_encoded]}| + [!$()*+,;=] + )/iox + REGEXEN[:validate_resid] = /(?: + #{REGEXEN[:validate_url_unreserved]}| + #{REGEXEN[:validate_url_pct_encoded]}| + #{REGEXEN[:validate_url_sub_delims]} + )/iox + REGEXEN[:valid_xmpp_uri] = %r{ + ( # $1 total match + (#{REGEXEN[:valid_url_preceding_chars]}) # $2 Preceding character + ( # $3 URL + ((?:xmpp):) # $4 Protocol + (//#{REGEXEN[:validate_nodeid]}+@#{REGEXEN[:valid_domain]}/)? # $5 Authority (optional) + (#{REGEXEN[:validate_nodeid]}+@)? # $6 Username in path (optional) + (#{REGEXEN[:valid_domain]}) # $7 Domain in path + (/#{REGEXEN[:validate_resid]}+)? # $8 Resource in path (optional) + (\?#{REGEXEN[:valid_url_query_chars]}*#{REGEXEN[:valid_url_query_ending_chars]})? # $9 Query String + ) + ) + }iox + end + + module Extractor + # Extracts a list of all XMPP URIs included in the Tweet text along + # with the indices. If the text is nil or contains no + # XMPP URIs an empty array will be returned. + # + # If a block is given then it will be called for each XMPP URI. + def extract_xmpp_uris_with_indices(text, options = {}) # :yields: uri, start, end + return [] unless text && text.index(":") + urls = [] + + text.to_s.scan(Twitter::Regex[:valid_xmpp_uri]) do + valid_uri_match_data = $~ + + start_position = valid_uri_match_data.char_begin(3) + end_position = valid_uri_match_data.char_end(3) + + urls << { + :url => valid_uri_match_data[3], + :indices => [start_position, end_position] + } + end + urls.each{|url| yield url[:url], url[:indices].first, url[:indices].last} if block_given? + urls + end end end diff --git a/spec/lib/formatter_spec.rb b/spec/lib/formatter_spec.rb index b8108a247..83be0a588 100644 --- a/spec/lib/formatter_spec.rb +++ b/spec/lib/formatter_spec.rb @@ -242,6 +242,22 @@ RSpec.describe Formatter do is_expected.to include '/tags/hashtag%E3%82%BF%E3%82%B0" class="mention hashtag" rel="tag">#hashtagタグ' end end + + context 'given a stand-alone xmpp: URI' do + let(:text) { 'xmpp:user@instance.com' } + + it 'matches the full URI' do + is_expected.to include 'href="xmpp:user@instance.com"' + end + end + + context 'given a an xmpp: URI with a query-string' do + let(:text) { 'please join xmpp:muc@instance.com?join right now' } + + it 'matches the full URI' do + is_expected.to include 'href="xmpp:muc@instance.com?join"' + end + end end describe '#format_spoiler' do -- cgit From 57e2833f6a34c78c933b2941305eac8995c115e8 Mon Sep 17 00:00:00 2001 From: ThibG Date: Sat, 11 Jan 2020 21:36:53 +0100 Subject: Remove dependency on OStatus2 gem (#12822) --- Gemfile | 1 - Gemfile.lock | 4 ---- app/models/account.rb | 4 ---- spec/models/account_spec.rb | 7 ------- 4 files changed, 16 deletions(-) (limited to 'spec') diff --git a/Gemfile b/Gemfile index 2ebea745d..f0adf610c 100644 --- a/Gemfile +++ b/Gemfile @@ -68,7 +68,6 @@ gem 'nilsimsa', git: 'https://github.com/witgo/nilsimsa', ref: 'fd184883048b922b gem 'nokogiri', '~> 1.10' gem 'nsa', '~> 0.2' gem 'oj', '~> 3.10' -gem 'ostatus2', '~> 2.0' gem 'ox', '~> 2.11' gem 'parslet' gem 'parallel', '~> 1.19' diff --git a/Gemfile.lock b/Gemfile.lock index b70c59ad5..650d5ef85 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -409,10 +409,6 @@ GEM omniauth (~> 1.3, >= 1.3.2) ruby-saml (~> 1.7) orm_adapter (0.5.0) - ostatus2 (2.0.3) - addressable (~> 2.5) - http (~> 3.0) - nokogiri (~> 1.8) ox (2.11.0) paperclip (6.0.0) activemodel (>= 4.2.0) diff --git a/app/models/account.rb b/app/models/account.rb index feaf273c1..1e8abe6ec 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -314,10 +314,6 @@ class Account < ApplicationRecord self.fields = tmp end - def subscription(webhook_url) - @subscription ||= OStatus2::Subscription.new(remote_url, secret: secret, webhook: webhook_url, hub: hub_url) - end - def save_with_optional_media! save! rescue ActiveRecord::RecordInvalid diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index 3cca9b343..4266122b2 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -215,13 +215,6 @@ RSpec.describe Account, type: :model do end end - describe '#subscription' do - it 'returns an OStatus subscription' do - account = Fabricate(:account) - expect(account.subscription('')).to be_instance_of OStatus2::Subscription - end - end - describe '#object_type' do it 'is always a person' do account = Fabricate(:account) -- cgit