1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.social.core.processor;
18
19 import java.util.List;
20 import java.util.Map;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23
24 import org.exoplatform.container.xml.InitParams;
25 import org.exoplatform.portal.config.UserPortalConfigService;
26 import org.exoplatform.portal.webui.util.Util;
27 import org.exoplatform.social.core.BaseActivityProcessorPlugin;
28 import org.exoplatform.social.core.activity.model.ExoSocialActivity;
29 import org.exoplatform.social.core.identity.model.Identity;
30 import org.exoplatform.social.core.identity.provider.OrganizationIdentityProvider;
31 import org.exoplatform.social.core.service.LinkProvider;
32
33
34
35
36
37
38 public class MentionsProcessor extends BaseActivityProcessorPlugin {
39
40 private static final Pattern pattern = Pattern.compile("@([^\\s]+)|@([^\\s]+)$");
41
42 private UserPortalConfigService userPortalConfigService;
43
44 public MentionsProcessor(InitParams params, UserPortalConfigService userPortalConfigService) {
45 super(params);
46 this.userPortalConfigService = userPortalConfigService;
47 }
48
49 public void processActivity(ExoSocialActivity activity) {
50 if (activity != null) {
51 String portalOwner = null;
52 try{
53 portalOwner = Util.getPortalRequestContext().getPortalOwner();
54 } catch (Exception e){
55
56 portalOwner = userPortalConfigService.getDefaultPortal();
57 }
58 activity.setTitle(substituteUsernames(portalOwner, activity.getTitle()));
59 activity.setBody(substituteUsernames(portalOwner, activity.getBody()));
60 Map<String, String> templateParams = activity.getTemplateParams();
61 List<String> templateParamKeys = getTemplateParamKeysToFilter(activity);
62 for(String key : templateParamKeys){
63 templateParams.put(key, substituteUsernames(portalOwner, templateParams.get(key)));
64 }
65 }
66 }
67
68
69
70
71 private String substituteUsernames(String portalOwner, String message) {
72 if (message == null || message.trim().isEmpty()) {
73 return message;
74 }
75
76 Matcher matcher = pattern.matcher(message);
77
78
79 StringBuffer buf = new StringBuffer();
80 while (matcher.find()) {
81
82 String username = matcher.group().substring(1);
83 if (username == null || username.isEmpty()) {
84 continue;
85 }
86 Identity identity = LinkProvider.getIdentityManager().getOrCreateIdentity(OrganizationIdentityProvider.NAME, username, false);
87 if (identity == null || identity.isDeleted() || !identity.isEnable()) {
88 continue;
89 }
90 try {
91 username = LinkProvider.getProfileLink(username, portalOwner);
92 } catch (Exception e) {
93 continue;
94 }
95
96 if (username != null) {
97 matcher.appendReplacement(buf, username);
98 }
99 }
100 if (buf.length() > 0) {
101 matcher.appendTail(buf);
102 return buf.toString();
103 }
104 return message;
105 }
106 }