|
| 1 | +import Logger from "bunyan"; |
| 2 | +import { Config } from "coral-server/config"; |
| 3 | +import { MongoContext } from "coral-server/data/context"; |
| 4 | +import { Comment, getLatestRevision } from "coral-server/models/comment"; |
| 5 | +import { getURLWithCommentID, retrieveStory } from "coral-server/models/story"; |
| 6 | +import { User } from "coral-server/models/user"; |
| 7 | +import { convert } from "html-to-text"; |
| 8 | +import fetch from "node-fetch"; |
| 9 | + |
| 10 | +const NotificationSource = "Coral"; |
| 11 | +const ProfileType = "Coral"; |
| 12 | + |
| 13 | +// From Coral input types |
| 14 | + |
| 15 | +interface CreateReplyInput { |
| 16 | + from: User; |
| 17 | + to: User; |
| 18 | + parent: Comment; |
| 19 | + reply: Comment; |
| 20 | +} |
| 21 | + |
| 22 | +interface CreateRecInput { |
| 23 | + from: User; |
| 24 | + to: User; |
| 25 | + comment: Comment; |
| 26 | +} |
| 27 | + |
| 28 | +// To notifications service types |
| 29 | + |
| 30 | +// eslint-disable-next-line no-shadow |
| 31 | +enum NotificationType { |
| 32 | + CoralRec = "CoralRec", |
| 33 | + CoralReply = "CoralReply", |
| 34 | +} |
| 35 | + |
| 36 | +interface ExternalUserProfile { |
| 37 | + id: string; |
| 38 | + type: string; |
| 39 | + username?: string; |
| 40 | + email?: string; |
| 41 | + profileUrl?: string; |
| 42 | + avatarUrl?: string; |
| 43 | + ssoIds?: string[]; |
| 44 | +} |
| 45 | + |
| 46 | +interface CommentPayload { |
| 47 | + id: string; |
| 48 | + storyId: string; |
| 49 | + snippet?: string | null; |
| 50 | + url?: string | null; |
| 51 | +} |
| 52 | + |
| 53 | +const CreateNotificationsMutation = ` |
| 54 | + mutation CreateNotificationsMutation( |
| 55 | + $input: CreateNotificationsInput! |
| 56 | + ) { |
| 57 | + createNotifications(input: $input) { |
| 58 | + id |
| 59 | + } |
| 60 | + } |
| 61 | +`; |
| 62 | + |
| 63 | +export class ExternalNotificationsService { |
| 64 | + private url?: string | null; |
| 65 | + private apiKey?: string | null; |
| 66 | + private logger: Logger; |
| 67 | + private mongo: MongoContext; |
| 68 | + |
| 69 | + constructor(config: Config, logger: Logger, mongo: MongoContext) { |
| 70 | + this.logger = logger; |
| 71 | + this.mongo = mongo; |
| 72 | + |
| 73 | + this.url = config.get("external_notifications_api_url"); |
| 74 | + this.apiKey = config.get("external_notifications_api_key"); |
| 75 | + } |
| 76 | + |
| 77 | + public active(): boolean { |
| 78 | + return !!(this.url && this.apiKey); |
| 79 | + } |
| 80 | + |
| 81 | + private userToExternalProfile(user: User): ExternalUserProfile { |
| 82 | + return { |
| 83 | + id: user.id, |
| 84 | + type: ProfileType, |
| 85 | + username: user.username, |
| 86 | + email: user.email, |
| 87 | + profileUrl: user.ssoURL, |
| 88 | + avatarUrl: user.avatar, |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + private computeSnippetFromComment(comment: Comment): string { |
| 93 | + const latestRevision = getLatestRevision(comment); |
| 94 | + const formatted = convert(latestRevision.body); |
| 95 | + const split = formatted.split(/(\s+)/); |
| 96 | + |
| 97 | + if (split.length < 50) { |
| 98 | + return split.join(" "); |
| 99 | + } else { |
| 100 | + return split.slice(0, 50).join(" "); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 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; |
| 111 | + |
| 112 | + return { |
| 113 | + id: comment.id, |
| 114 | + storyId: comment.storyID, |
| 115 | + snippet: this.computeSnippetFromComment(comment), |
| 116 | + url, |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | + public async createRec(input: CreateRecInput) { |
| 121 | + if (!this.active()) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + try { |
| 126 | + const data = { |
| 127 | + source: NotificationSource, |
| 128 | + type: NotificationType.CoralRec, |
| 129 | + from: this.userToExternalProfile(input.from), |
| 130 | + to: this.userToExternalProfile(input.to), |
| 131 | + storyId: input.comment.storyID, |
| 132 | + comment: this.commentToPayload(input.comment), |
| 133 | + }; |
| 134 | + |
| 135 | + return await this.send(data); |
| 136 | + } catch (err) { |
| 137 | + this.logger.warn( |
| 138 | + { err, input }, |
| 139 | + "an error occurred while sending a rec notification" |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + return false; |
| 144 | + } |
| 145 | + |
| 146 | + public async createReply(input: CreateReplyInput) { |
| 147 | + if (!this.active()) { |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + try { |
| 152 | + const data = { |
| 153 | + source: NotificationSource, |
| 154 | + type: NotificationType.CoralReply, |
| 155 | + from: this.userToExternalProfile(input.from), |
| 156 | + to: this.userToExternalProfile(input.to), |
| 157 | + storyId: input.parent.storyID, |
| 158 | + comment: await this.commentToPayload(input.parent), |
| 159 | + reply: await this.commentToPayload(input.reply), |
| 160 | + }; |
| 161 | + |
| 162 | + return await this.send(data); |
| 163 | + } catch (err) { |
| 164 | + this.logger.warn( |
| 165 | + { err, input }, |
| 166 | + "an error occurred while sending a reply notification" |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | + private async send(notification: any) { |
| 174 | + if (!this.active()) { |
| 175 | + return false; |
| 176 | + } |
| 177 | + |
| 178 | + const data = { |
| 179 | + query: CreateNotificationsMutation, |
| 180 | + variables: { |
| 181 | + input: { |
| 182 | + items: [notification], |
| 183 | + }, |
| 184 | + }, |
| 185 | + }; |
| 186 | + |
| 187 | + const response = await fetch(this.url!, { |
| 188 | + method: "POST", |
| 189 | + headers: { |
| 190 | + "Content-Type": "application/json", |
| 191 | + "x-notifications-api-key": this.apiKey!, |
| 192 | + }, |
| 193 | + body: JSON.stringify(data), |
| 194 | + }); |
| 195 | + |
| 196 | + if (!response.ok) { |
| 197 | + this.logger.info( |
| 198 | + { status: response.status, text: await response.text() }, |
| 199 | + "error while sending external notifications info" |
| 200 | + ); |
| 201 | + |
| 202 | + return false; |
| 203 | + } |
| 204 | + |
| 205 | + return true; |
| 206 | + } |
| 207 | +} |
0 commit comments