Skip to content

Commit

Permalink
create mailer and worker for updated discussions
Browse files Browse the repository at this point in the history
  • Loading branch information
arpansac committed Dec 25, 2018
1 parent 07c8cc0 commit 6795e8a
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 2 deletions.
7 changes: 7 additions & 0 deletions app/assets/stylesheets/discussions.scss
@@ -1,5 +1,10 @@
.user-discussion-card{
width: 100%;

&:hover{
box-shadow: 1px 1px 1px 1px lightgrey;
}

.existing-profile-image{
width: 18px;

Expand All @@ -21,6 +26,8 @@

.discussion-messages-list{
margin-bottom: 5px;
max-height: 250px;
overflow-y: scroll;

li{
padding: 0;
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/discussions_controller.rb
Expand Up @@ -10,6 +10,13 @@ def create
@discussion.user = current_user

@discussion.save

DiscussionFollower.find_or_create_by(user_id: current_user.id, discussion_id: @discussion.id)

if (@discussion.parent_type == 'SpeakerResource')
DiscussionFollower.find_or_create_by(user_id: @discussion.parent.user.id, discussion_id: @discussion.id)
end

end


Expand Down
7 changes: 7 additions & 0 deletions app/controllers/user_messages_controller.rb
Expand Up @@ -8,6 +8,13 @@ def create
@user_message.user = current_user

@user_message.save

if (@user_message.parent_type == 'Discussion')
# add the user to follow that discussion
DiscussionFollower.find_or_create_by(user_id: current_user.id, discussion_id: @user_message.parent_id)

end

end


Expand Down
12 changes: 12 additions & 0 deletions app/mailers/event_communication_mailer.rb
Expand Up @@ -128,6 +128,18 @@ def comment_reply_email(user, comment, event)
to: @user.email,
subject: "#{@event.name} :: #{@replying_user.name} :: New Comment"
)
end


def discussions_updated_email(user, discussions)

@user = user
@discussions = discussions

mail(
to: @user.email,
subject: 'Communities :: Discussion Updates On Sessions You Follow'
)

end

Expand Down
3 changes: 3 additions & 0 deletions app/models/data_form_entity_response_group.rb
Expand Up @@ -11,6 +11,9 @@ class DataFormEntityResponseGroup < ApplicationRecord
has_one :speaker_resource


delegate :event, :to => :event_data_form_entity_group, :allow_nil => true


# setting the default value of registration_status
attribute :registration_status_id, :integer, default: RegistrationStatus.find_by(name: "registered").id

Expand Down
1 change: 1 addition & 0 deletions app/models/discussion.rb
Expand Up @@ -4,6 +4,7 @@ class Discussion < ApplicationRecord


has_many :user_messages, as: :parent
has_many :discussion_followers


end
2 changes: 2 additions & 0 deletions app/models/speaker_resource.rb
Expand Up @@ -5,5 +5,7 @@ class SpeakerResource < ApplicationRecord


delegate :user, :to => :data_form_entity_response_group, :allow_nil => true
delegate :event, :to => :data_form_entity_response_group, :allow_nil => true


end
1 change: 1 addition & 0 deletions app/models/user.rb
Expand Up @@ -18,6 +18,7 @@ class User < ApplicationRecord
has_many :user_event_locations
has_many :fixed_email_edfegs
has_many :event_updates
has_many :discussion_followers

has_one_attached :profile_image

Expand Down
@@ -0,0 +1,50 @@

<table style="width: 550px; margin: 0 auto; background-color: white; border-radius: 5px; padding: 25px;">
<tbody>

<tr>
<td>
<p>Hi <%= @user.name %>!</p>
<p>
Here's the list of your followed discussions which have updates!
</p>
<div style="text-align: center">
</div>
<div>
<ul style="padding: 0 2px;">
<% @discussions.group_by(&:parent).each do |parent, discussions| %>
<li style="list-style: none; padding: 3px; background: #84f1ff12;">
<h4> Session :: <%= parent.title.blank? ? parent.event.name : parent.title %></h4>
<ul style="padding: 0;">
<% discussions.each do |discussion| %>
<li style="list-style: none; padding: 5px; background: #d3d3d33b; border-bottom: 1px dotted lightgrey;">
<p style="font-weight: bold; margin: 0;">
<%= discussion.topic %>
<br>
<small style="background-color: #f99952; color: white; border-radius: 15px; padding: 2px 5px;">
<% if discussion.discussion_followers.length == 1 %>
New!
<% else %>
<%= discussion.discussion_followers.length %> Followers
<% end %>
</small>
</p>
<p style="margin: 0;">
<small>
<%= simple_format(discussion.description) %>
</small>
</p>
</li>
<% end %>
</ul>
</li>
<% end %>
</ul>
</div>

</td>
</tr>

</tbody>

</table>
2 changes: 1 addition & 1 deletion app/views/speaker_resources/session_discussions.html.erb
Expand Up @@ -51,7 +51,7 @@
<div class="current-discussions">
<% if allowed_view?(:discussions, :create) %>
<button type="button" class="btn btn-primary mb-3" data-toggle="modal" data-target="#new-discussion-modal">
Ask The Speaker
Ask <%= @resource.user.name %>!
</button>
<% else %>
<%= link_to "Sign In / Sign Up To Create Discussion", user_google_oauth2_omniauth_authorize_path, class: 'btn btn-primary mb-3' %>
Expand Down
24 changes: 24 additions & 0 deletions app/workers/discussion_messages_worker.rb
@@ -0,0 +1,24 @@
class DiscussionMessagesWorker
@queue = :discussion_messages_mails

# send an email for all the new messages to the speaker and the person who created the discussion
# this email needs to be sent exactly at 11.59pm everyday for perfect functioning
def self.perform
# get all the discussions which were created from the last time
discussions = Discussion.where(created_at: Time.now.beginning_of_day..Time.now)

# get all the messages which were created from the last time
messages = UserMessage.includes(:user).where(created_at: Time.now.beginning_of_day..Time.now, parent_type: "Discussion")


all_discussion_ids = (discussions.map(&:id) + messages.map(&:parent_id)).uniq
# get all the discussion followers and send them an update about all these discussions updated
DiscussionFollower.where(discussion_id: all_discussion_ids).group_by(&:user).each do |user, followers|
discussions = Discussion.where(id: followers.map(&:discussion_id)).uniq
EventCommunicationMailer.discussions_updated_email(user, discussions).deliver_now
end



end
end
@@ -0,0 +1,5 @@
class AddSendDiscussionMailsToUsers < ActiveRecord::Migration[5.2]
def change
add_column :users, :send_discussion_mails, :boolean, default: true
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_12_18_010416) do
ActiveRecord::Schema.define(version: 2018_12_24_154449) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -462,6 +462,7 @@
t.text "linkedin"
t.text "github"
t.text "twitter"
t.boolean "send_discussion_mails", default: true
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
Expand Down
Binary file modified erd.pdf
Binary file not shown.
6 changes: 6 additions & 0 deletions test/mailers/previews/event_communication_mailer_preview.rb
Expand Up @@ -46,4 +46,10 @@ def comment_reply_email
)
end


def discussions_updated_email
EventCommunicationMailer.discussions_updated_email(SpeakerResource.last.user, Discussion.all.limit(25))

end

end

0 comments on commit 6795e8a

Please sign in to comment.