search: Check if user can access stream topics before fetching history.

When building search suggestions for stream topics, instead of
assuming that the user has access to the stream's topic history
from the server, we check whether the user has access to the
stream's topics.
This commit is contained in:
Lauryn Menard
2022-10-18 19:01:58 +02:00
committed by Tim Abbott
parent c4823bfd03
commit cba8738eea
2 changed files with 20 additions and 11 deletions

View File

@@ -389,21 +389,23 @@ function get_topic_suggestions(last, operators) {
return []; return [];
} }
const stream_id = stream_data.get_stream_id(stream); const stream_sub = stream_data.get_sub(stream);
if (!stream_id) { if (!stream_sub) {
return []; return [];
} }
// Fetch topic history from the server, in case we will need it. if (stream_data.can_access_topic_history(stream_sub)) {
// Note that we won't actually use the results from the server here // Fetch topic history from the server, in case we will need it.
// for this particular keystroke from the user, because we want to // Note that we won't actually use the results from the server here
// show results immediately. Assuming the server responds quickly, // for this particular keystroke from the user, because we want to
// as the user makes their search more specific, subsequent calls to // show results immediately. Assuming the server responds quickly,
// this function will get more candidates from calling // as the user makes their search more specific, subsequent calls to
// stream_topic_history.get_recent_topic_names. // this function will get more candidates from calling
stream_topic_history_util.get_server_history(stream_id, () => {}); // stream_topic_history.get_recent_topic_names.
stream_topic_history_util.get_server_history(stream_sub.stream_id, () => {});
}
const candidate_topics = stream_topic_history.get_recent_topic_names(stream_id); const candidate_topics = stream_topic_history.get_recent_topic_names(stream_sub.stream_id);
if (!candidate_topics || !candidate_topics.length) { if (!candidate_topics || !candidate_topics.length) {
return []; return [];

View File

@@ -530,6 +530,13 @@ export function can_toggle_subscription(sub) {
); );
} }
export function can_access_topic_history(sub) {
// Anyone can access topic history for web-public streams and
// subscriptions; additionally, members can access history for
// public streams.
return sub.is_web_public || can_toggle_subscription(sub);
}
export function can_preview(sub) { export function can_preview(sub) {
return sub.subscribed || !sub.invite_only || sub.previously_subscribed; return sub.subscribed || !sub.invite_only || sub.previously_subscribed;
} }