Added big data classes to do centralised lookups.

This commit is contained in:
Martin Edenhofer 2015-07-07 07:56:31 +02:00
parent f956948299
commit 3a30bffbc0
7 changed files with 151 additions and 21 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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