Skip to content

Commit 032c7db

Browse files
committed
provide CHATRIX_LOCAL_STORAGE_KEY for storing sessions removing session cleanup responsibility outside of chatrix
1 parent d00db79 commit 032c7db

File tree

4 files changed

+18
-47
lines changed

4 files changed

+18
-47
lines changed

src/main.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ function getLoginToken(): string | null {
4747
return new URLSearchParams(window.location.search).get("loginToken");
4848
}
4949

50-
function getBackendUserId(): string | null {
51-
return new URLSearchParams(window.location.search).get("backendUserId");
50+
function getLocalStorageKey(): string | null {
51+
return new URLSearchParams(window.location.search).get("localStorageKey");
5252
}
5353

5454
async function main() {
@@ -60,8 +60,8 @@ async function main() {
6060
root.className = "hydrogen";
6161
const config = await fetchConfig();
6262

63-
const backendUserId = getBackendUserId();
64-
const platform = new ChatrixPlatform({container: root, assetPaths, config: {}, options: { development: import.meta.env.DEV }}, config.instance_id, backendUserId);
63+
const localStorageKey = getLocalStorageKey();
64+
const platform = new ChatrixPlatform({container: root, assetPaths, config: {}, options: { development: import.meta.env.DEV }}, localStorageKey);
6565
attachLogExportToWindow(platform);
6666
const navigation = new Navigation(allowsChild);
6767
platform.setNavigation(navigation);

src/parent/load.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ function loadIframe(minimized = false) {
7373
throw new Error("CHATRIX_CONFIG_LOCATION is not set");
7474
}
7575

76-
const backendUserId = (window as any).CHATRIX_BACKEND_USER_ID || null;
76+
const localStorageKey = (window as any).CHATRIX_LOCAL_STORAGE_KEY || null;
7777

7878
const urlParams = new URLSearchParams(window.location.search);
7979
const loginToken = urlParams.get("loginToken");
8080
urlParams.delete("loginToken");
8181
window.history.replaceState( null, '', (urlParams.entries.length ? '?' + urlParams : './' ) + location.hash );
8282

8383
iframe.src = new URL(
84-
`${htmlLocation}?config=${configLocation}${minimized? "&minimized=true": ""}${loginToken? "&loginToken="+encodeURIComponent(loginToken): ""}${backendUserId? "&backendUserId="+encodeURIComponent(backendUserId): ""}`,
84+
`${htmlLocation}?config=${configLocation}${minimized? "&minimized=true": ""}${loginToken? "&loginToken="+encodeURIComponent(loginToken): ""}${localStorageKey? "&localStorageKey="+encodeURIComponent(localStorageKey): ""}`,
8585
hostRoot
8686
).href;
8787
iframe.className = "chatrix-iframe";

src/platform/ChatrixPlatform.ts

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,9 @@ import { Platform } from "hydrogen-view-sdk";
1818
import { SessionInfoStorage } from "./SessionInfoStorage";
1919

2020
export class ChatrixPlatform extends Platform {
21-
constructor(options, instanceId: string | null, backendUserId: string | null) {
21+
constructor(options, localStorageKey: string) {
2222
super(options);
23-
let sessionPrefix = "hydrogen_sessions_v1";
24-
let sessionName = sessionPrefix;
25-
if (instanceId && instanceId !== "") {
26-
sessionName = `${sessionName}_${instanceId}`;
27-
}
28-
if (backendUserId && backendUserId !== "") {
29-
sessionName = `${sessionName}_${backendUserId}`;
30-
this.cleanupSessions(sessionPrefix,backendUserId);
31-
}
32-
33-
this.sessionInfoStorage = new SessionInfoStorage(sessionName);
34-
}
35-
36-
cleanupSessions(sessionPrefix: string, backendUserId: string) {
37-
for (let i=0; i<localStorage.length; i++) {
38-
let key = localStorage.key(i);
39-
if (!key.startsWith(sessionPrefix) || key.endsWith(backendUserId)) {
40-
continue;
41-
}
42-
this.invalidateSession(
43-
JSON.parse(localStorage.getItem(key))[0]
44-
);
45-
localStorage.removeItem(key);
46-
}
47-
}
48-
49-
async invalidateSession(session: {[key: string]: string}) {
50-
await fetch(session.homeserver + '/_matrix/client/v3/logout', {
51-
method: 'POST',
52-
headers: {
53-
'Authorization': 'Bearer ' + session.accessToken,
54-
},
55-
});
23+
localStorageKey = localStorageKey ?? "hydrogen_sessions_v1";
24+
this.sessionInfoStorage = new SessionInfoStorage(localStorageKey);
5625
}
5726
}

wordpress/chatrix/src/plugin.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Automattic\Chatrix;
44

5+
const LOCAL_STORAGE_KEY_PREFIX = 'hydrogen_sessions_v1';
6+
57
function asset_url( $asset_path ): string {
68
return plugins_url( "../frontend/$asset_path", __FILE__ );
79
}
@@ -71,15 +73,16 @@ function () {
7173
$config = chatrix_config();
7274
if ( $config ) {
7375
$current_user = wp_get_current_user();
76+
if ( 0 === $current_user->ID ) {
77+
$local_storage_key = LOCAL_STORAGE_KEY_PREFIX;
78+
} else {
79+
$local_storage_key = LOCAL_STORAGE_KEY_PREFIX . '_' . $current_user->user_login;
80+
}
7481
?>
7582
<script type="text/javascript">
7683
window.CHATRIX_HTML_LOCATION = "<?php echo esc_url( asset_url( 'chatrix.html' ) ); ?>";
7784
window.CHATRIX_CONFIG_LOCATION = "<?php echo esc_url( $config['url'] ); ?>";
78-
<?php if ( 0 === $current_user->ID ) { ?>
79-
window.CHATRIX_BACKEND_USER_ID = null;
80-
<?php } else { ?>
81-
window.CHATRIX_BACKEND_USER_ID = "<?php echo esc_js( $current_user->user_login ); ?>";
82-
<?php } ?>
85+
window.CHATRIX_LOCAL_STORAGE_KEY = "<?php echo esc_js( $local_storage_key ); ?>";
8386
</script>
8487
<?php
8588
}
@@ -103,10 +106,9 @@ function() {
103106
});
104107
}
105108
(function() {
106-
let sessionPrefix = "hydrogen_sessions_v1";
107109
for (let i=0; i<localStorage.length; i++) {
108110
let key = localStorage.key(i);
109-
if (!key.startsWith(sessionPrefix)) {
111+
if (!key.startsWith(LOCAL_STORAGE_KEY_PREFIX)) {
110112
continue;
111113
}
112114
this.invalidateChatrixSession(

0 commit comments

Comments
 (0)