function loadChatClient(session = null) {
	let roomId = '';
	const matches = RegExp('/courses/([^/]+).*').exec(window.location.pathname);
	if (matches && matches.length >= 2) {
		roomId = matches[1];
	}

	// create chat tag
	const riotBox = document.createElement('section');
	riotBox.id = 'matrixchat';
	riotBox.dataset.vectorIndexeddbWorkerScript = '/indexeddb-worker.js';
	riotBox.dataset.vectorConfig = '/riot_config.json';
	riotBox.dataset.vectorDefaultToggled = 'true';
	riotBox.dataset.matrixLang = 'de';

	if (session && roomId) {
		const servername = session.userId.substr(session.userId.indexOf(':') + 1);
		riotBox.dataset.matrixRoomId = `#course_${roomId}:${servername}`;
	}
	if (session) {
		riotBox.dataset.matrixHomeserverUrl = session.homeserverUrl;
		riotBox.dataset.matrixUserId = session.userId;
		riotBox.dataset.matrixAccessToken = session.accessToken;
	}
	document.body.appendChild(riotBox);

	// load javascript
	const bundle = window.matrixBundle;
	if (!bundle) {
		throw new Error('window.matrixBundle has to be defined.');
	}
	const riotScript = document.createElement('script');
	riotScript.src = bundle;
	riotScript.type = 'text/javascript';
	document.head.appendChild(riotScript);
}

function requestSession() {
	return $.getJSON('/messenger/token');
}

function ready() {
	if (window.innerWidth < 768) { // breakpoint: md
		return; // screen to small to use embedded messenger
	}

	// Find Matrix Session
	// > in localstorage?
	if (window.localStorage
		&& window.localStorage.getItem('mx_hs_url')
		&& window.localStorage.getItem('mx_access_token')
		&& window.localStorage.getItem('mx_user_id')
	) {
		// session available, the chat will access it itself
		loadChatClient();
	} else {
		// > get new Session from Server and pass it to the chat
		requestSession()
			.then(loadChatClient)
			.catch((error) => {
				/* eslint-disable-next-line no-console */
				console.error('Failed to request Messenger Session.', error);
			});
	}
}

$(document).ready(ready);

@@ -40,163 +40,533 @@
  %7B%0A%09
-let roomId = '';%0A%09const matches = RegExp('/courses/(%5B%5E/%5D+).*').exec(window.location.pathname);%0A%09if (matches && matches.length %3E= 2) %7B%0A%09%09roomId = matches%5B1%5D
+// extract user id%0A%09let matrixUserId = '';%0A%09if (session) %7B%0A%09%09matrixUserId = session.userId;%0A%09%7D else %7B%0A%09%09matrixUserId = window.localStorage.getItem('mx_user_id');%0A%09%7D%0A%0A%09// extract room id%0A%09let roomId = '';%0A%09const matches = RegExp('/courses/(%5B%5E/%5D+).*').exec(window.location.pathname);%0A%09if (matches && matches.length %3E= 2) %7B%0A%09%09roomId = matches%5B1%5D;%0A%09%7D%0A%09let matrixRoomId = '';%0A%09if (matrixUserId && roomId) %7B%0A%09%09const servername = matrixUserId.substr(matrixUserId.indexOf(':') + 1);%0A%09%09matrixRoomId = %60#course_$%7BroomId%7D:$%7Bservername%7D%60
 ;%0A%09%7D
@@ -879,182 +879,54 @@
 e';%0A
-%0A%09if (session && roomId) %7B%0A%09%09const servername = session.userId.substr(session.userId.indexOf(':') + 1);%0A%09%09riotBox.dataset.matrixRoomId = %60#course_$%7BroomId%7D:$%7Bservername%7D%60;%0A%09%7D
+%09riotBox.dataset.matrixRoomId = matrixRoomId;%0A
 %0A%09if

optimize servername extraction