Skip to content

Commit

Permalink
Add social sharing functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
kcne committed Dec 17, 2024
1 parent e8029e2 commit 95e1aff
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/assets/images/social_icons/email.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions app/assets/images/social_icons/facebook.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions app/assets/images/social_icons/linkedin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions app/assets/images/social_icons/mastodon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions app/assets/images/social_icons/telegram.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions app/assets/images/social_icons/x.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions app/assets/javascripts/social_share_button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function openShareUrl(url, initialWidth = 640, initialHeight = 480) {
const width = Math.max(100, Math.min(screen.width, initialWidth));
const height = Math.max(100, Math.min(screen.height, initialHeight));

const left = (screen.width / 2) - (width / 2);
const top = (screen.height * 0.3) - (height / 2);
const opts = `width=${width},height=${height},left=${left},top=${top},menubar=no,status=no,location=no`;

window.open(url, "popup", opts);
}

$(document).ready(function () {
$(".ssb-icon").on("click", function (e) {
const shareUrl = $(this).attr("href");
if (!shareUrl.startsWith("mailto:")) {
e.preventDefault();
openShareUrl(shareUrl);
}
});
});

29 changes: 29 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module ApplicationHelper
require "rexml/document"
include SocialShareButtonHelper

def linkify(text)
if text.html_safe?
Expand Down Expand Up @@ -75,4 +76,32 @@ def render_flash(flash)
rescue StandardError
flash.inspect if Rails.env.development?
end

# Generates a set of social share buttons based on the specified options.
def render_social_share_buttons(opts = {})
sites = opts.fetch(:allow_sites, [])
valid_sites, invalid_sites = SocialShareButtonHelper.filter_allowed_sites(sites)

# Log invalid sites
invalid_sites.each do |invalid_site|
Rails.logger.error("Invalid site or icon not configured: #{invalid_site}")
end

tag.div(
:class => "social-share-button d-flex gap-1 align-items-end flex-wrap mb-3"
) do
valid_sites.map do |site|
link_options = {
:rel => ["nofollow", opts[:rel]].compact,
:class => "ssb-icon rounded-circle",
:title => I18n.t("application.share.#{site}.title"),
:target => "_blank"
}

link_to SocialShareButtonHelper.generate_share_url(site, opts), link_options do
image_tag(SocialShareButtonHelper.icon_path(site), :alt => I18n.t("application.share.#{site}.alt"), :size => 28)
end
end.join.html_safe
end
end
end
1 change: 1 addition & 0 deletions app/views/diary_entries/_diary_entry.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@
<% end %>
</ul>
</nav>

</article>
8 changes: 8 additions & 0 deletions app/views/diary_entries/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<% content_for :head do %>
<%= javascript_include_tag "social_share_button" %>
<% end %>

<% content_for :heading do %>
<div class="row">
<div class="col-sm-auto">
Expand All @@ -11,6 +15,10 @@
<% end %>

<%= render @entry %>
<%= render_social_share_buttons({
:title => @entry.title,
:url => diary_entry_url(@entry.user, @entry)
}) %>

<div id="comments" class="comments mb-3 overflow-hidden">
<div class="row border-bottom border-secondary-subtle">
Expand Down
19 changes: 19 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2604,6 +2604,25 @@ en:
wikipedia:
title: Log in with Wikipedia
alt: Wikipedia logo
share:
email:
title: Share via Email
alt: Email icon
facebook:
title: Share via Facebook
alt: Facebook Icon
linkedin:
title: Share via LinkedIn
alt: LinkedIn Icon
mastodon:
title: Share on Mastodon
alt: Mastodon Icon
telegram:
title: Share on Telegram
alt: Telegram Icon
x:
title: Share on X
alt: X Icon
oauth:
permissions:
missing: "You have not permitted the application access to this facility"
Expand Down
51 changes: 51 additions & 0 deletions lib/social_share_button_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module SocialShareButtonHelper
require "uri"

