Forum Discussion

PerBonomi's avatar
7 years ago

Count unique users in a thread

Hi all.

 

I recently got the request to have some indication of how big a reported issue is, directly on a topic page. As you know, a thread can have a hundred replies, but it might only be five users and some mods going back and forth.

 

So, I put this very simple component together; not to get an exact count, but to get some indication of the gravity of a situation. It basically takes a maximum of 500 (configurable) replies and counts the number of unique users.

 

You can easily wrap it in a conditional that will only make it run for specific roles, you could also add some code to use the cache and not have it run every time the page loads, that way the impact shouldn't be too big, but I thought I'd just share the basic premise.

 

<#assign msg_id = page.context.message.uniqueId/>
<#assign limit = 500/>
<#assign query = restadmin('2.0','/search?q=' + 'SELECT author.id FROM messages WHERE parent.id = "${msg_id}" LIMIT ${limit}'?url)/>
<#assign user_ids = []/>
<#list query.data.items as q>
	<#if (user_ids?seq_index_of(q.author.id) < 0)>
		<#assign user_ids = user_ids + [q.author.id]/>
	</#if>
</#list>
<#assign id_count = user_ids?size?number + 1/>
<#if (id_count >= limit)>
	<#assign id_count = id_count + "+"/>
</#if>
<span class="cust-unique-users-count">Unique users in this thread: ${id_count}</span>