Skip to content

Commit

Permalink
set twitter images from our db or cloudinary conditionally
Browse files Browse the repository at this point in the history
  • Loading branch information
harunkumars committed Aug 26, 2023
1 parent 65af126 commit 85e516b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
26 changes: 24 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
module ApplicationHelper
def get_twitter_image(twitter_handle)
image_tag "https://res.cloudinary.com/#{ENV['CLOUDINARY_HANDLE']}/image/twitter_name/#{twitter_handle}.jpg", onerror: 'this.error=null;this.src="https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png"'
def get_twitter_image(user)
image_tag image_for_twitter_handle(user), onerror: 'this.error=null;this.src="https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png"'
end

private

RubyConfIndia2023 = Date.parse('2023-08-26')

def image_for_twitter_handle(user)
if user.updated_at.after?(RubyConfIndia2023) && user.image.present?
# this is a quick workaround for being unable to fetch images via Cloudinary for new twitter sign_ins due to Twitter API policy changes.
# one problem with continuing to use this long term would be:
# - if a user changes their profile picture, the url saved on our model will become invalid
# we do have a fallback to default image in place, but it's not ideal
# - As an improvement, we can try to cache it (more like a permanent snapshot until their next login)
# - But a proper solution would be to fetch their current profile picture via a service or to periodically refresh it ourself
user.image
else
cloudinary_image_url(user.twitter_handle)
end
end

def cloudinary_image_url(twitter_handle)
"https://res.cloudinary.com/#{ENV['CLOUDINARY_HANDLE']}/image/twitter_name/#{twitter_handle}.jpg"
end
end
2 changes: 1 addition & 1 deletion app/views/home/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
= time_ago_in_words letter.created_at
ago
.pull-right.thumbnail.imageWrapper{ style: 'max-height: 50px; max-width: 50px' }
= get_twitter_image(letter.user.twitter_handle)
= get_twitter_image(letter.user)

-# TODO refactor this
- unless letter.user_id == 43
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
%tr
%td
.thumbnail.pull-left{ style: 'max-height: 50px; max-width: 50px' }
= get_twitter_image(l.user.twitter_handle)
= get_twitter_image(l.user)
.info.pull-left
= truncate("#{l.user.name} (#{l.user.twitter_handle})", length: 27)
%br
Expand Down
2 changes: 1 addition & 1 deletion app/views/letters/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
= time_ago_in_words @letter.created_at
ago
.pull-right.thumbnail.imageWrapper{ style: 'max-height: 50px; max-width: 50px' }
= get_twitter_image(@letter.user.twitter_handle)
= get_twitter_image(@letter.user)
= markdown(@letter.description)
%br
%div
Expand Down

0 comments on commit 85e516b

Please sign in to comment.