Skip to content

Commit

Permalink
Merge pull request #271 from fablabbcn/bugfix/always-send-profile-pic…
Browse files Browse the repository at this point in the history
…ture-with-user

Move all avatars to profile_picture and ensure that profile_picture is included in responses
  • Loading branch information
oscgonfer authored Oct 26, 2023
2 parents 45b9c18 + 5ecdbad commit af9a720
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 22 deletions.
2 changes: 2 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# this file is required for errbit notifier
class ApplicationController < ActionController::API
include ActionController::ImplicitRender
include ActionController::Helpers
end
3 changes: 3 additions & 0 deletions app/controllers/v0/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require_relative '../../helpers/user_helper'
module V0
class ApplicationController < ActionController::API

Expand All @@ -10,6 +11,8 @@ class ApplicationController < ActionController::API
include PrettyJSON
include ErrorHandlers

helper ::UserHelper

respond_to :json

before_action :prepend_view_paths
Expand Down
9 changes: 9 additions & 0 deletions app/helpers/user_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module UserHelper
def profile_picture_url(user)
if user.profile_picture.attached?
polymorphic_url(user.profile_picture, only_path: false)
else
''
end
end
end
3 changes: 3 additions & 0 deletions app/views/v0/devices/_device.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ if device.owner
json.uuid device.owner.uuid
json.username device.owner.username
json.avatar device.owner.avatar

json.profile_picture profile_picture_url(device.owner)

json.url device.owner.url
json.joined_at device.owner.joined_at
json.location device.owner.location
Expand Down
16 changes: 1 addition & 15 deletions app/views/v0/devices/index.jbuilder
Original file line number Diff line number Diff line change
@@ -1,15 +1 @@
json.array! @devices, partial: 'device', as: :device

# json.array! @devices do |device|
# json.id device.id
# json.name device.name
# json.description device.description
# json.status device.status
# json.added_at device.added_at
# json.last_reading_at device.last_reading_at
# json.updated_at device.updated_at

# json.kit_id device.kit_id

# json.owner device.owner, :id, :username, :avatar, :url, :joined_at, :location, :device_ids
# end
json.array! @devices, partial: 'device', as: :device
8 changes: 1 addition & 7 deletions app/views/v0/users/_user.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ json.(user,
:updated_at
)

if user.profile_picture.attached?
json.profile_picture polymorphic_url(user.profile_picture, only_path: false)

else
json.profile_picture ''
end

json.profile_picture profile_picture_url(user)

if current_user and (current_user.is_admin? or current_user == user)
json.merge! email: user.email
Expand Down
23 changes: 23 additions & 0 deletions lib/tasks/import.rake
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,29 @@ namespace :import do
end
end

desc "Converts old avatar images stored in S3 to use ActiveStorage"
task :old_avatar_images => :environment do
s3 = Fog::Storage.new({
:provider => 'AWS',
:aws_access_key_id => ENV['aws_access_key'],
:aws_secret_access_key => ENV['aws_secret_key'],
:region => ENV['aws_region'],
})
bucket = ENV['s3_bucket']
User.select { |u| !u.avatar_url.nil? && !u.profile_picture.attached? }.each do |user|
puts "Importing avatar for user with id #{user.id}"
begin
avatar_url = user.avatar_url
s3_key = avatar_url.gsub("https://images.smartcitizen.me/s100/", "")
filename = s3_key.split("/")[-1]
object = s3.get_object(bucket, s3_key)
user.profile_picture.attach(io: StringIO.new(object.body), filename: filename)
rescue Exception => e
puts " -- Error #{e.class.name}. Skipping..."
next
end
end
end
end


Expand Down

0 comments on commit af9a720

Please sign in to comment.