forked from tobymao/18xx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.rb
executable file
·72 lines (57 loc) · 1.96 KB
/
queue.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# frozen_string_literal: true
require 'logger'
require 'require_all'
require_relative 'models'
require_rel './models'
require_relative 'lib/assets'
require_relative 'lib/bus'
require_relative 'lib/hooks'
require_relative 'lib/mail'
PRODUCTION = ENV['RACK_ENV'] == 'production'
LOGGER = Logger.new($stdout)
Bus.configure
ASSETS = Assets.new(precompiled: PRODUCTION)
MessageBus.subscribe '/turn' do |msg|
data = msg.data
users = User.where(id: data['user_ids']).all
game = Game[data['game_id']]
minute_ago = (Time.now - 60).to_i
users.each do |user|
next if user.settings['notifications'] != 'webhook'
next if (user.settings['webhook_user_id']&.strip || '') == ''
begin
message = "<@#{user.settings['webhook_user_id']}> #{data['type']} in #{game.title} \"#{game.description}\" " \
"(#{game.round} #{game.turn})\n#{data['game_url']}"
Hooks.send(user, message)
rescue Exception => e # rubocop:disable Lint/RescueException
puts e.backtrace
puts e
end
end
users = users.reject do |user|
notifications = user.settings['notifications'] || 'none'
next true if notifications == 'none'
next true if /@(msn|hotmail|outlook|live|passport)/.match?(user.email.downcase)
next true if notifications != 'email'
next false if data['force']
next true if (Bus[Bus::USER_TS % user.id].to_i || minute_ago) > minute_ago
email_sent = user.settings['email_sent'] || 0
email_sent > minute_ago
end
next if users.empty?
html = ASSETS.html(
'assets/app/mail/turn.rb',
game_data: game.to_h(include_actions: true),
game_url: data['game_url'],
)
users.each do |user|
Mail.send(user, "18xx.games Game: #{game.title} - #{game.id} - #{data['type']}", html)
LOGGER.info("mail sent for game: #{game.id} to user: #{user.id}")
user.settings['email_sent'] = Time.now.to_i
user.save
end
rescue Exception => e # rubocop:disable Lint/RescueException
puts e.backtrace
puts e
end
sleep