Skip to content

Commit

Permalink
user_settings: Automate 'Include realm name in message email subject'.
Browse files Browse the repository at this point in the history
Currently, there is a checkbox setting for whether to
"Include realm name in subject of message notification emails".

This commit replaces the checkbox setting with a dropdown
having values: Automatic [default], Yes, No.

The Automatic option includes the realm name if, and only if,
there are multiple Zulip realms associated with the user's email.

Tests are added and(or) modified.

Fixes: zulip#19905.
  • Loading branch information
prakhar1144 committed Jan 26, 2023
1 parent d05f672 commit d80374a
Show file tree
Hide file tree
Showing 18 changed files with 296 additions and 31 deletions.
1 change: 1 addition & 0 deletions static/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export function build_page() {
realm_user_settings_defaults.enable_stream_audible_notifications,
email_notifications_batching_period_values:
settings_config.email_notifications_batching_period_values,
realm_name_in_notifications_values: settings_config.realm_name_in_notifications_values,
twenty_four_hour_time_values: settings_config.twenty_four_hour_time_values,
create_web_public_stream_policy_values:
settings_config.create_web_public_stream_policy_values,
Expand Down
2 changes: 1 addition & 1 deletion static/js/realm_user_settings_defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type RealmDefaultSettings = {
notification_sound: string;
pm_content_in_desktop_notifications: boolean;
presence_enabled: boolean;
realm_name_in_notifications: boolean;
realm_name_in_notifications: number;
starred_message_counts: boolean;
translate_emoticons: boolean;
twenty_four_hour_time: boolean;
Expand Down
1 change: 1 addition & 0 deletions static/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export function build_page() {
notification_settings: settings_config.all_notifications(user_settings).settings,
email_notifications_batching_period_values:
settings_config.email_notifications_batching_period_values,
realm_name_in_notifications_values: settings_config.realm_name_in_notifications_values,
desktop_icon_count_display_values: settings_config.desktop_icon_count_display_values,
show_push_notifications_tooltip:
settings_config.all_notifications(user_settings).show_push_notifications_tooltip,
Expand Down
21 changes: 17 additions & 4 deletions static/js/settings_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,10 +677,7 @@ export const email_notifications_batching_period_values = [
},
];

const email_message_notification_settings = [
"message_content_in_email_notifications",
"realm_name_in_notifications",
];
const email_message_notification_settings = ["message_content_in_email_notifications"];

const other_email_settings = [
"enable_digest_emails",
Expand All @@ -697,6 +694,7 @@ const other_notification_settings = desktop_notification_settings.concat(
mobile_notification_settings,
email_notification_settings,
["email_notifications_batching_period_seconds"],
["realm_name_in_notifications"],
["notification_sound"],
);

Expand Down Expand Up @@ -787,6 +785,21 @@ export const all_notifications = (settings_object: Settings): AllNotifications =
},
});

export const realm_name_in_notifications_values = {
automatic: {
code: 1,
description: $t({defaultMessage: "Automatic"}),
},
yes: {
code: 2,
description: $t({defaultMessage: "Yes"}),
},
no: {
code: 3,
description: $t({defaultMessage: "No"}),
},
};

