Merge pull request #1396 from Retrospring/bugfix/inbox-sharing

This commit is contained in:
Andreas Nedbal 2023-10-19 23:57:02 +02:00 committed by GitHub
commit 6ff7b08f15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 41 additions and 15 deletions

View File

@ -3,9 +3,7 @@
require "cgi" require "cgi"
class Ajax::AnswerController < AjaxController class Ajax::AnswerController < AjaxController
include SocialHelper::TwitterMethods include SocialHelper
include SocialHelper::TumblrMethods
include SocialHelper::TelegramMethods
def create def create
params.require :id params.require :id
@ -73,6 +71,8 @@ class Ajax::AnswerController < AjaxController
private private
def sharing_hash(answer) = { def sharing_hash(answer) = {
url: answer_share_url(answer),
text: prepare_tweet(answer, nil, true),
twitter: twitter_share_url(answer), twitter: twitter_share_url(answer),
tumblr: tumblr_share_url(answer), tumblr: tumblr_share_url(answer),
telegram: telegram_share_url(answer), telegram: telegram_share_url(answer),

View File

@ -5,17 +5,20 @@ require "cgi"
module SocialHelper::TwitterMethods module SocialHelper::TwitterMethods
include MarkdownHelper 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') question_content = twitter_markdown answer.question.content.gsub(/@(\w+)/, '\1')
original_question_length = question_content.length original_question_length = question_content.length
answer_content = twitter_markdown answer.content answer_content = twitter_markdown answer.content
original_answer_length = answer_content.length original_answer_length = answer_content.length
answer_url = answer_url(
id: answer.id, unless omit_url
username: answer.user.screen_name, answer_url = answer_url(
host: APP_CONFIG["hostname"], id: answer.id,
protocol: (APP_CONFIG["https"] ? :https : :http), username: answer.user.screen_name,
) host: APP_CONFIG["hostname"],
protocol: (APP_CONFIG["https"] ? :https : :http),
)
end
parsed_tweet = { valid: false } parsed_tweet = { valid: false }
tweet_text = "" tweet_text = ""

View File

@ -23,7 +23,7 @@ export default class extends Controller {
this.twitterTarget.addEventListener('click', () => this.close()); this.twitterTarget.addEventListener('click', () => this.close());
this.tumblrTarget.addEventListener('click', () => this.close()); this.tumblrTarget.addEventListener('click', () => this.close());
this.telegramTarget.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) { if (this.hasCustomTarget) {
this.customTarget.addEventListener('click', () => this.close()); this.customTarget.addEventListener('click', () => this.close());
@ -50,4 +50,8 @@ export default class extends Controller {
close(): void { close(): void {
(this.element.closest(".inbox-entry")).remove(); (this.element.closest(".inbox-entry")).remove();
} }
closeAfterShare(): void {
this.otherTarget.addEventListener('retrospring:shared', () => this.close());
}
} }

View File

@ -1,4 +1,5 @@
import { Controller } from '@hotwired/stimulus'; import { Controller } from '@hotwired/stimulus';
import noop from 'utilities/noop';
export default class extends Controller { export default class extends Controller {
static values = { static values = {
@ -35,8 +36,10 @@ export default class extends Controller {
}; };
} }
navigator.share(shareConfiguration).then(() => { navigator.share(shareConfiguration)
this.element.dispatchEvent(new CustomEvent('retrospring:shared')); .then(() => {
}); this.element.dispatchEvent(new CustomEvent('retrospring:shared'));
})
.catch(noop);
} }
} }

View File

@ -32,7 +32,8 @@ export function answerEntryHandler(event: Event): void {
const shareButton = inboxEntry.querySelector<HTMLButtonElement>('[data-controller="share"]'); const shareButton = inboxEntry.querySelector<HTMLButtonElement>('[data-controller="share"]');
if (shareButton != null) { 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<HTMLElement>('.inbox-entry__sharing'); const sharing = inboxEntry.querySelector<HTMLElement>('.inbox-entry__sharing');

View File

@ -91,6 +91,8 @@ describe Ajax::AnswerController, :ajax_controller, type: :controller do
let(:expected_response) do let(:expected_response) do
super().merge( super().merge(
"sharing" => { "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/"), "twitter" => a_string_matching("https://twitter.com/"),
"tumblr" => a_string_matching("https://www.tumblr.com/"), "tumblr" => a_string_matching("https://www.tumblr.com/"),
"telegram" => a_string_matching("https://t.me/"), "telegram" => a_string_matching("https://t.me/"),
@ -170,6 +172,8 @@ describe Ajax::AnswerController, :ajax_controller, type: :controller do
let(:expected_response) do let(:expected_response) do
super().merge( super().merge(
"sharing" => { "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/"), "twitter" => a_string_matching("https://twitter.com/"),
"tumblr" => a_string_matching("https://www.tumblr.com/"), "tumblr" => a_string_matching("https://www.tumblr.com/"),
"telegram" => a_string_matching("https://t.me/"), "telegram" => a_string_matching("https://t.me/"),

View File

@ -40,6 +40,17 @@ describe SocialHelper::TwitterMethods, type: :helper do
end end
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 context "when a suffix has been passed and the tweet needs to be shortened" do
subject { prepare_tweet(answer, "#askracc") } subject { prepare_tweet(answer, "#askracc") }