forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes zulip#8041.
- Loading branch information
Showing
32 changed files
with
725 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
zrequire('locking'); | ||
|
||
(function test_edge_cases() { | ||
assert(!locking.is_topic_locked(undefined, undefined)); | ||
assert(!locking.is_topic_locked('nonexistent', undefined)); | ||
}()); | ||
|
||
(function test_basics() { | ||
assert(!locking.is_topic_locked('devel', 'java')); | ||
locking.add_locked_topic('devel', 'java'); | ||
assert(locking.is_topic_locked('devel', 'java')); | ||
|
||
// test idempotentcy | ||
locking.add_locked_topic('devel', 'java'); | ||
assert(locking.is_topic_locked('devel', 'java')); | ||
|
||
locking.remove_locked_topic('devel', 'java'); | ||
assert(!locking.is_topic_locked('devel', 'java')); | ||
|
||
// test idempotentcy | ||
locking.remove_locked_topic('devel', 'java'); | ||
assert(!locking.is_topic_locked('devel', 'java')); | ||
|
||
// test unknown stream is harmless too | ||
locking.remove_locked_topic('unknown', 'java'); | ||
assert(!locking.is_topic_locked('unknown', 'java')); | ||
}()); | ||
|
||
(function test_set_locked_topics() { | ||
locking.set_locked_topics([ | ||
['social', 'breakfast'], | ||
['design', 'typography'], | ||
]); | ||
assert(locking.is_topic_locked('social', 'breakfast')); | ||
assert(locking.is_topic_locked('design', 'typography')); | ||
locking.set_locked_topics([ | ||
['devel', 'java'], | ||
]); | ||
assert(!locking.is_topic_locked('social', 'breakfast')); | ||
assert(!locking.is_topic_locked('design', 'typography')); | ||
assert(locking.is_topic_locked('devel', 'java')); | ||
}()); | ||
|
||
(function test_case_insensitivity() { | ||
locking.set_locked_topics([]); | ||
assert(!locking.is_topic_locked('SOCial', 'breakfast')); | ||
locking.set_locked_topics([ | ||
['SOCial', 'breakfast'], | ||
]); | ||
assert(locking.is_topic_locked('SOCial', 'breakfast')); | ||
assert(locking.is_topic_locked('social', 'breakfast')); | ||
assert(locking.is_topic_locked('social', 'breakFAST')); | ||
}()); | ||
|
||
set_global('page_params', { | ||
is_admin: false, | ||
}); | ||
|
||
(function test_can_lock_topic_no_admin() { | ||
locking.set_locked_topics([]); | ||
assert(!locking.can_lock_topic('social', 'breakfast')); | ||
assert(!locking.can_unlock_topic('social', 'breakfast')); | ||
assert(!locking.can_lock_topic(undefined, 'breakfast')); | ||
assert(!locking.can_lock_topic('social', undefined)); | ||
assert(!locking.can_unlock_topic(undefined, 'breakfast')); | ||
assert(!locking.can_unlock_topic('social', undefined)); | ||
locking.add_locked_topic('social', 'breakfast'); | ||
assert(!locking.can_lock_topic('social', 'breakfast')); | ||
assert(!locking.can_unlock_topic('social', 'breakfast')); | ||
}()); | ||
|
||
set_global('page_params', { | ||
is_admin: true, | ||
}); | ||
|
||
(function test_can_lock_topic_admin() { | ||
locking.set_locked_topics([]); | ||
assert(locking.can_lock_topic('social', 'breakfast')); | ||
assert(!locking.can_unlock_topic('social', 'breakfast')); | ||
assert(!locking.can_lock_topic(undefined, 'breakfast')); | ||
assert(!locking.can_lock_topic('social', undefined)); | ||
assert(!locking.can_unlock_topic(undefined, 'breakfast')); | ||
assert(!locking.can_unlock_topic('social', undefined)); | ||
locking.add_locked_topic('social', 'breakfast'); | ||
assert(!locking.can_lock_topic('social', 'breakfast')); | ||
assert(locking.can_unlock_topic('social', 'breakfast')); | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
var locking = (function () { | ||
|
||
var exports = {}; | ||
|
||
var locked_topics = new Dict({fold_case: true}); | ||
|
||
exports.add_locked_topic = function (stream, topic) { | ||
var sub_dict = locked_topics.get(stream); | ||
if (!sub_dict) { | ||
sub_dict = new Dict({fold_case: true}); | ||
locked_topics.set(stream, sub_dict); | ||
} | ||
sub_dict.set(topic, true); | ||
}; | ||
|
||
exports.remove_locked_topic = function (stream, topic) { | ||
var sub_dict = locked_topics.get(stream); | ||
if (sub_dict) { | ||
sub_dict.del(topic); | ||
} | ||
}; | ||
|
||
exports.set_locked_topics = function (tuples) { | ||
locked_topics = new Dict({fold_case: true}); | ||
|
||
_.each(tuples, function (tuple) { | ||
var stream = tuple[0]; | ||
var topic = tuple[1]; | ||
|
||
exports.add_locked_topic(stream, topic); | ||
}); | ||
}; | ||
|
||
exports.can_lock_topic = function (stream, topic) { | ||
if (stream === undefined || topic === undefined) { | ||
return false; | ||
} | ||
if (!page_params.is_admin) { | ||
return false; | ||
} | ||
var sub_dict = locked_topics.get(stream); | ||
return !sub_dict || !sub_dict.get(topic); | ||
}; | ||
|
||
exports.can_unlock_topic = function (stream, topic) { | ||
if (!page_params.is_admin || stream === undefined || topic === undefined) { | ||
return false; | ||
} | ||
return !exports.can_lock_topic(stream, topic); | ||
}; | ||
|
||
exports.is_topic_locked = function (stream, topic) { | ||
if (stream === undefined || topic === undefined) { | ||
return false; | ||
} | ||
var sub_dict = locked_topics.get(stream); | ||
return sub_dict && sub_dict.get(topic); | ||
}; | ||
|
||
return exports; | ||
}()); | ||
if (typeof module !== 'undefined') { | ||
module.exports = locking; | ||
} |
Oops, something went wrong.