import type { Action , Key } from "./types" ; import getName from "@cocalc/server/accounts/get-name" ; import getEmailAddress from "@cocalc/server/accounts/get-email-address" ; import getProjectTitle from "@cocalc/server/projects/get-title" ; import { trunc } from "@cocalc/util/misc" ; import siteURL from "@cocalc/server/settings/site-url" ; import { getServerSettings } from "@cocalc/server/settings" ; import sendEmail from "@cocalc/server/email/send-email" ; export default async function sendNotificationIfPossible ( key , source , description ) { const to = await getEmailAddress ( key . target ) ; if ( ! to ) { return "nothing" ; } const sourceName = trunc ( ( await getName ( source ) ) ?? "Unknown User" , 60 ) ; const projectTitle = await getProjectTitle ( key . project_id ) ; const context = description . length > 0 ? `<br/><blockquote>${ description }</blockquote>` : "" ; const subject = `[${ trunc ( projectTitle , 40 ) }] ${ key . path }` ; const url = `${ await siteURL ( ) }/projects/${ key . project_id }/files/${ key . path }` ; const html = `
${ sourceName } mentioned you in
<a href="${ url }">a chat at ${ key . path } in ${ projectTitle }</a>.
${ context }
` ; const text = `
${ sourceName } mentioned you in a chat at ${ key . path } in ${ projectTitle }.

    ${ url }

${ description ? "> " : "" }${ description }
` ; const { help_email } = await getServerSettings ( ) ; const from = `${ sourceName } <${ help_email }>` ; await sendEmail ( { from , to , subject , text , html , categories : [ "notification" ] , asm_group : 148185 , } , source ) ; return "email" ; }