SOCIAL_SHARE_CONFIG = {
:email => "social_icons/email.svg",
:facebook => "social_icons/facebook.svg",
:linkedin => "social_icons/linkedin.svg",
:mastodon => "social_icons/mastodon.svg",
:telegram => "social_icons/telegram.svg",
:x => "social_icons/x.svg"
}.freeze

def self.filter_allowed_sites(sites)
valid_sites = sites.empty? ? SOCIAL_SHARE_CONFIG.keys : sites.select { |site| valid_site?(site) }
invalid_sites = sites - valid_sites
[valid_sites, invalid_sites]
end

def self.icon_path(site)
SOCIAL_SHARE_CONFIG[site.to_sym] || ""
end

def self.valid_site?(site)
SOCIAL_SHARE_CONFIG.key?(site.to_sym)
end

def self.generate_share_url(site, params)
site = site.to_sym
case site
when :email
to = params[:to] || ""
subject = CGI.escape(params[:title])
body = CGI.escape(params[:url])
"mailto:#{to}?subject=#{subject}&body=#{body}"
when :x
via_str = params[:via] ? "&via=#{URI.encode_www_form_component(params[:via])}" : ""
hashtags_str = params[:hashtags] ? "&hashtags=#{URI.encode_www_form_component(params[:hashtags].join(','))}" : ""
"https://x.com/intent/tweet?url=#{URI.encode_www_form_component(params[:url])}&text=#{URI.encode_www_form_component(params[:title])}#{hashtags_str}#{via_str}"
when :linkedin
"https://www.linkedin.com/sharing/share-offsite/?url=#{URI.encode_www_form_component(params[:url])}"
when :facebook
"https://www.facebook.com/sharer/sharer.php?u=#{URI.encode_www_form_component('params[:url]')}&t=#{URI.encode_www_form_component(params[:title])}"
when :mastodon
"https://mastodonshare.com/?text=#{URI.encode_www_form_component(params[:title])}&url=#{URI.encode_www_form_component(params[:url])}"
when :telegram
"https://t.me/share/url?url=#{URI.encode_www_form_component(params[:url])}&text=#{URI.encode_www_form_component(params[:title])}"
else
raise ArgumentError, "Unsupported platform: #{platform}"
end
end
end
48 changes: 48 additions & 0 deletions test/helpers/social_share_button_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "test_helper"

class SocialShareButtonHelperTest < ActionView::TestCase
include SocialShareButtonHelper
include ApplicationHelper

def setup
@options = {
:allow_sites => %w[x facebook linkedin],
:title => "Test Title",
:url => "https://example.com",
:desc => "Test Description",
:via => "testuser"
}
end

def test_render_social_share_buttons_with_valid_sites
result = render_social_share_buttons(@options)
assert_includes result, "x"
assert_includes result, "facebook"
assert_includes result, "linkedin"
end

def test_render_social_share_buttons_with_invalid_site
@options[:allow_sites] << "invalid_site"
result = render_social_share_buttons(@options)
assert_not_includes result, "invalid_site"
end

def test_render_social_share_buttons_with_no_sites
@options[:allow_sites] = []
result = render_social_share_buttons(@options)
SocialShareButtonHelper::SOCIAL_SHARE_CONFIG.each_key do |site|
assert_includes result, site.to_s # Convert symbol to string
end
end

def test_filter_allowed_sites
valid_sites, invalid_sites = SocialShareButtonHelper.filter_allowed_sites(%w[x facebook invalid_site])
assert_equal %w[x facebook], valid_sites
assert_equal %w[invalid_site], invalid_sites
end

def test_icon_path
assert_equal "social_icons/x.svg", SocialShareButtonHelper.icon_path("x")
assert_equal "", SocialShareButtonHelper.icon_path("invalid_site")
end
end

0 comments on commit 95e1aff

Please sign in to comment.