2020-07-18 17:41:07 +02:00
|
|
|
"use strict";
|
|
|
|
|
2021-01-12 21:38:01 +01:00
|
|
|
const peer_data = require("./peer_data");
|
|
|
|
|
2020-07-24 17:44:09 +02:00
|
|
|
function display_pill(sub) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
exports.create_item_from_stream_name = function (stream_name, current_items) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
const item = {
|
|
|
|
display_value: display_pill(sub),
|
|
|
|
stream_id: sub.stream_id,
|
|
|
|
stream_name: sub.name,
|
|
|
|
};
|
|
|
|
|
|
|
|
return item;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_stream_name_from_item = function (item) {
|
|
|
|
return item.stream_name;
|
|
|
|
};
|
|
|
|
|
2020-07-18 17:41:07 +02:00
|
|
|
function get_user_ids_from_subs(items) {
|
|
|
|
let user_ids = [];
|
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) {
|
2021-01-12 21:38:01 +01:00
|
|
|
user_ids = user_ids.concat(peer_data.get_subscribers(item.stream_id));
|
2020-07-18 17:41:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return user_ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.get_user_ids = function (pill_widget) {
|
|
|
|
const items = pill_widget.items();
|
|
|
|
let user_ids = get_user_ids_from_subs(items);
|
|
|
|
user_ids = Array.from(new Set(user_ids));
|
|
|
|
|
|
|
|
user_ids = user_ids.filter(Boolean);
|
|
|
|
return user_ids;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.append_stream = function (stream, pill_widget) {
|
|
|
|
pill_widget.appendValidatedData({
|
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();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_stream_ids = function (pill_widget) {
|
|
|
|
const items = pill_widget.items();
|
|
|
|
let stream_ids = items.map((item) => item.stream_id);
|
|
|
|
stream_ids = stream_ids.filter(Boolean);
|
|
|
|
|
|
|
|
return stream_ids;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.filter_taken_streams = function (items, pill_widget) {
|
|
|
|
const taken_stream_ids = exports.get_stream_ids(pill_widget);
|
|
|
|
items = items.filter((item) => !taken_stream_ids.includes(item.stream_id));
|
|
|
|
return items;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.typeahead_source = function (pill_widget) {
|
|
|
|
const potential_streams = stream_data.get_unsorted_subs();
|
|
|
|
return exports.filter_taken_streams(potential_streams, pill_widget);
|
|
|
|
};
|
|
|
|
|
|
|
|
window.stream_pill = exports;
|