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-15 05:21:04 +02:00
|
|
|
import type {CombinedPillContainer} from "./typeahead_helper";
|
2021-01-12 21:38:01 +01:00
|
|
|
|
2024-04-15 05:21:04 +02:00
|
|
|
export type StreamPill = {
|
|
|
|
type: "stream";
|
2023-09-22 18:30:23 +02:00
|
|
|
stream_id: number;
|
|
|
|
stream_name: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type StreamPillWidget = InputPillContainer<StreamPill>;
|
|
|
|
|
2024-04-13 19:40:18 +02:00
|
|
|
export type StreamPillData = StreamSubscription & {type: "stream"};
|
|
|
|
|
2023-09-22 18:30:23 +02:00
|
|
|
function display_pill(sub: StreamSubscription): string {
|
2021-01-21 15:55:49 +01:00
|
|
|
const sub_count = peer_data.get_subscriber_count(sub.stream_id);
|
|
|
|
return "#" + sub.name + ": " + sub_count + " users";
|
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,
|
|
|
|
current_items: InputPillItem<StreamPill>[],
|
|
|
|
): InputPillItem<StreamPill> | undefined {
|
2020-07-24 17:44:09 +02:00
|
|
|
stream_name = stream_name.trim();
|
|
|
|
if (!stream_name.startsWith("#")) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2020-07-24 17:44:09 +02:00
|
|
|
}
|
2020-10-07 09:41:22 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
const existing_ids = current_items.map((item) => item.stream_id);
|
|
|
|
if (existing_ids.includes(sub.stream_id)) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2020-07-24 17:44:09 +02:00
|
|
|
}
|
|
|
|
|
2024-04-15 05:21:04 +02:00
|
|
|
return {
|
2021-04-27 16:56:20 +02:00
|
|
|
type: "stream",
|
2020-07-24 17:44:09 +02:00
|
|
|
display_value: display_pill(sub),
|
|
|
|
stream_id: sub.stream_id,
|
|
|
|
stream_name: sub.name,
|
|
|
|
};
|
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 {
|
2020-07-24 17:44:09 +02:00
|
|
|
return item.stream_name;
|
2021-02-10 16:47:46 +01:00
|
|
|
}
|
2020-07-24 17:44:09 +02:00
|
|
|
|
2023-09-22 18:30:23 +02:00
|
|
|
function get_user_ids_from_subs(items: InputPillItem<StreamPill>[]): number[] {
|
|
|
|
let user_ids: number[] = [];
|
2021-01-12 16:05:24 +01:00
|
|
|
for (const item of items) {
|
|
|
|
// only some of our items have streams (for copy-from-stream)
|
|
|
|
if (item.stream_id !== undefined) {
|
2023-03-02 01:58:25 +01:00
|
|
|
user_ids = [...user_ids, ...peer_data.get_subscribers(item.stream_id)];
|
2020-07-18 17:41:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return user_ids;
|
|
|
|
}
|
|
|
|
|
2023-09-22 18:30:23 +02:00
|
|
|
export function get_user_ids(pill_widget: StreamPillWidget): number[] {
|
2020-07-18 17:41:07 +02:00
|
|
|
const items = pill_widget.items();
|
|
|
|
let user_ids = get_user_ids_from_subs(items);
|
2023-03-02 01:58:25 +01:00
|
|
|
user_ids = [...new Set(user_ids)];
|
2020-07-18 17:41:07 +02:00
|
|
|
|
|
|
|
user_ids = user_ids.filter(Boolean);
|
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,
|
|
|
|
pill_widget: CombinedPillContainer,
|
|
|
|
): void {
|
2020-07-18 17:41:07 +02:00
|
|
|
pill_widget.appendValidatedData({
|
2021-04-27 16:56:20 +02:00
|
|
|
type: "stream",
|
2020-07-24 17:44:09 +02:00
|
|
|
display_value: display_pill(stream),
|
2020-07-18 17:41:07 +02:00
|
|
|
stream_id: stream.stream_id,
|
2020-07-24 17:44:09 +02:00
|
|
|
stream_name: stream.name,
|
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-04-15 05:21:04 +02:00
|
|
|
export function get_stream_ids(pill_widget: CombinedPillContainer): number[] {
|
2020-07-18 17:41:07 +02:00
|
|
|
const items = pill_widget.items();
|
2024-04-15 05:21:04 +02:00
|
|
|
return items.flatMap((item) => (item.type === "stream" ? item.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-04-15 05:21:04 +02:00
|
|
|
pill_widget: 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-04-15 05:21:04 +02:00
|
|
|
export function typeahead_source(pill_widget: CombinedPillContainer): StreamPillData[] {
|
2020-07-18 17:41:07 +02:00
|
|
|
const potential_streams = 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
|
|
|
}
|