Add config option to specify period dimension of the invitation scheduler.

This commit is contained in:
Anatoliy Sablin
2021-01-13 21:59:47 +03:00
parent a0f6fe9b0d
commit a71d32ba77
3 changed files with 61 additions and 22 deletions

View File

@@ -204,3 +204,10 @@ threepid:
# root: error # default level for all loggers (apps and thirdparty libraries) # root: error # default level for all loggers (apps and thirdparty libraries)
# app: info # log level only for the ma1sd # app: info # log level only for the ma1sd
# requests: false # or true to dump full requests and responses # requests: false # or true to dump full requests and responses
# Config invitation manager
#invite:
# resolution:
# timer: 10
# period: seconds # search invites every 10 seconds (by default 5 minutes)

View File

@@ -67,6 +67,7 @@ public class InvitationConfig {
private boolean recursive = true; private boolean recursive = true;
private long timer = 5; private long timer = 5;
private PeriodDimension period = PeriodDimension.minutes;
public boolean isRecursive() { public boolean isRecursive() {
return recursive; return recursive;
@@ -84,6 +85,13 @@ public class InvitationConfig {
this.timer = timer; this.timer = timer;
} }
public PeriodDimension getPeriod() {
return period;
}
public void setPeriod(PeriodDimension period) {
this.period = period;
}
} }
public static class SenderPolicy { public static class SenderPolicy {
@@ -147,4 +155,10 @@ public class InvitationConfig {
log.info("Policies: {}", GsonUtil.get().toJson(getPolicy())); log.info("Policies: {}", GsonUtil.get().toJson(getPolicy()));
} }
public enum PeriodDimension {
minutes,
seconds
}
} }

View File

