Browse code

FEATBL-727 Feat: set default alarm on meeting request

David Phan authored on 13/03/2019 08:55:20
Showing 3 changed files
... ...
@@ -38,6 +38,7 @@ import org.joda.time.DateTime;
38 38
 import org.joda.time.DateTimeZone;
39 39
 import org.junit.After;
40 40
 import org.junit.Before;
41
+import org.junit.BeforeClass;
41 42
 import org.junit.Test;
42 43
 import org.vertx.java.core.AsyncResult;
43 44
 import org.vertx.java.core.Handler;
... ...
@@ -75,6 +76,7 @@ import net.bluemind.icalendar.api.ICalendarElement.Attendee;
75 76
 import net.bluemind.icalendar.api.ICalendarElement.ParticipationStatus;
76 77
 import net.bluemind.icalendar.api.ICalendarElement.RRule;
77 78
 import net.bluemind.icalendar.api.ICalendarElement.RRule.Frequency;
79
+import net.bluemind.icalendar.api.ICalendarElement.VAlarm;
78 80
 import net.bluemind.imip.parser.IMIPInfos;
79 81
 import net.bluemind.imip.parser.ITIPMethod;
80 82
 import net.bluemind.lib.vertx.VertxPlatform;
... ...
@@ -107,10 +109,15 @@ public class ImipFilterVEventTests {
107 109
 	private ICalendar user1Calendar;
108 110
 	private ItemValue<Domain> domain;
109 111
 
112
+	@BeforeClass
113
+	public static void oneShotBefore() {
114
+		System.setProperty("es.mailspool.count", "1");
115
+	}
116
+
110 117
 	@Before
111 118
 	public void before() throws Exception {
112 119
 		JdbcTestHelper.getInstance().beforeTest();
113
-		
120
+
114 121
 		ElasticsearchTestHelper.getInstance().beforeTest();
115 122
 
116 123
 		final CountDownLatch launched = new CountDownLatch(1);
... ...
@@ -126,9 +133,7 @@ public class ImipFilterVEventTests {
126 133
 		esServer.ip = ElasticsearchTestHelper.getInstance().getHost();
127 134
 		System.out.println("IP " + esServer.ip);
128 135
 		esServer.tags = Lists.newArrayList("bm/es");
129
-		// ElasticsearchTestHelper.getInstance().getClient().prepareIndex("event",
130
-		// "vevent")
131
-		// .setSource("{\"test\":\"test\"}").setId("zz").execute().actionGet();
136
+
132 137
 		String cyrusIp = new BmConfIni().get("imap-role");
133 138
 		Server imapServer = new Server();
134 139
 		imapServer.ip = cyrusIp;
... ...
@@ -208,6 +213,33 @@ public class ImipFilterVEventTests {
208 213
 	}
209 214
 
210 215
 	@Test
216
+	public void requestHandler_DefaultAlert() throws Exception {
217
+		IIMIPHandler handler = new FakeEventRequestHandlerFactory().create();
218
+		ItemValue<VEvent> event = defaultVEvent();
219
+
220
+		IMIPInfos imip = imip(ITIPMethod.REQUEST, defaultExternalSenderVCard(), event.uid);
221
+
222
+		imip.iCalendarElements = Arrays.asList(event.value);
223
+		imip.uid = event.uid;
224
+		LmtpAddress recipient = new LmtpAddress("<user1@domain.lan>", null, null);
225
+
226
+		handler.handle(imip, recipient, domain, user1Mailbox);
227
+
228
+		ItemValue<VEventSeries> res = user1Calendar.getComplete(event.uid);
229
+		assertEquals(1, res.value.main.alarm.size());
230
+		assertEquals(-900, res.value.main.alarm.get(0).trigger.intValue());
231
+
232
+		// ensure alarms are not erased
233
+		res.value.main.alarm.add(VAlarm.create(-60));
234
+		user1Calendar.update(event.uid, res.value, false);
235
+
236
+		handler.handle(imip, recipient, domain, user1Mailbox);
237
+
238
+		res = user1Calendar.getComplete(event.uid);
239
+		assertEquals(2, res.value.main.alarm.size());
240
+	}
241
+
242
+	@Test
211 243
 	public void testRequestHandlerEventHeader() throws Exception {
212 244
 		IIMIPHandler handler = new FakeEventRequestHandlerFactory().create();
213 245
 
... ...
@@ -660,7 +692,7 @@ public class ImipFilterVEventTests {
660 692
 				.getComplete(resource.uid);
661 693
 		ICalendar resourceCalendar = testContext.provider().instance(ICalendar.class,
662 694
 				CalendarContainerType.TYPE + ":" + resource.uid);
663
-		
695
+
664 696
 		FakeSendmail mailer = new FakeSendmail();
665 697
 		IIMIPHandler handler = new FakeEventRequestHandlerFactory().create(mailer);
666 698
 
... ...
@@ -681,8 +713,7 @@ public class ImipFilterVEventTests {
681 713
 
682 714
 		imip.iCalendarElements = Arrays.asList(event.value);
683 715
 
684
-		LmtpAddress recipient = new LmtpAddress("<" + user1.value.defaultEmailAddress(domainUid) + ">", null,
685
-				null);
716
+		LmtpAddress recipient = new LmtpAddress("<" + user1.value.defaultEmailAddress(domainUid) + ">", null, null);
686 717
 
687 718
 		try {
688 719
 			handler.handle(imip, recipient, domain, resourceMailbox);
... ...
@@ -694,9 +725,8 @@ public class ImipFilterVEventTests {
694 725
 		}
695 726
 		ItemValue<VEventSeries> evt = user1Calendar.getComplete(event.uid);
696 727
 		assertNull("Event should not have been created in user1 calendar", evt);
697
-		
698
-		recipient = new LmtpAddress("<" + resource.value.emails.iterator().next().address + ">", null,
699
-				null);
728
+
729
+		recipient = new LmtpAddress("<" + resource.value.emails.iterator().next().address + ">", null, null);
700 730
 
701 731
 		try {
702 732
 			handler.handle(imip, recipient, domain, resourceMailbox);
... ...
@@ -709,7 +739,6 @@ public class ImipFilterVEventTests {
709 739
 		evt = resourceCalendar.getComplete(event.uid);
710 740
 		assertNull("Event should not have been created in resource calendar", evt);
711 741
 
712
-
713 742
 	}
714 743
 
715 744
 	private ItemValue<ResourceDescriptor> createResource() throws ServerFault {
... ...
@@ -34,6 +34,7 @@ import org.joda.time.DateTime;
34 34
 import org.joda.time.DateTimeZone;
35 35
 import org.junit.After;
36 36
 import org.junit.Before;
37
+import org.junit.BeforeClass;
37 38
 import org.junit.Test;
38 39
 import org.vertx.java.core.AsyncResult;
39 40
 import org.vertx.java.core.Handler;
... ...
@@ -84,10 +85,15 @@ public class ImipFilterVTodoTests {
84 85
 	private ItemValue<Mailbox> adminMailbox;
85 86
 	private ITodoList adminTodolist;
86 87
 
88
+	@BeforeClass
89
+	public static void oneShotBefore() {
90
+		System.setProperty("es.mailspool.count", "1");
91
+	}
92
+
87 93
 	@Before
88 94
 	public void before() throws Exception {
89 95
 		JdbcTestHelper.getInstance().beforeTest();
90
-		
96
+
91 97
 		ElasticsearchTestHelper.getInstance().beforeTest();
92 98
 		final CountDownLatch launched = new CountDownLatch(1);
93 99
 		VertxPlatform.spawnVerticles(new Handler<AsyncResult<Void>>() {
... ...
@@ -71,6 +71,7 @@ import net.bluemind.icalendar.api.ICalendarElement.Attendee;
71 71
 import net.bluemind.icalendar.api.ICalendarElement.CUType;
72 72
 import net.bluemind.icalendar.api.ICalendarElement.ParticipationStatus;
73 73
 import net.bluemind.icalendar.api.ICalendarElement.Role;
74
+import net.bluemind.icalendar.api.ICalendarElement.VAlarm;
74 75
 import net.bluemind.imip.parser.IMIPInfos;
75 76
 import net.bluemind.lmtp.backend.LmtpAddress;
76 77
 import net.bluemind.lmtp.backend.PermissionDeniedException.MailboxInvitationDeniedException;
... ...
@@ -89,17 +90,17 @@ public class EventRequestHandler extends RequestHandler implements IIMIPHandler
89 90
 			.expireAfterAccess(2, TimeUnit.MINUTES).build();
90 91
 
91 92
 	private ISendmail mailer;
92
-	
93
+
93 94
 	public EventRequestHandler() {
94 95
 		this(new Sendmail());
95
-		
96
+
96 97
 	}
97
-	
98
+
98 99
 	EventRequestHandler(ISendmail mailer) {
99 100
 		super();
100 101
 		this.mailer = mailer;
101 102
 	}
102
-	
103
+
103 104
 	@Override
104 105
 	public IMIPResponse handle(IMIPInfos imip, LmtpAddress recipient, ItemValue<Domain> domain,
105 106
 			ItemValue<Mailbox> recipientMailbox) throws ServerFault {
... ...
@@ -142,7 +143,11 @@ public class EventRequestHandler extends RequestHandler implements IIMIPHandler
142 143
 			});
143 144
 
144 145
 			List<ItemValue<VEventSeries>> vseries = cal.getByIcsUid(imip.uid);
146
+
147
+			setDefaultAlarm(domain, recipientMailbox.uid, series);
148
+
145 149
 			VEventChanges changes = new CoreStrategyFactory().getStrategy(vseries, series).merge(vseries, series, imip);
150
+
146 151
 			cal.updates(changes);
147 152
 			logger.info("[{}] {} new series, {} updated series, {} deleted series in BM (calendar {})", imip.messageId,
148 153
 					changes.add == null ? 0 : changes.add.size(), changes.modify == null ? 0 : changes.modify.size(),
... ...
@@ -156,6 +161,32 @@ public class EventRequestHandler extends RequestHandler implements IIMIPHandler
156 161
 		}
157 162
 	}
158 163
 
164
+	private void setDefaultAlarm(ItemValue<Domain> domain, String uid, VEventSeries series) {
165
+		Map<String, String> settings = provider().instance(IUserSettings.class, domain.uid).get(uid);
166
+		setAlarm(series.main, settings);
167
+		series.occurrences.forEach(occurrence -> {
168
+			setAlarm(occurrence, settings);
169
+		});
170
+	}
171
+
172
+	private void setAlarm(VEvent evt, Map<String, String> settings) {
173
+		if (evt == null) {
174
+			return;
175
+		}
176
+
177
+		String trigger = settings.get("default_event_alert");
178
+		if (evt.allDay()) {
179
+			trigger = settings.get("default_allday_event_alert");
180
+		}
181
+		if (trigger != null) {
182
+			try {
183
+				evt.alarm = Arrays.asList(VAlarm.create(-Integer.parseInt(trigger)));
184
+			} catch (NumberFormatException e) {
185
+				logger.warn("Failed to set alarm, invalid trigger {}", trigger);
186
+			}
187
+		}
188
+	}
189
+
159 190
 	private boolean needResponse(ItemValue<Domain> domain, ItemValue<Mailbox> recipientMailbox, VEvent event) {
160 191
 		for (Attendee att : event.attendees) {
161 192
 			if (att.mailto != null) {
... ...
@@ -211,8 +242,8 @@ public class EventRequestHandler extends RequestHandler implements IIMIPHandler
211 242
 	 * @param calElement
212 243
 	 * @throws ServerFault
213 244
 	 */
214
-	private void notifyForbiddenToSender(IMIPInfos imip, LmtpAddress recipient, ItemValue<Mailbox> mailbox, ItemValue<User> sender,
215
-			VEventSeries series) throws ServerFault {
245
+	private void notifyForbiddenToSender(IMIPInfos imip, LmtpAddress recipient, ItemValue<Mailbox> mailbox,
246
+			ItemValue<User> sender, VEventSeries series) throws ServerFault {
216 247
 
217 248
 		VEvent event = series.main != null ? series.main : series.occurrences.get(0);
218 249
 		Attendee a = new VEvent.Attendee();
... ...
@@ -365,10 +396,18 @@ public class EventRequestHandler extends RequestHandler implements IIMIPHandler
365 396
 			} else {
366 397
 				adjustEventExceptionsValues(series, main);
367 398
 			}
399
+
400
+			adjustAlarms(series.main, main);
368 401
 			series.main = main;
369 402
 			return series;
370 403
 		}
371 404
 
405
+		private void adjustAlarms(VEvent bmEvent, VEvent evt) {
406
+			if (bmEvent.hasAlarm()) {
407
+				evt.alarm = bmEvent.alarm;
408
+			}
409
+		}
410
+
372 411
 		private void adjustEventExceptionsValues(final VEventSeries oldEvent, final VEvent imipVEvent)
373 412
 				throws ServerFault {
374 413
 
... ...
@@ -386,6 +425,7 @@ public class EventRequestHandler extends RequestHandler implements IIMIPHandler
386 425
 				evt.description = adjustEventValue(existingEvent.description, imipVEvent.description, evt.description);
387 426
 				evt.categories = adjustEventValue(existingEvent.categories, imipVEvent.categories, evt.categories);
388 427
 				adjustAttendees(existingEvent.attendees, imipVEvent.attendees, evt.attendees);
428
+				adjustAlarms(evt, imipVEvent);
389 429
 
390 430
 			});
391 431
 		}
... ...
@@ -409,7 +449,8 @@ public class EventRequestHandler extends RequestHandler implements IIMIPHandler
409 449
 
410 450
 			if (oldValue != null) {
411 451
 				if (!oldValue.equals(exceptionValue)) {
412
-					// value has already been modified in exception, don't overwrite
452
+					// value has already been modified in exception, don't
453
+					// overwrite
413 454
 					return exceptionValue;
414 455
 				}
415 456
 			}