Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion Morphic.Server/Community/CommunitiesEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
using System;
using System.Text.Json.Serialization;
using System.Collections.Generic;
using System.Text;
using Hangfire;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using Morphic.Server.Email;
using Stripe;

namespace Morphic.Server.Community
Expand All @@ -39,10 +44,13 @@ namespace Morphic.Server.Community
public class CommunitiesEndpoint: Endpoint
{

public CommunitiesEndpoint(IPaymentProcessor paymentProcessor, Plans plans, IHttpContextAccessor contextAccessor, ILogger<Endpoint> logger): base(contextAccessor, logger)
public CommunitiesEndpoint(IPaymentProcessor paymentProcessor, Plans plans,
IHttpContextAccessor contextAccessor, ILogger<Endpoint> logger, IBackgroundJobClient jobClient)
: base(contextAccessor, logger)
{
this.paymentProcessor = paymentProcessor;
this.plans = plans;
this.jobClient = jobClient;
}

public override async Task LoadResource()
Expand All @@ -53,6 +61,7 @@ public override async Task LoadResource()
private User User = null!;
private IPaymentProcessor paymentProcessor;
private Plans plans;
private readonly IBackgroundJobClient jobClient;

[Method]
public async Task Post()
Expand Down Expand Up @@ -196,6 +205,24 @@ public async Task Post()
member.LastName.PlainText = User.LastName?.PlainText;
await db.Save(member);


// Send an email alert when someone signs up
StringBuilder emailInfo = new StringBuilder();
JsonWriterSettings writerSettings = new JsonWriterSettings()
{
Indent = true
};

emailInfo.AppendFormat("Community '{0}' created by '{1}' {2}",
community.Name, User.FullName, User.Email.PlainText).AppendLine();

emailInfo.AppendLine("Member:").AppendLine(member.ToJson(writerSettings));
emailInfo.AppendLine("Community:").AppendLine(community.ToJson(writerSettings));

jobClient.Enqueue<InternalAlertEmail>(x =>
x.SendEmail("new community", emailInfo.ToString(), this.Request.ClientIp()));


await Respond(new CommunityPutResponse()
{
Community = community
Expand Down
3 changes: 3 additions & 0 deletions Morphic.Server/Email/EmailSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public class EmailSettings
/// The default 'name' we use to send emails.
/// </summary>
public string EmailFromFullname { get; set; } = "Morphic Support";

public string? InternalAlertEmail { get; set; }

/// <summary>
/// Some SendGrid settings.
/// </summary>
Expand Down
74 changes: 74 additions & 0 deletions Morphic.Server/Email/InternalAlertEmail.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2021 Raising the Floor - International
//
// Licensed under the New BSD license. You may not use this file except in
// compliance with this License.
//
// You may obtain a copy of the License at
// https://github.com/GPII/universal/blob/master/LICENSE.txt
//
// The R&D leading to these results received funding from the:
// * Rehabilitation Services Administration, US Dept. of Education under
// grant H421A150006 (APCP)
// * National Institute on Disability, Independent Living, and
// Rehabilitation Research (NIDILRR)
// * Administration for Independent Living & Dept. of Education under grants
// H133E080022 (RERC-IT) and H133E130028/90RE5003-01-00 (UIITA-RERC)
// * European Union's Seventh Framework Programme (FP7/2007-2013) grant
// agreement nos. 289016 (Cloud4all) and 610510 (Prosperity4All)
// * William and Flora Hewlett Foundation
// * Ontario Ministry of Research and Innovation
// * Canadian Foundation for Innovation
// * Adobe Foundation
// * Consumer Electronics Association Foundation

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Hangfire;
using Microsoft.Extensions.Logging;
using Morphic.Server.Db;

namespace Morphic.Server.Email
{
/// <summary>
/// Sends an email alert, for when something interesting happens.
/// </summary>
// ReSharper disable once UnusedType.Global
public class InternalAlertEmail : EmailJob
{
public InternalAlertEmail(MorphicSettings morphicSettings, EmailSettings settings, ILogger<EmailJob> logger, Database db) : base(morphicSettings, settings, logger, db)
{
// TODO: Re-using the invitation email for now
EmailType = EmailConstants.EmailTypes.CommunityInvitation;
}


[AutomaticRetry(Attempts = 20)]
public async Task SendEmail(string eventName, string details, string? clientIp)
{
if (EmailSettings.Type == EmailSettings.EmailTypeDisabled)
{
// Email shouldn't be disabled, but if it is, we want to
// fail this job so it retries
throw new Exception("Email is disabled, failing job to it will retry");
}

string toAddress = this.EmailSettings.InternalAlertEmail ?? this.EmailSettings.EmailFromAddress;

Attributes.Add("EmailType", EmailType.ToString());
Attributes.Add("ToUserName", toAddress);
Attributes.Add("ToEmail", toAddress);
Attributes.Add("FromUserName", EmailSettings.EmailFromFullname);
Attributes.Add("FromEmail", EmailSettings.EmailFromAddress);

Attributes.Add("Message", details);
Attributes.Add("Event", eventName);

Attributes.Add("ClientIp", clientIp ?? UnknownClientIp);
Attributes.Add("Link", this.GetFrontEndUri("/").ToString());

await SendOneEmail(EmailType, Attributes);
}
}
}