This repository was archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
FEATURE: Chat notification emails #838
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2a80a8d
FEATURE: Chat notification emails
romanrizzi 72c2f7c
Use existing logic for dm channel titles. Recently improved in #859
romanrizzi 5aacdd2
Use correct format for constants in chat-preferences
romanrizzi c16e7ef
FIX: Ensure we won't send repeated mentions and remove unsubscribe link
romanrizzi 4a87f78
View messages link should highlight the first mention included in the…
romanrizzi 984a92f
Extra security check before sendin summaries
romanrizzi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module Jobs | ||
class EmailChatNotifications < ::Jobs::Scheduled | ||
every 5.minutes | ||
|
||
def execute(args = {}) | ||
return unless SiteSetting.chat_enabled | ||
|
||
DiscourseChat::ChatMailer.send_unread_mentions_summary | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<div class="summary-email"> | ||
<table class="chat-summary-header text-header with-dir" style="background-color:#<%= @header_bgcolor -%>;width:100%;min-width:100%;"> | ||
<tr> | ||
<td align="center" style="text-align: center;padding: 20px 0; font-family:Arial,sans-serif;"> | ||
|
||
<a href="<%= Discourse.base_url %>" style="color:#<%= @header_color -%>;font-size:22px;text-decoration:none;"> | ||
<%- if logo_url.blank? %> | ||
<%= SiteSetting.title %> | ||
<%- else %> | ||
<img src="<%= logo_url %>" height="40" style="clear:both;display:block;height:40px;margin:auto;max-width:100%;outline:0;text-decoration:none;" alt="<%= SiteSetting.title %>"> | ||
<%- end %> | ||
</a> | ||
|
||
</td> | ||
</tr> | ||
<tr> | ||
<td align="center" style="font-weight:bold;font-size:22px;color:#0a0a0a"> | ||
<%= I18n.t("user_notifications.chat_summary.description", count: @messages.size) %> | ||
</td> | ||
</tr> | ||
</table> | ||
|
||
<%- @grouped_mentions.each do |chat_channel, messages| %> | ||
<%- other_messages_count = messages.size - 2 %> | ||
<table class="chat-summary-content" style="padding:1em;margin-top:20px;width:100%;min-width:100%;background-color:#f7f7f7;"> | ||
<tbody> | ||
<tr> | ||
<td colspan="100%"> | ||
<h5 style="margin:0.5em 0 0.5em 0;font-size:0.9em;"><%= chat_channel.title(@user) %></h5> | ||
</td> | ||
</tr> | ||
<%- messages.take(2).each do |chat_message| %> | ||
<%- sender = chat_message.user %> | ||
<%- sender_name = @display_usernames ? sender.username : sender.name %> | ||
|
||
<tr class="message-row"> | ||
<td style="white-space:nowrap;vertical-align:top;padding:<%= rtl? ? '1em 2em 0 0' : '1em 0 0 2em' %>"> | ||
<img src="<%= sender.small_avatar_url -%>" style="height:20px;width:20px;margin:<%= rtl? ? '0 0 5px 0' : '0 5px 0 0' %>;border-radius:50%;clear:both;display:inline-block;outline:0;text-decoration:none;vertical-align:top;" alt="<%= sender_name -%>"> | ||
<span style="display:inline-block;color:#0a0a0a;vertical-align:top;font-weight:bold;"> | ||
<%= sender_name -%> | ||
</span> | ||
<span style="display:inline-block;color:#0a0a0a;font-size:0.8em;"> | ||
<%= I18n.l(@user_tz.to_local(chat_message.created_at), format: :long) -%> | ||
</span> | ||
</td> | ||
<td style="width:99%;margin:0;padding:<%= rtl? ? '0 2em 0 0' : '0 0 0 2em' %>;vertical-align:top;"> | ||
<%= email_excerpt(chat_message.cooked_for_excerpt) %> | ||
</td> | ||
</tr> | ||
<%- end %> | ||
<tr> | ||
<td colspan="100%" style="padding:<%= rtl? ? '2em 2em 0 0' : '2em 0 0 2em' %>"> | ||
<a class="more-messages-link" href="<%= messages.first.url %>"> | ||
<%- if other_messages_count <= 0 %> | ||
<%= I18n.t("user_notifications.chat_summary.view_messages", count: messages.size)%> | ||
<%- else %> | ||
<%= I18n.t("user_notifications.chat_summary.view_more", count: other_messages_count)%> | ||
<%- end %> | ||
</a> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
<%- end %> | ||
</div> | ||
|
||
<table class='summary-footer with-dir' style="margin-top:2em;width:100%"> | ||
<tr> | ||
<td align="center"> | ||
<%= raw(t 'user_notifications.chat_summary.unsubscribe', | ||
site_link: html_site_link, | ||
email_preferences_link: link_to(t('user_notifications.chat_summary.your_chat_settings'), Discourse.base_url + '/my/preferences/chat')) | ||
%> | ||
</td> | ||
</tr> | ||
</table> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<%- site_link = raw(@markdown_linker.create(@site_name, '/')) %> | ||
<%= t('user_notifications.chat_summary.description', count: @messages.size,) %> | ||
<%= raw(@markdown_linker.create(t("user_notifications.chat_summary.view_messages", count: @messages.size), "/chat")) %> | ||
<%= raw(t :'user_notifications.chat_summary.unsubscribe', | ||
site_link: site_link, | ||
email_preferences_link: @markdown_linker.create(t('user_notifications.chat_summary.your_chat_settings'), Discourse.base_url + '/my/preferences/chat')) | ||
%> | ||
<%= raw(@markdown_linker.references) %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
db/migrate/20220518140004_track_last_unread_mention_when_emailed.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class TrackLastUnreadMentionWhenEmailed < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :user_chat_channel_memberships, :last_unread_mention_when_emailed_id, :integer | ||
end | ||
end |
14 changes: 14 additions & 0 deletions
14
db/post_migrate/20220516142658_remove_email_statuses_table.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class RemoveEmailStatusesTable < ActiveRecord::Migration[7.0] | ||
def up | ||
remove_index :chat_message_email_statuses, :status | ||
remove_index :chat_message_email_statuses, [:user_id, :chat_message_id] | ||
|
||
Migration::TableDropper.execute_drop("chat_message_email_statuses") | ||
end | ||
|
||
def down | ||
raise ActiveRecord::IrreversibleMigration | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
db/post_migrate/20220518180642_remove_user_option_last_emailed_at.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class RemoveUserOptionLastEmailedAt < ActiveRecord::Migration[7.0] | ||
def change | ||
remove_column :user_options, :last_emailed_for_chat, :datetime | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
class DiscourseChat::ChatMailer | ||
def send_unread_mentions_summary | ||
return unless SiteSetting.chat_enabled | ||
|
||
users_with_unprocessed_unread_mentions.find_each do |user| | ||
# user#memberships_with_unread_messages is a nested array that looks like [[membership_id, unread_message_id]] | ||
# Find the max unread id per membership. | ||
membership_and_max_unread_mention_ids = user.memberships_with_unread_messages | ||
.group_by { |memberships| memberships[0] } | ||
.transform_values do |membership_and_msg_ids| | ||
membership_and_msg_ids.max_by { |membership, msg| msg } | ||
end.values | ||
|
||
Jobs.enqueue(:user_email, | ||
type: "chat_summary", | ||
user_id: user.id, | ||
force_respect_seen_recently: true, | ||
memberships_to_update_data: membership_and_max_unread_mention_ids | ||
) | ||
end | ||
end | ||
|
||
private | ||
|
||
def users_with_unprocessed_unread_mentions | ||
when_away_frequency = UserOption.chat_email_frequencies[:when_away] | ||
allowed_group_ids = DiscourseChat.allowed_group_ids | ||
|
||
User | ||
.select('users.id', 'ARRAY_AGG(ARRAY[uccm.id, c_msg.id]) AS memberships_with_unread_messages') | ||
.joins(:user_option) | ||
.where(user_options: { chat_enabled: true, chat_email_frequency: when_away_frequency }) | ||
.where('users.last_seen_at < ?', 15.minutes.ago) | ||
.joins(:groups) | ||
.where(groups: { id: allowed_group_ids }) | ||
.joins('INNER JOIN user_chat_channel_memberships uccm ON uccm.user_id = users.id') | ||
.joins('INNER JOIN chat_channels cc ON cc.id = uccm.chat_channel_id') | ||
.joins('INNER JOIN chat_messages c_msg ON c_msg.chat_channel_id = uccm.chat_channel_id') | ||
.joins('LEFT OUTER JOIN chat_mentions c_mentions ON c_mentions.chat_message_id = c_msg.id') | ||
.where(uccm: { following: true }) | ||
.where('c_msg.deleted_at IS NULL AND c_msg.user_id <> users.id') | ||
.where( | ||
<<~SQL | ||
(c_mentions.user_id = uccm.user_id OR cc.chatable_type = 'DirectMessageChannel') AND | ||
(uccm.last_read_message_id IS NULL OR c_msg.id > uccm.last_read_message_id) AND | ||
(uccm.last_unread_mention_when_emailed_id IS NULL OR c_msg.id > uccm.last_unread_mention_when_emailed_id) | ||
SQL | ||
) | ||
.group('users.id, uccm.user_id') | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.