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.

emoji: Validate parameters with Zod.

Anders Kaseorgd5cc7d7b7976
diff --git a/web/src/emoji.ts b/web/src/emoji.ts
index 2787e71b870..9d3aedc41b7 100644
--- a/web/src/emoji.ts
+++ b/web/src/emoji.ts
@@ -1,22 +1,13 @@
import _ from "lodash";
+import type {z} from "zod";
import * as blueslip from "./blueslip";
-import type {User} from "./people";
+import type {StateData, realm_emoji_map_schema, server_emoji_schema} from "./state_data";
// This is the data structure that we get from the server on initialization.
-export type ServerEmoji = {
- id: string;
- author_id: number;
- deactivated: boolean;
- name: string;
- source_url: string;
- still_url: string | null;
+export type ServerEmoji = z.infer<typeof server_emoji_schema>;
- // Added later in `settings_emoji.ts` when setting up the emoji settings.
- author?: User | null;
-};
-
-type RealmEmojiMap = Record<string, ServerEmoji>;
+type RealmEmojiMap = z.infer<typeof realm_emoji_map_schema>;
// The data the server provides about unicode emojis.
type ServerUnicodeEmojiData = {
@@ -27,11 +18,6 @@ type ServerUnicodeEmojiData = {
names: string[];
};
-type EmojiParams = {
- realm_emoji: RealmEmojiMap;
- emoji_codes: ServerUnicodeEmojiData;
-};
-
export type EmoticonTranslation = {
regex: RegExp;
replacement_text: string;
@@ -379,7 +365,9 @@ function build_default_emoji_aliases({
return map;
}
-export function initialize(params: EmojiParams): void {
+export function initialize(
+ params: StateData["emoji"] & {emoji_codes: ServerUnicodeEmojiData},
+): void {
emoji_codes = params.emoji_codes;
emoticon_translations = build_emoticon_translations({
diff --git a/web/src/state_data.ts b/web/src/state_data.ts
index fb89076019d..b2c947354e1 100644
--- a/web/src/state_data.ts
+++ b/web/src/state_data.ts
@@ -110,6 +110,20 @@ export const cross_realm_bot_schema = user_schema.and(
}),
);
+export const server_emoji_schema = z.object({
+ id: z.string(),
+ author_id: z.number(),
+ deactivated: z.boolean(),
+ name: z.string(),
+ source_url: z.string(),
+ still_url: z.string().nullable(),
+
+ // Added later in `settings_emoji.ts` when setting up the emoji settings.
+ author: user_schema.nullish(),
+});
+
+export const realm_emoji_map_schema = z.record(server_emoji_schema);
+
// Sync this with zerver.lib.events.do_events_register.
const current_user_schema = z.object({
avatar_source: z.string(),
@@ -311,7 +325,7 @@ const realm_schema = z.object({
export const state_data_schema = z
.object({alert_words: NOT_TYPED_YET})
.transform((alert_words) => ({alert_words}))
- .and(z.object({realm_emoji: NOT_TYPED_YET}).transform((emoji) => ({emoji})))
+ .and(z.object({realm_emoji: realm_emoji_map_schema}).transform((emoji) => ({emoji})))
.and(z.object({realm_bots: NOT_TYPED_YET}).transform((bot) => ({bot})))
.and(
z
diff --git a/web/src/ui_init.js b/web/src/ui_init.js
index 980e3742b01..0cbb2cb8a48 100644
--- a/web/src/ui_init.js
+++ b/web/src/ui_init.js
@@ -467,7 +467,7 @@ export function initialize_everything(state_data) {
// The emoji module must be initialized before the right sidebar
// module, so that we can display custom emoji in statuses.
emoji.initialize({
- realm_emoji: state_data.emoji.realm_emoji,
+ ...state_data.emoji,
emoji_codes: generated_emoji_codes,
});