mirror of
https://github.com/zulip/zulip.git
synced 2025-11-11 09:27:43 +00:00
user_topics: Add set_user_topic helper function.
As we plan to move towards using `user_topics` instead of `muted_topics`, this helper method will be used to set an individual `user_topic` event in the corresponding data structure in the web app.
This commit is contained in:
committed by
Tim Abbott
parent
6cdf2853ff
commit
cf4bbf1ba5
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
const {strict: assert} = require("assert");
|
const {strict: assert} = require("assert");
|
||||||
|
|
||||||
|
const {visibility_policy} = require("../../static/js/user_topics");
|
||||||
const {zrequire} = require("../zjsunit/namespace");
|
const {zrequire} = require("../zjsunit/namespace");
|
||||||
const {run_test} = require("../zjsunit/test");
|
const {run_test} = require("../zjsunit/test");
|
||||||
const blueslip = require("../zjsunit/zblueslip");
|
const blueslip = require("../zjsunit/zblueslip");
|
||||||
@@ -209,6 +210,64 @@ test("unknown streams", () => {
|
|||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("set_user_topics", () => {
|
||||||
|
blueslip.expect("warn", "Unknown stream ID in set_user_topic: 999");
|
||||||
|
|
||||||
|
user_topics.set_muted_topics([]);
|
||||||
|
assert.ok(!user_topics.is_topic_muted(social.stream_id, "breakfast"));
|
||||||
|
assert.ok(!user_topics.is_topic_muted(design.stream_id, "typography"));
|
||||||
|
|
||||||
|
const user_topic_events = [
|
||||||
|
{
|
||||||
|
stream_id: social.stream_id,
|
||||||
|
topic_name: "breakfast",
|
||||||
|
last_updated: "1577836800",
|
||||||
|
visibility_policy: visibility_policy.MUTED,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
stream_id: design.stream_id,
|
||||||
|
topic_name: "typography",
|
||||||
|
last_updated: "1577836800",
|
||||||
|
visibility_policy: visibility_policy.MUTED,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
stream_id: 999, // BOGUS STREAM ID
|
||||||
|
topic_name: "random",
|
||||||
|
last_updated: "1577836800",
|
||||||
|
visibility_policy: visibility_policy.MUTED,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const user_topic of user_topic_events) {
|
||||||
|
user_topics.set_user_topic(user_topic);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.deepEqual(user_topics.get_muted_topics().sort(), [
|
||||||
|
{
|
||||||
|
date_muted: 1577836800000,
|
||||||
|
date_muted_str: "Jan\u00A001,\u00A02020",
|
||||||
|
stream: social.name,
|
||||||
|
stream_id: social.stream_id,
|
||||||
|
topic: "breakfast",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date_muted: 1577836800000,
|
||||||
|
date_muted_str: "Jan\u00A001,\u00A02020",
|
||||||
|
stream: design.name,
|
||||||
|
stream_id: design.stream_id,
|
||||||
|
topic: "typography",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
user_topics.set_user_topic({
|
||||||
|
stream_id: design.stream_id,
|
||||||
|
topic_name: "typography",
|
||||||
|
last_updated: "1577836800",
|
||||||
|
visibility_policy: visibility_policy.VISIBILITY_POLICY_INHERIT,
|
||||||
|
});
|
||||||
|
assert.ok(!user_topics.is_topic_muted(design.stream_id, "typography"));
|
||||||
|
});
|
||||||
|
|
||||||
test("case_insensitivity", () => {
|
test("case_insensitivity", () => {
|
||||||
user_topics.set_muted_topics([]);
|
user_topics.set_muted_topics([]);
|
||||||
assert.ok(!user_topics.is_topic_muted(social.stream_id, "breakfast"));
|
assert.ok(!user_topics.is_topic_muted(social.stream_id, "breakfast"));
|
||||||
|
|||||||
@@ -7,6 +7,13 @@ import {get_time_from_date_muted} from "./util";
|
|||||||
|
|
||||||
const muted_topics = new Map();
|
const muted_topics = new Map();
|
||||||
|
|
||||||
|
export const visibility_policy = {
|
||||||
|
VISIBILITY_POLICY_INHERIT: 0,
|
||||||
|
MUTED: 1,
|
||||||
|
UNMUTED: 2,
|
||||||
|
FOLLOWED: 3,
|
||||||
|
};
|
||||||
|
|
||||||
export function add_muted_topic(stream_id, topic, date_muted) {
|
export function add_muted_topic(stream_id, topic, date_muted) {
|
||||||
let sub_dict = muted_topics.get(stream_id);
|
let sub_dict = muted_topics.get(stream_id);
|
||||||
if (!sub_dict) {
|
if (!sub_dict) {
|
||||||
@@ -51,6 +58,28 @@ export function get_muted_topics() {
|
|||||||
return topics;
|
return topics;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function set_user_topic(user_topic) {
|
||||||
|
const stream_id = user_topic.stream_id;
|
||||||
|
const topic = user_topic.topic_name;
|
||||||
|
const date_muted = user_topic.last_updated;
|
||||||
|
|
||||||
|
const stream_name = stream_data.maybe_get_stream_name(stream_id);
|
||||||
|
|
||||||
|
if (!stream_name) {
|
||||||
|
blueslip.warn("Unknown stream ID in set_user_topic: " + stream_id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (user_topic.visibility_policy) {
|
||||||
|
case visibility_policy.MUTED:
|
||||||
|
add_muted_topic(stream_id, topic, date_muted);
|
||||||
|
break;
|
||||||
|
case visibility_policy.VISIBILITY_POLICY_INHERIT:
|
||||||
|
remove_muted_topic(stream_id, topic);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function set_muted_topics(tuples) {
|
export function set_muted_topics(tuples) {
|
||||||
muted_topics.clear();
|
muted_topics.clear();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user