From 341ba92f23e2bd0af514f1779d554862ac70a972 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Wed, 1 Nov 2023 22:15:12 -0700 Subject: [PATCH] stream_settings_api: Convert module to TypeScript. Signed-off-by: Anders Kaseorg --- tools/test-js-with-node | 2 +- web/src/stream_settings_api.js | 28 ------------------- web/src/stream_settings_api.ts | 50 ++++++++++++++++++++++++++++++++++ web/src/sub_store.ts | 9 ++++-- 4 files changed, 57 insertions(+), 32 deletions(-) delete mode 100644 web/src/stream_settings_api.js create mode 100644 web/src/stream_settings_api.ts diff --git a/tools/test-js-with-node b/tools/test-js-with-node index e1560e5aba..b3d192468d 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -232,7 +232,7 @@ EXEMPT_FILES = make_set( "web/src/stream_list.js", "web/src/stream_muting.js", "web/src/stream_popover.js", - "web/src/stream_settings_api.js", + "web/src/stream_settings_api.ts", "web/src/stream_settings_components.js", "web/src/stream_settings_containers.ts", "web/src/stream_settings_ui.js", diff --git a/web/src/stream_settings_api.js b/web/src/stream_settings_api.js deleted file mode 100644 index c070f36fe6..0000000000 --- a/web/src/stream_settings_api.js +++ /dev/null @@ -1,28 +0,0 @@ -import * as channel from "./channel"; -import * as settings_ui from "./settings_ui"; -import * as sub_store from "./sub_store"; - -export function bulk_set_stream_property(sub_data, $status_element) { - const url = "/json/users/me/subscriptions/properties"; - const data = {subscription_data: JSON.stringify(sub_data)}; - if (!$status_element) { - return channel.post({ - url, - data, - timeout: 10 * 1000, - }); - } - - settings_ui.do_settings_change(channel.post, url, data, $status_element); - return undefined; -} - -export function set_stream_property(sub, data, $status_element) { - const sub_data = {stream_id: sub.stream_id, ...data}; - bulk_set_stream_property([sub_data], $status_element); -} - -export function set_color(stream_id, color) { - const sub = sub_store.get(stream_id); - set_stream_property(sub, {property: "color", value: color}); -} diff --git a/web/src/stream_settings_api.ts b/web/src/stream_settings_api.ts new file mode 100644 index 0000000000..e81c53fb77 --- /dev/null +++ b/web/src/stream_settings_api.ts @@ -0,0 +1,50 @@ +import assert from "minimalistic-assert"; + +import * as channel from "./channel"; +import * as settings_ui from "./settings_ui"; +import type {StreamProperties, StreamSubscription} from "./sub_store"; +import * as sub_store from "./sub_store"; + +export function bulk_set_stream_property( + sub_data: { + [Property in keyof StreamProperties]: { + stream_id: number; + property: Property; + value: StreamProperties[Property]; + }; + }[keyof StreamProperties][], + $status_element?: JQuery, +): void { + const url = "/json/users/me/subscriptions/properties"; + const data = {subscription_data: JSON.stringify(sub_data)}; + if (!$status_element) { + return void channel.post({ + url, + data, + timeout: 10 * 1000, + }); + } + + settings_ui.do_settings_change(channel.post, url, data, $status_element); + return undefined; +} + +export function set_stream_property( + sub: StreamSubscription, + data: { + [Property in keyof StreamProperties]: { + property: Property; + value: StreamProperties[Property]; + }; + }[keyof StreamProperties], + $status_element?: JQuery, +): void { + const sub_data = {stream_id: sub.stream_id, ...data}; + bulk_set_stream_property([sub_data], $status_element); +} + +export function set_color(stream_id: number, color: string): void { + const sub = sub_store.get(stream_id); + assert(sub !== undefined); + set_stream_property(sub, {property: "color", value: color}); +} diff --git a/web/src/sub_store.ts b/web/src/sub_store.ts index f514b5a03b..50dced51c2 100644 --- a/web/src/sub_store.ts +++ b/web/src/sub_store.ts @@ -39,12 +39,15 @@ export type NeverSubscribedStream = Stream & { subscribers?: number[]; }; -// This is the raw data we get from the server for a subscription. -export type ApiStreamSubscription = (Stream & StreamSpecificNotificationSettings) & { +export type StreamProperties = StreamSpecificNotificationSettings & { color: string; - email_address: string; is_muted: boolean; pin_to_top: boolean; +}; + +// This is the raw data we get from the server for a subscription. +export type ApiStreamSubscription = (Stream & StreamProperties) & { + email_address: string; stream_weekly_traffic: number | null; subscribers?: number[]; };