Add some placeholders handling for e-mail template
This commit is contained in:
@@ -63,7 +63,11 @@ class InvitationController {
|
||||
@RequestParam String medium,
|
||||
@RequestParam String address,
|
||||
@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)
|
||||
|
||||
return gson.toJson(new ThreePidInviteReplyIO(reply, keyMgr.getPublicKeyBase64(keyMgr.getCurrentIndex()), srvCfg.getPublicUrl()))
|
||||
|
||||
@@ -22,6 +22,8 @@ package io.kamax.mxisd.invitation;
|
||||
|
||||
import io.kamax.matrix._MatrixID;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface IThreePidInvite {
|
||||
|
||||
_MatrixID getSender();
|
||||
@@ -32,4 +34,6 @@ public interface IThreePidInvite {
|
||||
|
||||
String getRoomId();
|
||||
|
||||
Map<String, String> getProperties();
|
||||
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import io.kamax.mxisd.lookup.SingleLookupRequest;
|
||||
import io.kamax.mxisd.lookup.ThreePidMapping;
|
||||
import io.kamax.mxisd.lookup.strategy.LookupStrategy;
|
||||
import io.kamax.mxisd.signature.SignatureManager;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang.RandomStringUtils;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
@@ -184,7 +185,8 @@ public class InvitationManager {
|
||||
log.warn("No HS found for domain {} - ignoring publishing", domain);
|
||||
} else {
|
||||
// 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");
|
||||
// Expected body: https://matrix.to/#/!HUeDbmFUsWAhxHHvFG:matrix.org/$150469846739DCLWc:matrix.trancendances.fr
|
||||
JSONObject obj = new JSONObject(); // TODO use Gson instead
|
||||
@@ -200,7 +202,7 @@ public class InvitationManager {
|
||||
objUp.put("room_id", reply.getInvite().getRoomId());
|
||||
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
|
||||
JSONArray invites = new JSONArray();
|
||||
@@ -212,14 +214,17 @@ public class InvitationManager {
|
||||
|
||||
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);
|
||||
entity.setContentType("application/json");
|
||||
req.setEntity(entity);
|
||||
try {
|
||||
log.info("Posting onBind event to {}", req.getURI());
|
||||
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();
|
||||
} catch (IOException e) {
|
||||
log.warn("Unable to tell HS {} about invite being mapped", domain, e);
|
||||
|
||||
@@ -22,18 +22,28 @@ package io.kamax.mxisd.invitation;
|
||||
|
||||
import io.kamax.matrix._MatrixID;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ThreePidInvite implements IThreePidInvite {
|
||||
|
||||
private _MatrixID sender;
|
||||
private String medium;
|
||||
private String address;
|
||||
private String roomId;
|
||||
private Map<String, String> properties;
|
||||
|
||||
public ThreePidInvite(_MatrixID sender, String medium, String address, String roomId) {
|
||||
this.sender = sender;
|
||||
this.medium = medium;
|
||||
this.address = address;
|
||||
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
|
||||
@@ -56,4 +66,9 @@ public class ThreePidInvite implements IThreePidInvite {
|
||||
return roomId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import io.kamax.matrix.ThreePidMedium;
|
||||
import io.kamax.mxisd.config.invite.sender.EmailSenderConfig;
|
||||
import io.kamax.mxisd.exception.ConfigurationException;
|
||||
import io.kamax.mxisd.invitation.IThreePidInviteReply;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -39,6 +40,7 @@ import javax.mail.internet.MimeMessage;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
|
||||
@Component
|
||||
@@ -75,11 +77,16 @@ public class EmailInviteSender implements IInviteSender {
|
||||
}
|
||||
|
||||
try {
|
||||
MimeMessage msg = new MimeMessage(session, new FileInputStream(cfg.getContentPath()));
|
||||
msg.setHeader("X-Mailer", "mxisd");
|
||||
String templateBody = IOUtils.toString(new FileInputStream(cfg.getContentPath()), StandardCharsets.UTF_8);
|
||||
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.setRecipients(Message.RecipientType.TO, invite.getInvite().getAddress());
|
||||
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());
|
||||
SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
|
||||
|
||||
Reference in New Issue
Block a user