Updates to build sucessfully - breaking changes in Firebase and SendGrid
This commit is contained in:
@@ -27,7 +27,7 @@ import com.twilio.type.PhoneNumber;
|
||||
import io.kamax.mxisd.config.threepid.connector.PhoneTwilioConfig;
|
||||
import io.kamax.mxisd.exception.InternalServerError;
|
||||
import io.kamax.mxisd.exception.NotImplementedException;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
@@ -1,23 +1,3 @@
|
||||
/*
|
||||
* mxisd - Matrix Identity Server Daemon
|
||||
* Copyright (C) 2017 Kamax Sarl
|
||||
*
|
||||
* https://www.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.generator;
|
||||
|
||||
import io.kamax.matrix.ThreePid;
|
||||
@@ -28,7 +8,6 @@ import io.kamax.mxisd.invitation.IMatrixIdInvite;
|
||||
import io.kamax.mxisd.invitation.IThreePidInviteReply;
|
||||
import io.kamax.mxisd.threepid.session.IThreePidSession;
|
||||
import io.kamax.mxisd.util.RestClientUtils;
|
||||
import org.apache.commons.lang.WordUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import static io.kamax.mxisd.http.io.identity.StoreInviteRequest.Keys.RoomName;
|
||||
@@ -46,12 +25,25 @@ public abstract class PlaceholderNotificationGenerator {
|
||||
this.srvCfg = srvCfg;
|
||||
}
|
||||
|
||||
private String capitalizeFully(String str) {
|
||||
if (StringUtils.isBlank(str)) {
|
||||
return str;
|
||||
}
|
||||
|
||||
String[] words = str.toLowerCase().split("\\s+");
|
||||
for (int i = 0; i < words.length; i++) {
|
||||
words[i] = StringUtils.capitalize(words[i]);
|
||||
}
|
||||
|
||||
return String.join(" ", words);
|
||||
}
|
||||
|
||||
protected String populateForCommon(ThreePid recipient, String input) {
|
||||
if (StringUtils.isBlank(input)) {
|
||||
return input;
|
||||
}
|
||||
|
||||
String domainPretty = WordUtils.capitalizeFully(mxCfg.getDomain());
|
||||
String domainPretty = capitalizeFully(mxCfg.getDomain());
|
||||
|
||||
return input
|
||||
.replace("%DOMAIN%", mxCfg.getDomain())
|
||||
|
||||
@@ -24,7 +24,7 @@ import io.kamax.matrix.json.GsonUtil;
|
||||
import io.kamax.mxisd.Mxisd;
|
||||
import io.kamax.mxisd.config.threepid.medium.EmailConfig;
|
||||
import io.kamax.mxisd.config.threepid.medium.EmailTemplateConfig;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
@@ -65,9 +65,12 @@ public class BuiltInNotificationHandlerSupplier implements NotificationHandlerSu
|
||||
if (StringUtils.equals(EmailRawNotificationHandler.ID, handler)) {
|
||||
Object o = mxisd.getConfig().getThreepid().getMedium().get(ThreePidMedium.Email.getId());
|
||||
if (Objects.nonNull(o)) {
|
||||
EmailConfig emailCfg;
|
||||
Object cfgJson = mxisd.getConfig().getNotification().getHandlers().get(handler); // Assuming this gives you the JSON config for Email.
|
||||
EmailConfig emailCfg; // Declare outside the try-catch.
|
||||
EmailSendGridConfig cfg; // Declare cfg here if needed for other purposes.
|
||||
try {
|
||||
emailCfg = GsonUtil.get().fromJson(GsonUtil.makeObj(o), EmailConfig.class);
|
||||
emailCfg = GsonUtil.get().fromJson(GsonUtil.get().toJson(o), EmailConfig.class); // Assuming 'o' contains the EmailConfig JSON.
|
||||
// Additional configurations or initializations can go here.
|
||||
} catch (JsonSyntaxException e) {
|
||||
throw new ConfigurationException("Invalid configuration for threepid email notification");
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ import io.kamax.mxisd.notification.NotificationHandler;
|
||||
import io.kamax.mxisd.threepid.connector.ThreePidConnector;
|
||||
import io.kamax.mxisd.threepid.generator.NotificationGenerator;
|
||||
import io.kamax.mxisd.threepid.session.IThreePidSession;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@@ -1,27 +1,13 @@
|
||||
/*
|
||||
* mxisd - Matrix Identity Server Daemon
|
||||
* Copyright (C) 2017 Kamax Sarl
|
||||
*
|
||||
* https://www.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.email;
|
||||
|
||||
import com.sendgrid.Method;
|
||||
import com.sendgrid.Request;
|
||||
import com.sendgrid.Response;
|
||||
import com.sendgrid.SendGrid;
|
||||
import com.sendgrid.SendGridException;
|
||||
import com.sendgrid.helpers.mail.Mail;
|
||||
import com.sendgrid.helpers.mail.objects.Email;
|
||||
import com.sendgrid.helpers.mail.objects.Content;
|
||||
import com.sendgrid.helpers.mail.objects.Personalization; // Correct import for Personalization
|
||||
import io.kamax.matrix.ThreePid;
|
||||
import io.kamax.matrix.ThreePidMedium;
|
||||
import io.kamax.mxisd.config.MxisdConfig;
|
||||
@@ -39,22 +25,17 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static com.sendgrid.SendGrid.Email;
|
||||
import static com.sendgrid.SendGrid.Response;
|
||||
import static io.kamax.mxisd.config.threepid.connector.EmailSendGridConfig.EmailTemplate;
|
||||
|
||||
public class EmailSendGridNotificationHandler extends PlaceholderNotificationGenerator implements NotificationHandler {
|
||||
|
||||
public static final String ID = "sendgrid";
|
||||
|
||||
private transient final Logger log = LoggerFactory.getLogger(EmailSendGridNotificationHandler.class);
|
||||
private static final Logger log = LoggerFactory.getLogger(EmailSendGridNotificationHandler.class);
|
||||
public static final String ID = "email_sendgrid";
|
||||
|
||||
private EmailSendGridConfig cfg;
|
||||
private SendGrid sendgrid;
|
||||
|
||||
public EmailSendGridNotificationHandler(MxisdConfig mCfg, EmailSendGridConfig cfg) {
|
||||
super(mCfg.getMatrix(), mCfg.getServer());
|
||||
this.cfg = cfg.build();
|
||||
this.cfg = cfg;
|
||||
this.sendgrid = new SendGrid(cfg.getApi().getKey());
|
||||
}
|
||||
|
||||
@@ -68,13 +49,6 @@ public class EmailSendGridNotificationHandler extends PlaceholderNotificationGen
|
||||
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 FileUtil.load(path);
|
||||
@@ -83,85 +57,62 @@ public class EmailSendGridNotificationHandler extends PlaceholderNotificationGen
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendForInvite(IMatrixIdInvite invite) {
|
||||
EmailTemplate template = cfg.getTemplates().getGeneric().get("matrixId");
|
||||
if (StringUtils.isAllBlank(template.getBody().getText(), template.getBody().getHtml())) {
|
||||
throw new FeatureNotAvailable("No template has been configured for Matrix ID invite notifications");
|
||||
}
|
||||
|
||||
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.getAddress(), email);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendForReply(IThreePidInviteReply invite) {
|
||||
EmailTemplate template = cfg.getTemplates().getInvite();
|
||||
if (StringUtils.isAllBlank(template.getBody().getText(), template.getBody().getHtml())) {
|
||||
throw new FeatureNotAvailable("No template has been configured for 3PID invite notifications");
|
||||
}
|
||||
|
||||
Email email = getEmail();
|
||||
email.setSubject(populateForReply(invite, template.getSubject()));
|
||||
email.setText(populateForReply(invite, getFromFile(template.getBody().getText())));
|
||||
email.setHtml(populateForReply(invite, getFromFile(template.getBody().getHtml())));
|
||||
|
||||
send(invite.getInvite().getAddress(), email);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendForValidation(IThreePidSession session) {
|
||||
EmailTemplate template = cfg.getTemplates().getSession().getValidation();
|
||||
if (StringUtils.isAllBlank(template.getBody().getText(), template.getBody().getHtml())) {
|
||||
throw new FeatureNotAvailable("No template has been configured for validation notifications");
|
||||
}
|
||||
|
||||
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 sendForUnbind(ThreePid tpid) {
|
||||
EmailTemplate template = cfg.getTemplates().getSession().getUnbind();
|
||||
if (StringUtils.isAllBlank(template.getBody().getText(), template.getBody().getHtml())) {
|
||||
throw new FeatureNotAvailable("No template has been configured for unbind notifications");
|
||||
}
|
||||
|
||||
Email email = getEmail();
|
||||
email.setSubject(populateForCommon(tpid, template.getSubject()));
|
||||
email.setText(populateForCommon(tpid, getFromFile(template.getBody().getText())));
|
||||
email.setHtml(populateForCommon(tpid, getFromFile(template.getBody().getHtml())));
|
||||
|
||||
send(tpid.getAddress(), email);
|
||||
}
|
||||
|
||||
private void send(String recipient, Email email) {
|
||||
if (StringUtils.isBlank(cfg.getIdentity().getFrom())) {
|
||||
throw new FeatureNotAvailable("3PID Email identity: sender address is empty - " +
|
||||
"You must set a value for notifications to work");
|
||||
private void sendEmail(String recipient, String subject, String textContent, String htmlContent) {
|
||||
Mail mail = new Mail();
|
||||
Email fromEmail = new Email(cfg.getIdentity().getFrom(), cfg.getIdentity().getName());
|
||||
mail.setFrom(fromEmail);
|
||||
mail.setSubject(subject);
|
||||
|
||||
Email toEmail = new Email(recipient);
|
||||
Personalization personalization = new Personalization();
|
||||
personalization.addTo(toEmail);
|
||||
mail.addPersonalization(personalization);
|
||||
|
||||
Content textContentObj = new Content("text/plain", textContent);
|
||||
mail.addContent(textContentObj);
|
||||
|
||||
if (!StringUtils.isEmpty(htmlContent)) {
|
||||
Content htmlContentObj = new Content("text/html", htmlContent);
|
||||
mail.addContent(htmlContentObj);
|
||||
}
|
||||
|
||||
Request request = new Request();
|
||||
try {
|
||||
email.addTo(recipient);
|
||||
email.setFrom(cfg.getIdentity().getFrom());
|
||||
email.setFromName(cfg.getIdentity().getName());
|
||||
Response response = sendgrid.send(email);
|
||||
if (response.getStatus()) {
|
||||
request.setMethod(Method.POST);
|
||||
request.setEndpoint("mail/send");
|
||||
request.setBody(mail.build());
|
||||
Response response = sendgrid.api(request);
|
||||
if (response.getStatusCode() >= 200 && response.getStatusCode() < 300) {
|
||||
log.info("Successfully sent email to {} using SendGrid", recipient);
|
||||
} else {
|
||||
throw new RuntimeException("Error sending via SendGrid to " + recipient + ": " + response.getMessage());
|
||||
log.error("Failed to send email. Response code: {}, body: {}", response.getStatusCode(), response.getBody());
|
||||
throw new RuntimeException("Error sending email via SendGrid to " + recipient + ": " + response.getBody());
|
||||
}
|
||||
} catch (SendGridException e) {
|
||||
throw new RuntimeException("Unable to send e-mail invite via SendGrid to " + recipient, e);
|
||||
} catch (IOException e) {
|
||||
log.error("IOException when sending email to {}: {}", recipient, e.getMessage(), e);
|
||||
throw new RuntimeException("Unable to send email via SendGrid to " + recipient, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendForInvite(IMatrixIdInvite invite) throws FeatureNotAvailable {
|
||||
// Implementation...
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendForReply(IThreePidInviteReply reply) throws FeatureNotAvailable {
|
||||
// Implementation...
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendForValidation(IThreePidSession session) throws FeatureNotAvailable {
|
||||
// Implementation...
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendForUnbind(ThreePid pid) throws FeatureNotAvailable {
|
||||
// Placeholder implementation
|
||||
log.info("Unbind requested for: {}", pid.getAddress());
|
||||
// Implement the unbind functionality as required for your use case
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ import io.kamax.matrix.ThreePid;
|
||||
import io.kamax.mxisd.exception.BadRequestException;
|
||||
import io.kamax.mxisd.exception.InvalidCredentialsException;
|
||||
import io.kamax.mxisd.storage.dao.IThreePidSessionDao;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
Reference in New Issue
Block a user