diff --git a/Envfile b/Envfile index 0d8bfa24b6faa..821ed1b837d46 100644 --- a/Envfile +++ b/Envfile @@ -163,6 +163,10 @@ variable :TWITCH_WEBHOOK_SECRET, :String, default: "Optional" # (https://api.stackexchange.com/docs) variable :STACK_EXCHANGE_APP_KEY, :String, default: "" +# For Heroku domain +# ("https://medium.com/") +variable :MEDIUM_DOMIAN, :String, default: "https://medium.com/" + # For setting the IDs of the staff and welcoming users variable :STAFF_USER_ID, :Integer, default: 1 variable :WELCOMING_USER_ID, :Integer, default: 1 @@ -182,4 +186,5 @@ group :production do # Heroku variable :HEROKU_APP_URL, :String # practicaldev.herokuapp.com + variable :MEDIUM_DOMIAN, :String, default: "https://medium.com/" end diff --git a/Gemfile b/Gemfile index cbf0a9a9b858b..f95d8f520b131 100644 --- a/Gemfile +++ b/Gemfile @@ -64,6 +64,7 @@ gem "oj", "~> 3.10" # JSON parser and object serializer gem "omniauth", "~> 1.9" # A generalized Rack framework for multiple-provider authentication gem "omniauth-github", "~> 1.3" # OmniAuth strategy for GitHub gem "omniauth-twitter", "~> 1.4" # OmniAuth strategy for Twitter +gem "opengraph_parser", "~> 0.2.3" # Open Graph Protocol information from a website gem "pg", "~> 1.2" # Pg is the Ruby interface to the PostgreSQL RDBMS gem "puma", "~> 4.3" # Puma is a simple, fast, threaded, and highly concurrent HTTP 1.1 server gem "pundit", "~> 2.1" # Object oriented authorization for Rails applications diff --git a/Gemfile.lock b/Gemfile.lock index 2ec15b6efd5a6..c66d47f389b11 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -512,6 +512,9 @@ GEM omniauth-twitter (1.4.0) omniauth-oauth (~> 1.1) rack + opengraph_parser (0.2.3) + addressable + nokogiri orm_adapter (0.5.0) os (1.0.1) parallel (1.19.1) @@ -921,6 +924,7 @@ DEPENDENCIES omniauth (~> 1.9) omniauth-github (~> 1.3) omniauth-twitter (~> 1.4) + opengraph_parser (~> 0.2.3) parallel_tests (~> 2.31) pg (~> 1.2) pry (~> 0.12) diff --git a/app/models/message.rb b/app/models/message.rb index 4aeb6f1f9014a..50cb4761f4ef2 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -118,6 +118,13 @@ def append_rich_links(html) #{user.name} ".html_safe + elsif (medium = rich_medium_link(anchor)) + html += " + #{"
" if medium.image_url.present?} +

#{medium.title}

+

#{medium.description}

".html_safe end end html @@ -163,6 +170,14 @@ def rich_user_link(link) User.find_by(username: link["href"].split("/")[3].split("/")[0]) if link["href"].include?("//#{ApplicationConfig['APP_DOMAIN']}/") end + def rich_medium_link(link) + # restricted to preview only medium urls. Not sure we need to suport others or not. + return unless link["href"].include?(ApplicationConfig["MEDIUM_DOMIAN"]) && link["href"].split("/")[4] + + website = OpenGraph.new(link["href"]) + OpenStruct.new(title: website.title, url: website.url, description: website.description.presence, image_url: website.images.present? ? website.images.first : nil) if website + end + def send_email_if_appropriate recipient = direct_receiver return if !chat_channel.direct? ||