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/helpers/social_helper/twitter_methods.rb b/app/helpers/social_helper/twitter_methods.rb index fd143392..61ce366a 100644 --- a/app/helpers/social_helper/twitter_methods.rb +++ b/app/helpers/social_helper/twitter_methods.rb @@ -5,17 +5,20 @@ 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 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 + 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 = "" 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()); + } } 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); } } 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'); 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/"), diff --git a/spec/helpers/social_helper/twitter_methods_spec.rb b/spec/helpers/social_helper/twitter_methods_spec.rb index cd97fdcc..6157298f 100644 --- a/spec/helpers/social_helper/twitter_methods_spec.rb +++ b/spec/helpers/social_helper/twitter_methods_spec.rb @@ -40,6 +40,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") }