From 3a30bffbc029048bf763cf7ab7b30d9a9bf35094 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 7 Jul 2015 07:56:31 +0200 Subject: [PATCH] Added big data classes to do centralised lookups. --- app/controllers/users_controller.rb | 7 ++- app/models/avatar.rb | 24 ++-------- app/models/setting.rb | 12 +++++ lib/auto_wizard.rb | 5 +++ lib/zammad/big_data/base.rb | 10 +++++ lib/zammad/big_data/organization.rb | 69 +++++++++++++++++++++++++++++ lib/zammad/big_data/user.rb | 45 +++++++++++++++++++ 7 files changed, 151 insertions(+), 21 deletions(-) create mode 100644 lib/zammad/big_data/base.rb create mode 100644 lib/zammad/big_data/organization.rb create mode 100644 lib/zammad/big_data/user.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 29d270517..aeaf6acb3 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -99,7 +99,7 @@ class UsersController < ApplicationController user.role_ids = role_ids user.group_ids = group_ids - # else do assignment as defined + # else do assignment as defined else # permission check by role @@ -127,6 +127,11 @@ class UsersController < ApplicationController # if first user was added, set system init done if count <= 2 Setting.set( 'system_init_done', true ) + + # fetch org logo + if user.email + Zammad::BigData::Organization.suggest_system_image(user.email) + end end # send inviteation if needed / only if session exists diff --git a/app/models/avatar.rb b/app/models/avatar.rb index b3b226587..50c879c87 100644 --- a/app/models/avatar.rb +++ b/app/models/avatar.rb @@ -146,32 +146,16 @@ add a avatar end # fetch image - response = UserAgent.post( - 'https://bigdata.zammad.com/api/v1/person/image', - { - email: data[:url] - }, - { - open_timeout: 4, - read_timeout: 6, - }, - ) - if !response.success? - logger.info "Can't fetch image for '#{data[:url]}' (maybe no avatar available), http code: #{response.code}" - return - end - logger.info "Fetched image for '#{data[:url]}', http code: #{response.code}" - mime_type = 'image/jpeg' + image = Zammad::BigData::User.image(data[:url]) + return if !image if !data[:resize] data[:resize] = {} end - data[:resize][:content] = response.body - data[:resize][:mime_type] = mime_type + data[:resize] = image if !data[:full] data[:full] = {} end - data[:full][:content] = response.body - data[:full][:mime_type] = mime_type + data[:full] = image end # check if avatar need to be updated diff --git a/app/models/setting.rb b/app/models/setting.rb index 6f5b62e34..5198eb52c 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -51,6 +51,18 @@ class Setting < ApplicationModel @@current[:settings_config][name] end + def self.reset(name) + setting = Setting.find_by( name: name ) + if !setting + fail "Can't find config setting '#{name}'" + end + setting.state = setting.state_initial + setting.save + logger.info "Setting.reset() name:#{name}, value:#{setting.state.inspect}" + load + @@current[:settings_config][name] + end + private def delete_cache diff --git a/lib/auto_wizard.rb b/lib/auto_wizard.rb index 5b5b01e32..fac763fee 100644 --- a/lib/auto_wizard.rb +++ b/lib/auto_wizard.rb @@ -108,6 +108,11 @@ returns admin_user = created_user UserInfo.current_user_id = admin_user.id + + # fetch org logo + if admin_user.email + Zammad::BigData::Organization.suggest_system_image(admin_user.email) + end } end diff --git a/lib/zammad/big_data/base.rb b/lib/zammad/big_data/base.rb new file mode 100644 index 000000000..95f405f17 --- /dev/null +++ b/lib/zammad/big_data/base.rb @@ -0,0 +1,10 @@ +module Zammad + module BigData + class Base + # rubocop:disable Style/ClassVars + @@api_host = 'https://bigdata.zammad.com' + @@open_timeout = 4 + @@read_timeout = 6 + end + end +end diff --git a/lib/zammad/big_data/organization.rb b/lib/zammad/big_data/organization.rb new file mode 100644 index 000000000..f15f12056 --- /dev/null +++ b/lib/zammad/big_data/organization.rb @@ -0,0 +1,69 @@ +module Zammad + module BigData + class Organization < Zammad::BigData::Base + +=begin + + file = Zammad::BigData::Organization.image('edenhofer.de') + + file = Zammad::BigData::Organization.image('user@edenhofer.de') # will just use domain + +returns + + { + content: content, + mime_type: mime_type, + } + +=end + def self.image(domain) + + # strip, just use domain name + domain = domain.sub(/^.+?@(.+?)$/, '\1') + + # fetch org logo + response = UserAgent.post( + "#{@@api_host}/api/v1/organization/image", + { + domain: domain + }, + { + open_timeout: @@open_timeout, + read_timeout: @@read_timeout, + }, + ) + if !response.success? + Rails.logger.info "Can't fetch image for '#{domain}' (maybe no avatar available), http code: #{response.code}" + return + end + Rails.logger.info "Fetched image for '#{domain}', http code: #{response.code}" + mime_type = 'image/png' + + { + content: response.body, + mime_type: mime_type, + } + end + +=begin + + result = Zammad::BigData::Organization.suggest_system_image('edenhofer.de') + +returns + + true # or false + +=end + def self.suggest_system_image(domain) + image = self.image(domain) + return false if !image + + # store image 1:1 + product_logo = StaticAssets.store_raw( image[:content], image[:mime_type] ) + Setting.set('product_logo', product_logo) + + true + end + end + end +end diff --git a/lib/zammad/big_data/user.rb b/lib/zammad/big_data/user.rb new file mode 100644 index 000000000..6ae5f6846 --- /dev/null +++ b/lib/zammad/big_data/user.rb @@ -0,0 +1,45 @@ +module Zammad + module BigData + class User < Zammad::BigData::Base + +=begin + + file = Zammad::BigData::User.image('client@edenhofer.de') + +returns + + { + content: content, + mime_type: mime_type, + } + +=end + + def self.image(email) + + # fetch logo + response = UserAgent.post( + "#{@@api_host}/api/v1/person/image", + { + email: email, + }, + { + open_timeout: @@open_timeout, + read_timeout: @@read_timeout, + }, + ) + if !response.success? + Rails.logger.info "Can't fetch image for '#{data[:url]}' (maybe no avatar available), http code: #{response.code}" + return + end + Rails.logger.info "Fetched image for '#{data[:url]}', http code: #{response.code}" + mime_type = 'image/jpeg' + { + content: response.body, + mime_type: mime_type, + } + end + + end + end +end