Moved to current channel api.

This commit is contained in:
Martin Edenhofer 2015-12-14 10:23:14 +01:00
parent 5852dc6d8c
commit 1d4e1310d1
9 changed files with 178 additions and 83 deletions

View file

@ -6,11 +6,15 @@ class Channel::Driver::Smtp
instance = Channel::Driver::Smtp.new instance = Channel::Driver::Smtp.new
instance.send( instance.send(
{
host: 'some.host', host: 'some.host',
port: 25, port: 25,
enable_starttls_auto: true, # optional enable_starttls_auto: true, # optional
user: 'someuser', user: 'someuser',
password: 'somepass' password: 'somepass'
},
mail_attributes,
notification
) )
=end =end

View file

@ -1,12 +1,55 @@
# Copyright (C) 2012-2015 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2015 Zammad Foundation, http://zammad-foundation.org/
=begin
fetch tweets from twitter account
options = {
adapter: 'twitter',
auth: {
consumer_key: consumer_key,
consumer_secret: consumer_secret,
oauth_token: armin_theo_token,
oauth_token_secret: armin_theo_token_secret,
},
sync: {
search: [
{
term: '#citheo42',
group_id: 2,
},
{
term: '#citheo24',
group_id: 1,
},
],
mentions: {
group_id: 2,
},
direct_messages: {
group_id: 2,
}
}
}
instance = Channel::Driver::Twitter.new
result = instance.fetch(options, channel)
returns
{
result: 'ok',
}
=end
class Channel::Driver::Twitter class Channel::Driver::Twitter
def fetch (_adapter_options, channel) def fetch (options, channel)
@tweet = Tweet.new(options[:auth])
@sync = options[:sync]
@channel = channel @channel = channel
@tweet = Tweet.new(@channel[:options][:auth])
@sync = @channel[:options][:sync]
Rails.logger.debug 'twitter fetch started' Rails.logger.debug 'twitter fetch started'
@ -17,16 +60,39 @@ class Channel::Driver::Twitter
disconnect disconnect
Rails.logger.debug 'twitter fetch completed' Rails.logger.debug 'twitter fetch completed'
{
result: 'ok',
}
end end
def send(article, _notification = false) =begin
@channel = Channel.find_by(area: 'Twitter::Account', active: true) instance = Channel::Driver::Twitter.new
@tweet = Tweet.new(@channel[:options][:auth]) instance.send(
{
adapter: 'twitter',
auth: {
consumer_key: consumer_key,
consumer_secret: consumer_secret,
oauth_token: armin_theo_token,
oauth_token_secret: armin_theo_token_secret,
},
},
twitter_attributes,
notification
)
=end
def send(options, article, _notification = false)
# return if we run import mode
return if Setting.get('import_mode')
@tweet = Tweet.new(options[:auth])
tweet = @tweet.from_article(article) tweet = @tweet.from_article(article)
disconnect disconnect
tweet tweet
end end
@ -54,7 +120,7 @@ class Channel::Driver::Twitter
break if search[:limit] && search[:limit] <= counter break if search[:limit] && search[:limit] <= counter
break if Ticket::Article.find_by(message_id: tweet.id) break if Ticket::Article.find_by(message_id: tweet.id)
@tweet.to_group(tweet, search[:group_id]) @tweet.to_group(tweet, search[:group_id], @channel)
counter += 1 counter += 1
} }
@ -74,7 +140,7 @@ class Channel::Driver::Twitter
break if @sync[:mentions][:limit] && @sync[:mentions][:limit] <= counter break if @sync[:mentions][:limit] && @sync[:mentions][:limit] <= counter
break if Ticket::Article.find_by(message_id: tweet.id) break if Ticket::Article.find_by(message_id: tweet.id)
@tweet.to_group(tweet, @sync[:mentions][:group_id]) @tweet.to_group(tweet, @sync[:mentions][:group_id], @channel)
counter += 1 counter += 1
} }
@ -93,7 +159,7 @@ class Channel::Driver::Twitter
break if @sync[:direct_messages][:limit] && @sync[:direct_messages][:limit] <= counter break if @sync[:direct_messages][:limit] && @sync[:direct_messages][:limit] <= counter
break if Ticket::Article.find_by(message_id: tweet.id) break if Ticket::Article.find_by(message_id: tweet.id)
@tweet.to_group(tweet, @sync[:direct_messages][:group_id]) @tweet.to_group(tweet, @sync[:direct_messages][:group_id], @channel)
counter += 1 counter += 1
} }

