zulip/web/src/subscriber_api.js
palashb01 4e786293ae user_profile: Add the subscribe widget to user profile.
This commit adds a subscription widget to the user profile,
including the logic to prevent non-admin users from seeing the
subscription widget of other users. Additionally, as it is not
possible to subscribe generic bots to streams, and the user should
not be a deactivated user, we check for these conditions before
displaying the subscription widget.

To ensure that the alert for both subscribing and unsubscribing
appears on top of the subscribe widget, changed the location of
the alert to be displayed at the top.

Additionally, considering that no stream will be initially selected,
we have made the decision to disable the subscribe button. Once the
user selects a stream, we will enable the subscribe button
accordingly.

Changed the add_user_ids_to_stream function inside subscriber_api.js
to support self subscribe also, so that we don't have to duplicate the
logic in user_profile.js

Created a separate file for the subscribe widget called
user_profile_subscribe_widget.hbs.

Fixes: #18883
2023-09-10 11:20:15 -07:00

45 lines
1.5 KiB
JavaScript

import * as channel from "./channel";
import * as people from "./people";
/*
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, sub, success, failure) {
// 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;
return channel.post({
url: "/json/users/me/subscriptions",
data: {subscriptions: JSON.stringify([{name: stream_name, color}])},
success,
error: failure,
});
}
return channel.post({
url: "/json/users/me/subscriptions",
data: {
subscriptions: JSON.stringify([{name: stream_name}]),
principals: JSON.stringify(user_ids),
},
success,
error: failure,
});
}
export function remove_user_id_from_stream(user_id, sub, success, failure) {
// TODO: use stream_id when backend supports it
const stream_name = sub.name;
return channel.del({
url: "/json/users/me/subscriptions",
data: {subscriptions: JSON.stringify([stream_name]), principals: JSON.stringify([user_id])},
success,
error: failure,
});
}