From 8b40cbed70e12e96dd8324a1b051931c9baa1dc8 Mon Sep 17 00:00:00 2001 From: Elliot Braem <16282460+elliotBraem@users.noreply.github.com> Date: Wed, 3 Jan 2024 12:04:20 -0500 Subject: [PATCH] enhancements --- apps/builddao/widget/Compose.jsx | 151 ++++++++++++++++++++++--------- apps/builddao/widget/Feed.jsx | 16 ++-- 2 files changed, 114 insertions(+), 53 deletions(-) diff --git a/apps/builddao/widget/Compose.jsx b/apps/builddao/widget/Compose.jsx index 950b33ef..ded34833 100644 --- a/apps/builddao/widget/Compose.jsx +++ b/apps/builddao/widget/Compose.jsx @@ -28,6 +28,50 @@ function tagsFromLabels(labels) { ); } +const 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]; +}; + +const extractHashtags = (text) => { + const hashtagRegex = /#(\w+)/gi; + hashtagRegex.lastIndex = 0; + const hashtags = new Set(); + for (const match of text.matchAll(hashtagRegex)) { + if ( + !/[\w`]/.test(match.input.charAt(match.index - 1)) && + !/[/\w`]/.test(match.input.charAt(match.index + match[0].length)) + ) { + hashtags.add(match[1].toLowerCase()); + } + } + return [...hashtags]; +}; + +const extractMentionNotifications = (text, item) => + extractMentions(text || "") + .filter((accountId) => accountId !== context.accountId) + .map((accountId) => ({ + key: accountId, + value: { + type: "mention", + item, + }, + })); + function checkAndAppendHashtag(input, target) { if (input.toLowerCase().includes(`#${target.toLowerCase()}`)) { return input; @@ -44,8 +88,8 @@ const postToCustomFeed = ({ feed, text, labels }) => { labels.push(feed.name.toLowerCase()); const requiredHashtags = ["build"]; - requiredHashtags.push((feed.hashtag).toLowerCase()); - requiredHashtags.push((feed.name).toLowerCase()); + requiredHashtags.push(feed.hashtag.toLowerCase()); + requiredHashtags.push(feed.name.toLowerCase()); text = text + `\n\n`; @@ -53,50 +97,67 @@ const postToCustomFeed = ({ feed, text, labels }) => { text = checkAndAppendHashtag(text, hashtag); }); - return Social.set( - { - // [feed.name]: { - // [postId]: { - // "": JSON.stringify({ - // type: "md", - // text, - // labels, - // }), - // metadata: { - // type: feed.name, - // tags: tagsFromLabels(labels), - // }, - // }, - // }, - post: { - main: JSON.stringify({ - type: "md", - text, - // tags: tagsFromLabels(labels), - // postType: feed.name, - }), - }, - index: { - post: JSON.stringify({ key: "main", value: { type: "md" } }), - // every: JSON.stringify({ key: feed.name, value: { type: "md" } }), - hashtag: JSON.stringify( - requiredHashtags.map((hashtag) => ({ - key: hashtag, - value: { type: "social", path: `${context.accountId}/post/main` }, - })) - ), - }, + const data = { + // [feed.name]: { + // [postId]: { + // "": JSON.stringify({ + // type: "md", + // text, + // labels, + // }), + // metadata: { + // type: feed.name, + // tags: tagsFromLabels(labels), + // }, + // }, + // }, + post: { + main: JSON.stringify({ + type: "md", + text, + // tags: tagsFromLabels(labels), + // postType: feed.name, + }), }, - { - force: true, - onCommit: () => { - console.log(`Commited ${feed}: #${postId}`); - }, - onCancel: () => { - console.log(`Cancelled ${feed}: #${postId}`); - }, - } - ); + index: { + post: JSON.stringify({ key: "main", value: { type: "md" } }), + // every: JSON.stringify({ key: feed.name, value: { type: "md" } }), + }, + }; + + const item = { + type: "social", + path: `${context.accountId}/post/main`, + }; + + const notifications = extractMentionNotifications(text, item); + + if (notifications.length) { + data.index.notify = JSON.stringify( + notifications.length > 1 ? notifications : notifications[0] + ); + } + + const hashtags = extractHashtags(text); + + if (hashtags.length) { + data.index.hashtag = JSON.stringify( + hashtags.map((hashtag) => ({ + key: hashtag, + value: item, + })) + ); + } + + return Social.set(data, { + force: true, + onCommit: () => { + // console.log(`Commited ${feed}: #${postId}`); + }, + onCancel: () => { + // console.log(`Cancelled ${feed}: #${postId}`); + }, + }); }; const PostCreator = styled.div` diff --git a/apps/builddao/widget/Feed.jsx b/apps/builddao/widget/Feed.jsx index 93da292b..09fbc763 100644 --- a/apps/builddao/widget/Feed.jsx +++ b/apps/builddao/widget/Feed.jsx @@ -68,17 +68,17 @@ const feedsDict = { icon: "bi-calendar3", name: "resolution", hashtag: "nearyearresolutions2024", - template: `## 🎉 NEAR YEAR RESOLUTIONS: 2024 + template: `### 🎉 NEAR YEAR RESOLUTIONS: 2024 -### 🌟 REFLECTIONS ON THE PAST YEAR: +**🌟 REFLECTIONS ON THE PAST YEAR:** - [Reflection 1 from the past year] - [Reflection 2 from the past year] -### 🎯 NEW YEAR'S RESOLUTIONS: +**🎯 NEW YEAR'S RESOLUTIONS:** - [Resolution 1] - [Resolution 2] -### 📊 MEASURING SUCCESS: +**📊 MEASURING SUCCESS:** - [Metric 1 for Success] - [Metric 2 for Success] `, @@ -88,17 +88,17 @@ const feedsDict = { label: "Updates", icon: "bi-bell", name: "update", - template: `## 🔔 DAILY UPDATE: ${formatDate(new Date())} + template: `### 🔔 DAILY UPDATE: ${formatDate(new Date())} -### 📆 YESTERDAY: +**📆 YESTERDAY:** - [Task 1, hyperlink proof] - [Task 2, hyperlink proof] -### 💻 WHAT I AM DOING TODAY: +**💻 WHAT I AM DOING TODAY:** - [Task 1] - [Task 2] -### 🛑 BLOCKERS: +**🛑 BLOCKERS: ** - @anyone that is causing a blocker or outline any blockers in general`, }, };