-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_votes.rb
47 lines (46 loc) · 2.08 KB
/
check_votes.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
check_votes = Rufus::Scheduler.start_new
check_votes.every '1m' do
Book.all.each do |book|
next if book.fin?
numvotes = book.pparas.all.map{|o| o.votes}.flatten.uniq.length
num_subs = DB[:subs].where(book_id: book.id).count
if numvotes > (num_subs) && numvotes > 1
winning_para = book.pparas.all.map{|o| [o,o.vote_count]}.sort_by{|o| o[1]}.last[0]
to_del = book.pparas.all
to_del.each do |para|
book.pparas.delete(para)
unless para == winning_para
DB[:paras].where(id: para.id).delete
end
end
if winning_para.chapname.nil?
book.chaps.last.paras << winning_para
else
c = Chap.create(name: winning_para.chapname,
auth: winning_para.auth)
book.chaps << c
c.paras << winning_para
end
#Now email everyone about the new paragraph that just came out!
DB[:subs].where(book_id: book.id).all.each do |row|
user_id = row[:user_id]
user = User.new(user_id)
if user.veri
Pony.mail(to: user.email,
from: "[email protected]",
subject: "New paragraph in #{book.strname}",
body: "Hey #{user.name}, the voting has ended and a new paragraph has been added! Go to http://www.storybouncer.com/book/#{book.id}/#{book.chaps.length}/ to see it! (copy+paste the url into your browser)",
html_body: makehtml do |h|
h.head{h.title{"New paragraph in #{CGI.escapeHTML(book.strname)}"}} #is this neccesary?
h.body do
h.span do
"Hey #{CGI.escapeHTML(user.name)}, the voting has ended and a new paragraph has been added! Go to <a href=\"http://www.storybouncer.com/book/#{book.id}/#{book.chaps.length}/\">http://www.storybouncer.com/book/#{book.id}/#{book.chaps.length}/</a> to see it! (if the link doesn't work, copy+paste the url into your browser)"
end
end
end)
#done!?
end
end
end
end
end