Generic Feedback Widget, Part II

“Slow and steady wins the race.”
Aesop

Yesterday, we made a bit of progress on my Generic Feedback Widget concept, but we left with quite a few loose ends that needed tidying up. I wasn’t too happy with the way that the date fields came out, so I went sniffing around for an out-of-the-box date formatter and came across the GlideTimeAgo object. This cool little doodad will not only format the date and time, but for relatively recent values, will turn it into a text describing just how long ago it was. To put it to use, I adapted a function that I discovered on another page and threw it down into the bottom of the widget’s server side code.

function getTimeAgo(glidedatetime) {
	var response = '';
	if (glidedatetime) {
		var timeago = new GlideTimeAgo();
		response = timeago.format(glidedatetime);
	}
	return response;
}

Then I updated the code that pulled the value out of the record.

feedback.dateTime = getTimeAgo(new GlideDateTime(fb.getValue('sys_created_on')));

Now recent comments will have timestamps such as 7 minutes ago or Just Now or 4 days ago. I like that much better.

Another issue was the need to translate live_profile sys_ids into sys_user sys_ids. I actually found that I needed to translate from one to the other in both directions, so I set up a couple of maps to retain anything that I found (so I wouldn’t have to look it up again if I needed to translate it again or go back the other direction). I ended up creating four different functions related to this process, one to pull from the map and another to populate the map for each of the two different ways this could be approached (live_profile to sys_user and sys_user to live_profile).

function getUserSysId(liveProfileId) {
	if (!data.userSysIdMap) {
		data.userSysIdMap = {};
	}
	if (!data.userSysIdMap[liveProfileId]) {
		fetchUserSysId(liveProfileId);
	}
	return data.userSysIdMap[liveProfileId];
}

function fetchUserSysId(liveProfileId) {
	if (!data.profileSysIdMap) {
		data.profileSysIdMap = {};
	}
	var lp = new GlideRecord('live_profile');
	if (lp.get(liveProfileId)) {
		var userSysId = lp.getValue('document');
		data.userSysIdMap[liveProfileId] = userSysId;
		data.profileSysIdMap[userSysId] = liveProfileId;
	}
}

function getProfileSysId(userSysId) {
	if (!data.profileSysIdMap) {
		data.profileSysIdMap = {};
	}
	if (!data.profileSysIdMap[userSysId]) {
		fetchProfileSysId(userSysId);
	}
	return data.profileSysIdMap[userSysId];
}

function fetchProfileSysId(userSysId) {
	if (!data.userSysIdMap) {
		data.userSysIdMap = {};
	}
	var lp = new GlideRecord('live_profile');
	lp.addQuery('document', userSysId);
	lp.query();
	if (lp.next()) {
		var liveProfileId = lp.getValue('sys_id');
		data.userSysIdMap[liveProfileId] = userSysId;
		data.profileSysIdMap[userSysId] = liveProfileId;
	}
}

To fix the sys_id references in the comment author link, I leveraged one of the above functions to swap out the live_profile id with the one that I needed from the sys_user table.

feedback.userSysId = getUserSysId(fb.getValue('profile'));

I also utilized those functions formatting the @mentions that can be present in live_message text. I wanted those to be links to the User Profile page as well, so I hunted down some existing code that formats @mentions on the Live Feed page and adapted it for my own purpose.

function formatMentions(text) {
	if (!text) {
		text = '';
	}
	var regexMentionParts = /[\w\d\s/\']+/gi;
	text = text.replace(/@\[[\w\d\s]+:[\w\d\s/\']+\]/gi, function (mention) {
		var response = mention;
		var mentionParts = mention.match(regexMentionParts);
		if (mentionParts.length === 2) {
			var liveProfileId = mentionParts[0];
			var name = mentionParts[1];
			response = '<a href="?id=user_profile&table=sys_user&sys_id=';
			response += getUserSysId(liveProfileId);
			response += '">@';
			response += name;
			response += '</a>';
		}
		return response;
	});
	return text.replace('\n', '<br/>');
}

Most of that is regex gobbledygook that I couldn’t possibly understand, but it does work, so now any @mentions in the text of message are converted into links to the User Profile page of the person mentioned. As with the other corrections, I just needed to modify the code that populates the feedback object to make use of this new function.

feedback.comment = formatMentions(fb.getDisplayValue('message'));

Well, we didn’t get to the part where we take in new feedback, so we will have to take that up next time out. Still, we did make quite a few improvements in the way that the existing feedback is formatted, so we are making progress.