Skip to content

Commit 83d0ea8

Browse files
committed
fix user mention handling
1 parent 18e136e commit 83d0ea8

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

connect/connectNotificationServer.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,35 @@ const getNotificationsForMentionedUser = (eventConfig, content) => {
6666
}
6767

6868
let notifications = [];
69-
const regexUserId = /@([0-9]+)/g;
70-
let matches = regexUserId.exec(content);
69+
const regexUserId = /@([a-zA-Z0-9-_.{}\[\]]+)/g;
70+
let handles=[];
71+
let matches = regexUserId.exec(content);
72+
console.log("matches"+matches)
7173
while (matches) {
74+
let handle = matches[1].toString();
7275
notifications.push({
73-
userId: matches[1].toString(),
76+
userHandle: handle,
7477
newType: 'notifications.connect.project.post.mention',
7578
contents: {
7679
toUserHandle: true,
7780
},
7881
});
7982
matches = regexUserId.exec(content);
83+
handles.push(handle);
8084
}
81-
82-
// only one per userId
83-
notifications = _.uniqBy(notifications, 'userId');
84-
return Promise.resolve(notifications);
85+
// only one per userHandle
86+
notifications = _.uniqBy(notifications, 'userHandle');
87+
88+
return new Promise((resolve)=>{
89+
service.getUsersByHandle(handles).then((users)=>{
90+
console.log(users);
91+
_.map(notifications,(notification)=>{
92+
console.log("userhandle: "+notification.userHandle);
93+
notification.userId = _.find(users,{handle:notification.userHandle}).userId;
94+
});
95+
resolve(notifications);
96+
})
97+
});
8598
};
8699

87100
/**
@@ -284,7 +297,7 @@ const handler = (topic, message, callback) => {
284297
// - check that event has everything required or throw error
285298
getNotificationsForTopicStarter(eventConfig, message.topicId),
286299
getNotificationsForUserId(eventConfig, message.userId),
287-
message.contents && message.contents.postContent ? getNotificationsForMentionedUser(eventConfig, message.contents.postContent) : Promise.resolve([]),
300+
message.postContent ? getNotificationsForMentionedUser(eventConfig, message.postContent) : Promise.resolve([]),
288301
getProjectMembersNotifications(eventConfig, project),
289302
getTopCoderMembersNotifications(eventConfig),
290303
]).then((notificationsPerSource) => (

connect/service.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,36 @@ const getUsersById = (ids) => {
8989
});
9090
};
9191

92+
/**
93+
* Get users details by ids
94+
*
95+
* @param {Array} ids list of user ids
96+
*
97+
* @return {Promise} resolves to the list of user details
98+
*/
99+
const getUsersByHandle = (handles) => {
100+
const query = _.map(handles, (handle) => 'handle:' + handle).join(' OR ');
101+
return request
102+
.get(`${config.TC_API_V3_BASE_URL}/members/_search?fields=userId,handle,firstName,lastName&query=${query}`)
103+
.set('accept', 'application/json')
104+
.set('authorization', `Bearer ${config.TC_ADMIN_TOKEN}`)
105+
.then((res) => {
106+
if (!_.get(res, 'body.result.success')) {
107+
throw new Error(`Failed to get users by handle: ${handles}`);
108+
}
109+
110+
const users = _.get(res, 'body.result.content');
111+
112+
return users;
113+
}).catch((err) => {
114+
const errorDetails = _.get(err, 'response.body.result.content.message');
115+
throw new Error(
116+
`Failed to get users by handles: ${handles}.` +
117+
(errorDetails ? ' Server response: ' + errorDetails : '')
118+
);
119+
});
120+
};
121+
92122
/**
93123
* Get topic details
94124
*
@@ -119,5 +149,6 @@ module.exports = {
119149
getProject,
120150
getRoleMembers,
121151
getUsersById,
152+
getUsersByHandle,
122153
getTopic,
123154
};

0 commit comments

Comments
 (0)