-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
530 additions
and
50 deletions.
There are no files selected for viewing
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
14 changes: 14 additions & 0 deletions
14
packages/server/prisma/migrations/20240517130826_proposal_discusions/migration.sql
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,14 @@ | ||
-- AlterEnum | ||
-- This migration adds more than one value to an enum. | ||
-- With PostgreSQL versions 11 and earlier, this is not possible | ||
-- in a single migration. This can be worked around by creating | ||
-- multiple migrations, each migration adding only one value to | ||
-- the enum. | ||
|
||
|
||
ALTER TYPE "NotificationKind" ADD VALUE 'PROPOSAL_DISCUSSION_MENTION'; | ||
ALTER TYPE "NotificationKind" ADD VALUE 'PROPOSAL_DISCUSSION_REPLY'; | ||
ALTER TYPE "NotificationKind" ADD VALUE 'PROPOSAL_DISCUSSION_CREATOR'; | ||
ALTER TYPE "NotificationKind" ADD VALUE 'PROPOSAL_DISCUSSION_CONTRIBUTOR'; | ||
ALTER TYPE "NotificationKind" ADD VALUE 'PROPOSAL_DISCUSSION_ALL'; | ||
ALTER TYPE "NotificationKind" ADD VALUE 'PROPOSAL_ENTITY_DISCUSSION'; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { match } from 'ts-pattern' | ||
|
||
import { PIONEER_URL } from '@/common/config' | ||
import { renderPioneerEmail } from '@/common/email-templates/pioneer-email' | ||
|
||
import { EmailFromNotificationFn } from './utils' | ||
import { getProposalPost } from './utils/proposal' | ||
|
||
export const fromProposalPostCreatedNotification: EmailFromNotificationFn = async ({ id, kind, entityId, member }) => { | ||
if ( | ||
kind !== 'PROPOSAL_DISCUSSION_MENTION' && | ||
kind !== 'PROPOSAL_DISCUSSION_REPLY' && | ||
kind !== 'PROPOSAL_DISCUSSION_CREATOR' && | ||
kind !== 'PROPOSAL_DISCUSSION_CONTRIBUTOR' && | ||
kind !== 'PROPOSAL_DISCUSSION_ALL' && | ||
kind !== 'PROPOSAL_ENTITY_DISCUSSION' | ||
) { | ||
return | ||
} | ||
|
||
if (!entityId) { | ||
throw Error(`Missing proposal discussion post id in notification ${kind}, with id: ${id}`) | ||
} | ||
|
||
const { author, proposal, proposalId } = await getProposalPost(entityId) | ||
|
||
const emailSubject = `[Pioneer] proposal: ${proposal}` | ||
|
||
const emailSummary: string = match(kind) | ||
.with('PROPOSAL_DISCUSSION_MENTION', () => `${author} mentioned you regarding the proposal ${proposal}.`) | ||
.with('PROPOSAL_DISCUSSION_REPLY', () => `${author} replied to your post regarding the proposal ${proposal}.`) | ||
.with( | ||
'PROPOSAL_DISCUSSION_CREATOR', | ||
'PROPOSAL_DISCUSSION_CONTRIBUTOR', | ||
'PROPOSAL_DISCUSSION_ALL', | ||
'PROPOSAL_ENTITY_DISCUSSION', | ||
() => `${author} posted regarding the proposal ${proposal}.` | ||
) | ||
.exhaustive() | ||
|
||
const emailText: string = match(kind) | ||
.with('PROPOSAL_DISCUSSION_MENTION', () => `${author} mentioned you regarding the proposal ${proposal}.`) | ||
.with('PROPOSAL_DISCUSSION_REPLY', () => `${author} replied to your post regarding the proposal ${proposal}.`) | ||
.with( | ||
'PROPOSAL_DISCUSSION_CREATOR', | ||
'PROPOSAL_DISCUSSION_CONTRIBUTOR', | ||
'PROPOSAL_DISCUSSION_ALL', | ||
'PROPOSAL_ENTITY_DISCUSSION', | ||
() => `${author} posted regarding the proposal ${proposal}.` | ||
) | ||
.exhaustive() | ||
|
||
const emailHtml = renderPioneerEmail({ | ||
memberHandle: member.name, | ||
summary: emailSummary, | ||
text: emailText, | ||
button: { | ||
label: 'See on Pioneer', | ||
href: `${PIONEER_URL}/#/proposals/preview/${proposalId}?post=${entityId}`, | ||
}, | ||
}) | ||
|
||
return { | ||
subject: emailSubject, | ||
html: emailHtml, | ||
to: member.email, | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/server/src/notifier/model/email/utils/proposal.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,28 @@ | ||
import { request } from 'graphql-request' | ||
import { memoize } from 'lodash' | ||
|
||
import { QUERY_NODE_ENDPOINT } from '@/common/config' | ||
import { GetProposalDiscussionPostDocument } from '@/common/queries' | ||
|
||
interface ProposalDiscussionPost { | ||
author: string | ||
proposal: string | ||
proposalId: string | ||
} | ||
|
||
export const getProposalPost = memoize(async (id: string): Promise<ProposalDiscussionPost> => { | ||
const { proposalDiscussionPostByUniqueInput: post } = await request( | ||
QUERY_NODE_ENDPOINT, | ||
GetProposalDiscussionPostDocument, | ||
{ id } | ||
) | ||
if (!post) { | ||
throw Error(`Failed to fetch proposal discussion post ${id} on the QN`) | ||
} | ||
|
||
return { | ||
author: post.author.handle, | ||
proposal: post.discussionThread.proposal.title, | ||
proposalId: post.discussionThread.proposal.id, | ||
} | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { pick, uniq } from 'lodash' | ||
|
||
import { | ||
GetCurrentRolesQuery, | ||
ProposalDiscussionPostCreatedEventFieldsFragmentDoc, | ||
useFragment, | ||
} from '@/common/queries' | ||
|
||
import { NotifEventFromQNEvent, isOlderThan, getMentionedMemberIds } from './utils' | ||
|
||
export const fromProposalPostAddedEvent: NotifEventFromQNEvent< | ||
'ProposalDiscussionPostCreatedEvent', | ||
[GetCurrentRolesQuery] | ||
> = async (event, buildEvents, roles) => { | ||
const postCreatedEvent = useFragment(ProposalDiscussionPostCreatedEventFieldsFragmentDoc, event) | ||
const post = postCreatedEvent.post | ||
|
||
const mentionedMemberIds = getMentionedMemberIds(post.text, roles) | ||
const repliedToMemberId = post.repliesTo && [Number(post.repliesTo.authorId)] | ||
const earlierPosts = post.discussionThread.posts.filter(isOlderThan(post)) | ||
const earlierAuthors = uniq(earlierPosts.map((post) => Number(post.authorId))) | ||
|
||
const eventData = pick(postCreatedEvent, 'inBlock', 'id') | ||
|
||
return buildEvents(eventData, post.id, [post.authorId], ({ generalEvent, entityEvent }) => [ | ||
generalEvent('PROPOSAL_DISCUSSION_MENTION', mentionedMemberIds), | ||
generalEvent('PROPOSAL_DISCUSSION_REPLY', repliedToMemberId ?? []), | ||
generalEvent('PROPOSAL_DISCUSSION_CREATOR', [post.discussionThread.proposal.creatorId]), | ||
generalEvent('PROPOSAL_DISCUSSION_CONTRIBUTOR', earlierAuthors), | ||
entityEvent('PROPOSAL_ENTITY_DISCUSSION', post.discussionThread.proposal.id), | ||
generalEvent('PROPOSAL_DISCUSSION_ALL', 'ANY'), | ||
]) | ||
} |
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
13 changes: 13 additions & 0 deletions
13
packages/server/src/notifier/queries/entities/proposal.graphql
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,13 @@ | ||
query GetProposalDiscussionPost($id: ID!) { | ||
proposalDiscussionPostByUniqueInput(where: { id: $id }) { | ||
author { | ||
handle | ||
} | ||
discussionThread { | ||
proposal { | ||
id | ||
title | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.