summaryrefslogtreecommitdiffstats
path: root/models/watchable.js
diff options
context:
space:
mode:
authorLiming Xie <liming.xie@gmail.com>2016-01-05 23:26:02 +0800
committerLiming Xie <liming.xie@gmail.com>2016-01-05 23:26:02 +0800
commit9bbdacc79a89667e0d6f1ed30c415e5350ad468b (patch)
treefc6d9918dcd77699295ecb5bdbaf59f9d7c2f479 /models/watchable.js
parent9ef8ebaf09e52d7133ebe08ab1354ef663ee948b (diff)
downloadwekan-9bbdacc79a89667e0d6f1ed30c415e5350ad468b.tar.gz
wekan-9bbdacc79a89667e0d6f1ed30c415e5350ad468b.tar.bz2
wekan-9bbdacc79a89667e0d6f1ed30c415e5350ad468b.zip
Add notification, allow watch boards / lists / cards
Diffstat (limited to 'models/watchable.js')
-rw-r--r--models/watchable.js89
1 files changed, 89 insertions, 0 deletions
diff --git a/models/watchable.js b/models/watchable.js
new file mode 100644
index 00000000..6821f847
--- /dev/null
+++ b/models/watchable.js
@@ -0,0 +1,89 @@
+// simple version, only toggle watch / unwatch
+const simpleWatchable = (collection) => {
+ collection.attachSchema({
+ watchers: {
+ type: [String],
+ optional: true,
+ },
+ });
+
+ collection.helpers({
+ getWatchLevels() {
+ return [true, false];
+ },
+
+ watcherIndex(userId) {
+ return this.watchers.indexOf(userId);
+ },
+
+ findWatcher(userId) {
+ return _.contains(this.watchers, userId);
+ },
+ });
+
+ collection.mutations({
+ setWatcher(userId, level) {
+ // if level undefined or null or false, then remove
+ if (!level) return { $pull: { watchers: userId }};
+ return { $addToSet: { watchers: userId }};
+ },
+ });
+};
+
+// more complex version of same interface, with 3 watching levels
+const complexWatchOptions = ['watching', 'tracking', 'muted'];
+const complexWatchDefault = 'muted';
+
+const complexWatchable = (collection) => {
+ collection.attachSchema({
+ 'watchers.$.userId': {
+ type: String,
+ },
+ 'watchers.$.level': {
+ type: String,
+ allowedValues: complexWatchOptions,
+ },
+ });
+
+ collection.helpers({
+ getWatchOptions() {
+ return complexWatchOptions;
+ },
+
+ getWatchDefault() {
+ return complexWatchDefault;
+ },
+
+ watcherIndex(userId) {
+ return _.pluck(this.watchers, 'userId').indexOf(userId);
+ },
+
+ findWatcher(userId) {
+ return _.findWhere(this.watchers, { userId });
+ },
+
+ getWatchLevel(userId) {
+ const watcher = this.findWatcher(userId);
+ return watcher ? watcher.level : complexWatchDefault;
+ },
+ });
+
+ collection.mutations({
+ setWatcher(userId, level) {
+ // if level undefined or null or false, then remove
+ if (level === complexWatchDefault) level = null;
+ if (!level) return { $pull: { watchers: { userId }}};
+ const index = this.watcherIndex(userId);
+ if (index<0) return { $push: { watchers: { userId, level }}};
+ return {
+ $set: {
+ [`watchers.${index}.level`]: level,
+ },
+ };
+ },
+ });
+};
+
+complexWatchable(Boards);
+simpleWatchable(Lists);
+simpleWatchable(Cards);