Add avatars to rich text comments and mentions

pull/1/head
ansuz 3 years ago
parent 385cd4e947
commit 8d579c0376

@ -3456,7 +3456,8 @@ define([
name: f.displayName, name: f.displayName,
curvePublic: f.curvePublic, curvePublic: f.curvePublic,
profile: f.profile, profile: f.profile,
notifications: f.notifications notifications: f.notifications,
uid: f.uid,
}; };
}); });
}; };
@ -3555,7 +3556,7 @@ define([
}; };
// Set the value to receive from the autocomplete // Set the value to receive from the autocomplete
var toInsert = function (data, key) { var toInsert = function (data, key) {
var name = data.name.replace(/[^a-zA-Z0-9]+/g, "-"); var name = (data.name.replace(/[^a-zA-Z0-9]+/g, "-") || "").trim() || Messages.anonymous; // XXX
return "[@"+name+"|"+key+"]"; return "[@"+name+"|"+key+"]";
}; };
@ -3608,18 +3609,20 @@ define([
var avatar = h('span.cp-avatar', { var avatar = h('span.cp-avatar', {
contenteditable: false contenteditable: false
}); });
common.displayAvatar($(avatar), data.avatar, data.name);
var displayName = (data.name || "").trim() || Messages.anonymous;
common.displayAvatar($(avatar), data.avatar, displayName); // XXX
return h('span.cp-mentions', { return h('span.cp-mentions', {
'data-curve': data.curvePublic, 'data-curve': data.curvePublic,
'data-notifications': data.notifications, 'data-notifications': data.notifications,
'data-profile': data.profile, 'data-profile': data.profile,
'data-name': Util.fixHTML(data.name), 'data-name': Util.fixHTML(displayName),
'data-avatar': data.avatar || "", 'data-avatar': data.avatar || "",
}, [ }, [
avatar, avatar,
h('span.cp-mentions-name', { h('span.cp-mentions-name', {
contenteditable: false contenteditable: false
}, data.name) }, displayName)
]); ]);
}; };
} }
@ -3651,7 +3654,7 @@ define([
}).map(function (key) { }).map(function (key) {
var data = sources[key]; var data = sources[key];
return { return {
label: data.name, label: (data.name || "").trim() || Messages.anonymous,
value: key value: key
}; };
}); });
@ -3686,10 +3689,12 @@ define([
var obj = sources[key]; var obj = sources[key];
if (!obj) { return; } if (!obj) { return; }
var avatar = h('span.cp-avatar'); var avatar = h('span.cp-avatar');
common.displayAvatar($(avatar), obj.avatar, obj.name); var displayName = (obj.name || "").trim() || Messages.anonymous;
common.displayAvatar($(avatar), obj.avatar, displayName, Util.noop, obj.uid); // XXX
var li = h('li.cp-autocomplete-value', [ var li = h('li.cp-autocomplete-value', [
avatar, avatar,
h('span', obj.name) h('span', displayName),
]); ]);
return $(li).appendTo(ul); return $(li).appendTo(ul);
}; };

