forked from sayamindu/mechanicalmooc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
user-defer.rb
55 lines (44 loc) · 1.61 KB
/
user-defer.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
require 'mooc'
require 'seq-email'
require 'digest/sha1'
ENV['MMOOC_CONFIRM_SALT'] ||= "lalalalala"
module UserDefer
def self.send_to_all(users)
users.each do |email|
self.send_defer_email(email)
end
end
def self.send_defer_email(email)
body = File.read('./emails/user-defer.html')
link_yes_template = "http://learn.media.mit.edu/defer?email=%EMAIL%&auth_token=%AUTH_TOKEN%&defer=yes"
link_no_template = "http://learn.media.mit.edu/defer?email=%EMAIL%&auth_token=%AUTH_TOKEN%&defer=no"
yes_defer_link = link_yes_template.sub('%EMAIL%', email).sub('%AUTH_TOKEN%', email_auth(email))
no_defer_link = link_no_template.sub('%EMAIL%', email).sub('%AUTH_TOKEN%', email_auth(email))
body.sub!('%LINK_DEFER_YES%', yes_defer_link)
body.sub!('%LINK_DEFER_NO%', no_defer_link)
se = SequenceEmail.new
se.subject = "Learning Creative Learning course about to take off - Please confirm your seat!"
se.from = "The Machine (aka Oliver) <[email protected]>"
se.body = body
se.tags << "user-defer-email"
se.send_email_to(email)
end
def self.defer(email, auth_token, defer)
return false unless valid? email, auth_token
user = User.last(:email => email)
return false unless user
# This is to prevent a hacked get request from being saved to our DB
if defer == "yes"
user.defered = "yes"
elsif defer == "no"
user.defered = "no"
end
user.save
end
def self.valid?(email, auth_token)
email_auth(email) == auth_token
end
def self.email_auth(email)
Digest::SHA1.hexdigest email + ENV['MMOOC_CONFIRM_SALT']
end
end