1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.watchlist.internal; |
21 |
|
|
22 |
|
import java.util.Arrays; |
23 |
|
import java.util.Collection; |
24 |
|
import java.util.Collections; |
25 |
|
import java.util.Date; |
26 |
|
import java.util.HashMap; |
27 |
|
import java.util.List; |
28 |
|
import java.util.Map; |
29 |
|
|
30 |
|
import javax.inject.Inject; |
31 |
|
import javax.inject.Named; |
32 |
|
import javax.inject.Singleton; |
33 |
|
|
34 |
|
import org.slf4j.Logger; |
35 |
|
import org.xwiki.bridge.event.DocumentCreatedEvent; |
36 |
|
import org.xwiki.bridge.event.DocumentDeletedEvent; |
37 |
|
import org.xwiki.bridge.event.DocumentUpdatedEvent; |
38 |
|
import org.xwiki.component.annotation.Component; |
39 |
|
import org.xwiki.component.phase.InitializationException; |
40 |
|
import org.xwiki.configuration.ConfigurationSource; |
41 |
|
import org.xwiki.model.reference.DocumentReference; |
42 |
|
import org.xwiki.observation.AbstractEventListener; |
43 |
|
import org.xwiki.observation.ObservationContext; |
44 |
|
import org.xwiki.observation.event.Event; |
45 |
|
import org.xwiki.observation.remote.RemoteObservationManagerContext; |
46 |
|
import org.xwiki.watchlist.internal.api.WatchListEvent; |
47 |
|
import org.xwiki.watchlist.internal.api.WatchListEventType; |
48 |
|
import org.xwiki.watchlist.internal.api.WatchListNotifier; |
49 |
|
import org.xwiki.watchlist.internal.api.WatchListStore; |
50 |
|
import org.xwiki.watchlist.internal.notification.WatchListEventMimeMessageFactory; |
51 |
|
|
52 |
|
import com.xpn.xwiki.XWikiContext; |
53 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
@version |
59 |
|
|
60 |
|
@Component |
61 |
|
@Named(RealtimeNotificationGenerator.LISTENER_NAME) |
62 |
|
@Singleton |
|
|
| 78% |
Uncovered Elements: 13 (59) |
Complexity: 15 |
Complexity Density: 0.39 |
|
63 |
|
public class RealtimeNotificationGenerator extends AbstractEventListener |
64 |
|
{ |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
public static final String LISTENER_NAME = "RealtimeNotificationGenerator"; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
public static final String REALTIME_EMAIL_TEMPLATE = "XWiki.WatchListRealtimeMessage"; |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
private static final List<Event> EVENTS = Arrays.<Event>asList(new DocumentCreatedEvent(), |
79 |
|
new DocumentUpdatedEvent(), new DocumentDeletedEvent()); |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
@Inject |
86 |
|
private ObservationContext observationContext; |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
@Inject |
92 |
|
private RemoteObservationManagerContext remoteObservationManagerContext; |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
@Inject |
98 |
|
private WatchListStore store; |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
@Inject |
104 |
|
private WatchListNotifier notifier; |
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
@Inject |
110 |
|
private Logger logger; |
111 |
|
|
112 |
|
|
113 |
|
@link |
114 |
|
|
115 |
|
@Inject |
116 |
|
private WatchListEventMatcher watchlistEventMatcher; |
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
@Inject |
122 |
|
@Named("xwikiproperties") |
123 |
|
private ConfigurationSource xwikiProperties; |
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
private boolean allowRemote; |
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
133 |
1 |
public RealtimeNotificationGenerator()... |
134 |
|
{ |
135 |
1 |
super(LISTENER_NAME, EVENTS); |
136 |
|
} |
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
@throws |
142 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
143 |
0 |
public void initialize() throws InitializationException... |
144 |
|
{ |
145 |
0 |
this.allowRemote = this.xwikiProperties.getProperty("watchlist.realtime.allowRemote", false); |
146 |
|
} |
147 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
148 |
1 |
@Override... |
149 |
|
public List<Event> getEvents() |
150 |
|
{ |
151 |
1 |
if (this.xwikiProperties.getProperty("watchlist.realtime.enabled", false)) { |
152 |
|
|
153 |
|
|
154 |
1 |
return super.getEvents(); |
155 |
|
} else { |
156 |
|
|
157 |
0 |
return Collections.emptyList(); |
158 |
|
} |
159 |
|
} |
160 |
|
|
|
|
| 88.9% |
Uncovered Elements: 3 (27) |
Complexity: 7 |
Complexity Density: 0.37 |
|
161 |
73 |
@Override... |
162 |
|
public void onEvent(Event event, Object source, Object data) |
163 |
|
{ |
164 |
|
|
165 |
73 |
if (this.remoteObservationManagerContext.isRemoteState() && !this.allowRemote) { |
166 |
|
|
167 |
0 |
return; |
168 |
|
} |
169 |
|
|
170 |
73 |
XWikiDocument currentDoc = (XWikiDocument) source; |
171 |
73 |
XWikiContext context = (XWikiContext) data; |
172 |
|
|
173 |
|
|
174 |
73 |
if (observationContext.isIn(AutomaticWatchModeListener.SKIPPED_EVENTS)) { |
175 |
12 |
return; |
176 |
|
} |
177 |
|
|
178 |
|
|
179 |
|
|
180 |
61 |
try { |
181 |
|
|
182 |
61 |
WatchListEvent watchListEvent = getWatchListEvent(event, currentDoc, context); |
183 |
|
|
184 |
|
|
185 |
61 |
if (watchlistEventMatcher.isEventSkipped(watchListEvent)) { |
186 |
|
|
187 |
4 |
return; |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
57 |
Collection<String> subscribers = |
192 |
|
store.getSubscribers(DefaultWatchListNotificationCache.REALTIME_INTERVAL_ID); |
193 |
57 |
if (subscribers.size() == 0) { |
194 |
|
|
195 |
28 |
return; |
196 |
|
} |
197 |
|
|
198 |
|
|
199 |
29 |
Map<String, Object> notificationData = new HashMap<>(); |
200 |
29 |
notificationData.put(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER, REALTIME_EMAIL_TEMPLATE); |
201 |
29 |
notificationData.put(WatchListEventMimeMessageFactory.SKIP_CONTEXT_USER_PARAMETER, true); |
202 |
29 |
notificationData.put(WatchListEventMimeMessageFactory.ATTACH_AUTHOR_AVATARS_PARAMETER, true); |
203 |
|
|
204 |
|
|
205 |
29 |
notifier.sendNotification(subscribers, Arrays.asList(watchListEvent), notificationData); |
206 |
|
} catch (Exception e) { |
207 |
0 |
logger.error("Failed to send realtime notification to user [{}]", context.getUserReference(), e); |
208 |
|
} |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
|
@param |
213 |
|
@param |
214 |
|
@param |
215 |
|
@return@link |
216 |
|
|
|
|
| 70% |
Uncovered Elements: 6 (20) |
Complexity: 4 |
Complexity Density: 0.29 |
|
217 |
61 |
private WatchListEvent getWatchListEvent(Event event, XWikiDocument currentDoc, XWikiContext context)... |
218 |
|
{ |
219 |
61 |
String type = null; |
220 |
61 |
DocumentReference documentReference = currentDoc.getDocumentReference(); |
221 |
61 |
DocumentReference userReference = context.getUserReference(); |
222 |
61 |
String version = currentDoc.getVersion(); |
223 |
61 |
Date date = currentDoc.getDate(); |
224 |
|
|
225 |
61 |
if (event instanceof DocumentCreatedEvent) { |
226 |
12 |
type = WatchListEventType.CREATE; |
227 |
49 |
} else if (event instanceof DocumentUpdatedEvent) { |
228 |
49 |
type = WatchListEventType.UPDATE; |
229 |
0 |
} else if (event instanceof DocumentDeletedEvent) { |
230 |
0 |
version = currentDoc.getOriginalDocument().getVersion(); |
231 |
0 |
type = WatchListEventType.DELETE; |
232 |
|
} |
233 |
|
|
234 |
61 |
WatchListEvent watchListEvent = new WatchListEvent(documentReference, type, userReference, version, date); |
235 |
|
|
236 |
61 |
return watchListEvent; |
237 |
|
} |
238 |
|
} |