2015-10-22 21:14:36 +02:00
|
|
|
# All the mail and tweet related callbacks to event's lifecycle
|
2016-09-11 17:40:49 +02:00
|
|
|
# also the scheduled events
|
2015-10-22 21:14:36 +02:00
|
|
|
class EventCallbacks
|
2016-09-11 17:40:49 +02:00
|
|
|
def self.before_create(event)
|
2016-09-11 23:53:09 +02:00
|
|
|
event.secret ||= SecureRandom.urlsafe_base64(32)[0...32]
|
|
|
|
event.moderator_mail_id ||= SecureRandom.urlsafe_base64(32)[0...32]
|
|
|
|
event.submitter_mail_id ||= SecureRandom.urlsafe_base64(32)[0...32]
|
2016-09-11 17:40:49 +02:00
|
|
|
end
|
|
|
|
|
2015-10-22 21:14:36 +02:00
|
|
|
def self.after_create(event)
|
2016-10-15 16:39:15 +02:00
|
|
|
return unless event.event.nil?
|
|
|
|
EventMailer.create(event).deliver_now!
|
|
|
|
ModerationMailer.create(event).deliver_now!
|
2015-10-22 21:14:36 +02:00
|
|
|
end
|
|
|
|
|
2016-09-11 17:40:49 +02:00
|
|
|
def self.before_update(event)
|
2016-10-15 16:39:15 +02:00
|
|
|
return unless event.moderated_changed? && event.moderated?
|
|
|
|
event.decision_time = Time.zone.now
|
2017-01-22 14:20:47 +01:00
|
|
|
create_repeats event if event.repeat > 0 && event.rule
|
2016-09-11 17:40:49 +02:00
|
|
|
end
|
|
|
|
|
2015-10-22 21:14:36 +02:00
|
|
|
def self.after_update(event)
|
2016-10-31 22:35:14 +01:00
|
|
|
return unless ActionMailer::Base.default_url_options[:host]
|
|
|
|
|
2016-09-11 18:32:08 +02:00
|
|
|
if event.moderated_changed? && event.moderated?
|
2016-09-11 17:40:49 +02:00
|
|
|
tweet event
|
2015-10-22 21:14:36 +02:00
|
|
|
|
2016-10-31 22:35:14 +01:00
|
|
|
# Send an acceptation mail to its author
|
|
|
|
EventMailer.accept(event).deliver_now
|
2015-10-22 21:14:36 +02:00
|
|
|
|
2016-10-31 22:35:14 +01:00
|
|
|
# Send an acceptation mail to moderators
|
|
|
|
ModerationMailer.accept(event).deliver_now
|
2016-09-11 17:40:49 +02:00
|
|
|
|
2016-10-31 22:35:14 +01:00
|
|
|
else
|
2015-10-22 21:14:36 +02:00
|
|
|
# Send an update mail to moderators
|
|
|
|
ModerationMailer.update(event).deliver_now
|
2016-10-28 19:14:24 +02:00
|
|
|
|
|
|
|
# Included to Send an update mail to its author
|
|
|
|
EventMailer.update(event).deliver_now
|
2015-10-22 21:14:36 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.after_destroy(event)
|
2016-10-15 16:39:15 +02:00
|
|
|
return unless ActionMailer::Base.default_url_options[:host]
|
2017-05-06 15:46:50 +02:00
|
|
|
return if event.reason == 'r_0'
|
2016-10-15 16:39:15 +02:00
|
|
|
EventMailer.destroy(event).deliver_now
|
|
|
|
ModerationMailer.destroy(event).deliver_now
|
2016-09-11 17:40:49 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Create multiple events corresponding to a repetition
|
|
|
|
def self.create_repeats(event)
|
|
|
|
event.schedule.last(event.repeat).each do |schedule|
|
|
|
|
event.events.build create_sub_event(event, schedule)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_sub_event(event, schedule)
|
2016-12-17 16:59:11 +01:00
|
|
|
att = event.attributes.reject do |a|
|
|
|
|
a == 'id' || a == 'lock_version' || a == 'tags'
|
|
|
|
end
|
2016-09-11 17:40:49 +02:00
|
|
|
att[:start_time] = schedule.start_time
|
|
|
|
att[:end_time] = schedule.end_time
|
2017-01-02 10:29:02 +01:00
|
|
|
att[:tag_list] = event.tag_list
|
2016-09-11 17:40:49 +02:00
|
|
|
att
|
2015-10-22 21:14:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Tweet this event, if configured using apache/system variables!
|
|
|
|
def self.tweet(event)
|
|
|
|
client = Twitter::REST::Client.new do |config|
|
|
|
|
config.consumer_key = ENV['TWITTER_CONSUMER_KEY']
|
|
|
|
config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET']
|
|
|
|
config.access_token = ENV['TWITTER_ACCESS_TOKEN']
|
|
|
|
config.access_token_secret = ENV['TWITTER_ACCESS_SECRET']
|
|
|
|
end
|
2015-10-23 16:21:42 +02:00
|
|
|
client.update event.to_tweet if client.consumer_key
|
2015-10-22 21:14:36 +02:00
|
|
|
end
|
|
|
|
end
|