11import Logger from "bunyan" ;
22import { Config } from "coral-server/config" ;
3- import { MongoContext } from "coral-server/data/context" ;
43import { Comment , getLatestRevision } from "coral-server/models/comment" ;
5- import { getURLWithCommentID , retrieveStory } from "coral-server/models/story" ;
4+ import { Site } from "coral-server/models/site" ;
5+ import { getURLWithCommentID , Story } from "coral-server/models/story" ;
66import { User } from "coral-server/models/user" ;
77import { convert } from "html-to-text" ;
88import fetch from "node-fetch" ;
@@ -12,16 +12,22 @@ const ProfileType = "Coral";
1212
1313// From Coral input types
1414
15- interface CreateReplyInput {
15+ interface CreateReplyParams {
1616 from : User ;
1717 to : User ;
18+
19+ story : Readonly < Story > ;
20+ site : Readonly < Site > ;
1821 parent : Comment ;
1922 reply : Comment ;
2023}
2124
22- interface CreateRecInput {
25+ interface CreateRecParams {
2326 from : User ;
2427 to : User ;
28+
29+ story : Readonly < Story > ;
30+ site : Readonly < Site > ;
2531 comment : Comment ;
2632}
2733
@@ -33,6 +39,17 @@ enum NotificationType {
3339 CoralReply = "CoralReply" ,
3440}
3541
42+ interface StoryInput {
43+ id : string ;
44+ title : string ;
45+ url : string ;
46+ }
47+
48+ interface SiteInput {
49+ id : string ;
50+ name : string ;
51+ }
52+
3653interface ExternalUserProfile {
3754 id : string ;
3855 type : string ;
@@ -43,7 +60,7 @@ interface ExternalUserProfile {
4360 ssoIds ?: string [ ] ;
4461}
4562
46- interface CommentPayload {
63+ interface CommentInput {
4764 id : string ;
4865 storyId : string ;
4966 snippet ?: string | null ;
@@ -64,11 +81,9 @@ export class ExternalNotificationsService {
6481 private url ?: string | null ;
6582 private apiKey ?: string | null ;
6683 private logger : Logger ;
67- private mongo : MongoContext ;
6884
69- constructor ( config : Config , logger : Logger , mongo : MongoContext ) {
85+ constructor ( config : Config , logger : Logger ) {
7086 this . logger = logger ;
71- this . mongo = mongo ;
7287
7388 this . url = config . get ( "external_notifications_api_url" ) ;
7489 this . apiKey = config . get ( "external_notifications_api_key" ) ;
@@ -101,13 +116,11 @@ export class ExternalNotificationsService {
101116 }
102117 }
103118
104- private async commentToPayload ( comment : Comment ) : Promise < CommentPayload > {
105- const story = await retrieveStory (
106- this . mongo ,
107- comment . tenantID ,
108- comment . storyID
109- ) ;
110- const url = story ? getURLWithCommentID ( story . url , comment . id ) : null ;
119+ private async commentToInput (
120+ comment : Comment ,
121+ story : Story
122+ ) : Promise < CommentInput > {
123+ const url = getURLWithCommentID ( story . url , comment . id ) ;
111124
112125 return {
113126 id : comment . id ,
@@ -117,7 +130,22 @@ export class ExternalNotificationsService {
117130 } ;
118131 }
119132
120- public async createRec ( input : CreateRecInput ) {
133+ private storyToInput ( story : Story ) : StoryInput {
134+ return {
135+ id : story . id ,
136+ title : story . metadata ?. title ?? "" ,
137+ url : story . url ,
138+ } ;
139+ }
140+
141+ private siteToInput ( site : Site ) : SiteInput {
142+ return {
143+ id : site . id ,
144+ name : site . name ,
145+ } ;
146+ }
147+
148+ public async createRec ( input : CreateRecParams ) {
121149 if ( ! this . active ( ) ) {
122150 return ;
123151 }
@@ -128,8 +156,9 @@ export class ExternalNotificationsService {
128156 type : NotificationType . CoralRec ,
129157 from : this . userToExternalProfile ( input . from ) ,
130158 to : this . userToExternalProfile ( input . to ) ,
131- storyId : input . comment . storyID ,
132- comment : this . commentToPayload ( input . comment ) ,
159+ story : this . storyToInput ( input . story ) ,
160+ site : this . siteToInput ( input . site ) ,
161+ comment : this . commentToInput ( input . comment , input . story ) ,
133162 } ;
134163
135164 return await this . send ( data ) ;
@@ -143,7 +172,7 @@ export class ExternalNotificationsService {
143172 return false ;
144173 }
145174
146- public async createReply ( input : CreateReplyInput ) {
175+ public async createReply ( input : CreateReplyParams ) {
147176 if ( ! this . active ( ) ) {
148177 return ;
149178 }
@@ -154,9 +183,10 @@ export class ExternalNotificationsService {
154183 type : NotificationType . CoralReply ,
155184 from : this . userToExternalProfile ( input . from ) ,
156185 to : this . userToExternalProfile ( input . to ) ,
157- storyId : input . parent . storyID ,
158- comment : await this . commentToPayload ( input . parent ) ,
159- reply : await this . commentToPayload ( input . reply ) ,
186+ story : this . storyToInput ( input . story ) ,
187+ site : this . siteToInput ( input . site ) ,
188+ comment : await this . commentToInput ( input . parent , input . story ) ,
189+ reply : await this . commentToInput ( input . reply , input . story ) ,
160190 } ;
161191
162192 return await this . send ( data ) ;
0 commit comments