Fix specs for avatars

Implement avatar picker
Correct avatar related jobs
This commit is contained in:
Sam
2014-05-26 19:46:43 +10:00
committed by Sam Saffron
parent a864f8aefd
commit 504cfcff96
27 changed files with 228 additions and 158 deletions

View File

@ -1,7 +1,23 @@
require_dependency 'letter_avatar'
class AvatarController < ApplicationController
skip_before_filter :check_xhr, :verify_authenticity_token
class UserAvatarsController < ApplicationController
skip_before_filter :check_xhr, :verify_authenticity_token, only: :show
def refresh_gravatar
user = User.find_by(username_lower: params[:username].downcase)
guardian.ensure_can_edit!(user)
if user
user.create_user_avatar(user_id: user.id) unless user.user_avatar
user.user_avatar.update_gravatar!
render json: {upload_id: user.user_avatar.gravatar_upload_id}
else
raise Discourse::NotFound
end
end
def show
username = params[:username].to_s
@ -17,17 +33,17 @@ class AvatarController < ApplicationController
raise Discourse::NotFound unless version > 0 && user_avatar = user.user_avatar
upload = version if user_avatar.contains_upload?(version)
upload = Upload.find(version) if user_avatar.contains_upload?(version)
upload ||= user.uploaded_avatar if user.uploaded_avatar_id == version
if user.uploaded_avatar && !upload
return redirect_to "/avatar/#{user.username_lower}/#{size}/#{user.uploaded_avatar_id}.png"
elsif upload
# TODO broken with S3 (should retrun a permanent redirect)
original = Discourse.store.path_for(user.uploaded_avatar)
original = Discourse.store.path_for(upload)
if File.exists?(original)
optimized = OptimizedImage.create_for(
user.uploaded_avatar,
upload,
size,
size,
allow_animation: SiteSetting.allow_animated_avatars

View File

@ -7,7 +7,7 @@ class UsersController < ApplicationController
skip_before_filter :authorize_mini_profiler, only: [:avatar]
skip_before_filter :check_xhr, only: [:show, :password_reset, :update, :activate_account, :authorize_email, :user_preferences_redirect, :avatar, :my_redirect]
before_filter :ensure_logged_in, only: [:username, :update, :change_email, :user_preferences_redirect, :upload_user_image, :toggle_avatar, :clear_profile_background, :destroy]
before_filter :ensure_logged_in, only: [:username, :update, :change_email, :user_preferences_redirect, :upload_user_image, :pick_avatar, :clear_profile_background, :destroy]
before_filter :respond_to_suspicious_request, only: [:create]
# we need to allow account creation with bad CSRF tokens, if people are caching, the CSRF token on the
@ -364,12 +364,18 @@ class UsersController < ApplicationController
end
end
def toggle_avatar
params.require(:use_uploaded_avatar)
def pick_avatar
params.require(:upload_id)
user = fetch_user_from_params
guardian.ensure_can_edit!(user)
upload_id = params[:upload_id]
user.use_uploaded_avatar = params[:use_uploaded_avatar]
user.uploaded_avatar_id = upload_id
# ensure we associate the custom avatar properly
unless user.user_avatar.contains_upload?(upload_id)
user.user_avatar.custom_upload_id = upload_id
end
user.save!
render nothing: true
@ -421,8 +427,7 @@ class UsersController < ApplicationController
end
def upload_avatar_for(user, upload)
user.upload_avatar(upload)
render json: { url: upload.url, width: upload.width, height: upload.height }
render json: { upload_id: upload.id, url: upload.url, width: upload.width, height: upload.height }
end
def upload_profile_background_for(user, upload)