forked from stml/offbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
121 lines (102 loc) · 4.23 KB
/
Rakefile
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env rake
require File.expand_path('../config/application', __FILE__)
Offbot::Application.load_tasks
require "#{Rails.root}/lib/scheduled_requests_methods"
desc "Send out update requests"
task send_update_requests: :environment do
scheduled_requests = ScheduledRequestDate.where('request_date >= ? AND request_date <= ?', DateTime.now.beginning_of_hour, DateTime.now.end_of_hour)
scheduled_requests.uniq.each do |request_date|
ScheduledRequestsMethods.send_update_request(request_date.person, request_date.project)
request_date.destroy
end
end
desc "Schedule update requests for upcoming week"
task schedule_update_requests: :environment do
date = Date.today
fail "Only want to run this task on Sundays" if Time.now.wday != 0
Project.active.each do |project|
project.people.active.each do |person|
# on twice- or once-monthly projects only update the schedule once a month for next month
if ( (project.frequency == 3 or project.frequency == 4) and ((date.end_of_month-7)..date.end_of_month).member?(date) )
sunday = Date.today + 7
dates = ScheduledRequestsMethods.generate_scheduled_dates(project.frequency, sunday)
elsif (0..2).member?(project.frequency)
dates = ScheduledRequestsMethods.generate_scheduled_dates(project.frequency)
elsif project.frequency.nil?
dates = ScheduledRequestsMethods.generate_scheduled_dates(0)
end
if dates
dates.each do |date|
ScheduledRequestsMethods.create_scheduled_date(person, project, date)
end
end
end
end
end
task :extract_reply => :environment do
def extract_reply(text, address)
regex_arr = [
Regexp.new("From:\s*" + Regexp.escape(address), Regexp::IGNORECASE | Regexp::MULTILINE),
Regexp.new("<" + Regexp.escape(address) + ">", Regexp::IGNORECASE | Regexp::MULTILINE),
Regexp.new(Regexp.escape(address) + "\s+wrote:", Regexp::IGNORECASE | Regexp::MULTILINE),
Regexp.new("^.*On.*(\n)?wrote:$", Regexp::IGNORECASE | Regexp::MULTILINE),
Regexp.new("\s\S*On\s\w*.\s.*", Regexp::IGNORECASE | Regexp::MULTILINE),
Regexp.new("On\s.*,\s.*offbott.com\swrote:.*", Regexp::IGNORECASE | Regexp::MULTILINE),
Regexp.new("-+original\s+message-+\s*$", Regexp::IGNORECASE | Regexp::MULTILINE),
Regexp.new("from:\s*$", Regexp::IGNORECASE | Regexp::MULTILINE)
]
text_length = text.length
#calculates the matching regex closest to top of page
index = regex_arr.inject(text_length) do |min, regex|
puts min
[(text.index(regex) || text_length), min].min
end
text[0, index].strip
end
puts extract_reply(Update.last.body, '[email protected]')
end
desc "Send out a test update request"
task :test_update_request => :environment do
email = EmailMessage.new
email.person = Person.find_by_email("[email protected]")
email.project = Project.find_by_name("Test")
email.save
end
desc "Add project admins list to every project that's missing one"
task :add_project_admins_list => :environment do
Project.all.each do |project|
unless project.add_project_admins_list
admins_list = ProjectAdminsList.new
project.project_admins_list = admins_list
project.save
end
end
end
desc "Send out weekly digest"
task :send_out_weekly_digest => :environment do
todays_digests = Project.where(:weekly_digest_day => (Date.parse(Date.today.to_s).strftime("%A")))
todays_digests.each do |project|
project.people.each do |person|
WeeklyDigest.weekly_digest(project, person).deliver
end
end
end
desc 'Remove date duplicates'
task :remove_date_duplicates => :environment do
# I have a funny feeling I've done this wrong
ScheduledRequestDate.all.each do |date|
request_dates = ScheduledRequestDate.where('request_date >= ? AND request_date <= ? AND project_id = ? AND person_id = ?', date.request_date.beginning_of_day, date.request_date.end_of_day, date.project_id, date.person_id)
if request_dates.length > 1
puts "-----"
request_dates.each_with_index do |request_date, index|
puts index
if index > 0
puts '---'
puts request_date.inspect
request_date.destroy
end
end
puts "-----"
end
end
end