2024-06-19 22:00:56 +02:00
|
|
|
import {$t} from "./i18n";
|
2023-09-22 18:30:23 +02:00
|
|
|
import type {InputPillContainer, InputPillItem} from "./input_pill";
|
2021-02-10 16:47:46 +01:00
|
|
|
import * as peer_data from "./peer_data";
|
2021-02-28 00:53:59 +01:00
|
|
|
import * as stream_data from "./stream_data";
|
2023-09-22 18:30:23 +02:00
|
|
|
import type {StreamSubscription} from "./sub_store";
|
2024-04-23 14:30:17 +02:00
|
|
|
import type {CombinedPillContainer, CombinedPillItem} from "./typeahead_helper";
|
2021-01-12 21:38:01 +01:00
|
|
|
|
2024-04-15 05:21:04 +02:00
|
|
|
export type StreamPill = {
|
|
|
|
type: "stream";
|
2024-07-03 21:13:10 +02:00
|
|
|
stream: StreamSubscription;
|
2023-09-22 18:30:23 +02:00
|
|
|
};
|
|
|
|
|
2024-01-24 14:03:43 +01:00
|
|
|
export type StreamPillWidget = InputPillContainer<StreamPill>;
|
2023-09-22 18:30:23 +02:00
|
|
|
|
2024-04-13 19:40:18 +02:00
|
|
|
export type StreamPillData = StreamSubscription & {type: "stream"};
|
|
|
|
|
2024-06-22 08:58:16 +02:00
|
|
|
function format_stream_name_and_subscriber_count(sub: StreamSubscription): string {
|
2021-01-21 15:55:49 +01:00
|
|
|
const sub_count = peer_data.get_subscriber_count(sub.stream_id);
|
2024-06-19 22:00:56 +02:00
|
|
|
return $t(
|
2024-07-03 21:13:10 +02:00
|
|
|
{defaultMessage: "{stream_name}: {sub_count} users"},
|
2024-06-19 22:00:56 +02:00
|
|
|
{stream_name: sub.name, sub_count},
|
|
|
|
);
|
2020-07-24 17:44:09 +02:00
|
|
|
}
|
|
|
|
|
2023-09-22 18:30:23 +02:00
|
|
|
export function create_item_from_stream_name(
|
|
|
|
stream_name: string,
|
2024-04-23 14:30:17 +02:00
|
|
|
current_items: CombinedPillItem[],
|
2024-01-24 14:03:43 +01:00
|
|
|
stream_prefix_required = true,
|
|
|
|
get_allowed_streams: () => StreamSubscription[] = stream_data.get_unsorted_subs,
|
|
|
|
show_subscriber_count = true,
|
2023-09-22 18:30:23 +02:00
|
|
|
): InputPillItem<StreamPill> | undefined {
|
2020-07-24 17:44:09 +02:00
|
|
|
stream_name = stream_name.trim();
|
2024-01-24 14:03:43 +01:00
|
|
|
if (stream_prefix_required) {
|
|
|
|
if (!stream_name.startsWith("#")) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
stream_name = stream_name.slice(1);
|
2020-07-24 17:44:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const sub = stream_data.get_sub(stream_name);
|
|
|
|
if (!sub) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2020-07-24 17:44:09 +02:00
|
|
|
}
|
|
|
|
|
2024-01-24 14:03:43 +01:00
|
|
|
const streams = get_allowed_streams();
|
|
|
|
if (!streams.includes(sub)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2024-07-03 21:13:10 +02:00
|
|
|
if (
|
|
|
|
current_items.some(
|
|
|
|
(item) => item.type === "stream" && item.stream.stream_id === sub.stream_id,
|
|
|
|
)
|
|
|
|
) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2020-07-24 17:44:09 +02:00
|
|
|
}
|
|
|
|
|
2024-07-03 21:13:10 +02:00
|
|
|
let display_value = sub.name;
|
2024-01-24 14:03:43 +01:00
|
|
|
if (show_subscriber_count) {
|
|
|
|
display_value = format_stream_name_and_subscriber_count(sub);
|
|
|
|
}
|
|
|
|
|
2024-04-15 05:21:04 +02:00
|
|
|
return {
|
2021-04-27 16:56:20 +02:00
|
|
|
type: "stream",
|
2024-01-24 14:03:43 +01:00
|
|
|
display_value,
|
2024-07-03 21:13:10 +02:00
|
|
|
stream: sub,
|
2020-07-24 17:44:09 +02:00
|
|
|
};
|
2021-02-10 16:47:46 +01:00
|
|
|
}
|
2020-07-24 17:44:09 +02:00
|
|
|
|
2023-09-22 18:30:23 +02:00
|
|
|
export function get_stream_name_from_item(item: InputPillItem<StreamPill>): string {
|
2024-07-03 21:13:10 +02:00
|
|
|
return item.stream.name;
|
2021-02-10 16:47:46 +01:00
|
|
|
}
|
2020-07-24 17:44:09 +02:00
|
|
|
|
2024-04-23 14:30:17 +02:00
|
|
|
export function get_user_ids(pill_widget: StreamPillWidget | CombinedPillContainer): number[] {
|
2024-05-14 02:03:09 +02:00
|
|
|
let user_ids = pill_widget
|
|
|
|
.items()
|
|
|
|
.flatMap((item) =>
|
2024-07-03 21:13:10 +02:00
|
|
|
item.type === "stream" ? peer_data.get_subscribers(item.stream.stream_id) : [],
|
2024-05-14 02:03:09 +02:00
|
|
|
);
|
2023-03-02 01:58:25 +01:00
|
|
|
user_ids = [...new Set(user_ids)];
|
2022-02-22 15:25:13 +01:00
|
|
|
user_ids.sort((a, b) => a - b);
|
2020-07-18 17:41:07 +02:00
|
|
|
return user_ids;
|
2021-02-10 16:47:46 +01:00
|
|
|
}
|
2020-07-18 17:41:07 +02:00
|
|
|
|
2024-04-15 05:21:04 +02:00
|
|
|
export function append_stream(
|
|
|
|
stream: StreamSubscription,
|
2024-01-24 14:03:43 +01:00
|
|
|
pill_widget: StreamPillWidget | CombinedPillContainer,
|
|
|
|
show_subscriber_count = true,
|
2024-04-15 05:21:04 +02:00
|
|
|
): void {
|
2024-07-03 21:13:10 +02:00
|
|
|
let display_value = stream.name;
|
2024-01-24 14:03:43 +01:00
|
|
|
if (show_subscriber_count) {
|
|
|
|
display_value = format_stream_name_and_subscriber_count(stream);
|
|
|
|
}
|
2020-07-18 17:41:07 +02:00
|
|
|
pill_widget.appendValidatedData({
|
2021-04-27 16:56:20 +02:00
|
|
|
type: "stream",
|
2024-01-24 14:03:43 +01:00
|
|
|
display_value,
|
2024-07-03 21:13:10 +02:00
|
|
|
stream,
|
2020-07-18 17:41:07 +02:00
|
|
|
});
|
|
|
|
pill_widget.clear_text();
|
2021-02-10 16:47:46 +01:00
|
|
|
}
|
2020-07-18 17:41:07 +02:00
|
|
|
|
2024-01-24 14:03:43 +01:00
|
|
|
export function get_stream_ids(pill_widget: StreamPillWidget | CombinedPillContainer): number[] {
|
2020-07-18 17:41:07 +02:00
|
|
|
const items = pill_widget.items();
|
2024-07-03 21:13:10 +02:00
|
|
|
return items.flatMap((item) => (item.type === "stream" ? item.stream.stream_id : []));
|
2021-02-10 16:47:46 +01:00
|
|
|
}
|
2020-07-18 17:41:07 +02:00
|
|
|
|
2023-09-22 18:30:23 +02:00
|
|
|
export function filter_taken_streams(
|
|
|
|
items: StreamSubscription[],
|
2024-01-24 14:03:43 +01:00
|
|
|
pill_widget: StreamPillWidget | CombinedPillContainer,
|
2023-09-22 18:30:23 +02:00
|
|
|
): StreamSubscription[] {
|
2021-02-10 16:47:46 +01:00
|
|
|
const taken_stream_ids = get_stream_ids(pill_widget);
|
2020-07-18 17:41:07 +02:00
|
|
|
items = items.filter((item) => !taken_stream_ids.includes(item.stream_id));
|
|
|
|
return items;
|
2021-02-10 16:47:46 +01:00
|
|
|
}
|
2020-07-18 17:41:07 +02:00
|
|
|
|
2024-01-24 14:03:43 +01:00
|
|
|
export function typeahead_source(
|
|
|
|
pill_widget: StreamPillWidget | CombinedPillContainer,
|
|
|
|
invite_streams?: boolean,
|
|
|
|
): StreamPillData[] {
|
|
|
|
const potential_streams = invite_streams
|
|
|
|
? stream_data.get_invite_stream_data()
|
|
|
|
: stream_data.get_unsorted_subs();
|
2024-04-13 19:40:18 +02:00
|
|
|
return filter_taken_streams(potential_streams, pill_widget).map((stream) => ({
|
|
|
|
...stream,
|
|
|
|
type: "stream",
|
|
|
|
}));
|
2021-02-10 16:47:46 +01:00
|
|
|
}
|