@ -249,11 +249,18 @@ define([
if (existing.indexOf(n) !== -1) { n = 0; } if (existing.indexOf(n) !== -1) { n = 0; }
return n; return n;
}; };
funcs.getAuthorId = function(authors, curve) { funcs.getAuthorId = function(authors, curve, tokenId) {
var existing = Object.keys(authors || {}).map(Number); var existing = Object.keys(authors || {}).map(Number);
if (!funcs.isLoggedIn()) { return authorUid(existing); }
var uid; var uid;
if (!funcs.isLoggedIn()) {
existing.some(function (id) {
var author = authors[id] || {};
if (author.uid !== tokenId) { return; }
uid = Number(id);
return true;
});
return uid || authorUid(existing);
}
existing.some(function(id) { existing.some(function(id) {
var author = authors[id] || {}; var author = authors[id] || {};
if (author.curvePublic !== curve) { return; } if (author.curvePublic !== curve) { return; }

@ -43,18 +43,21 @@ define([
var canonicalize = function(t) { return t.replace(/\r\n/g, '\n'); }; var canonicalize = function(t) { return t.replace(/\r\n/g, '\n'); };
var getAuthorId = function(Env, curve) { var getAuthorId = function(Env, curve, uid) {
return Env.common.getAuthorId(Env.comments.authors, curve); return Env.common.getAuthorId(Env.comments.authors, curve, uid);
}; };
// Return the author ID and add/update the data for registered users // Return the author ID and add/update user data
// Return the username for unregistered users // associate data with a curvePublic for registered users and the uid otherwise
var updateAuthorData = function(Env, onChange) { var updateAuthorData = function(Env, onChange) {
var userData = Env.metadataMgr.getUserData(); var userData = Env.metadataMgr.getUserData();
var myAuthorId;
if (!Env.common.isLoggedIn()) { if (!Env.common.isLoggedIn()) {
return userData.name; myAuthorId = getAuthorId(Env, undefined, userData.uid);
} else {
myAuthorId = getAuthorId(Env, userData.curvePublic);
} }
var myAuthorId = getAuthorId(Env, userData.curvePublic);
var data = Env.comments.authors[myAuthorId] = Env.comments.authors[myAuthorId] || {}; var data = Env.comments.authors[myAuthorId] = Env.comments.authors[myAuthorId] || {};
var old = Sortify(data); var old = Sortify(data);
data.name = userData.name; data.name = userData.name;
@ -62,6 +65,8 @@ define([
data.profile = userData.profile; data.profile = userData.profile;
data.curvePublic = userData.curvePublic; data.curvePublic = userData.curvePublic;
data.notifications = userData.notifications; data.notifications = userData.notifications;
data.uid = userData.uid;
if (typeof(onChange) === "function" && Sortify(data) !== old) { if (typeof(onChange) === "function" && Sortify(data) !== old) {
onChange(); onChange();
} }
@ -82,6 +87,9 @@ define([
var userData = Env.metadataMgr.getUserData(); var userData = Env.metadataMgr.getUserData();
var privateData = Env.metadataMgr.getPrivateData(); var privateData = Env.metadataMgr.getPrivateData();
var others = {}; var others = {};
// XXX mentioned users should be excluded from the list of notified recipients to avoid notifying them twice
// Get all the other registered users with a mailbox // Get all the other registered users with a mailbox
thread.m.forEach(function(obj) { thread.m.forEach(function(obj) {
var u = obj.u; var u = obj.u;
@ -93,7 +101,8 @@ define([
curvePublic: author.curvePublic, curvePublic: author.curvePublic,
comment: obj.m, comment: obj.m,
content: obj.v, content: obj.v,
notifications: author.notifications notifications: author.notifications,
uid: author.uid,
}; };
}); });
// Send the notification // Send the notification
@ -146,7 +155,7 @@ define([
'aria-required': true, 'aria-required': true,
contenteditable: true, contenteditable: true,
}); });
Env.common.displayAvatar($(avatar), userData.avatar, name); Env.common.displayAvatar($(avatar), userData.avatar, name, Util.noop, userData.uid);
var cancel = h('button.btn.btn-cancel', { var cancel = h('button.btn.btn-cancel', {
tabindex: 1 tabindex: 1
@ -224,7 +233,9 @@ define([
if (Env.common.isLoggedIn()) { if (Env.common.isLoggedIn()) {
var authors = {}; var authors = {};
Object.keys((Env.comments && Env.comments.authors) ||  {}).forEach(function(id) { Object.keys((Env.comments && Env.comments.authors) ||  {})
.filter(function (id) { return Util.find(Env, ['commments', 'authors', id, 'curvePublic']); })
.forEach(function(id) {
var obj = Util.clone(Env.comments.authors[id]); var obj = Util.clone(Env.comments.authors[id]);
authors[obj.curvePublic] = obj; authors[obj.curvePublic] = obj;
}); });
@ -369,7 +380,7 @@ define([
var name = Util.fixHTML(author.name || Messages.anonymous); var name = Util.fixHTML(author.name || Messages.anonymous);
var date = new Date(msg.t); var date = new Date(msg.t);
var avatar = h('span.cp-avatar'); var avatar = h('span.cp-avatar');
Env.common.displayAvatar($(avatar), author.avatar, name); Env.common.displayAvatar($(avatar), author.avatar, name, Util.noop, author.uid);
if (author.profile) { if (author.profile) {
$(avatar).click(function(e) { $(avatar).click(function(e) {
Env.common.openURL(Hash.hashToHref(author.profile, 'profile')); Env.common.openURL(Hash.hashToHref(author.profile, 'profile'));
@ -393,7 +404,7 @@ define([
} }
cleanMentions($el); cleanMentions($el);
var avatar = h('span.cp-avatar'); var avatar = h('span.cp-avatar');
Env.common.displayAvatar($(avatar), avatarUrl, name); Env.common.displayAvatar($(avatar), avatarUrl, name, Util.noop, author.uid);
$el.append([ $el.append([
avatar, avatar,
h('span.cp-mentions-name', name) h('span.cp-mentions-name', name)

Loading…
Cancel
Save