Add some placeholders handling for e-mail template

This commit is contained in:
Maxime Dor
2017-09-06 23:08:47 +02:00
parent 1e3b832186
commit 158060a3b0
6 changed files with 48 additions and 9 deletions

View File

@@ -234,5 +234,9 @@ invite:
# The display name used in the e-mail # The display name used in the e-mail
name: "Matrix Identity" name: "Matrix Identity"
# The MIME content to send # The MIME content to send, UTF-8 expected
#
# The following placeholders are possible:
# - %SENDER_DISPLAY_NAME%
# - %ROOM_NAME%
contentPath: "/absolute/path/to/file" contentPath: "/absolute/path/to/file"

View File

@@ -63,7 +63,11 @@ class InvitationController {
@RequestParam String medium, @RequestParam String medium,
@RequestParam String address, @RequestParam String address,
@RequestParam("room_id") String roomId) { @RequestParam("room_id") String roomId) {
IThreePidInvite invite = new ThreePidInvite(new MatrixID(sender), medium, address, roomId) Map<String, String> parameters = new HashMap<>()
for (String key : request.getParameterMap().keySet()) {
parameters.put(key, request.getParameter(key));
}
IThreePidInvite invite = new ThreePidInvite(new MatrixID(sender), medium, address, roomId, parameters)
IThreePidInviteReply reply = mgr.storeInvite(invite) IThreePidInviteReply reply = mgr.storeInvite(invite)
return gson.toJson(new ThreePidInviteReplyIO(reply, keyMgr.getPublicKeyBase64(keyMgr.getCurrentIndex()), srvCfg.getPublicUrl())) return gson.toJson(new ThreePidInviteReplyIO(reply, keyMgr.getPublicKeyBase64(keyMgr.getCurrentIndex()), srvCfg.getPublicUrl()))

View File

@@ -22,6 +22,8 @@ package io.kamax.mxisd.invitation;
import io.kamax.matrix._MatrixID; import io.kamax.matrix._MatrixID;
import java.util.Map;
public interface IThreePidInvite { public interface IThreePidInvite {
_MatrixID getSender(); _MatrixID getSender();
@@ -32,4 +34,6 @@ public interface IThreePidInvite {
String getRoomId(); String getRoomId();
Map<String, String> getProperties();
} }

View File

@@ -29,6 +29,7 @@ import io.kamax.mxisd.lookup.SingleLookupRequest;
import io.kamax.mxisd.lookup.ThreePidMapping; import io.kamax.mxisd.lookup.ThreePidMapping;
import io.kamax.mxisd.lookup.strategy.LookupStrategy; import io.kamax.mxisd.lookup.strategy.LookupStrategy;
import io.kamax.mxisd.signature.SignatureManager; import io.kamax.mxisd.signature.SignatureManager;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.lang.RandomStringUtils;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
@@ -184,7 +185,8 @@ public class InvitationManager {
log.warn("No HS found for domain {} - ignoring publishing", domain); log.warn("No HS found for domain {} - ignoring publishing", domain);
} else { } else {
// TODO this is needed as this will block if called during authentication cycle due to synapse implementation // TODO this is needed as this will block if called during authentication cycle due to synapse implementation
new Thread(() -> { // FIXME need to make this retryable and within a general background working pool
new Thread(() -> { // FIXME need to make this retry-able and within a general background working pool
HttpPost req = new HttpPost(hsUrlOpt.get() + "/_matrix/federation/v1/3pid/onbind"); HttpPost req = new HttpPost(hsUrlOpt.get() + "/_matrix/federation/v1/3pid/onbind");
// Expected body: https://matrix.to/#/!HUeDbmFUsWAhxHHvFG:matrix.org/$150469846739DCLWc:matrix.trancendances.fr // Expected body: https://matrix.to/#/!HUeDbmFUsWAhxHHvFG:matrix.org/$150469846739DCLWc:matrix.trancendances.fr
JSONObject obj = new JSONObject(); // TODO use Gson instead JSONObject obj = new JSONObject(); // TODO use Gson instead
@@ -200,7 +202,7 @@ public class InvitationManager {
objUp.put("room_id", reply.getInvite().getRoomId()); objUp.put("room_id", reply.getInvite().getRoomId());
objUp.put("signed", obj); objUp.put("signed", obj);
String mapping = gson.toJson(objUp); // FIXME we shouldn't need to be doign this String mapping = gson.toJson(objUp); // FIXME we shouldn't need to be doing this
JSONObject content = new JSONObject(); // TODO use Gson instead JSONObject content = new JSONObject(); // TODO use Gson instead
JSONArray invites = new JSONArray(); JSONArray invites = new JSONArray();
@@ -212,14 +214,17 @@ public class InvitationManager {
content.put("signatures", signMgr.signMessageJson(content.toString())); content.put("signatures", signMgr.signMessageJson(content.toString()));
log.info("Will send following JSON to {}: {}", domain, content.toString());
StringEntity entity = new StringEntity(content.toString(), StandardCharsets.UTF_8); StringEntity entity = new StringEntity(content.toString(), StandardCharsets.UTF_8);
entity.setContentType("application/json"); entity.setContentType("application/json");
req.setEntity(entity); req.setEntity(entity);
try { try {
log.info("Posting onBind event to {}", req.getURI()); log.info("Posting onBind event to {}", req.getURI());
CloseableHttpResponse response = client.execute(req); CloseableHttpResponse response = client.execute(req);
log.info("Answer code: {}", response.getStatusLine().getStatusCode()); int statusCode = response.getStatusLine().getStatusCode();
log.info("Answer code: {}", statusCode);
if (statusCode >= 400) {
log.warn("Answer body: {}", IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8));
}
response.close(); response.close();
} catch (IOException e) { } catch (IOException e) {
log.warn("Unable to tell HS {} about invite being mapped", domain, e); log.warn("Unable to tell HS {} about invite being mapped", domain, e);

View File

@@ -22,18 +22,28 @@ package io.kamax.mxisd.invitation;
import io.kamax.matrix._MatrixID; import io.kamax.matrix._MatrixID;
import java.util.HashMap;
import java.util.Map;
public class ThreePidInvite implements IThreePidInvite { public class ThreePidInvite implements IThreePidInvite {
private _MatrixID sender; private _MatrixID sender;
private String medium; private String medium;
private String address; private String address;
private String roomId; private String roomId;
private Map<String, String> properties;
public ThreePidInvite(_MatrixID sender, String medium, String address, String roomId) { public ThreePidInvite(_MatrixID sender, String medium, String address, String roomId) {
this.sender = sender; this.sender = sender;
this.medium = medium; this.medium = medium;
this.address = address; this.address = address;
this.roomId = roomId; this.roomId = roomId;
this.properties = new HashMap<>();
}
public ThreePidInvite(_MatrixID sender, String medium, String address, String roomId, Map<String, String> properties) {
this(sender, medium, address, roomId);
this.properties = properties;
} }
@Override @Override
@@ -56,4 +66,9 @@ public class ThreePidInvite implements IThreePidInvite {
return roomId; return roomId;
} }
@Override
public Map<String, String> getProperties() {
return properties;
}
} }

View File

@@ -25,6 +25,7 @@ import io.kamax.matrix.ThreePidMedium;
import io.kamax.mxisd.config.invite.sender.EmailSenderConfig; import io.kamax.mxisd.config.invite.sender.EmailSenderConfig;
import io.kamax.mxisd.exception.ConfigurationException; import io.kamax.mxisd.exception.ConfigurationException;
import io.kamax.mxisd.invitation.IThreePidInviteReply; import io.kamax.mxisd.invitation.IThreePidInviteReply;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -39,6 +40,7 @@ import javax.mail.internet.MimeMessage;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Date; import java.util.Date;
@Component @Component
@@ -75,11 +77,16 @@ public class EmailInviteSender implements IInviteSender {
} }
try { try {
MimeMessage msg = new MimeMessage(session, new FileInputStream(cfg.getContentPath())); String templateBody = IOUtils.toString(new FileInputStream(cfg.getContentPath()), StandardCharsets.UTF_8);
msg.setHeader("X-Mailer", "mxisd"); templateBody =
templateBody.replace("%SENDER_DISPLAY_NAME%", invite.getInvite().getProperties().get("sender_display_name"))
.replace("%ROOM_NAME%", invite.getInvite().getProperties().get("room_name"));
MimeMessage msg = new MimeMessage(session, IOUtils.toInputStream(templateBody, StandardCharsets.UTF_8));
msg.setHeader("X-Mailer", "mxisd"); // TODO set version
msg.setSentDate(new Date()); msg.setSentDate(new Date());
msg.setRecipients(Message.RecipientType.TO, invite.getInvite().getAddress());
msg.setFrom(sender); msg.setFrom(sender);
msg.setRecipients(Message.RecipientType.TO, invite.getInvite().getAddress());
log.info("Sending invite to {} via SMTP using {}:{}", invite.getInvite().getAddress(), cfg.getHost(), cfg.getPort()); log.info("Sending invite to {} via SMTP using {}:{}", invite.getInvite().getAddress(), cfg.getHost(), cfg.getPort());
SMTPTransport transport = (SMTPTransport) session.getTransport("smtp"); SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");