export const desktop_icon_count_display_values = {
messages: {
code: 1,
Expand Down
8 changes: 7 additions & 1 deletion static/js/settings_notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ export function set_up(settings_panel) {
settings_object.email_notifications_batching_period_seconds,
);

const $realm_name_in_notifications_dropdown = $container.find(
".setting_realm_name_in_notifications",
);
$realm_name_in_notifications_dropdown.val(settings_object.realm_name_in_notifications);

set_enable_digest_emails_visibility(settings_panel);

if (for_realm_settings) {
Expand Down Expand Up @@ -232,7 +237,8 @@ export function update_page(settings_panel) {
set_notification_batching_ui($container, settings_object[setting]);
break;
}
case "notification_sound": {
case "notification_sound":
case "realm_name_in_notifications": {
$container.find(`.setting_${CSS.escape(setting)}`).val(settings_object[setting]);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion static/js/user_settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type UserSettings = (StreamNotificationSettings & PmNotificationSettings)
notification_sound: string;
pm_content_in_desktop_notifications: boolean;
presence_enabled: boolean;
realm_name_in_notifications: boolean;
realm_name_in_notifications: number;
user_list_style: number;
starred_message_counts: boolean;
translate_emoticons: boolean;
Expand Down
7 changes: 7 additions & 0 deletions static/templates/settings/notification_settings.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@
</div>
</div>

<div class="input-group">
<label for="realm_name_in_notifications" class="dropdown-title">{{ settings_label.realm_name_in_notifications }}</label>
<select name="realm_name_in_notifications" class="setting_realm_name_in_notifications prop-element" id="{{prefix}}realm_name_in_notifications" data-setting-widget-type="number">
{{> dropdown_options_widget option_values=realm_name_in_notifications_values}}
</select>
</div>

{{#each notification_settings.email_message_notification_settings}}
{{> settings_checkbox
setting_name=this
Expand Down
2 changes: 1 addition & 1 deletion templates/zerver/emails/missed_message.subject.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
{% else %}
{% trans %}New messages{% endtrans %}
{% endif %}
{% if realm_name_in_notifications %} [{{ realm_str }}]
{% if realm_name_allowed_in_missedmessage_emails_subject %} [{{ realm_str }}]
{% endif %}
21 changes: 20 additions & 1 deletion zerver/lib/email_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,23 @@ def message_content_allowed_in_missedmessage_emails(user_profile: UserProfile) -
)


def realm_name_allowed_in_missedmessage_emails_subject(user_profile: UserProfile) -> bool:
# if 'user_profile.realm_name_in_notifications' set to 'automatic',
# return true only if user is part of multiple realms.
if (
user_profile.realm_name_in_notifications
== UserProfile.REALM_NAME_IN_NOTIFICATIONS_AUTOMATIC
):
realms_count = UserProfile.objects.filter(
delivery_email=user_profile.delivery_email,
is_active=True,
is_bot=False,
realm__deactivated=False,
).count()
return realms_count > 1
return user_profile.realm_name_in_notifications == UserProfile.REALM_NAME_IN_NOTIFICATIONS_YES


@statsd_increment("missed_message_reminders")
def do_send_missedmessage_events_reply_in_zulip(
user_profile: UserProfile, missed_messages: List[Dict[str, Any]], message_count: int
Expand Down Expand Up @@ -402,7 +419,9 @@ def do_send_missedmessage_events_reply_in_zulip(
name=user_profile.full_name,
message_count=message_count,
unsubscribe_link=unsubscribe_link,
realm_name_in_notifications=user_profile.realm_name_in_notifications,
realm_name_allowed_in_missedmessage_emails_subject=realm_name_allowed_in_missedmessage_emails_subject(
user_profile
),
)

mentioned_user_group_name = get_mentioned_user_group_name(missed_messages, user_profile)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Generated by Django 4.1.5 on 2023-01-11 20:21

from django.db import migrations, models
from django.db.backends.postgresql.schema import BaseDatabaseSchemaEditor
from django.db.migrations.state import StateApps

# We include a copy of this structure as it was at the time this
# migration was merged, since future should not impact the migration.
REALM_NAME_IN_NOTIFICATIONS_AUTOMATIC = 1
REALM_NAME_IN_NOTIFICATIONS_YES = 2
REALM_NAME_IN_NOTIFICATIONS_NO = 3

# The value of 'realm_name_in_notifications' for those users
# who have manually changed the value of 'realm_name_in_notifications' as 'true'
# should be updated as 'Yes', not 'Automatic'
def update_realm_name_in_notifications_new_values(
apps: StateApps, schema_editor: BaseDatabaseSchemaEditor
) -> None:
UserProfile = apps.get_model("zerver", "UserProfile")
userprofiles = UserProfile.objects.filter(realm_name_in_notifications=True)
userprofiles.update(realm_name_in_notifications_new=REALM_NAME_IN_NOTIFICATIONS_YES)


def reverse_code(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
UserProfile = apps.get_model("zerver", "UserProfile")
userprofiles = UserProfile.objects.filter(
realm_name_in_notifications_new=REALM_NAME_IN_NOTIFICATIONS_YES
)
userprofiles.update(realm_name_in_notifications=True)


def update_realm_name_in_notifications_new_values_for_realm_user_default(
apps: StateApps, schema_editor: BaseDatabaseSchemaEditor
) -> None:
RealmUserDefault = apps.get_model("zerver", "RealmUserDefault")
realmuserdefaults = RealmUserDefault.objects.filter(realm_name_in_notifications=True)
realmuserdefaults.update(realm_name_in_notifications_new=REALM_NAME_IN_NOTIFICATIONS_YES)


def reverse_code_for_realm_user_default(
apps: StateApps, schema_editor: BaseDatabaseSchemaEditor
) -> None:
RealmUserDefault = apps.get_model("zerver", "RealmUserDefault")
realmuserdefaults = RealmUserDefault.objects.filter(
realm_name_in_notifications_new=REALM_NAME_IN_NOTIFICATIONS_YES
)
realmuserdefaults.update(realm_name_in_notifications=True)


class Migration(migrations.Migration):

dependencies = [
("zerver", "0423_fix_email_gateway_attachment_owner"),
]

operations = [
migrations.AddField(
model_name="realmuserdefault",
name="realm_name_in_notifications_new",
field=models.PositiveSmallIntegerField(default=REALM_NAME_IN_NOTIFICATIONS_AUTOMATIC),
),
migrations.RunPython(
update_realm_name_in_notifications_new_values_for_realm_user_default,
reverse_code=reverse_code_for_realm_user_default,
elidable=True,
),
migrations.RemoveField(
model_name="realmuserdefault",
name="realm_name_in_notifications",
),
migrations.RenameField(
model_name="realmuserdefault",
old_name="realm_name_in_notifications_new",
new_name="realm_name_in_notifications",
),
migrations.AddField(
model_name="userprofile",
name="realm_name_in_notifications_new",
field=models.PositiveSmallIntegerField(default=REALM_NAME_IN_NOTIFICATIONS_AUTOMATIC),
),
migrations.RunPython(
update_realm_name_in_notifications_new_values,
reverse_code=reverse_code,
elidable=True,
),
migrations.RemoveField(
model_name="userprofile",
name="realm_name_in_notifications",
),
migrations.RenameField(
model_name="userprofile",
old_name="realm_name_in_notifications_new",
new_name="realm_name_in_notifications",
),
]
15 changes: 13 additions & 2 deletions zerver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1595,9 +1595,20 @@ class UserBaseSettings(models.Model):
enable_digest_emails = models.BooleanField(default=True)
enable_login_emails = models.BooleanField(default=True)
enable_marketing_emails = models.BooleanField(default=True)
realm_name_in_notifications = models.BooleanField(default=False)
presence_enabled = models.BooleanField(default=True)

REALM_NAME_IN_NOTIFICATIONS_AUTOMATIC = 1
REALM_NAME_IN_NOTIFICATIONS_YES = 2
REALM_NAME_IN_NOTIFICATIONS_NO = 3
REALM_NAME_IN_NOTIFICATIONS_CHOICES = [
REALM_NAME_IN_NOTIFICATIONS_AUTOMATIC,
REALM_NAME_IN_NOTIFICATIONS_YES,
REALM_NAME_IN_NOTIFICATIONS_NO,
]
realm_name_in_notifications = models.PositiveSmallIntegerField(
default=REALM_NAME_IN_NOTIFICATIONS_AUTOMATIC
)

# Whether or not the user wants to sync their drafts.
enable_drafts_synchronization = models.BooleanField(default=True)

Expand Down Expand Up @@ -1646,7 +1657,7 @@ class UserBaseSettings(models.Model):
notification_sound=str,
pm_content_in_desktop_notifications=bool,
presence_enabled=bool,
realm_name_in_notifications=bool,
realm_name_in_notifications=int,
wildcard_mentions_notify=bool,
)

Expand Down
46 changes: 35 additions & 11 deletions zerver/openapi/zulip.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9035,10 +9035,18 @@ paths:
- name: realm_name_in_notifications
in: query
description: |
Include organization name in subject of message notification emails.
Whether to [include organization name in subject of message notification emails](/help/email-notifications#include-organization-name-in-subject-line).

- 1 - Automatic
- 2 - Yes
- 3 - No
schema:
type: boolean
example: true
type: integer
enum:
- 1
- 2
- 3
example: 1
- name: presence_enabled
in: query
description: |
Expand Down Expand Up @@ -10984,9 +10992,13 @@ paths:
- 2 - Private messages and mentions
- 3 - None
realm_name_in_notifications:
type: boolean
type: integer
description: |
Include organization name in subject of message notification emails.
Whether to [include organization name in subject of message notification emails](/help/email-notifications#include-organization-name-in-subject-line).

- 1 - Automatic
- 2 - Yes
- 3 - No
presence_enabled:
type: boolean
description: |
Expand Down Expand Up @@ -11378,7 +11390,7 @@ paths:
client capability and access the `user_settings` object instead.
realm_name_in_notifications:
deprecated: true
type: boolean
type: integer
description: |
Present if `update_global_notifications` is present in `fetch_event_types`
and only for clients that did not include `user_settings_object` in their
Expand Down Expand Up @@ -12885,9 +12897,13 @@ paths:
- 2 - Private messages and mentions
- 3 - None
realm_name_in_notifications:
type: boolean
type: integer
description: |
Include organization name in subject of message notification emails.
Whether to [include organization name in subject of message notification emails](/help/email-notifications#include-organization-name-in-subject-line).

- 1 - Automatic
- 2 - Yes
- 3 - No
presence_enabled:
type: boolean
description: |
Expand Down Expand Up @@ -14040,13 +14056,21 @@ paths:
- name: realm_name_in_notifications
in: query
description: |
Include organization name in subject of message notification emails.
Whether to [include organization name in subject of message notification emails](/help/email-notifications#include-organization-name-in-subject-line).

- 1 - Automatic
- 2 - Yes
- 3 - No

**Changes**: Before Zulip 5.0 (feature level 80), this setting was managed by
the `PATCH /settings/notifications` endpoint.
schema:
type: boolean
example: true
type: integer
enum:
- 1
- 2
- 3
example: 1
- name: presence_enabled
in: query
description: |
Expand Down

0 comments on commit d80374a

Please sign in to comment.