diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 731c625..41bbac6 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 diff --git a/app/views/home/index.html.haml b/app/views/home/index.html.haml index c6daec8..d159535 100644 --- a/app/views/home/index.html.haml +++ b/app/views/home/index.html.haml @@ -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 diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index e05d9a2..190eb1d 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -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 diff --git a/app/views/letters/show.html.haml b/app/views/letters/show.html.haml index 1e39a37..12f0b37 100644 --- a/app/views/letters/show.html.haml +++ b/app/views/letters/show.html.haml @@ -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