From dc8c5fbf824b73e5bf149579ef0e7c2fb7442abc Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 18 Oct 2023 20:01:07 +0200 Subject: [PATCH 1/7] Add option to omit URL to `prepare_tweet` helper --- app/helpers/social_helper/twitter_methods.rb | 4 ++-- spec/helpers/social_helper/twitter_methods_spec.rb | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/helpers/social_helper/twitter_methods.rb b/app/helpers/social_helper/twitter_methods.rb index 7f025f71..716060f8 100644 --- a/app/helpers/social_helper/twitter_methods.rb +++ b/app/helpers/social_helper/twitter_methods.rb @@ -3,7 +3,7 @@ require 'cgi' module SocialHelper::TwitterMethods include MarkdownHelper - def prepare_tweet(answer, post_tag = nil) + def prepare_tweet(answer, post_tag = nil, omit_url = false) question_content = twitter_markdown answer.question.content.gsub(/\@(\w+)/, '\1') original_question_length = question_content.length answer_content = twitter_markdown answer.content @@ -13,7 +13,7 @@ module SocialHelper::TwitterMethods username: answer.user.screen_name, host: APP_CONFIG['hostname'], protocol: (APP_CONFIG['https'] ? :https : :http) - ) + ) unless omit_url parsed_tweet = { :valid => false } tweet_text = "" diff --git a/spec/helpers/social_helper/twitter_methods_spec.rb b/spec/helpers/social_helper/twitter_methods_spec.rb index 7074f6e6..ad630bc4 100644 --- a/spec/helpers/social_helper/twitter_methods_spec.rb +++ b/spec/helpers/social_helper/twitter_methods_spec.rb @@ -38,6 +38,17 @@ describe SocialHelper::TwitterMethods, :type => :helper do end end + context "when the url should be omitted" do + let(:question_content) { "question" } + let(:answer_content) { "answer" } + + subject { prepare_tweet(answer, nil, true) } + + it "should include the suffix after the link" do + expect(subject).to eq("question — answer") + end + end + context 'when a suffix has been passed and the tweet needs to be shortened' do subject { prepare_tweet(answer, '#askracc') } @@ -69,4 +80,4 @@ describe SocialHelper::TwitterMethods, :type => :helper do expect(subject).to eq("https://twitter.com/intent/tweet?text=#{CGI.escape(prepare_tweet(answer))}") end end -end \ No newline at end of file +end From 8b86e2f1233aab000bcde620daaae585feec9406 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 18 Oct 2023 20:01:59 +0200 Subject: [PATCH 2/7] Set text and URL separately for `navigator.share` Apparently for most mobile OSses `url` is a required share option --- app/controllers/ajax/answer_controller.rb | 6 +++--- app/javascript/retrospring/features/inbox/entry/answer.ts | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/controllers/ajax/answer_controller.rb b/app/controllers/ajax/answer_controller.rb index 0c401638..65f8f29d 100644 --- a/app/controllers/ajax/answer_controller.rb +++ b/app/controllers/ajax/answer_controller.rb @@ -3,9 +3,7 @@ require "cgi" class Ajax::AnswerController < AjaxController - include SocialHelper::TwitterMethods - include SocialHelper::TumblrMethods - include SocialHelper::TelegramMethods + include SocialHelper def create params.require :id @@ -73,6 +71,8 @@ class Ajax::AnswerController < AjaxController private def sharing_hash(answer) = { + url: answer_share_url(answer), + text: prepare_tweet(answer, nil, true), twitter: twitter_share_url(answer), tumblr: tumblr_share_url(answer), telegram: telegram_share_url(answer), diff --git a/app/javascript/retrospring/features/inbox/entry/answer.ts b/app/javascript/retrospring/features/inbox/entry/answer.ts index c515dc30..b5a142ab 100644 --- a/app/javascript/retrospring/features/inbox/entry/answer.ts +++ b/app/javascript/retrospring/features/inbox/entry/answer.ts @@ -32,7 +32,8 @@ export function answerEntryHandler(event: Event): void { const shareButton = inboxEntry.querySelector('[data-controller="share"]'); if (shareButton != null) { - shareButton.dataset.shareTextValue = decodeURIComponent(data.sharing.custom).replaceAll('+', ' '); + shareButton.dataset.shareUrlValue = data.sharing.url; + shareButton.dataset.shareTextValue = data.sharing.text; } const sharing = inboxEntry.querySelector('.inbox-entry__sharing'); From bc45bda5178cb802d6468c74427d2b9ed99bbbbd Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 18 Oct 2023 20:04:14 +0200 Subject: [PATCH 3/7] Only remove inbox entry after share has been triggered --- .../retrospring/controllers/inbox_sharing_controller.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/javascript/retrospring/controllers/inbox_sharing_controller.ts b/app/javascript/retrospring/controllers/inbox_sharing_controller.ts index d087d62d..dcc7b44f 100644 --- a/app/javascript/retrospring/controllers/inbox_sharing_controller.ts +++ b/app/javascript/retrospring/controllers/inbox_sharing_controller.ts @@ -23,7 +23,7 @@ export default class extends Controller { this.twitterTarget.addEventListener('click', () => this.close()); this.tumblrTarget.addEventListener('click', () => this.close()); this.telegramTarget.addEventListener('click', () => this.close()); - this.otherTarget.addEventListener('click', () => this.close()); + this.otherTarget.addEventListener('click', () => this.closeAfterShare()); if (this.hasCustomTarget) { this.customTarget.addEventListener('click', () => this.close()); @@ -50,4 +50,8 @@ export default class extends Controller { close(): void { (this.element.closest(".inbox-entry")).remove(); } + + closeAfterShare(): void { + this.otherTarget.addEventListener('retrospring:shared', () => this.close()); + } } From 7156dc5c9b629faab8bc123aabdea262a85680b7 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 18 Oct 2023 20:04:41 +0200 Subject: [PATCH 4/7] Catch `navigator.share` errors with a no-op --- .../retrospring/controllers/share_controller.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/javascript/retrospring/controllers/share_controller.ts b/app/javascript/retrospring/controllers/share_controller.ts index e9e8d250..c1b5d350 100644 --- a/app/javascript/retrospring/controllers/share_controller.ts +++ b/app/javascript/retrospring/controllers/share_controller.ts @@ -1,4 +1,5 @@ import { Controller } from '@hotwired/stimulus'; +import noop from 'utilities/noop'; export default class extends Controller { static values = { @@ -35,8 +36,10 @@ export default class extends Controller { }; } - navigator.share(shareConfiguration).then(() => { - this.element.dispatchEvent(new CustomEvent('retrospring:shared')); - }); + navigator.share(shareConfiguration) + .then(() => { + this.element.dispatchEvent(new CustomEvent('retrospring:shared')); + }) + .catch(noop); } } From 274d480ba82aeae8c03ab053c4e03ca7c50f1595 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 18 Oct 2023 20:54:41 +0200 Subject: [PATCH 5/7] Adjust specs for AJAX answer endpoint --- spec/controllers/ajax/answer_controller_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spec/controllers/ajax/answer_controller_spec.rb b/spec/controllers/ajax/answer_controller_spec.rb index 2a8906ca..fd6b50ca 100644 --- a/spec/controllers/ajax/answer_controller_spec.rb +++ b/spec/controllers/ajax/answer_controller_spec.rb @@ -91,6 +91,8 @@ describe Ajax::AnswerController, :ajax_controller, type: :controller do let(:expected_response) do super().merge( "sharing" => { + "url" => a_string_matching("https://#{APP_CONFIG['hostname']}/"), + "text" => a_string_matching("Werfen Sie nicht länger das Fenster zum Geld hinaus!"), "twitter" => a_string_matching("https://twitter.com/"), "tumblr" => a_string_matching("https://www.tumblr.com/"), "telegram" => a_string_matching("https://t.me/"), @@ -170,6 +172,8 @@ describe Ajax::AnswerController, :ajax_controller, type: :controller do let(:expected_response) do super().merge( "sharing" => { + "url" => a_string_matching("https://#{APP_CONFIG['hostname']}/"), + "text" => a_string_matching("Werfen Sie nicht länger das Fenster zum Geld hinaus!"), "twitter" => a_string_matching("https://twitter.com/"), "tumblr" => a_string_matching("https://www.tumblr.com/"), "telegram" => a_string_matching("https://t.me/"), From f76c4e236b5a7f4350a0a464c9f95f9fdca84f8b Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 18 Oct 2023 21:07:17 +0200 Subject: [PATCH 6/7] Fix rubocop nits --- app/helpers/social_helper/twitter_methods.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/app/helpers/social_helper/twitter_methods.rb b/app/helpers/social_helper/twitter_methods.rb index 716060f8..318dd1f9 100644 --- a/app/helpers/social_helper/twitter_methods.rb +++ b/app/helpers/social_helper/twitter_methods.rb @@ -8,12 +8,15 @@ module SocialHelper::TwitterMethods original_question_length = question_content.length answer_content = twitter_markdown answer.content original_answer_length = answer_content.length - answer_url = answer_url( - id: answer.id, - username: answer.user.screen_name, - host: APP_CONFIG['hostname'], - protocol: (APP_CONFIG['https'] ? :https : :http) - ) unless omit_url + + unless omit_url + answer_url = answer_url( + id: answer.id, + username: answer.user.screen_name, + host: APP_CONFIG["hostname"], + protocol: (APP_CONFIG["https"] ? :https : :http), + ) + end parsed_tweet = { :valid => false } tweet_text = "" From f95e11cffcc5a97634e9a26ff3535ac86f027b63 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Thu, 19 Oct 2023 23:53:21 +0200 Subject: [PATCH 7/7] Fix rubocop nits --- app/helpers/social_helper/twitter_methods.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/helpers/social_helper/twitter_methods.rb b/app/helpers/social_helper/twitter_methods.rb index 6c7649ca..61ce366a 100644 --- a/app/helpers/social_helper/twitter_methods.rb +++ b/app/helpers/social_helper/twitter_methods.rb @@ -4,13 +4,13 @@ require "cgi" module SocialHelper::TwitterMethods include MarkdownHelper - + def prepare_tweet(answer, post_tag = nil, omit_url = false) question_content = twitter_markdown answer.question.content.gsub(/@(\w+)/, '\1') original_question_length = question_content.length answer_content = twitter_markdown answer.content original_answer_length = answer_content.length - + unless omit_url answer_url = answer_url( id: answer.id,