Send a notification when replying to a comment

pull/1/head
yflory 5 years ago
parent 52f2ad68d8
commit 473577a9f1

@ -315,6 +315,32 @@ define([
}
};
Messages.comments_notification = 'Replies to your comment "{0}" in <b>{1}</b>'; // 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"

@ -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) {

@ -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();

Loading…
Cancel
Save