Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sharing to other apps in inbox entries #1396

Merged
merged 8 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/controllers/ajax/answer_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -73,6 +71,8 @@ def destroy
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),
Expand Down
17 changes: 10 additions & 7 deletions app/helpers/social_helper/twitter_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
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 = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
}
}
9 changes: 6 additions & 3 deletions app/javascript/retrospring/controllers/share_controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Controller } from '@hotwired/stimulus';
import noop from 'utilities/noop';

export default class extends Controller {
static values = {
Expand Down Expand Up @@ -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);
}
}
3 changes: 2 additions & 1 deletion app/javascript/retrospring/features/inbox/entry/answer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export function answerEntryHandler(event: Event): void {

const shareButton = inboxEntry.querySelector<HTMLButtonElement>('[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<HTMLElement>('.inbox-entry__sharing');
Expand Down
4 changes: 4 additions & 0 deletions spec/controllers/ajax/answer_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
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/"),
Expand Down Expand Up @@ -170,6 +172,8 @@
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/"),
Expand Down
11 changes: 11 additions & 0 deletions spec/helpers/social_helper/twitter_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@
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") }

Expand Down