Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

user_settings: Validate parameters with Zod.

Anders Kaseorged1c169bfbea
diff --git a/web/src/state_data.ts b/web/src/state_data.ts
index 4381a01b2ba..8ab5a1e6555 100644
--- a/web/src/state_data.ts
+++ b/web/src/state_data.ts
@@ -1,5 +1,7 @@
import {z} from "zod";
+import {user_settings_schema} from "./user_settings";
+
const NOT_TYPED_YET = z.unknown();
const group_permission_setting_schema = z.object({
@@ -281,7 +283,11 @@ export const state_data_schema = z
.and(z.object({muted_users: NOT_TYPED_YET}).transform((muted_users) => ({muted_users})))
.and(z.object({user_topics: NOT_TYPED_YET}).transform((user_topics) => ({user_topics})))
.and(z.object({user_status: NOT_TYPED_YET}).transform((user_status) => ({user_status})))
- .and(z.object({user_settings: NOT_TYPED_YET}).transform((user_settings) => ({user_settings})))
+ .and(
+ z
+ .object({user_settings: user_settings_schema})
+ .transform((user_settings) => ({user_settings})),
+ )
.and(
z
.object({realm_user_settings_defaults: NOT_TYPED_YET})
diff --git a/web/src/user_settings.ts b/web/src/user_settings.ts
index e7270a910ea..3ac08102475 100644
--- a/web/src/user_settings.ts
+++ b/web/src/user_settings.ts
@@ -1,72 +1,83 @@
-export type StreamNotificationSettings = {
- enable_stream_audible_notifications: boolean;
- enable_stream_desktop_notifications: boolean;
- enable_stream_email_notifications: boolean;
- enable_stream_push_notifications: boolean;
- wildcard_mentions_notify: boolean;
-};
+import {z} from "zod";
-export type PmNotificationSettings = {
- enable_desktop_notifications: boolean;
- enable_offline_email_notifications: boolean;
- enable_offline_push_notifications: boolean;
- enable_sounds: boolean;
-};
+import type {StateData} from "./state_data";
-export type FollowedTopicNotificationSettings = {
- enable_followed_topic_audible_notifications: boolean;
- enable_followed_topic_desktop_notifications: boolean;
- enable_followed_topic_email_notifications: boolean;
- enable_followed_topic_push_notifications: boolean;
- enable_followed_topic_wildcard_mentions_notify: boolean;
-};
+export const stream_notification_settings_schema = z.object({
+ enable_stream_audible_notifications: z.boolean(),
+ enable_stream_desktop_notifications: z.boolean(),
+ enable_stream_email_notifications: z.boolean(),
+ enable_stream_push_notifications: z.boolean(),
+ wildcard_mentions_notify: z.boolean(),
+});
+export type StreamNotificationSettings = z.infer<typeof stream_notification_settings_schema>;
-export type UserSettings = (StreamNotificationSettings &
- PmNotificationSettings &
- FollowedTopicNotificationSettings) & {
- automatically_follow_topics_policy: number;
- automatically_follow_topics_where_mentioned: boolean;
- automatically_unmute_topics_in_muted_streams_policy: number;
- color_scheme: number;
- default_language: string;
- demote_inactive_streams: number;
- dense_mode: boolean;
- desktop_icon_count_display: number;
- display_emoji_reaction_users: boolean;
- email_notifications_batching_period_seconds: number;
- emojiset: string;
- enable_digest_emails: boolean;
- enable_drafts_synchronization: boolean;
- enable_login_emails: boolean;
- enable_marketing_emails: boolean;
- enable_online_push_notifications: boolean;
- enter_sends: boolean;
- fluid_layout_width: boolean;
- high_contrast_mode: boolean;
- message_content_in_email_notifications: boolean;
- notification_sound: string;
- pm_content_in_desktop_notifications: boolean;
- presence_enabled: boolean;
- realm_name_in_email_notifications_policy: number;
- receives_typing_notifications: boolean;
- send_private_typing_notifications: boolean;
- send_read_receipts: boolean;
- send_stream_typing_notifications: boolean;
- starred_message_counts: boolean;
- timezone: string;
- translate_emoticons: boolean;
- twenty_four_hour_time: boolean;
- user_list_style: number;
- web_escape_navigates_to_home_view: boolean;
- web_font_size_px: number;
- web_home_view: "inbox" | "recent_topics" | "all_messages";
- web_line_height_percent: number;
- web_mark_read_on_scroll_policy: number;
- web_stream_unreads_count_display_policy: number;
-};
+export const pm_notification_settings_schema = z.object({
+ enable_desktop_notifications: z.boolean(),
+ enable_offline_email_notifications: z.boolean(),
+ enable_offline_push_notifications: z.boolean(),
+ enable_sounds: z.boolean(),
+});
+export type PmNotificationSettings = z.infer<typeof pm_notification_settings_schema>;
+
+export const followed_topic_notification_settings_schema = z.object({
+ enable_followed_topic_audible_notifications: z.boolean(),
+ enable_followed_topic_desktop_notifications: z.boolean(),
+ enable_followed_topic_email_notifications: z.boolean(),
+ enable_followed_topic_push_notifications: z.boolean(),
+ enable_followed_topic_wildcard_mentions_notify: z.boolean(),
+});
+export type FollowedTopicNotificationSettings = z.infer<
+ typeof followed_topic_notification_settings_schema
+>;
+
+export const user_settings_schema = stream_notification_settings_schema
+ .merge(pm_notification_settings_schema)
+ .merge(followed_topic_notification_settings_schema)
+ .extend({
+ automatically_follow_topics_policy: z.number(),
+ automatically_follow_topics_where_mentioned: z.boolean(),
+ automatically_unmute_topics_in_muted_streams_policy: z.number(),
+ color_scheme: z.number(),
+ default_language: z.string(),
+ demote_inactive_streams: z.number(),
+ dense_mode: z.boolean(),
+ desktop_icon_count_display: z.number(),
+ display_emoji_reaction_users: z.boolean(),
+ email_notifications_batching_period_seconds: z.number(),
+ emojiset: z.string(),
+ enable_digest_emails: z.boolean(),
+ enable_drafts_synchronization: z.boolean(),
+ enable_login_emails: z.boolean(),
+ enable_marketing_emails: z.boolean(),
+ enable_online_push_notifications: z.boolean(),
+ enter_sends: z.boolean(),
+ fluid_layout_width: z.boolean(),
+ high_contrast_mode: z.boolean(),
+ message_content_in_email_notifications: z.boolean(),
+ notification_sound: z.string(),
+ pm_content_in_desktop_notifications: z.boolean(),
+ presence_enabled: z.boolean(),
+ realm_name_in_email_notifications_policy: z.number(),
+ receives_typing_notifications: z.boolean(),
+ send_private_typing_notifications: z.boolean(),
+ send_read_receipts: z.boolean(),
+ send_stream_typing_notifications: z.boolean(),
+ starred_message_counts: z.boolean(),
+ timezone: z.string(),
+ translate_emoticons: z.boolean(),
+ twenty_four_hour_time: z.boolean(),
+ user_list_style: z.number(),
+ web_escape_navigates_to_home_view: z.boolean(),
+ web_font_size_px: z.number(),
+ web_home_view: z.enum(["inbox", "recent_topics", "all_messages"]),
+ web_line_height_percent: z.number(),
+ web_mark_read_on_scroll_policy: z.number(),
+ web_stream_unreads_count_display_policy: z.number(),
+ });
+export type UserSettings = z.infer<typeof user_settings_schema>;
export let user_settings: UserSettings;
-export function initialize_user_settings(params: {user_settings: UserSettings}): void {
+export function initialize_user_settings(params: StateData["user_settings"]): void {
user_settings = params.user_settings;
}