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], Always, Never.

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 Feb 26, 2023
1 parent 3a9fb76 commit 21714cd
Show file tree
Hide file tree
Showing 20 changed files with 343 additions and 44 deletions.
7 changes: 7 additions & 0 deletions api_docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ format used by the Zulip server that they are interacting with.

## Changes in Zulip 7.0

**Feature level 165**

* [`PATCH /realm/user_settings_defaults`](/api/update-realm-user-settings-defaults),
[`POST /register`](/api/register-queue),
[`PATCH /settings`](/api/update-settings): Replaced the `realm_name_in_notifications`
boolean field with an integer field `realm_name_in_notifications_policy`.

**Feature level 164**

* [`POST /register`](/api/register-queue): Added the
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 %}
2 changes: 1 addition & 1 deletion version.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# Changes should be accompanied by documentation explaining what the
# new level means in api_docs/changelog.md, as well as "**Changes**"
# entries in the endpoint's documentation in `zulip.yaml`.
API_FEATURE_LEVEL = 164
API_FEATURE_LEVEL = 165

# Bump the minor PROVISION_VERSION to indicate that folks should provision
# only when going from an old version of the code to a newer version. Bump
Expand Down
2 changes: 2 additions & 0 deletions web/src/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ 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_policy_values:
settings_config.realm_name_in_notifications_policy_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 web/src/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_policy: number;
starred_message_counts: boolean;
translate_emoticons: boolean;
twenty_four_hour_time: boolean;
Expand Down
2 changes: 2 additions & 0 deletions web/src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ 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_policy_values:
settings_config.realm_name_in_notifications_policy_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
23 changes: 18 additions & 5 deletions web/src/settings_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ export const notification_settings_labels = {
message_content_in_email_notifications: $t({
defaultMessage: "Include message content in message notification emails",
}),
realm_name_in_notifications: $t({
realm_name_in_notifications_policy: $t({
defaultMessage: "Include organization name in subject of message notification emails",
}),
};
Expand Down 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_policy"],
["notification_sound"],
);

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

export const realm_name_in_notifications_policy_values = {
automatic: {
code: 1,
description: $t({defaultMessage: "Automatic"}),
},
always: {
code: 2,
description: $t({defaultMessage: "Always"}),
},
never: {
code: 3,
description: $t({defaultMessage: "Never"}),
},
};

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

const $realm_name_in_notifications_policy_dropdown = $container.find(
".setting_realm_name_in_notifications_policy",
);
$realm_name_in_notifications_policy_dropdown.val(
settings_object.realm_name_in_notifications_policy,
);

set_enable_digest_emails_visibility(settings_panel);

if (for_realm_settings) {
Expand Down Expand Up @@ -232,7 +239,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_policy": {
$container.find(`.setting_${CSS.escape(setting)}`).val(settings_object[setting]);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion web/src/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_policy: number;
user_list_style: number;
starred_message_counts: boolean;
translate_emoticons: boolean;
Expand Down
7 changes: 7 additions & 0 deletions web/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_policy" class="dropdown-title">{{ settings_label.realm_name_in_notifications_policy }}</label>
<select name="realm_name_in_notifications_policy" class="setting_realm_name_in_notifications_policy prop-element settings_select bootstrap-focus-style" id="{{prefix}}realm_name_in_notifications_policy" data-setting-widget-type="number">
{{> dropdown_options_widget option_values=realm_name_in_notifications_policy_values}}
</select>
</div>

{{#each notification_settings.email_message_notification_settings}}
{{> settings_checkbox
setting_name=this
Expand Down
24 changes: 23 additions & 1 deletion zerver/lib/email_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,26 @@ 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_policy' set to'automatic',
# return true only if user is part of multiple realms.
if (
user_profile.realm_name_in_notifications_policy
== UserProfile.REALM_NAME_IN_NOTIFICATIONS_POLICY_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_policy
== UserProfile.REALM_NAME_IN_NOTIFICATIONS_POLICY_ALWAYS
)


@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 @@ -417,7 +437,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,93 @@
# 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_POLICY_AUTOMATIC = 1
REALM_NAME_IN_NOTIFICATIONS_POLICY_ALWAYS = 2
REALM_NAME_IN_NOTIFICATIONS_POLICY_NEVER = 3


# The value of 'realm_name_in_notifications_policy' for those users
# who have manually changed the value of 'realm_name_in_notifications' as 'true'
# should be updated as 'Always', not 'Automatic'
def update_realm_name_in_notifications_policy_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_policy=REALM_NAME_IN_NOTIFICATIONS_POLICY_ALWAYS
)


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


def update_realm_name_in_notifications_policy_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_policy=REALM_NAME_IN_NOTIFICATIONS_POLICY_ALWAYS
)


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_policy=REALM_NAME_IN_NOTIFICATIONS_POLICY_ALWAYS
)
realmuserdefaults.update(realm_name_in_notifications=True)


class Migration(migrations.Migration):
dependencies = [
("zerver", "0430_fix_audit_log_objects_for_group_based_stream_settings"),
]

operations = [
migrations.AddField(
model_name="realmuserdefault",
name="realm_name_in_notifications_policy",
field=models.PositiveSmallIntegerField(
default=REALM_NAME_IN_NOTIFICATIONS_POLICY_AUTOMATIC
),
),
migrations.RunPython(
update_realm_name_in_notifications_policy_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.AddField(
model_name="userprofile",
name="realm_name_in_notifications_policy",
field=models.PositiveSmallIntegerField(
default=REALM_NAME_IN_NOTIFICATIONS_POLICY_AUTOMATIC
),
),
migrations.RunPython(
update_realm_name_in_notifications_policy_values,
reverse_code=reverse_code,
elidable=True,
),
migrations.RemoveField(
model_name="userprofile",
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 @@ -1589,9 +1589,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_POLICY_AUTOMATIC = 1
REALM_NAME_IN_NOTIFICATIONS_POLICY_ALWAYS = 2
REALM_NAME_IN_NOTIFICATIONS_POLICY_NEVER = 3
REALM_NAME_IN_NOTIFICATIONS_POLICY_CHOICES = [
REALM_NAME_IN_NOTIFICATIONS_POLICY_AUTOMATIC,
REALM_NAME_IN_NOTIFICATIONS_POLICY_ALWAYS,
REALM_NAME_IN_NOTIFICATIONS_POLICY_NEVER,
]
realm_name_in_notifications_policy = models.PositiveSmallIntegerField(
default=REALM_NAME_IN_NOTIFICATIONS_POLICY_AUTOMATIC
)

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

Expand Down Expand Up @@ -1663,7 +1674,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_policy=int,
wildcard_mentions_notify=bool,
)

Expand Down

0 comments on commit 21714cd

Please sign in to comment.