2024-07-30 06:13:31 +02:00
|
|
|
import assert from "minimalistic-assert";
|
|
|
|
|
2024-07-31 00:46:26 +02:00
|
|
|
import render_input_pill from "../templates/input_pill.hbs";
|
|
|
|
|
2024-06-19 22:00:56 +02:00
|
|
|
import {$t} from "./i18n";
|
2024-07-30 07:21:29 +02:00
|
|
|
import type {InputPillContainer} 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-07-30 07:21:29 +02:00
|
|
|
import type {CombinedPill, 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";
|
2024-07-31 00:46:26 +02:00
|
|
|
stream_id: number;
|
2024-07-30 06:13:31 +02:00
|
|
|
show_subscriber_count: boolean;
|
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-07-30 07:21:29 +02:00
|
|
|
current_items: CombinedPill[],
|
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,
|
2024-07-30 07:21:29 +02:00
|
|
|
): 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-31 00:46:26 +02:00
|
|
|
if (current_items.some((item) => item.type === "stream" && item.stream_id === 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",
|
2024-07-30 06:13:31 +02:00
|
|
|
show_subscriber_count,
|
2024-07-31 00:46:26 +02:00
|
|
|
stream_id: sub.stream_id,
|
2020-07-24 17:44:09 +02:00
|
|
|
};
|
2021-02-10 16:47:46 +01:00
|
|
|
}
|
2020-07-24 17:44:09 +02:00
|
|
|
|
2024-07-30 07:21:29 +02:00
|
|
|
export function get_stream_name_from_item(item: StreamPill): string {
|
2024-07-31 00:46:26 +02:00
|
|
|
const stream = stream_data.get_sub_by_id(item.stream_id);
|
|
|
|
assert(stream !== undefined);
|
|
|
|
return 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-31 00:46:26 +02:00
|
|
|
item.type === "stream" ? peer_data.get_subscribers(item.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-07-30 06:13:31 +02:00
|
|
|
export function get_display_value_from_item(item: StreamPill): string {
|
2024-07-31 00:46:26 +02:00
|
|
|
const stream = stream_data.get_sub_by_id(item.stream_id);
|
2024-07-30 06:13:31 +02:00
|
|
|
assert(stream !== undefined);
|
|
|
|
if (item.show_subscriber_count) {
|
|
|
|
return format_stream_name_and_subscriber_count(stream);
|
|
|
|
}
|
|
|
|
return stream.name;
|
|
|
|
}
|
|
|
|
|
2024-07-31 00:46:26 +02:00
|
|
|
export function generate_pill_html(item: StreamPill): string {
|
|
|
|
const stream = stream_data.get_sub_by_id(item.stream_id);
|
|
|
|
assert(stream !== undefined);
|
|
|
|
return render_input_pill({
|
|
|
|
has_stream: true,
|
|
|
|
stream,
|
|
|
|
display_value: get_display_value_from_item(item),
|
2024-09-10 18:30:14 +02:00
|
|
|
stream_id: item.stream_id,
|
2024-07-31 00:46:26 +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 {
|
2020-07-18 17:41:07 +02:00
|
|
|
pill_widget.appendValidatedData({
|
2021-04-27 16:56:20 +02:00
|
|
|
type: "stream",
|
2024-07-30 06:13:31 +02:00
|
|
|
show_subscriber_count,
|
2024-07-31 00:46:26 +02:00
|
|
|
stream_id: stream.stream_id,
|
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-31 00:46:26 +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-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
|
|
|
}
|