From 40afe824f3985dbfee44d16a4215b36dfd43e552 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 10 Jan 2016 14:24:54 +0100 Subject: [PATCH] Improved channel fetchable function. --- app/models/channel.rb | 3 ++- app/models/channel/driver/imap.rb | 4 ++++ app/models/channel/driver/pop3.rb | 17 ++++++++++++--- app/models/channel/driver/twitter.rb | 31 +++++++++++++++++----------- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/app/models/channel.rb b/app/models/channel.rb index 4b0384433..76c44f657 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -37,7 +37,7 @@ fetch one account =end - def fetch + def fetch(force = false) adapter = options[:adapter] adapter_options = options @@ -56,6 +56,7 @@ fetch one account driver_class = Object.const_get("Channel::Driver::#{adapter.to_classname}") driver_instance = driver_class.new + return if !force && !driver_instance.fetchable?(self) result = driver_instance.fetch(adapter_options, self) self.status_in = result[:result] self.last_log_in = result[:notice] diff --git a/app/models/channel/driver/imap.rb b/app/models/channel/driver/imap.rb index ab55b9ddd..dd723aa9e 100644 --- a/app/models/channel/driver/imap.rb +++ b/app/models/channel/driver/imap.rb @@ -4,6 +4,10 @@ require 'net/imap' class Channel::Driver::Imap < Channel::EmailParser + def fetchable?(_channel) + true + end + =begin fetch emails from IMAP account diff --git a/app/models/channel/driver/pop3.rb b/app/models/channel/driver/pop3.rb index fe54f0f74..99514277e 100644 --- a/app/models/channel/driver/pop3.rb +++ b/app/models/channel/driver/pop3.rb @@ -8,7 +8,7 @@ class Channel::Driver::Pop3 < Channel::EmailParser fetch emails from Pop3 account - instance = Channel::Driver::Imap.new + instance = Channel::Driver::Pop3.new result = instance.fetch(params[:inbound][:options], channel, 'verify', subject_looking_for) returns @@ -21,7 +21,7 @@ returns check if connect to Pop3 account is possible, return count of mails in mailbox - instance = Channel::Driver::Imap.new + instance = Channel::Driver::Pop3.new result = instance.fetch(params[:inbound][:options], channel, 'check') returns @@ -33,7 +33,7 @@ returns verify Pop3 account, check if search email is in there - instance = Channel::Driver::Imap.new + instance = Channel::Driver::Pop3.new result = instance.fetch(params[:inbound][:options], channel, 'verify', subject_looking_for) returns @@ -162,6 +162,17 @@ returns } end +=begin + + instance = Channel::Driver::Pop3.new + instance.fetchable?(channel) + +=end + + def fetchable?(_channel) + true + end + def disconnect return if !@pop @pop.finish diff --git a/app/models/channel/driver/twitter.rb b/app/models/channel/driver/twitter.rb index 4e2d6a872..869aa6666 100644 --- a/app/models/channel/driver/twitter.rb +++ b/app/models/channel/driver/twitter.rb @@ -1,5 +1,7 @@ # Copyright (C) 2012-2015 Zammad Foundation, http://zammad-foundation.org/ +class Channel::Driver::Twitter + =begin fetch tweets from twitter account @@ -43,22 +45,10 @@ returns =end -class Channel::Driver::Twitter - def fetch (options, channel) options = check_external_credential(options) - # only fetch once a hour - if Rails.env.production? || Rails.env.development? - if channel.preferences && channel.preferences[:last_fetch] && channel.preferences[:last_fetch] > Time.zone.now - 1.hour - return { - result: 'ok', - notice: '', - } - end - end - @rest_client = TweetRest.new(options[:auth]) @sync = options[:sync] @channel = channel @@ -79,6 +69,23 @@ class Channel::Driver::Twitter } end +=begin + + instance = Channel::Driver::Twitter.new + instance.fetchable?(channel) + +=end + + def fetchable?(channel) + return true if Rails.env.test? + + # only fetch once a hour + return true if !channel.preferences + return true if !channel.preferences[:last_fetch] + return false if channel.preferences[:last_fetch] > Time.zone.now - 1.hour + true + end + =begin instance = Channel::Driver::Twitter.new