zulip/web/src/subscriber_api.ts
Kislay Verma f448890158 channel: Add option to notify users newly added to a channel.
When a user is added to a channel, we send
the user that was added a Notification Bot
DMs to let them know about it.

In this commit, we add an option for whether or not
this message is sent.

If more than 100 users are added at once, we
do not send notification bot DMs since it would
be a performance-costly operation.

We also send this threshold value of 100 in the
initial state data to the clients.

Fixes part of #31189
2025-06-26 10:08:11 -07:00

62 lines
1.9 KiB
TypeScript

import * as channel from "./channel.ts";
import * as people from "./people.ts";
import type {StreamSubscription} from "./sub_store.ts";
/*
This module simply encapsulates our legacy API for subscribing
or unsubscribing users from streams. Callers don't need to
know the strange names of "subscriptions" and "principals",
nor how to JSON.stringify things, nor the URL scheme.
*/
export function add_user_ids_to_stream(
user_ids: number[],
sub: StreamSubscription,
send_new_subscription_messages: boolean,
success: (data: unknown) => void,
failure: (xhr: JQuery.jqXHR<unknown>) => void,
): void {
// TODO: use stream_id when backend supports it
const stream_name = sub.name;
if (user_ids.length === 1 && people.is_my_user_id(Number(user_ids[0]))) {
// Self subscribe
const color = sub.color;
void channel.post({
url: "/json/users/me/subscriptions",
data: {
subscriptions: JSON.stringify([{name: stream_name, color}]),
send_new_subscription_messages,
},
success,
error: failure,
});
return;
}
void channel.post({
url: "/json/users/me/subscriptions",
data: {
subscriptions: JSON.stringify([{name: stream_name}]),
principals: JSON.stringify(user_ids),
send_new_subscription_messages,
},
success,
error: failure,
});
}
export function remove_user_id_from_stream(
user_id: number,
sub: StreamSubscription,
success: (data: unknown) => void,
failure: (xhr: JQuery.jqXHR<unknown>) => void,
): void {
// TODO: use stream_id when backend supports it
const stream_name = sub.name;
void channel.del({
url: "/json/users/me/subscriptions",
data: {subscriptions: JSON.stringify([stream_name]), principals: JSON.stringify([user_id])},
success,
error: failure,
});
}