View file

@ -440,13 +440,20 @@ retrns
# create new ticket # create new ticket
if !ticket if !ticket
# set attributes preferences = {}
if channel[:id]
preferences = {
channel_id: channel[:id]
}
end
ticket = Ticket.new( ticket = Ticket.new(
group_id: channel[:group_id] || 1, group_id: channel[:group_id] || 1,
customer_id: user.id, customer_id: user.id,
title: mail[:subject] || '', title: mail[:subject] || '',
state_id: Ticket::State.find_by(name: 'new').id, state_id: Ticket::State.find_by(name: 'new').id,
priority_id: Ticket::Priority.find_by(name: '2 normal').id, priority_id: Ticket::Priority.find_by(name: '2 normal').id,
preferences: preferences,
) )
set_attributes_by_x_headers(ticket, 'ticket', mail) set_attributes_by_x_headers(ticket, 'ticket', mail)

View file

@ -18,16 +18,21 @@ class Observer::Ticket::Article::CommunicateTwitter < ActiveRecord::Observer
# only apply on tweets # only apply on tweets
type = Ticket::Article::Type.lookup(id: record.type_id) type = Ticket::Article::Type.lookup(id: record.type_id)
return if type['name'] !~ /\Atwitter/ return if type['name'] !~ /\Atwitter/i
twitter = Channel::Driver::Twitter.new ticket = Ticket.lookup(id: record.ticket_id)
tweet = twitter.send({ fail "Can't find ticket.preferences for Ticket.find(#{record.ticket_id})" if !ticket.preferences
fail "Can't find ticket.preferences['channel_id'] for Ticket.find(#{record.ticket_id})" if !ticket.preferences['channel_id']
channel = Channel.lookup(id: ticket.preferences['channel_id'])
fail "Channel.find(#{channel.id}) isn't a twitter channel!" if channel.options[:adapter] !~ /\Atwitter/i
tweet = channel.deliver(
type: type['name'], type: type['name'],
to: record.to, to: record.to,
body: record.body, body: record.body,
in_reply_to: record.in_reply_to in_reply_to: record.in_reply_to
}) )
record.message_id = tweet.id record.message_id = tweet.id
record.save record.save
end end
end end

View file

@ -14,6 +14,7 @@ class Ticket < ApplicationModel
include Ticket::SearchIndex include Ticket::SearchIndex
extend Ticket::Search extend Ticket::Search
store :preferences
before_create :check_generate, :check_defaults, :check_title before_create :check_generate, :check_defaults, :check_title
before_update :check_defaults, :check_title, :reset_pending_time before_update :check_defaults, :check_title, :reset_pending_time
before_destroy :destroy_dependencies before_destroy :destroy_dependencies

View file

@ -0,0 +1,5 @@
class UpdateTicketPreferences < ActiveRecord::Migration
def up
add_column :tickets, :preferences, :text, limit: 500.kilobytes + 1, null: true
end
end

View file

