diff --git a/www/common/notifications.js b/www/common/notifications.js index 8546f2787..8db8db284 100644 --- a/www/common/notifications.js +++ b/www/common/notifications.js @@ -315,6 +315,32 @@ define([ } }; + Messages.comments_notification = 'Replies to your comment "{0}" in {1}'; // XXX + handlers['COMMENT_REPLY'] = function (common, data) { + var content = data.content; + var msg = content.msg; + + // Display the notification + //var name = Util.fixHTML(msg.content.user.displayName) || Messages.anonymous; + var comment = Util.fixHTML(msg.content.comment).slice(0,20).trim(); + if (msg.content.comment.length > 20) { + comment += '...'; + } + var title = Util.fixHTML(msg.content.title); + var href = msg.content.href; + + content.getFormatText = function () { + return Messages._getKey('comments_notification', [comment, title]); + }; + content.handler = function () { + common.openURL(href); + defaultDismiss(common, data)(); + }; + if (!content.archived) { + content.dismissHandler = defaultDismiss(common, data); + } + }; + // NOTE: don't forget to fixHTML everything returned by "getFormatText" diff --git a/www/common/outer/mailbox-handlers.js b/www/common/outer/mailbox-handlers.js index fec467fc5..cc90861c1 100644 --- a/www/common/outer/mailbox-handlers.js +++ b/www/common/outer/mailbox-handlers.js @@ -527,6 +527,61 @@ define([ }; + // Hide duplicates when receiving a SHARE_PAD notification: + // Keep only one notification per channel: the stronger and more recent one + var comments = {}; + handlers['COMMENT_REPLY'] = function (ctx, box, data, cb) { + var msg = data.msg; + var hash = data.hash; + var content = msg.content; + // content.channel + + //if (isMuted(ctx, data)) { return void cb(true); } + + var channel = content.channel; + if (!channel) { return void cb(true); } + var res = ctx.store.manager.findChannel(channel); + if (!res.length) { return void cb(true); } + + var title, href; + // Check if the pad is in our drive + if (!res.some(function (obj) { + if (!obj.data) { return; } + href = obj.data.href; + title = obj.data.filename || obj.data.title; + return true; + })) { return void cb(true); } + + // If we don't have the edit url, ignore this notification + if (!href) { return void cb(true); } + + // Add the title + content.href = href; + content.title = title; + + // Remove duplicates + var old = comments[channel]; + var toRemove = old ? old.data : undefined; + + // Update the data + comments[channel] = { + data: { + type: box.type, + hash: hash + } + }; + + cb(false, toRemove); + }; + removeHandlers['COMMENT_REPLY'] = function (ctx, box, data, hash) { + var content = data.content; + var channel = content.channel; + var old = comments[channel]; + if (old && old.data && old.data.hash === hash) { + delete comments[channel]; + } + }; + return { add: function (ctx, box, data, cb) { diff --git a/www/pad/comments.js b/www/pad/comments.js index bdb65b95f..d881ba2fd 100644 --- a/www/pad/comments.js +++ b/www/pad/comments.js @@ -80,6 +80,7 @@ define([ data.avatar = userData.avatar; data.profile = userData.profile; data.curvePublic = userData.curvePublic; + data.notifications = userData.notifications; if (typeof(onChange) === "function" && Sortify(data) !== old) { onChange(); } @@ -92,6 +93,43 @@ define([ Env.metadataMgr.updateMetadata(md); }; + var sendReplyNotification = function (Env, uid) { + if (!Env.comments || !Env.comments.data || !Env.comments.authors) { return; } + if (!Env.common.isLoggedIn()) { return; } + var thread = Env.comments.data[uid]; + if (!thread || !Array.isArray(thread.m)) { return; } + var userData = Env.metadataMgr.getUserData(); + var privateData = Env.metadataMgr.getPrivateData(); + var others = {}; + // Get all the other registered users with a mailbox + thread.m.forEach(function (obj) { + var u = obj.u; + if (typeof(u) !== "number") { return; } + var author = Env.comments.authors[u]; + if (!author || others[u] || !author.notifications || !author.curvePublic) { return; } + if (author.curvePublic === userData.curvePublic) { return; } // don't send to yourself + others[u] = { + curvePublic: author.curvePublic, + comment: obj.m, + content: obj.v, + notifications: author.notifications + }; + }); + // Send the notification + Object.keys(others).forEach(function (id) { + var data = others[id]; + Env.common.mailbox.sendTo("COMMENT_REPLY", { + channel: privateData.channel, + comment: data.comment, + content: data.content + }, { + channel: data.notifications, + curvePublic: data.curvePublic + }); + }); + + }; + Messages.comments_submit = "Submit"; // XXX Messages.comments_reply = "Reply"; // XXX Messages.comments_resolve = "Resolve"; // XXX @@ -276,6 +314,9 @@ define([ v: value }); + // Notify other users + sendReplyNotification(Env, key); + // Send to chainpad updateMetadata(Env); Env.framework.localChange();