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.ArrayList; |
23 |
|
import java.util.Collection; |
24 |
|
import java.util.Collections; |
25 |
|
import java.util.Date; |
26 |
|
import java.util.List; |
27 |
|
|
28 |
|
import javax.inject.Inject; |
29 |
|
import javax.inject.Provider; |
30 |
|
import javax.inject.Singleton; |
31 |
|
|
32 |
|
import org.apache.commons.collections4.CollectionUtils; |
33 |
|
import org.apache.commons.lang3.StringUtils; |
34 |
|
import org.slf4j.Logger; |
35 |
|
import org.xwiki.component.annotation.Component; |
36 |
|
import org.xwiki.model.reference.DocumentReference; |
37 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
38 |
|
import org.xwiki.security.authorization.AuthorizationManager; |
39 |
|
import org.xwiki.security.authorization.Right; |
40 |
|
import org.xwiki.watchlist.internal.api.WatchListEvent; |
41 |
|
import org.xwiki.watchlist.internal.api.WatchListStore; |
42 |
|
import org.xwiki.watchlist.internal.api.WatchedElementType; |
43 |
|
|
44 |
|
import com.xpn.xwiki.XWikiContext; |
45 |
|
import com.xpn.xwiki.plugin.activitystream.api.ActivityEvent; |
46 |
|
import com.xpn.xwiki.plugin.activitystream.api.ActivityEventType; |
47 |
|
import com.xpn.xwiki.plugin.activitystream.api.ActivityStream; |
48 |
|
import com.xpn.xwiki.plugin.activitystream.plugin.ActivityStreamPlugin; |
49 |
|
|
50 |
|
|
51 |
|
@link |
52 |
|
|
53 |
|
@version |
54 |
|
|
55 |
|
@Component |
56 |
|
@Singleton |
|
|
| 89.8% |
Uncovered Elements: 6 (59) |
Complexity: 13 |
Complexity Density: 0.3 |
|
57 |
|
public class DefaultWatchListEventMatcher implements WatchListEventMatcher |
58 |
|
{ |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
private static final List<String> MATCHING_EVENT_TYPES = new ArrayList<String>() |
64 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
65 |
1 |
{... |
66 |
1 |
add(ActivityEventType.CREATE); |
67 |
1 |
add(ActivityEventType.UPDATE); |
68 |
1 |
add(ActivityEventType.DELETE); |
69 |
|
} |
70 |
|
}; |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
@Inject |
76 |
|
private Logger logger; |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
@Inject |
82 |
|
private WatchListStore store; |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
@Inject |
88 |
|
private Provider<XWikiContext> contextProvider; |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
@Inject |
94 |
|
private AuthorizationManager authorizationManager; |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
@Inject |
100 |
|
private DocumentReferenceResolver<String> resolver; |
101 |
|
|
102 |
|
|
103 |
|
@link@link |
104 |
|
|
105 |
|
@Inject |
106 |
|
private WatchListEventConverter<ActivityEvent> eventConverter; |
107 |
|
|
|
|
| 94.4% |
Uncovered Elements: 1 (18) |
Complexity: 3 |
Complexity Density: 0.19 |
|
108 |
1 |
@Override... |
109 |
|
public List<WatchListEvent> getEventsSince(Date start) |
110 |
|
{ |
111 |
1 |
List<WatchListEvent> events = new ArrayList<>(); |
112 |
|
|
113 |
1 |
XWikiContext context = getXWikiContext(); |
114 |
|
|
115 |
1 |
ActivityStream actStream = |
116 |
|
((ActivityStreamPlugin) context.getWiki().getPlugin(ActivityStreamPlugin.PLUGIN_NAME, context)) |
117 |
|
.getActivityStream(); |
118 |
1 |
List<Object> parameters = new ArrayList<Object>(); |
119 |
1 |
parameters.add(start); |
120 |
|
|
121 |
1 |
try { |
122 |
|
|
123 |
1 |
List<ActivityEvent> rawEvents = |
124 |
|
actStream.searchEvents( |
125 |
|
"act.date > ? and act.type in ('" + StringUtils.join(MATCHING_EVENT_TYPES, "','") + "')", false, |
126 |
|
true, 0, 0, parameters, context); |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
1 |
for (ActivityEvent rawEvent : rawEvents) { |
133 |
25 |
WatchListEvent event = this.eventConverter.convert(rawEvent); |
134 |
|
|
135 |
25 |
int existingIndex = events.indexOf(event); |
136 |
25 |
if (existingIndex == -1) { |
137 |
|
|
138 |
10 |
events.add(event); |
139 |
|
} else { |
140 |
|
|
141 |
15 |
WatchListEvent existingCompositeEvent = events.get(existingIndex); |
142 |
15 |
existingCompositeEvent.addEvent(event); |
143 |
|
} |
144 |
|
} |
145 |
|
} catch (Exception e) { |
146 |
0 |
logger.error("Failed to retrieve updated documents from activity stream since [{}]", start, e); |
147 |
|
} |
148 |
|
|
149 |
1 |
return events; |
150 |
|
} |
151 |
|
|
|
|
| 76.5% |
Uncovered Elements: 4 (17) |
Complexity: 4 |
Complexity Density: 0.36 |
|
152 |
25 |
@Override... |
153 |
|
public List<WatchListEvent> getMatchingVisibleEvents(List<WatchListEvent> events, String subscriber) |
154 |
|
{ |
155 |
25 |
List<WatchListEvent> result = new ArrayList<WatchListEvent>(); |
156 |
|
|
157 |
25 |
for (WatchListEvent event : events) { |
158 |
34 |
if (isEventSkipped(event)) { |
159 |
|
|
160 |
0 |
continue; |
161 |
|
} |
162 |
|
|
163 |
34 |
if (!isEventViewable(event, subscriber)) { |
164 |
|
|
165 |
0 |
continue; |
166 |
|
} |
167 |
|
|
168 |
34 |
if (!isEventMatching(event, subscriber)) { |
169 |
|
|
170 |
31 |
continue; |
171 |
|
} |
172 |
|
|
173 |
3 |
result.add(event); |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
25 |
Collections.sort(result); |
178 |
|
|
179 |
25 |
return result; |
180 |
|
} |
181 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
|
182 |
34 |
@Override... |
183 |
|
public boolean isEventMatching(WatchListEvent event, String subscriber) |
184 |
|
{ |
185 |
34 |
boolean isWatched = false; |
186 |
|
|
187 |
34 |
try { |
188 |
|
|
189 |
34 |
Collection<String> watchedUsers = store.getWatchedElements(subscriber, WatchedElementType.USER); |
190 |
|
|
191 |
34 |
isWatched |= store.isWatched(event.getWiki(), subscriber, WatchedElementType.WIKI); |
192 |
34 |
isWatched |= store.isWatched(event.getPrefixedSpace(), subscriber, WatchedElementType.SPACE); |
193 |
34 |
isWatched |= store.isWatched(event.getPrefixedFullName(), subscriber, WatchedElementType.DOCUMENT); |
194 |
34 |
isWatched |= CollectionUtils.intersection(watchedUsers, event.getAuthors()).size() > 0; |
195 |
|
} catch (Exception e) { |
196 |
0 |
logger.error("Failed to determine if an event for the document [{}] is interesting to [{}]", |
197 |
|
event.getDocumentReference(), subscriber, e); |
198 |
|
} |
199 |
|
|
200 |
34 |
return isWatched; |
201 |
|
} |
202 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
203 |
95 |
@Override... |
204 |
|
public boolean isEventSkipped(WatchListEvent event) |
205 |
|
{ |
206 |
|
|
207 |
|
|
208 |
95 |
Collection<String> possibleIntervals = store.getIntervals(); |
209 |
95 |
return possibleIntervals.contains(event.getFullName()); |
210 |
|
} |
211 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
212 |
34 |
@Override... |
213 |
|
public boolean isEventViewable(WatchListEvent event, String subscriber) |
214 |
|
{ |
215 |
34 |
DocumentReference userReference = resolver.resolve(subscriber); |
216 |
34 |
return authorizationManager.hasAccess(Right.VIEW, userReference, event.getDocumentReference()); |
217 |
|
} |
218 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
219 |
1 |
private XWikiContext getXWikiContext()... |
220 |
|
{ |
221 |
1 |
return contextProvider.get(); |
222 |
|
} |
223 |
|
} |