From 4029987caa7e5153df3109558c6a7390c6a0567b Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 8 Oct 2015 08:33:01 +0200 Subject: [PATCH] Detect signature for users. Added signature lookup for new customer articles. --- .../ticket_zoom/article_view.coffee | 18 +++++- .../views/ticket_zoom/article_view.jst.eco | 2 +- .../article/email_signature_detection.rb | 29 ++++++++++ config/application.rb | 1 + lib/signature_detection.rb | 56 +++++++++++++++++-- 5 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 app/models/observer/ticket/article/email_signature_detection.rb diff --git a/app/assets/javascripts/app/controllers/ticket_zoom/article_view.coffee b/app/assets/javascripts/app/controllers/ticket_zoom/article_view.coffee index 3565dcd45..0a161c81c 100644 --- a/app/assets/javascripts/app/controllers/ticket_zoom/article_view.coffee +++ b/app/assets/javascripts/app/controllers/ticket_zoom/article_view.coffee @@ -108,11 +108,25 @@ class ArticleViewItem extends App.Controller return # prepare html body + signatureDetected = false if @article.content_type is 'text/html' @article['html'] = @article.body else - @article['html'] = App.Utils.textCleanup( @article.body ) - @article['html'] = App.Utils.text2html( @article.body ) + + # check if signature got detected in backend + body = @article.body + if @article.preferences && @article.preferences.signature_detection + signatureDetected = '########SIGNATURE########' + body = body.split("\n") + body.splice(@article.preferences.signature_detection, 0, signatureDetected) + body = body.join("\n") + body = App.Utils.textCleanup(body) + @article['html'] = App.Utils.text2html(body) + + if signatureDetected + @article['html'] = @article['html'].replace(signatureDetected, '') + else + @article['html'] = App.Utils.signatureIdentify( @article['html'] ) @html App.view('ticket_zoom/article_view')( ticket: @ticket diff --git a/app/assets/javascripts/app/views/ticket_zoom/article_view.jst.eco b/app/assets/javascripts/app/views/ticket_zoom/article_view.jst.eco index 80bf73eb6..714ac349f 100644 --- a/app/assets/javascripts/app/views/ticket_zoom/article_view.jst.eco +++ b/app/assets/javascripts/app/views/ticket_zoom/article_view.jst.eco @@ -41,7 +41,7 @@
- <%- App.Utils.signatureIdentify( @article.html ) %> + <%- @article.html %>
<%- @T('See more') %>
diff --git a/app/models/observer/ticket/article/email_signature_detection.rb b/app/models/observer/ticket/article/email_signature_detection.rb new file mode 100644 index 000000000..595da2296 --- /dev/null +++ b/app/models/observer/ticket/article/email_signature_detection.rb @@ -0,0 +1,29 @@ +# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ + +class Observer::Ticket::Article::EmailSignatureDetection < ActiveRecord::Observer + observe 'ticket::_article' + + def before_create(record) + + # return if we run import mode + return if Setting.get('import_mode') + + # if sender is not customer, do not change anything + sender = Ticket::Article::Sender.lookup( id: record.sender_id ) + return if !sender + return if sender['name'] != 'Customer' + + # set email attributes + type = Ticket::Article::Type.lookup( id: record.type_id ) + return if type['name'] != 'email' + + # user + user = User.lookup(id: record.created_by_id) + return if !user + return if !user.preferences + return if !user.preferences[:signature_detection] + + record.preferences[:signature_detection] = SignatureDetection.find_signature_line(user.preferences[:signature_detection], record.body) + + end +end diff --git a/config/application.rb b/config/application.rb index 7f77e647f..4417a999c 100644 --- a/config/application.rb +++ b/config/application.rb @@ -30,6 +30,7 @@ module Zammad 'observer::_ticket::_article::_communicate_email', 'observer::_ticket::_article::_communicate_facebook', 'observer::_ticket::_article::_communicate_twitter', + 'observer::_ticket::_article::_email_signature_detection', 'observer::_ticket::_notification', 'observer::_ticket::_reset_new_state', 'observer::_ticket::_escalation_calculation', diff --git a/lib/signature_detection.rb b/lib/signature_detection.rb index 81f55e665..435d21066 100644 --- a/lib/signature_detection.rb +++ b/lib/signature_detection.rb @@ -18,7 +18,6 @@ returns possible_signatures = {} # loop all strings in array - #for main_string_index in 0 .. string_list.length - 1 ( 0..string_list.length - 1 ).each {|main_string_index| break if main_string_index + 1 > string_list.length - 1 @@ -76,6 +75,7 @@ returns possible_signatures[match_content] ||= 0 possible_signatures[match_content] += 1 + break end match_block = nil @@ -115,8 +115,56 @@ returns return if search_position.nil? # count new lines up to signature - search_newlines = string[0..search_position].split("\n").length + 1 - - search_newlines + string[0..search_position].split("\n").length + 1 end + +=begin + +this function will search for a signature string in all articles of a given user_id + + signature = SignatureDetection.by_user_id(user_id) + +returns + + signature = '...signature possible match...' + +=end + + def self.by_user_id(user_id) + + article_type = Ticket::Article::Type.lookup(name: 'email') + article_bodies = [] + tickets = Ticket.where(created_by_id: user_id, create_article_type_id: article_type.id).limit(10).order(id: :desc) + tickets.each {|ticket| + article = ticket.articles.first + article_bodies.push article.body + } + find_signature( article_bodies ) + end + +=begin + +rebuild signature for each user + + SignatureDetection.rebuild_all + +returns + + true/false + +=end + + def self.rebuild_all + + User.select('id').where(active: true).each {|local_user| + signature_detection = by_user_id(local_user.id) + next if !signature_detection + user = User.find(local_user.id) + next if user.preferences[:signature_detection] == signature_detection + user.preferences[:signature_detection] = signature_detection + user.save + } + true + end + end