Skip to content

Commit

Permalink
add mentions notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Megha-Dev-19 committed Dec 14, 2023
1 parent 5db2210 commit 63f0129
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/devhub/entity/community/Compose.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,36 @@ const content = {
text: state.text,
};

function extractMentions(text) {
const mentionRegex =
/@((?:(?:[a-z\d]+[-_])*[a-z\d]+\.)*(?:[a-z\d]+[-_])*[a-z\d]+)/gi;
mentionRegex.lastIndex = 0;
const accountIds = new Set();
for (const match of text.matchAll(mentionRegex)) {
if (
!/[\w`]/.test(match.input.charAt(match.index - 1)) &&
!/[/\w`]/.test(match.input.charAt(match.index + match[0].length)) &&
match[1].length >= 2 &&
match[1].length <= 64
) {
accountIds.add(match[1].toLowerCase());
}
}
return [...accountIds];
}

function extractTagNotifications(text, item) {
return extractMentions(text || "")
.filter((accountId) => accountId !== context.accountId)
.map((accountId) => ({
key: accountId,
value: {
type: "mention",
item,
},
}));
}

function composeData() {
const data = {
post: {
Expand All @@ -37,6 +67,17 @@ function composeData() {
},
};

const notifications = extractTagNotifications(state.text, {
type: "social",
path: `${context.accountId}/post/main`,
});

if (notifications.length) {
data.index.notify = JSON.stringify(
notifications.length > 1 ? notifications : notifications[0]
);
}

return data;
}

Expand Down

0 comments on commit 63f0129

Please sign in to comment.