-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #329 from polkasafe/fix/postCommentAddedIssue
fix: post comment added issue
- Loading branch information
Showing
4 changed files
with
171 additions
and
23 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
cloud-functions/src/notification-engine/townhall/_utils/getMentionedUsernames.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default function getMentionedUsernames(content: string): string[] { | ||
// matches for spaces around it and/or html tags, except for the anchor tag | ||
const mentionedUsernamesPattern = /(?<=(?:^|\s+|<((?!a\b)\w+)>| )@)\w+(?=(?:\s+| |<\/((?!a\b)\w+)>))/g; | ||
return String(content).match(mentionedUsernamesPattern) || []; | ||
} |
78 changes: 78 additions & 0 deletions
78
cloud-functions/src/notification-engine/townhall/_utils/sendMentionNotifications.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { NotificationService } from '../../NotificationService'; | ||
import getTemplateRender from '../../global-utils/getTemplateRender'; | ||
import { NOTIFICATION_SOURCE } from '../../notification_engine_constants'; | ||
import getHouseNotificationPrefsFromTHNotificationPrefs from './getHouseNotificationPrefsFromTHNotificationPrefs'; | ||
import getMentionedUsernames from './getMentionedUsernames'; | ||
import { ETHContentType, ITHUser } from './types'; | ||
|
||
interface Args { | ||
firestore_db: FirebaseFirestore.Firestore | ||
authorUsername: string | null; | ||
htmlContent: string; | ||
house_id: string; | ||
type: ETHContentType; | ||
url: string; | ||
} | ||
|
||
const TRIGGER_NAME = 'newMention'; | ||
const SOURCE = NOTIFICATION_SOURCE.TOWNHALL; | ||
|
||
export default async function sendMentionNotifications(args : Args) { | ||
if (!args) throw Error(`Missing arguments for trigger: ${TRIGGER_NAME}`); | ||
const { firestore_db, authorUsername, htmlContent, house_id, type, url } = args; | ||
console.log(`Running trigger: ${TRIGGER_NAME}, with args: ${JSON.stringify({ authorUsername, htmlContent, house_id, type, url })}`); | ||
|
||
const mentionedUsernames = getMentionedUsernames(htmlContent).filter((username) => username !== authorUsername); | ||
console.log(`Mentioned usernames: ${JSON.stringify(mentionedUsernames)}`); | ||
if (!mentionedUsernames.length) return; | ||
|
||
for (const mentionedUsername of mentionedUsernames) { | ||
// get user preferences | ||
const mentionedUserDocSnapshot = await firestore_db.collection('users').where('username', '==', mentionedUsername).limit(1).get(); | ||
if (mentionedUserDocSnapshot.empty) continue; | ||
|
||
const mentionedUserData = mentionedUserDocSnapshot.docs[0].data() as ITHUser; | ||
if (!mentionedUserData || !mentionedUserData.notification_preferences) continue; | ||
|
||
const mentionedUserNotificationPreferences = getHouseNotificationPrefsFromTHNotificationPrefs(mentionedUserData.notification_preferences, house_id); | ||
if (!mentionedUserNotificationPreferences) continue; | ||
|
||
if (!mentionedUserNotificationPreferences.triggerPreferences?.[TRIGGER_NAME].enabled || !(mentionedUserNotificationPreferences.triggerPreferences?.[TRIGGER_NAME]?.mention_types || []).includes(type)) continue; | ||
|
||
let domain = ''; | ||
try { | ||
const urlObject = new URL(url); | ||
domain = urlObject.origin; // This gets the protocol + hostname + port (if any) | ||
} catch (error) { | ||
console.error('Invalid URL: ', error); | ||
} | ||
|
||
const { htmlMessage, markdownMessage, textMessage, subject } = await getTemplateRender( | ||
SOURCE, | ||
TRIGGER_NAME, | ||
{ | ||
...args, | ||
authorUsername, | ||
url, | ||
content: htmlContent, | ||
domain: domain, | ||
username: mentionedUsername, | ||
mentionType: type | ||
}); | ||
|
||
const notificationServiceInstance = new NotificationService( | ||
SOURCE, | ||
TRIGGER_NAME, | ||
htmlMessage, | ||
markdownMessage, | ||
textMessage, | ||
subject, | ||
{ | ||
link: url | ||
} | ||
); | ||
|
||
console.log(`Sending notification for trigger: ${TRIGGER_NAME}, mention type: ${type} by user ${mentionedUserData.id}`); | ||
await notificationServiceInstance.notifyAllChannels(mentionedUserNotificationPreferences); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters