How to get auth_token in muc-room-created and muc-room-destroyed events?

Hi,
I’m developing a custom prosody module to monitor muc-room-created and muc-room-destroyed events. To do so I enabled token authentication and now I need to access session.auth_token inside these events so I can make a request to my monitoring API.

I can successfully get session.auth_token on muc-occupant-joined and muc-occupant-left like this:

module:hook("muc-occupant-joined", function (event)
	local stanza = event.stanza;

	if not stanza then
			return -- No such stanza
	end

	local session = prosody.full_sessions[stanza.attr.from];

	-- do the API call using session.auth_token
end);

But on muc-room-created and muc-room-destroyed events I get focus’ stanza and therefore can’t access session.auth_token.

I found this topic but for some reason it doesn’t work. I get only focus as occupant.

I also tried logging all join and leave events and I found that focus is joining the room first and leaving last. I’m not that familiar with Prosody and Jitsi but maybe it has to do something with the stanza in muc-room-created and muc-room-destroyed events. If it’s so is there an option to disable this behaviour or other ways of getting session.auth_token inside muc-room-created and muc-room-destroyed events?

The rooms are always created by jicofo. When your browser connects you should be getting “muc-occupant-joined” and as you are using bosh you should be getting session.auth_token if you have token.

Okay, I’m going to use muc-occupant-joined and muc-occupant-left for detecting when a room is going to be created or destroyed. Thank you a lot!

In case someone needs something similar here is how I did it:

-- Room created
module:hook("muc-occupant-joined", function(event)
	local room = event.room;

	if room._data.persistent then
		return; -- Don't monitor persistent rooms
	end

	local participant_count = 0;

	for _, occupant in room:each_occupant() do
		-- don't count jicofo's admin account (focus)
		if string.sub(occupant.nick,-string.len("/focus")) ~= "/focus" then
			participant_count = participant_count + 1;
		end
	end

	if participant_count ~= 1 then
		return;
	end

	-- Here you're sure that this participant is the first in the room.
	-- You can get auth_token from the session and make your request/logging.
end);

-- Room destroyed
module:hook("muc-occupant-left", function(event)
	local room = event.room;

	if room._data.persistent then
		return; -- Don't monitor persistent rooms
	end

	local participant_count = 0;

	for _, occupant in room:each_occupant() do
		-- don't count jicofo's admin account (focus)
		if string.sub(occupant.nick,-string.len("/focus")) ~= "/focus" then
			participant_count = participant_count + 1;
		end
	end

	if participant_count ~= 0 then
		return;
	end

	-- Here you're sure that this participant is the last in the room.
	-- You can get auth_token from the session and make your request/logging.
end);
5 Likes

I almost never comment when searching for info, but thank you so much dude, this snippet saved me loads of time!