Add support for SendGrid as Email notification handler
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* mxisd - Matrix Identity Server Daemon
|
||||
* Copyright (C) 2017 Maxime Dor
|
||||
*
|
||||
* https://max.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.mxisd.threepid.connector.email;
|
||||
|
||||
import com.sendgrid.SendGrid;
|
||||
import com.sendgrid.SendGridException;
|
||||
import io.kamax.matrix.ThreePidMedium;
|
||||
import io.kamax.mxisd.config.MatrixConfig;
|
||||
import io.kamax.mxisd.config.ServerConfig;
|
||||
import io.kamax.mxisd.config.threepid.connector.EmailSendGridConfig;
|
||||
import io.kamax.mxisd.invitation.IThreePidInviteReply;
|
||||
import io.kamax.mxisd.notification.INotificationHandler;
|
||||
import io.kamax.mxisd.threepid.notification.PlaceholderNotificationGenerator;
|
||||
import io.kamax.mxisd.threepid.session.IThreePidSession;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static com.sendgrid.SendGrid.Email;
|
||||
import static com.sendgrid.SendGrid.Response;
|
||||
import static io.kamax.mxisd.config.threepid.connector.EmailSendGridConfig.EmailTemplate;
|
||||
|
||||
@Component
|
||||
public class EmailSendGridNotificationHandler extends PlaceholderNotificationGenerator implements INotificationHandler {
|
||||
|
||||
private Logger log = LoggerFactory.getLogger(EmailSendGridNotificationHandler.class);
|
||||
|
||||
private EmailSendGridConfig cfg;
|
||||
private SendGrid sendgrid;
|
||||
|
||||
@Autowired
|
||||
public EmailSendGridNotificationHandler(MatrixConfig mxCfg, ServerConfig srvCfg, EmailSendGridConfig cfg) {
|
||||
super(mxCfg, srvCfg);
|
||||
this.cfg = cfg;
|
||||
this.sendgrid = new SendGrid(cfg.getApi().getKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return "sendgrid";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMedium() {
|
||||
return ThreePidMedium.Email.getId();
|
||||
}
|
||||
|
||||
protected Email getEmail() {
|
||||
Email email = new Email();
|
||||
email.setFrom(cfg.getIdentity().getFrom());
|
||||
email.setFromName(cfg.getIdentity().getName());
|
||||
return email;
|
||||
}
|
||||
|
||||
private String getFromFile(String path) {
|
||||
try {
|
||||
return IOUtils.toString(new FileInputStream(path), StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Couldn't create notification content using file " + path, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendForInvite(IThreePidInviteReply invite) {
|
||||
EmailTemplate template = cfg.getTemplates().getInvite();
|
||||
Email email = getEmail();
|
||||
email.setSubject(populateForInvite(invite, template.getSubject()));
|
||||
email.setText(populateForInvite(invite, getFromFile(template.getBody().getText())));
|
||||
email.setHtml(populateForInvite(invite, getFromFile(template.getBody().getHtml())));
|
||||
|
||||
send(invite.getInvite().getAddress(), email);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendForValidation(IThreePidSession session) {
|
||||
EmailTemplate template = cfg.getTemplates().getSession().getLocal();
|
||||
Email email = getEmail();
|
||||
email.setSubject(populateForValidation(session, template.getSubject()));
|
||||
email.setText(populateForValidation(session, getFromFile(template.getBody().getText())));
|
||||
email.setHtml(populateForValidation(session, getFromFile(template.getBody().getHtml())));
|
||||
|
||||
send(session.getThreePid().getAddress(), email);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendForRemoteValidation(IThreePidSession session) {
|
||||
EmailTemplate template = cfg.getTemplates().getSession().getLocal();
|
||||
Email email = getEmail();
|
||||
email.setSubject(populateForRemoteValidation(session, template.getSubject()));
|
||||
email.setText(populateForRemoteValidation(session, getFromFile(template.getBody().getText())));
|
||||
email.setHtml(populateForRemoteValidation(session, getFromFile(template.getBody().getHtml())));
|
||||
|
||||
send(session.getThreePid().getAddress(), email);
|
||||
}
|
||||
|
||||
private void send(String recipient, Email email) {
|
||||
try {
|
||||
email.addTo(recipient);
|
||||
email.setFrom(cfg.getIdentity().getFrom());
|
||||
email.setFromName(cfg.getIdentity().getName());
|
||||
Response response = sendgrid.send(email);
|
||||
if (response.getStatus()) {
|
||||
log.info("Successfully sent email to {} using SendGrid", recipient);
|
||||
} else {
|
||||
throw new RuntimeException("Error sending via SendGrid to " + recipient + ": " + response.getMessage());
|
||||
}
|
||||
} catch (SendGridException e) {
|
||||
throw new RuntimeException("Unable to send e-mail invite via SendGrid to " + recipient, e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,17 +20,14 @@
|
||||
|
||||
package io.kamax.mxisd.threepid.notification;
|
||||
|
||||
import io.kamax.mxisd.ThreePid;
|
||||
import io.kamax.mxisd.config.MatrixConfig;
|
||||
import io.kamax.mxisd.config.ServerConfig;
|
||||
import io.kamax.mxisd.config.threepid.medium.GenericTemplateConfig;
|
||||
import io.kamax.mxisd.controller.v1.IdentityAPIv1;
|
||||
import io.kamax.mxisd.exception.InternalServerError;
|
||||
import io.kamax.mxisd.invitation.IThreePidInviteReply;
|
||||
import io.kamax.mxisd.threepid.session.IThreePidSession;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang.WordUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -43,39 +40,20 @@ import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@Component
|
||||
public abstract class GenericTemplateNotificationGenerator implements INotificationGenerator {
|
||||
public abstract class GenericTemplateNotificationGenerator extends PlaceholderNotificationGenerator implements INotificationGenerator {
|
||||
|
||||
private Logger log = LoggerFactory.getLogger(GenericTemplateNotificationGenerator.class);
|
||||
|
||||
private MatrixConfig mxCfg;
|
||||
private ServerConfig srvCfg;
|
||||
private GenericTemplateConfig cfg;
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext app;
|
||||
|
||||
public GenericTemplateNotificationGenerator(MatrixConfig mxCfg, ServerConfig srvCfg, GenericTemplateConfig cfg) {
|
||||
this.mxCfg = mxCfg;
|
||||
this.srvCfg = srvCfg;
|
||||
super(mxCfg, srvCfg);
|
||||
this.cfg = cfg;
|
||||
}
|
||||
|
||||
protected String populateForCommon(String body, ThreePid recipient) {
|
||||
return body;
|
||||
}
|
||||
|
||||
private String populateCommon(String body, ThreePid recipient) {
|
||||
body = populateForCommon(body, recipient);
|
||||
|
||||
String domainPretty = WordUtils.capitalizeFully(mxCfg.getDomain());
|
||||
body = body.replace("%DOMAIN%", mxCfg.getDomain());
|
||||
body = body.replace("%DOMAIN_PRETTY%", domainPretty);
|
||||
body = body.replace("%RECIPIENT_MEDIUM%", recipient.getMedium());
|
||||
body = body.replace("%RECIPIENT_ADDRESS%", recipient.getAddress());
|
||||
|
||||
return body;
|
||||
}
|
||||
|
||||
private String getTemplateContent(String location) {
|
||||
try {
|
||||
InputStream is = StringUtils.startsWith(location, "classpath:") ?
|
||||
@@ -86,65 +64,22 @@ public abstract class GenericTemplateNotificationGenerator implements INotificat
|
||||
}
|
||||
}
|
||||
|
||||
private String getTemplateAndPopulate(String location, ThreePid recipient) {
|
||||
return populateCommon(getTemplateContent(location), recipient);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getForInvite(IThreePidInviteReply invite) {
|
||||
ThreePid tpid = new ThreePid(invite.getInvite().getMedium(), invite.getInvite().getAddress());
|
||||
String templateBody = getTemplateAndPopulate(cfg.getInvite(), tpid);
|
||||
|
||||
String senderName = invite.getInvite().getProperties().getOrDefault("sender_display_name", "");
|
||||
String senderNameOrId = StringUtils.defaultIfBlank(senderName, invite.getInvite().getSender().getId());
|
||||
String roomName = invite.getInvite().getProperties().getOrDefault("room_name", "");
|
||||
String roomNameOrId = StringUtils.defaultIfBlank(roomName, invite.getInvite().getRoomId());
|
||||
|
||||
templateBody = templateBody.replace("%SENDER_ID%", invite.getInvite().getSender().getId());
|
||||
templateBody = templateBody.replace("%SENDER_NAME%", senderName);
|
||||
templateBody = templateBody.replace("%SENDER_NAME_OR_ID%", senderNameOrId);
|
||||
templateBody = templateBody.replace("%INVITE_MEDIUM%", tpid.getMedium());
|
||||
templateBody = templateBody.replace("%INVITE_ADDRESS%", tpid.getAddress());
|
||||
templateBody = templateBody.replace("%ROOM_ID%", invite.getInvite().getRoomId());
|
||||
templateBody = templateBody.replace("%ROOM_NAME%", roomName);
|
||||
templateBody = templateBody.replace("%ROOM_NAME_OR_ID%", roomNameOrId);
|
||||
|
||||
return templateBody;
|
||||
log.info("Generating notification content for 3PID invite");
|
||||
return populateForInvite(invite, getTemplateContent(cfg.getInvite()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getForValidation(IThreePidSession session) {
|
||||
log.info("Generating notification content for 3PID Session validation");
|
||||
String templateBody = getTemplateAndPopulate(cfg.getSession().getValidation().getLocal(), session.getThreePid());
|
||||
|
||||
String validationLink = srvCfg.getPublicUrl() + IdentityAPIv1.getValidate(
|
||||
session.getThreePid().getMedium(),
|
||||
session.getId(),
|
||||
session.getSecret(),
|
||||
session.getToken());
|
||||
|
||||
templateBody = templateBody.replace("%VALIDATION_LINK%", validationLink);
|
||||
templateBody = templateBody.replace("%VALIDATION_TOKEN%", session.getToken());
|
||||
|
||||
return templateBody;
|
||||
return populateForValidation(session, getTemplateContent(cfg.getSession().getValidation().getLocal()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getForRemoteValidation(IThreePidSession session) {
|
||||
log.info("Generating notification content for remote-only 3PID session");
|
||||
String templateBody = getTemplateAndPopulate(cfg.getSession().getValidation().getRemote(), session.getThreePid());
|
||||
|
||||
String validationLink = srvCfg.getPublicUrl() + IdentityAPIv1.getValidate(
|
||||
session.getThreePid().getMedium(),
|
||||
session.getId(),
|
||||
session.getSecret(),
|
||||
session.getToken());
|
||||
|
||||
templateBody = templateBody.replace("%VALIDATION_LINK%", validationLink);
|
||||
templateBody = templateBody.replace("%VALIDATION_TOKEN%", session.getToken());
|
||||
templateBody = templateBody.replace("%NEXT_URL%", validationLink);
|
||||
|
||||
return templateBody;
|
||||
return populateForRemoteValidation(session, getTemplateContent(cfg.getSession().getValidation().getRemote()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* mxisd - Matrix Identity Server Daemon
|
||||
* Copyright (C) 2017 Maxime Dor
|
||||
*
|
||||
* https://max.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.mxisd.threepid.notification;
|
||||
|
||||
import io.kamax.mxisd.ThreePid;
|
||||
import io.kamax.mxisd.config.MatrixConfig;
|
||||
import io.kamax.mxisd.config.ServerConfig;
|
||||
import io.kamax.mxisd.controller.v1.IdentityAPIv1;
|
||||
import io.kamax.mxisd.invitation.IThreePidInviteReply;
|
||||
import io.kamax.mxisd.threepid.session.IThreePidSession;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang.WordUtils;
|
||||
|
||||
public abstract class PlaceholderNotificationGenerator {
|
||||
|
||||
private MatrixConfig mxCfg;
|
||||
private ServerConfig srvCfg;
|
||||
|
||||
public PlaceholderNotificationGenerator(MatrixConfig mxCfg, ServerConfig srvCfg) {
|
||||
this.mxCfg = mxCfg;
|
||||
this.srvCfg = srvCfg;
|
||||
}
|
||||
|
||||
protected String populateForCommon(String input, ThreePid recipient) {
|
||||
String domainPretty = WordUtils.capitalizeFully(mxCfg.getDomain());
|
||||
|
||||
return input
|
||||
.replace("%DOMAIN%", mxCfg.getDomain())
|
||||
.replace("%DOMAIN_PRETTY%", domainPretty)
|
||||
.replace("%RECIPIENT_MEDIUM%", recipient.getMedium())
|
||||
.replace("%RECIPIENT_ADDRESS%", recipient.getAddress());
|
||||
}
|
||||
|
||||
protected String populateForInvite(IThreePidInviteReply invite, String input) {
|
||||
ThreePid tpid = new ThreePid(invite.getInvite().getMedium(), invite.getInvite().getAddress());
|
||||
|
||||
String senderName = invite.getInvite().getProperties().getOrDefault("sender_display_name", "");
|
||||
String senderNameOrId = StringUtils.defaultIfBlank(senderName, invite.getInvite().getSender().getId());
|
||||
String roomName = invite.getInvite().getProperties().getOrDefault("room_name", "");
|
||||
String roomNameOrId = StringUtils.defaultIfBlank(roomName, invite.getInvite().getRoomId());
|
||||
|
||||
return populateForCommon(input, tpid)
|
||||
.replace("%SENDER_ID%", invite.getInvite().getSender().getId())
|
||||
.replace("%SENDER_NAME%", senderName)
|
||||
.replace("%SENDER_NAME_OR_ID%", senderNameOrId)
|
||||
.replace("%INVITE_MEDIUM%", tpid.getMedium())
|
||||
.replace("%INVITE_ADDRESS%", tpid.getAddress())
|
||||
.replace("%ROOM_ID%", invite.getInvite().getRoomId())
|
||||
.replace("%ROOM_NAME%", roomName)
|
||||
.replace("%ROOM_NAME_OR_ID%", roomNameOrId);
|
||||
}
|
||||
|
||||
protected String populateForValidation(IThreePidSession session, String input) {
|
||||
String validationLink = srvCfg.getPublicUrl() + IdentityAPIv1.getValidate(
|
||||
session.getThreePid().getMedium(),
|
||||
session.getId(),
|
||||
session.getSecret(),
|
||||
session.getToken()
|
||||
);
|
||||
|
||||
return populateForCommon(input, session.getThreePid())
|
||||
.replace("%VALIDATION_LINK%", validationLink)
|
||||
.replace("%VALIDATION_TOKEN%", session.getToken())
|
||||
.replace("%NEXT_URL%", validationLink);
|
||||
}
|
||||
|
||||
protected String populateForRemoteValidation(IThreePidSession session, String input) {
|
||||
return populateForValidation(session, input);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -30,16 +30,21 @@ import org.springframework.stereotype.Component;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class EmailNotificationHandler extends GenericNotificationHandler<IEmailConnector, IEmailNotificationGenerator> {
|
||||
public class EmailRawNotificationHandler extends GenericNotificationHandler<IEmailConnector, IEmailNotificationGenerator> {
|
||||
|
||||
private EmailConfig cfg;
|
||||
|
||||
@Autowired
|
||||
public EmailNotificationHandler(EmailConfig cfg, List<IEmailNotificationGenerator> generators, List<IEmailConnector> connectors) {
|
||||
public EmailRawNotificationHandler(EmailConfig cfg, List<IEmailNotificationGenerator> generators, List<IEmailConnector> connectors) {
|
||||
this.cfg = cfg;
|
||||
process(connectors, generators);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return "raw";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMedium() {
|
||||
return ThreePidMedium.Email.getId();
|
||||
@@ -40,6 +40,11 @@ public class PhoneNotificationHandler extends GenericNotificationHandler<IPhoneC
|
||||
process(connectors, generators);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return "raw";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMedium() {
|
||||
return ThreePidMedium.PhoneNumber.getId();
|
||||
|
||||
Reference in New Issue
Block a user