From 5f276ff3492b9e034eba7abf43ecbdb3b47d75b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Thu, 15 May 2025 22:04:25 +0400 Subject: [PATCH] Queue XmppSetAvatarJob when new avatar is uploaded And let job do nothing in development for now --- app/controllers/settings_controller.rb | 1 + app/jobs/xmpp_set_avatar_job.rb | 4 +++- spec/features/settings/profile_spec.rb | 21 +++++++++++++++------ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb index ef03455..a44f78c 100644 --- a/app/controllers/settings_controller.rb +++ b/app/controllers/settings_controller.rb @@ -36,6 +36,7 @@ class SettingsController < ApplicationController if @user.avatar_new.present? if store_user_avatar LdapManager::UpdateAvatar.call(user: @user) + XmppSetAvatarJob.perform_later(user: @user) else @validation_errors = @user.errors render :show, status: :unprocessable_entity and return diff --git a/app/jobs/xmpp_set_avatar_job.rb b/app/jobs/xmpp_set_avatar_job.rb index b5bc1f0..642225c 100644 --- a/app/jobs/xmpp_set_avatar_job.rb +++ b/app/jobs/xmpp_set_avatar_job.rb @@ -5,11 +5,12 @@ class XmppSetAvatarJob < ApplicationJob queue_as :default def perform(user:, overwrite: false) + return if Rails.env.development? @user = user unless overwrite current_avatar = get_current_avatar - Rails.logger.debug { "User #{user.cn} already has an avatar set. Nothing to do." } + Rails.logger.info { "User #{user.cn} already has an avatar set" } return if current_avatar.present? end @@ -56,6 +57,7 @@ class XmppSetAvatarJob < ApplicationJob end end + # See https://xmpp.org/extensions/xep-0084.html def build_xep0084_stanzas img_data = process_avatar sha1_hash = Digest::SHA1.hexdigest(img_data) diff --git a/spec/features/settings/profile_spec.rb b/spec/features/settings/profile_spec.rb index 95a4798..52932b4 100644 --- a/spec/features/settings/profile_spec.rb +++ b/spec/features/settings/profile_spec.rb @@ -46,6 +46,8 @@ RSpec.describe 'Profile settings', type: :feature do feature "Update avatar" do scenario "fails with validation error for wrong content type" do + expect(LdapManager::UpdateAvatar).not_to receive(:call) + visit setting_path(:profile) attach_file "Avatar", "#{Rails.root}/spec/fixtures/files/bitcoin.pdf" click_button "Save" @@ -57,8 +59,7 @@ RSpec.describe 'Profile settings', type: :feature do end scenario "fails with validation error for file size too large" do - expect_any_instance_of(LdapManager::UpdateAvatar) - .not_to receive(:replace_attribute).and_return(true) + expect(LdapManager::UpdateAvatar).not_to receive(:call) visit setting_path(:profile) attach_file "Avatar", "#{Rails.root}/spec/fixtures/files/fsociety-irc.png" @@ -73,8 +74,12 @@ RSpec.describe 'Profile settings', type: :feature do scenario 'works with valid JPG file' do file_path = "#{Rails.root}/spec/fixtures/files/taipei.jpg" - expect_any_instance_of(LdapManager::UpdateAvatar) - .to receive(:replace_attribute).and_return(true) + expect(LdapManager::UpdateAvatar) + .to receive(:call).with(user: user) + .and_return(true) + expect(XmppSetAvatarJob) + .to receive(:perform_later).with(user: user) + .and_return(true) visit setting_path(:profile) attach_file "Avatar", file_path @@ -89,8 +94,12 @@ RSpec.describe 'Profile settings', type: :feature do scenario 'works with valid PNG file' do file_path = "#{Rails.root}/spec/fixtures/files/bender.png" - expect_any_instance_of(LdapManager::UpdateAvatar) - .to receive(:replace_attribute).and_return(true) + expect(LdapManager::UpdateAvatar) + .to receive(:call).with(user: user) + .and_return(true) + expect(XmppSetAvatarJob) + .to receive(:perform_later).with(user: user) + .and_return(true) visit setting_path(:profile) attach_file "Avatar", file_path