UNPKG

@replyke/core

Version:

Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.

210 lines 11.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const getUserName_1 = require("./getUserName"); // Utility function to replace $variable placeholders in a template string const replaceTemplateVariables = (template, variables) => { return Object.entries(variables).reduce((result, [key, value]) => result.replace(new RegExp(`\\$${key}`, "g"), value), template); }; // Resolves a template field: calls the function with vars, or replaces placeholders in the string. // Falls back to the default string template when no custom field is provided. const resolveField = (field, defaultTemplate, vars) => { if (field === undefined) { return replaceTemplateVariables(defaultTemplate, vars); } if (typeof field === "function") { return field(vars); } return replaceTemplateVariables(field, vars); }; const configureMessage = (vars, defaultTitleTemplate, defaultContentTemplate, notificationTemplate) => { const title = resolveField(notificationTemplate?.title, defaultTitleTemplate, vars); const content = resolveField(notificationTemplate?.content, defaultContentTemplate, vars); return { title: title.trim(), content: content.trim() }; }; // Builds initiator name/username from notification metadata const getInitiatorVars = (notification) => ({ initiatorName: (0, getUserName_1.getUserName)({ id: notification.metadata.initiatorId, name: notification.metadata.initiatorName, username: notification.metadata.initiatorUsername, }, "name"), initiatorUsername: (0, getUserName_1.getUserName)({ id: notification.metadata.initiatorId, name: notification.metadata.initiatorName, username: notification.metadata.initiatorUsername, }, "username"), }); // Main notification mapping logic exports.default = (notifications, notificationTemplates) => { return notifications.map((notification) => { if (notification.title) return notification; let title = ""; let content; switch (notification.type) { case "system": title = notification.metadata.title || "System message"; content = notification.metadata.content || "You have a new system message"; break; case "entity-comment": { const vars = { ...getInitiatorVars(notification), entityTitle: notification.metadata.entityTitle || "", entityContent: notification.metadata.entityContent || "", commentContent: notification.metadata.commentContent || "", }; ({ title, content } = configureMessage(vars, `$initiatorUsername commented on your post`, `$commentContent`, notificationTemplates?.entityComment)); break; } case "comment-reply": { const vars = { ...getInitiatorVars(notification), entityTitle: notification.metadata.entityTitle || "", entityContent: notification.metadata.entityContent || "", commentContent: notification.metadata.commentContent || "", replyContent: notification.metadata.replyContent || "", }; ({ title, content } = configureMessage(vars, `$initiatorUsername replied to your comment`, `$replyContent`, notificationTemplates?.commentReply)); break; } case "entity-mention": { const vars = { ...getInitiatorVars(notification), entityTitle: notification.metadata.entityTitle || "", entityContent: notification.metadata.entityContent || "", }; ({ title, content } = configureMessage(vars, `$initiatorUsername mentioned you in their post`, `$entityTitle`, notificationTemplates?.entityMention)); break; } case "comment-mention": { const vars = { ...getInitiatorVars(notification), entityTitle: notification.metadata.entityTitle || "", entityContent: notification.metadata.entityContent || "", commentContent: notification.metadata.commentContent || "", }; ({ title, content } = configureMessage(vars, `$initiatorUsername mentioned you in their comment`, `$commentContent`, notificationTemplates?.commentMention)); break; } case "entity-upvote": { const vars = { ...getInitiatorVars(notification), entityTitle: notification.metadata.entityTitle || "", entityContent: notification.metadata.entityContent || "", }; ({ title, content } = configureMessage(vars, `$initiatorUsername upvoted your post`, ``, notificationTemplates?.entityUpvote)); break; } case "comment-upvote": { const vars = { ...getInitiatorVars(notification), entityTitle: notification.metadata.entityTitle || "", entityContent: notification.metadata.entityContent || "", commentContent: notification.metadata.commentContent || "", }; ({ title, content } = configureMessage(vars, `$initiatorUsername upvoted your comment`, `$commentContent`, notificationTemplates?.commentUpvote)); break; } case "new-follow": { const vars = { ...getInitiatorVars(notification), }; ({ title, content } = configureMessage(vars, `$initiatorUsername started following you`, ``, notificationTemplates?.newFollow)); break; } case "entity-reaction": { const vars = { ...getInitiatorVars(notification), entityTitle: notification.metadata.entityTitle || "", entityContent: notification.metadata.entityContent || "", reactionType: notification.metadata.reactionType || "", }; ({ title, content } = configureMessage(vars, `$initiatorUsername reacted $reactionType to your post`, ``, notificationTemplates?.entityReaction)); break; } case "comment-reaction": { const vars = { ...getInitiatorVars(notification), entityTitle: notification.metadata.entityTitle || "", entityContent: notification.metadata.entityContent || "", commentContent: notification.metadata.commentContent || "", reactionType: notification.metadata.reactionType || "", }; ({ title, content } = configureMessage(vars, `$initiatorUsername reacted $reactionType to your comment`, `$commentContent`, notificationTemplates?.commentReaction)); break; } case "entity-reaction-milestone-specific": { const vars = { entityTitle: notification.metadata.entityTitle || "", entityContent: notification.metadata.entityContent || "", reactionType: notification.metadata.reactionType || "", milestoneCount: notification.metadata.milestoneCount?.toString() || "", }; ({ title, content } = configureMessage(vars, `Your post reached $milestoneCount $reactionType reactions`, ``, notificationTemplates?.entityReactionMilestoneSpecific)); break; } case "entity-reaction-milestone-total": { const vars = { entityTitle: notification.metadata.entityTitle || "", entityContent: notification.metadata.entityContent || "", milestoneCount: notification.metadata.milestoneCount?.toString() || "", }; ({ title, content } = configureMessage(vars, `Your post reached $milestoneCount reactions`, ``, notificationTemplates?.entityReactionMilestoneTotal)); break; } case "comment-reaction-milestone-specific": { const vars = { entityTitle: notification.metadata.entityTitle || "", entityContent: notification.metadata.entityContent || "", commentContent: notification.metadata.commentContent || "", reactionType: notification.metadata.reactionType || "", milestoneCount: notification.metadata.milestoneCount?.toString() || "", }; ({ title, content } = configureMessage(vars, `Your comment reached $milestoneCount $reactionType reactions`, `$commentContent`, notificationTemplates?.commentReactionMilestoneSpecific)); break; } case "comment-reaction-milestone-total": { const vars = { entityTitle: notification.metadata.entityTitle || "", entityContent: notification.metadata.entityContent || "", commentContent: notification.metadata.commentContent || "", milestoneCount: notification.metadata.milestoneCount?.toString() || "", }; ({ title, content } = configureMessage(vars, `Your comment reached $milestoneCount reactions`, `$commentContent`, notificationTemplates?.commentReactionMilestoneTotal)); break; } case "connection-request": { const vars = { ...getInitiatorVars(notification), }; ({ title, content } = configureMessage(vars, `$initiatorUsername sent you a connection request`, ``, notificationTemplates?.connectionRequest)); break; } case "connection-accepted": { const vars = { ...getInitiatorVars(notification), }; ({ title, content } = configureMessage(vars, `$initiatorUsername accepted your connection request`, ``, notificationTemplates?.connectionAccepted)); break; } case "space-membership-approved": { const vars = { spaceName: notification.metadata.spaceName || "", spaceShortId: notification.metadata.spaceShortId || "", spaceSlug: notification.metadata.spaceSlug || "", }; ({ title, content } = configureMessage(vars, `Your request to join $spaceName has been approved`, ``, notificationTemplates?.spaceMembershipApproved)); break; } default: break; } return { ...notification, title, content, }; }); }; //# sourceMappingURL=addNotificationsMessages.js.map