1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.macro.useravatar; |
21 |
|
|
22 |
|
import java.util.Collections; |
23 |
|
import java.util.List; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
import javax.inject.Named; |
27 |
|
import javax.inject.Singleton; |
28 |
|
|
29 |
|
import org.apache.commons.lang3.StringUtils; |
30 |
|
import org.apache.commons.lang3.exception.ExceptionUtils; |
31 |
|
import org.slf4j.Logger; |
32 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
33 |
|
import org.xwiki.bridge.SkinAccessBridge; |
34 |
|
import org.xwiki.component.annotation.Component; |
35 |
|
import org.xwiki.model.EntityType; |
36 |
|
import org.xwiki.model.reference.AttachmentReference; |
37 |
|
import org.xwiki.model.reference.DocumentReference; |
38 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
39 |
|
import org.xwiki.model.reference.EntityReference; |
40 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
41 |
|
import org.xwiki.model.reference.EntityReferenceValueProvider; |
42 |
|
import org.xwiki.rendering.block.Block; |
43 |
|
import org.xwiki.rendering.block.ImageBlock; |
44 |
|
import org.xwiki.rendering.listener.reference.ResourceReference; |
45 |
|
import org.xwiki.rendering.listener.reference.ResourceType; |
46 |
|
import org.xwiki.rendering.macro.AbstractMacro; |
47 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
48 |
|
import org.xwiki.rendering.macro.useravatar.UserAvatarMacroParameters; |
49 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
@version |
55 |
|
@since |
56 |
|
|
57 |
|
@Component |
58 |
|
@Named("useravatar") |
59 |
|
@Singleton |
|
|
| 100% |
Uncovered Elements: 0 (40) |
Complexity: 10 |
Complexity Density: 0.4 |
|
60 |
|
public class UserAvatarMacro extends AbstractMacro<UserAvatarMacroParameters> |
61 |
|
{ |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
private static final String DESCRIPTION = "Allows displaying the avatar for a specific user."; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
private static final String USER_SPACE = "XWiki"; |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
@Inject |
76 |
|
private DocumentAccessBridge documentAccessBridge; |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
@Inject |
82 |
|
private SkinAccessBridge skinAccessBridge; |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
@Inject |
89 |
|
@Named("current") |
90 |
|
private DocumentReferenceResolver<String> currentDocumentReferenceResolver; |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
@Inject |
97 |
|
@Named("compactwiki") |
98 |
|
private EntityReferenceSerializer<String> compactWikiEntityReferenceSerializer; |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
@Inject |
104 |
|
@Named("current") |
105 |
|
private EntityReferenceValueProvider currentEntityReferenceValueProvider; |
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
@Inject |
111 |
|
private Logger logger; |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
116 |
6 |
public UserAvatarMacro()... |
117 |
|
{ |
118 |
6 |
super("User Avatar", DESCRIPTION, UserAvatarMacroParameters.class); |
119 |
6 |
setDefaultCategory(DEFAULT_CATEGORY_CONTENT); |
120 |
|
} |
121 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (34) |
Complexity: 8 |
Complexity Density: 0.36 |
|
122 |
6 |
@Override... |
123 |
|
public List<Block> execute(UserAvatarMacroParameters parameters, String content, MacroTransformationContext context) |
124 |
|
throws MacroExecutionException |
125 |
|
{ |
126 |
6 |
DocumentReference userReference = |
127 |
|
this.currentDocumentReferenceResolver.resolve(parameters.getUsername(), new EntityReference(USER_SPACE, |
128 |
|
EntityType.SPACE)); |
129 |
|
|
130 |
|
|
131 |
6 |
String fileName = null; |
132 |
6 |
if (this.documentAccessBridge.exists(userReference)) { |
133 |
5 |
Object avatarProperty = |
134 |
|
this.documentAccessBridge.getProperty(userReference, new DocumentReference(userReference |
135 |
|
.getWikiReference().getName(), USER_SPACE, "XWikiUsers"), "avatar"); |
136 |
5 |
if (avatarProperty != null) { |
137 |
3 |
fileName = avatarProperty.toString(); |
138 |
|
} |
139 |
|
} else { |
140 |
1 |
throw new MacroExecutionException("User [" |
141 |
|
+ this.compactWikiEntityReferenceSerializer.serialize(userReference) |
142 |
|
+ "] is not registered in this wiki"); |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
5 |
ResourceReference imageReference = |
147 |
|
new ResourceReference(this.skinAccessBridge.getSkinFile("icons/xwiki/noavatar.png"), ResourceType.URL); |
148 |
|
|
149 |
|
|
150 |
5 |
if (!StringUtils.isBlank(fileName)) { |
151 |
3 |
AttachmentReference attachmentReference = new AttachmentReference(fileName, userReference); |
152 |
|
|
153 |
|
|
154 |
3 |
try { |
155 |
2 |
if (documentAccessBridge.getAttachmentVersion(attachmentReference) != null) { |
156 |
|
|
157 |
1 |
imageReference = |
158 |
|
new ResourceReference(this.compactWikiEntityReferenceSerializer.serialize(attachmentReference), |
159 |
|
ResourceType.ATTACHMENT); |
160 |
|
} |
161 |
|
} catch (Exception e) { |
162 |
|
|
163 |
1 |
logger.warn("Failed to get the avatar for user [{}]: [{}]. Using default.", |
164 |
|
this.compactWikiEntityReferenceSerializer.serialize(userReference), |
165 |
|
ExceptionUtils.getRootCauseMessage(e)); |
166 |
|
} |
167 |
|
} |
168 |
5 |
ImageBlock imageBlock = new ImageBlock(imageReference, false); |
169 |
|
|
170 |
5 |
imageBlock.setParameter("alt", "Picture of " + userReference.getName()); |
171 |
5 |
imageBlock.setParameter("title", userReference.getName()); |
172 |
|
|
173 |
5 |
if (parameters.getWidth() != null) { |
174 |
2 |
imageBlock.setParameter("width", String.valueOf(parameters.getWidth())); |
175 |
|
} |
176 |
|
|
177 |
5 |
if (parameters.getHeight() != null) { |
178 |
1 |
imageBlock.setParameter("height", String.valueOf(parameters.getHeight())); |
179 |
|
} |
180 |
|
|
181 |
5 |
return Collections.singletonList((Block) imageBlock); |
182 |
|
} |
183 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
184 |
1 |
@Override... |
185 |
|
public boolean supportsInlineMode() |
186 |
|
{ |
187 |
1 |
return true; |
188 |
|
} |
189 |
|
} |