Skip to content

Commit 8aa13b7

Browse files
committed
session management
1 parent 7f8f6ef commit 8aa13b7

File tree

4 files changed

+150
-9
lines changed

4 files changed

+150
-9
lines changed

src/webpage/jsontypes.ts

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,7 @@ interface readyjson {
7373
country_code: string;
7474
users: userjson[];
7575
merged_members: [memberjson][];
76-
sessions: {
77-
active: boolean;
78-
activities: []; //will need to find example of this
79-
client_info: {
80-
version: number;
81-
};
82-
session_id: string;
83-
status: string;
84-
}[];
76+
sessions: sessionJson[];
8577
resume_gateway_url: string;
8678
consents: {
8779
personalization: {
@@ -104,6 +96,50 @@ interface readyjson {
10496
};
10597
};
10698
}
99+
export interface sessionJson {
100+
active: boolean;
101+
activities: []; //will need to find example of this
102+
client_info: {
103+
version: number;
104+
};
105+
session_id: string;
106+
status: string;
107+
}
108+
export interface sesLocation {
109+
is_eu: boolean;
110+
city: string | null;
111+
region: string | null;
112+
region_code: string | null;
113+
country_name: string;
114+
country_code: string;
115+
continent_name: string;
116+
continent_code: string;
117+
latitude: number;
118+
longitude: number;
119+
postal: string | null;
120+
calling_code: string;
121+
flag: string;
122+
emoji_flag: string;
123+
emoji_unicode: string;
124+
}
125+
export interface expSessionJson {
126+
id: string;
127+
id_hash: string;
128+
status: string;
129+
activities: [];
130+
client_status: {};
131+
approx_last_used_time: string;
132+
client_info: {
133+
platform?: string;
134+
location?: string;
135+
os?: string;
136+
version?: number;
137+
};
138+
last_seen: string;
139+
last_seen_ip: string;
140+
last_seen_location: string | null;
141+
last_seen_location_info: sesLocation | null;
142+
}
107143
export interface GuildOverrides {
108144
channel_overrides: {
109145
message_notifications: number;
@@ -1187,6 +1223,7 @@ type opRTC12 = {
11871223
];
11881224
};
11891225
};
1226+
11901227
export {
11911228
readyjson,
11921229
dirrectjson,

src/webpage/localuser.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {createImg, getapiurls, getBulkUsers, installPGet, SW} from "./utils/util
66
import {getBulkInfo, setTheme, Specialuser} from "./utils/utils.js";
77
import {
88
channeljson,
9+
expSessionJson,
910
guildFolder,
1011
mainuserjson,
1112
memberjson,
@@ -14,6 +15,7 @@ import {
1415
messagejson,
1516
presencejson,
1617
readyjson,
18+
sessionJson,
1719
startTypingjson,
1820
wsjson,
1921
} from "./jsontypes.js";
@@ -2973,6 +2975,86 @@ class Localuser {
29732975
devPortal.addHTMLArea(appListContainer);
29742976
});
29752977
}
2978+
//TODO session
2979+
2980+
{
2981+
const manageSessions = settings.addButton(I18n.deviceManage.title());
2982+
(async () => {
2983+
const json = (await (
2984+
await fetch(this.info.api + "/auth/sessions?extended=true", {headers: this.headers})
2985+
).json()) as {user_sessions: expSessionJson[]};
2986+
for (const session of json.user_sessions.sort(
2987+
(a, b) => +new Date(a.last_seen) - +new Date(b.last_seen),
2988+
)) {
2989+
const div = document.createElement("div");
2990+
div.classList.add("flexltr", "sessionDiv");
2991+
2992+
const info = document.createElement("div");
2993+
info.classList.add("flexttb");
2994+
div.append(info);
2995+
2996+
let line2 = "";
2997+
const last = session.last_seen_location_info;
2998+
if (last) {
2999+
line2 += last.country_name;
3000+
if (last.region) line2 += ", " + last.region;
3001+
if (last.city) line2 += ", " + last.city;
3002+
}
3003+
if (line2) {
3004+
line2 += " • ";
3005+
}
3006+
const format = new Intl.RelativeTimeFormat(I18n.lang, {style: "short"});
3007+
const time = (Date.now() - +new Date(session.last_seen)) / 1000;
3008+
if (time < 60) {
3009+
line2 += format.format(-Math.floor(time), "seconds");
3010+
} else if (time < 60 * 60) {
3011+
line2 += format.format(-Math.floor(time / 60), "minutes");
3012+
} else if (time < 60 * 60 * 24) {
3013+
line2 += format.format(-Math.floor(time / 60 / 60), "hours");
3014+
} else if (time < 60 * 60 * 24 * 7) {
3015+
line2 += format.format(-Math.floor(time / 60 / 60 / 24), "days");
3016+
} else if (time < 60 * 60 * 24 * 365) {
3017+
line2 += format.format(-Math.floor(time / 60 / 60 / 24 / 7), "weeks");
3018+
} else {
3019+
line2 += format.format(-Math.floor(time / 60 / 60 / 24 / 365), "years");
3020+
}
3021+
const loc = document.createElement("span");
3022+
loc.textContent = line2;
3023+
info.append(loc);
3024+
const r = manageSessions.addHTMLArea(div);
3025+
div.onclick = () => {
3026+
const sub = manageSessions.addSubOptions(I18n.deviceManage.manageDev());
3027+
sub.addText(I18n.deviceManage.ip(session.last_seen_ip));
3028+
sub.addText(I18n.deviceManage.last(session.approx_last_used_time));
3029+
if (last) {
3030+
sub.addText(I18n.deviceManage.estimateWarn());
3031+
sub.addText(I18n.deviceManage.continent(last.continent_name));
3032+
sub.addText(I18n.deviceManage.country(last.country_name));
3033+
if (last.region) sub.addText(I18n.deviceManage.region(last.region));
3034+
if (last.city) sub.addText(I18n.deviceManage.city(last.city));
3035+
if (last.postal) sub.addText(I18n.deviceManage.postal(last.postal));
3036+
sub.addText(I18n.deviceManage.longitude(last.longitude + ""));
3037+
sub.addText(I18n.deviceManage.latitude(last.latitude + ""));
3038+
}
3039+
if (session.id !== this.session_id) {
3040+
sub.addButtonInput("", I18n.deviceManage.logout(), () => {
3041+
div.remove();
3042+
r.html = document.createElement("div");
3043+
manageSessions.returnFromSub();
3044+
fetch(this.info.api + "/auth/sessions/logout", {
3045+
method: "POST",
3046+
headers: this.headers,
3047+
body: JSON.stringify({
3048+
session_id_hashes: [session.id_hash],
3049+
}),
3050+
});
3051+
});
3052+
} else sub.addText(I18n.deviceManage.curSes());
3053+
};
3054+
}
3055+
})();
3056+
}
3057+
29763058
{
29773059
const deleteAccount = settings.addButton(I18n.localuser.deleteAccount()).addForm(
29783060
"",

src/webpage/style.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ body {
183183
min-height: 0;
184184
display: flex;
185185
}
186+
.sessionDiv {
187+
padding: 8px;
188+
background: #00000040;
189+
border-radius: 4px;
190+
cursor: pointer;
191+
}
186192
.trustedDomain {
187193
display: flex;
188194
align-items: center;

translations/en.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,22 @@
821821
"report":"If you found this page within the client please report it:",
822822
"whatelse":"What else do you think should happen?"
823823
},
824+
"deviceManage":{
825+
"title":"Manage Sessions",
826+
"manageDev":"Manage Device",
827+
"ip":"Last known IP: $1",
828+
"estimateWarn":"Warning: All of this information is just a best guess, this could be incorrect.",
829+
"country":"Country: $1",
830+
"region":"Region: $1",
831+
"city":"City: $1",
832+
"postal":"Postal: $1",
833+
"continent":"Continent: $1",
834+
"latitude":"Latitude: $1",
835+
"longitude":"Longitude: $1",
836+
"last":"Approximately last used: $1",
837+
"logout":"Logout",
838+
"curSes":"This is the current session, you have to logout via the other menu"
839+
},
824840
"widget": "Guild Widget",
825841
"widgetEnabled": "Widget enabled",
826842
"incorrectURLS": "## This instance has likely sent the incorrect URLs.\n### If you're the instance owner please see [here](https://docs.spacebar.chat/setup/server/) under *Connecting from remote machines* to correct the issue.\n Would you like Fermi to automatically try to fix this error to let you connect to the instance?",

0 commit comments

Comments
 (0)