-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommunity_single_post_email_job.rb
72 lines (60 loc) · 2.11 KB
/
community_single_post_email_job.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
class CommunitySinglePostEmailJob
include MailUrls
@queue = :community_single_post_email
def self.url(path)
if path.start_with?("http")
path
else
if Rails.env.development?
"http://localhost:5000" + path
else
"https://www.ourcommonplace.com" + path
end
end
end
def self.asset_url(path)
if path.start_with?("http")
path
else
"https://s3.amazonaws.com/commonplace-mail-assets-production/#{path}"
end
end
def self.community_url(community, path)
CommunitySinglePostEmailJob.url("/#{community.slug}#{path}")
end
def self.show_announcement_url(community, id)
CommunitySinglePostEmailJob.community_url(community, "/show/announcements/#{id}")
end
def self.show_post_url(community, id)
CommunitySinglePostEmailJob.community_url(community, "/show/posts/#{id}")
end
def self.show_event_url(community, id)
CommunitySinglePostEmailJob.community_url(community, "/show/events/#{id}")
end
def self.message_user_url(community, id)
CommunitySinglePostEmailJob.community_url(community, "/message/users/#{id}")
end
def self.perform(community_id, time_start, time_end)
kickoff = KickOff.new
community = Community.find(community_id)
start_date = DateTime.parse(time_start)
end_date = DateTime.parse(time_end)
posts = community.posts.between(start_date, end_date).map do |post|
Serializer::serialize(post).tap do |post|
post['replies'].each do |reply|
reply['published_at'] = reply['published_at'].strftime("%l:%M%P")
reply['avatar_url'] = CommunitySinglePostEmailJob.asset_url(reply['avatar_url'])
end
post['avatar_url'] = CommunitySinglePostEmailJob.asset_url(post['avatar_url'])
post['url'] = CommunitySinglePostEmailJob.show_post_url(community, post['id'])
post['new_message_url'] = CommunitySinglePostEmailJob.message_user_url(community, post['user_id'])
end
end
community.users.receives_posts_live_limited.each do |user|
begin
kickoff.deliver_single_post_email(user.id, posts.sample)
rescue
end
end
end
end