@ -86,7 +86,7 @@ class Tweet
user user
end end
def to_ticket(tweet, user, group_id) def to_ticket(tweet, user, group_id, channel)
Rails.logger.debug 'Create ticket from tweet...' Rails.logger.debug 'Create ticket from tweet...'
Rails.logger.debug tweet.inspect Rails.logger.debug tweet.inspect
@ -118,6 +118,9 @@ class Tweet
group_id: group_id, group_id: group_id,
state_id: Ticket::State.find_by(name: 'new').id, state_id: Ticket::State.find_by(name: 'new').id,
priority_id: Ticket::Priority.find_by(name: '2 normal').id, priority_id: Ticket::Priority.find_by(name: '2 normal').id,
preferences: {
channel_id: channel.id
},
) )
end end
@ -164,7 +167,7 @@ class Tweet
) )
end end
def to_group(tweet, group_id) def to_group(tweet, group_id, channel)
Rails.logger.debug 'import tweet' Rails.logger.debug 'import tweet'
@ -177,19 +180,19 @@ class Tweet
# check if parent exists # check if parent exists
user = to_user(tweet) user = to_user(tweet)
if tweet.class == Twitter::DirectMessage if tweet.class == Twitter::DirectMessage
ticket = to_ticket(tweet, user, group_id) ticket = to_ticket(tweet, user, group_id, channel)
to_article(tweet, user, ticket) to_article(tweet, user, ticket)
elsif tweet.class == Twitter::Tweet elsif tweet.class == Twitter::Tweet
if tweet.in_reply_to_status_id if tweet.in_reply_to_status_id && tweet.in_reply_to_status_id.to_s != ''
existing_article = Ticket::Article.find_by(message_id: tweet.in_reply_to_status_id) existing_article = Ticket::Article.find_by(message_id: tweet.in_reply_to_status_id)
if existing_article if existing_article
ticket = existing_article.ticket ticket = existing_article.ticket
else else
parent_tweet = @client.status(tweet.in_reply_to_status_id) parent_tweet = @client.status(tweet.in_reply_to_status_id)
ticket = to_group(parent_tweet, group_id) ticket = to_group(parent_tweet, group_id, channel)
end end
else else
ticket = to_ticket(tweet, user, group_id) ticket = to_ticket(tweet, user, group_id, channel)
end end
to_article(tweet, user, ticket) to_article(tweet, user, ticket)
else else

View file

@ -48,7 +48,7 @@ class TwitterTest < ActiveSupport::TestCase
# add channel # add channel
current = Channel.where(area: 'Twitter::Account') current = Channel.where(area: 'Twitter::Account')
current.each(&:destroy) current.each(&:destroy)
Channel.create( channel = Channel.create(
area: 'Twitter::Account', area: 'Twitter::Account',
options: { options: {
adapter: 'twitter', adapter: 'twitter',
@ -93,6 +93,9 @@ class TwitterTest < ActiveSupport::TestCase
group_id: 2, group_id: 2,
state: Ticket::State.find_by(name: 'new'), state: Ticket::State.find_by(name: 'new'),
priority: Ticket::Priority.find_by(name: '2 normal'), priority: Ticket::Priority.find_by(name: '2 normal'),
preferences: {
channel_id: channel.id,
},
updated_by_id: 1, updated_by_id: 1,
created_by_id: 1, created_by_id: 1,
) )
@ -173,7 +176,7 @@ class TwitterTest < ActiveSupport::TestCase
tweet = client.update( tweet = client.update(
text, text,
) )
sleep 10 sleep 15
# fetch check system account # fetch check system account
article = nil article = nil
@ -243,7 +246,7 @@ class TwitterTest < ActiveSupport::TestCase
text, text,
) )
assert(dm, "dm with ##{hash} created") assert(dm, "dm with ##{hash} created")
sleep 10 sleep 15
# fetch check system account # fetch check system account
article = nil article = nil
@ -288,7 +291,7 @@ class TwitterTest < ActiveSupport::TestCase
text, text,
) )
assert(dm, "second dm with ##{hash} created") assert(dm, "second dm with ##{hash} created")
sleep 10 sleep 15
# fetch check system account # fetch check system account
article = nil article = nil
@ -321,6 +324,7 @@ class TwitterTest < ActiveSupport::TestCase
text, text,
) )
assert(dm, "third dm with ##{hash} created") assert(dm, "third dm with ##{hash} created")
sleep 15
# fetch check system account # fetch check system account
article = nil article = nil