@replyke/core
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
89 lines • 5.52 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var getUserName_1 = require("./getUserName");
// Utility function to replace variables in a template
var replaceTemplateVariables = function (template, variables) {
return Object.entries(variables).reduce(function (result, _a) {
var key = _a[0], value = _a[1];
return result.replace(new RegExp("\\$".concat(key), "g"), value);
}, template);
};
// Centralized logic to build variable replacements
var getReplacementVariables = function (notification) {
return {
userName: (0, getUserName_1.getUserName)({
id: notification.metadata.initiatorId,
name: notification.metadata.initiatorName,
username: notification.metadata.initiatorUsername,
avatar: notification.metadata.initiatorAvatar,
}, "name"),
userUsername: (0, getUserName_1.getUserName)({
id: notification.metadata.initiatorId,
name: notification.metadata.initiatorName,
username: notification.metadata.initiatorUsername,
avatar: notification.metadata.initiatorAvatar,
}, "username"),
entityTitle: notification.metadata.entityTitle || "",
entityContent: notification.metadata.entityContent || "",
commentContent: notification.metadata.commentContent || "",
replyContent: notification.metadata.replyContent || "",
};
};
// Generalized message configuration function
var configureMessage = function (notification, defaultTitleTemplate, defaultContentTemplate, notificationTemplate) {
var _a, _b;
var variables = getReplacementVariables(notification);
var titleTemplate = (_a = notificationTemplate === null || notificationTemplate === void 0 ? void 0 : notificationTemplate.title) !== null && _a !== void 0 ? _a : defaultTitleTemplate;
var contentTemplate = (_b = notificationTemplate === null || notificationTemplate === void 0 ? void 0 : notificationTemplate.content) !== null && _b !== void 0 ? _b : defaultContentTemplate;
var title = replaceTemplateVariables(titleTemplate, variables);
var content = replaceTemplateVariables(contentTemplate, variables);
return { title: title.trim(), content: content.trim() };
};
// Main notification mapping logic
exports.default = (function (notifications, notificationTemplates) {
return notifications.map(function (notification) {
var _a, _b, _c, _d, _e, _f, _g;
if (notification.title)
return notification;
var title = "";
var content;
switch (notification.type) {
case "entity-comment":
(_a = configureMessage(notification, "$userName commented on your post \"$entityTitle\"", "$commentContent", notificationTemplates === null || notificationTemplates === void 0 ? void 0 : notificationTemplates.entityComment), title = _a.title, content = _a.content);
break;
case "comment-reply":
(_b = configureMessage(notification, "$userName replied to your comment on \"$entityTitle\"", "$replyContent", notificationTemplates === null || notificationTemplates === void 0 ? void 0 : notificationTemplates.commentReply), title = _b.title, content = _b.content);
break;
case "entity-mention":
(_c = configureMessage(notification, "$userName mentioned you in their post", "$entityTitle", notificationTemplates === null || notificationTemplates === void 0 ? void 0 : notificationTemplates.entityMention), title = _c.title, content = _c.content);
break;
case "comment-mention":
(_d = configureMessage(notification, "$userName mentioned you in their comment on \"$entityTitle\"", "$commentContent", notificationTemplates === null || notificationTemplates === void 0 ? void 0 : notificationTemplates.commentMention), title = _d.title, content = _d.content);
break;
case "entity-upvote":
(_e = configureMessage(notification, "$userName upvoted your post \"$entityTitle\"", "", notificationTemplates === null || notificationTemplates === void 0 ? void 0 : notificationTemplates.entityUpvote), title = _e.title, content = _e.content);
break;
case "comment-upvote":
(_f = configureMessage(notification, "$userName upvoted your comment on \"$entityTitle\"", "$commentContent", notificationTemplates === null || notificationTemplates === void 0 ? void 0 : notificationTemplates.commentUpvote), title = _f.title, content = _f.content);
break;
case "new-follow":
(_g = configureMessage(notification, "$userName started following you", "", notificationTemplates === null || notificationTemplates === void 0 ? void 0 : notificationTemplates.newFollow), title = _g.title, content = _g.content);
break;
default:
break;
}
return __assign(__assign({}, notification), { title: title, content: content });
});
});
//# sourceMappingURL=addNotificationsMessages.js.map