@@ -99,14 +99,14 @@ public class InvitationManager {
private Map<String, IThreePidInviteReply> invitations = new ConcurrentHashMap<>(); private Map<String, IThreePidInviteReply> invitations = new ConcurrentHashMap<>();
public InvitationManager( public InvitationManager(
MxisdConfig mxisdCfg, MxisdConfig mxisdCfg,
IStorage storage, IStorage storage,
LookupStrategy lookupMgr, LookupStrategy lookupMgr,
KeyManager keyMgr, KeyManager keyMgr,
SignatureManager signMgr, SignatureManager signMgr,
HomeserverFederationResolver resolver, HomeserverFederationResolver resolver,
NotificationManager notifMgr, NotificationManager notifMgr,
ProfileManager profileMgr ProfileManager profileMgr
) { ) {
this.cfg = requireValid(mxisdCfg); this.cfg = requireValid(mxisdCfg);
this.srvCfg = mxisdCfg.getServer(); this.srvCfg = mxisdCfg.getServer();
@@ -124,11 +124,11 @@ public class InvitationManager {
io.getProperties().putIfAbsent(CreatedAtPropertyKey, defaultCreateTs); io.getProperties().putIfAbsent(CreatedAtPropertyKey, defaultCreateTs);
log.debug("Processing invite {}", GsonUtil.get().toJson(io)); log.debug("Processing invite {}", GsonUtil.get().toJson(io));
ThreePidInvite invite = new ThreePidInvite( ThreePidInvite invite = new ThreePidInvite(
MatrixID.asAcceptable(io.getSender()), MatrixID.asAcceptable(io.getSender()),
io.getMedium(), io.getMedium(),
io.getAddress(), io.getAddress(),
io.getRoomId(), io.getRoomId(),
io.getProperties() io.getProperties()
); );
ThreePidInviteReply reply = new ThreePidInviteReply(io.getId(), invite, io.getToken(), "", Collections.emptyList()); ThreePidInviteReply reply = new ThreePidInviteReply(io.getId(), invite, io.getToken(), "", Collections.emptyList());
@@ -155,7 +155,17 @@ public class InvitationManager {
log.error("Error when running background maintenance", t); log.error("Error when running background maintenance", t);
} }
} }
}, 5000L, TimeUnit.MILLISECONDS.convert(cfg.getResolution().getTimer(), TimeUnit.MINUTES)); }, 5000L, TimeUnit.MILLISECONDS.convert(cfg.getResolution().getTimer(), getTimeUnit()));
}
private TimeUnit getTimeUnit() {
switch (cfg.getResolution().getPeriod()) {
case seconds:
return TimeUnit.SECONDS;
case minutes:
default:
return TimeUnit.MINUTES;
}
} }
private InvitationConfig requireValid(MxisdConfig cfg) { private InvitationConfig requireValid(MxisdConfig cfg) {
@@ -176,7 +186,8 @@ public class InvitationManager {
if (StringUtils.isBlank(cfg.getInvite().getExpiration().getResolveTo())) { if (StringUtils.isBlank(cfg.getInvite().getExpiration().getResolveTo())) {
String localpart = cfg.getAppsvc().getUser().getInviteExpired(); String localpart = cfg.getAppsvc().getUser().getInviteExpired();
if (StringUtils.isBlank(localpart)) { if (StringUtils.isBlank(localpart)) {
throw new ConfigurationException("Could not compute the Invitation expiration resolution target from App service user: not set"); throw new ConfigurationException(
"Could not compute the Invitation expiration resolution target from App service user: not set");
} }
cfg.getInvite().getExpiration().setResolveTo(MatrixID.asAcceptable(localpart, cfg.getMatrix().getDomain()).getId()); cfg.getInvite().getExpiration().setResolveTo(MatrixID.asAcceptable(localpart, cfg.getMatrix().getDomain()).getId());
@@ -198,7 +209,8 @@ public class InvitationManager {
} }
private String getIdForLog(IThreePidInviteReply reply) { private String getIdForLog(IThreePidInviteReply reply) {
return reply.getInvite().getSender().getId() + ":" + reply.getInvite().getRoomId() + ":" + reply.getInvite().getMedium() + ":" + reply.getInvite().getAddress(); return reply.getInvite().getSender().getId() + ":" + reply.getInvite().getRoomId() + ":" + reply.getInvite()
.getMedium() + ":" + reply.getInvite().getAddress();
} }
private Optional<SingleLookupReply> lookup3pid(String medium, String address) { private Optional<SingleLookupReply> lookup3pid(String medium, String address) {
@@ -252,13 +264,16 @@ public class InvitationManager {
} }
String invId = computeId(invitation); String invId = computeId(invitation);
log.info("Handling invite for {}:{} from {} in room {}", invitation.getMedium(), invitation.getAddress(), invitation.getSender(), invitation.getRoomId()); log.info("Handling invite for {}:{} from {} in room {}", invitation.getMedium(), invitation.getAddress(), invitation.getSender(),
invitation.getRoomId());
IThreePidInviteReply reply = invitations.get(invId); IThreePidInviteReply reply = invitations.get(invId);
if (reply != null) { if (reply != null) {
log.info("Invite is already pending for {}:{}, returning data", invitation.getMedium(), invitation.getAddress()); log.info("Invite is already pending for {}:{}, returning data", invitation.getMedium(), invitation.getAddress());
if (!StringUtils.equals(invitation.getRoomId(), reply.getInvite().getRoomId())) { if (!StringUtils.equals(invitation.getRoomId(), reply.getInvite().getRoomId())) {
log.info("Sending new notification as new invite room {} is different from the original {}", invitation.getRoomId(), reply.getInvite().getRoomId()); log.info("Sending new notification as new invite room {} is different from the original {}", invitation.getRoomId(),
notifMgr.sendForReply(new ThreePidInviteReply(reply.getId(), invitation, reply.getToken(), reply.getDisplayName(), reply.getPublicKeys())); reply.getInvite().getRoomId());
notifMgr.sendForReply(
new ThreePidInviteReply(reply.getId(), invitation, reply.getToken(), reply.getDisplayName(), reply.getPublicKeys()));
} else { } else {
// FIXME we should check attempt and send if bigger // FIXME we should check attempt and send if bigger
} }
@@ -295,7 +310,8 @@ public class InvitationManager {
log.info("Storing invite under ID {}", invId); log.info("Storing invite under ID {}", invId);
storage.insertInvite(reply); storage.insertInvite(reply);
invitations.put(invId, reply); invitations.put(invId, reply);
log.info("A new invite has been created for {}:{} on HS {}", invitation.getMedium(), invitation.getAddress(), invitation.getSender().getDomain()); log.info("A new invite has been created for {}:{} on HS {}", invitation.getMedium(), invitation.getAddress(),
invitation.getSender().getDomain());
return reply; return reply;
} }
@@ -385,8 +401,10 @@ public class InvitationManager {
public void publishMappingIfInvited(ThreePidMapping threePid) { public void publishMappingIfInvited(ThreePidMapping threePid) {
log.info("Looking up possible pending invites for {}:{}", threePid.getMedium(), threePid.getValue()); log.info("Looking up possible pending invites for {}:{}", threePid.getMedium(), threePid.getValue());
for (IThreePidInviteReply reply : invitations.values()) { for (IThreePidInviteReply reply : invitations.values()) {
if (StringUtils.equalsIgnoreCase(reply.getInvite().getMedium(), threePid.getMedium()) && StringUtils.equalsIgnoreCase(reply.getInvite().getAddress(), threePid.getValue())) { if (StringUtils.equalsIgnoreCase(reply.getInvite().getMedium(), threePid.getMedium()) && StringUtils
log.info("{}:{} has an invite pending on HS {}, publishing mapping", threePid.getMedium(), threePid.getValue(), reply.getInvite().getSender().getDomain()); .equalsIgnoreCase(reply.getInvite().getAddress(), threePid.getValue())) {
log.info("{}:{} has an invite pending on HS {}, publishing mapping", threePid.getMedium(), threePid.getValue(),
reply.getInvite().getSender().getDomain());
publishMapping(reply, threePid.getMxid()); publishMapping(reply, threePid.getMxid());
} }
} }