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.send(
{
host: 'some.host',
port: 25,
enable_starttls_auto: true, # optional
user: 'someuser',
password: 'somepass'
},
mail_attributes,
notification
)
=end

View file

@ -1,12 +1,55 @@
# 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
def fetch (_adapter_options, channel)
def fetch (options, channel)
@tweet = Tweet.new(options[:auth])
@sync = options[:sync]
@channel = channel
@tweet = Tweet.new(@channel[:options][:auth])
@sync = @channel[:options][:sync]
Rails.logger.debug 'twitter fetch started'
@ -17,16 +60,39 @@ class Channel::Driver::Twitter
disconnect
Rails.logger.debug 'twitter fetch completed'
{
result: 'ok',
}
end
def send(article, _notification = false)
=begin
@channel = Channel.find_by(area: 'Twitter::Account', active: true)
@tweet = Tweet.new(@channel[:options][:auth])
instance = Channel::Driver::Twitter.new
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)
disconnect
tweet
end
@ -54,7 +120,7 @@ class Channel::Driver::Twitter
break if search[:limit] && search[:limit] <= counter
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
}
@ -74,7 +140,7 @@ class Channel::Driver::Twitter
break if @sync[:mentions][:limit] && @sync[:mentions][:limit] <= counter
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
}
@ -93,7 +159,7 @@ class Channel::Driver::Twitter
break if @sync[:direct_messages][:limit] && @sync[:direct_messages][:limit] <= counter
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
}

View file

@ -440,13 +440,20 @@ retrns
# create new ticket
if !ticket
# set attributes
preferences = {}
if channel[:id]
preferences = {
channel_id: channel[:id]
}
end
ticket = Ticket.new(
group_id: channel[:group_id] || 1,
customer_id: user.id,
title: mail[:subject] || '',
state_id: Ticket::State.find_by(name: 'new').id,
priority_id: Ticket::Priority.find_by(name: '2 normal').id,
preferences: preferences,
)
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
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
tweet = twitter.send({
ticket = Ticket.lookup(id: record.ticket_id)
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'],
to: record.to,
body: record.body,
in_reply_to: record.in_reply_to
})
)
record.message_id = tweet.id
record.save
end
end

View file

@ -14,6 +14,7 @@ class Ticket < ApplicationModel
include Ticket::SearchIndex
extend Ticket::Search
store :preferences
before_create :check_generate, :check_defaults, :check_title
before_update :check_defaults, :check_title, :reset_pending_time
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
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 tweet.inspect
@ -118,6 +118,9 @@ class Tweet
group_id: group_id,
state_id: Ticket::State.find_by(name: 'new').id,
priority_id: Ticket::Priority.find_by(name: '2 normal').id,
preferences: {
channel_id: channel.id
},
)
end
@ -164,7 +167,7 @@ class Tweet
)
end
def to_group(tweet, group_id)
def to_group(tweet, group_id, channel)
Rails.logger.debug 'import tweet'
@ -177,19 +180,19 @@ class Tweet
# check if parent exists
user = to_user(tweet)
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)
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)
if existing_article
ticket = existing_article.ticket
else
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
else
ticket = to_ticket(tweet, user, group_id)
ticket = to_ticket(tweet, user, group_id, channel)
end
to_article(tweet, user, ticket)
else

View file

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