aboutsummaryrefslogtreecommitdiff
path: root/src/server/ApiManagers/UserManager.ts
diff options
context:
space:
mode:
authorSam Wilkins <samwilkins333@gmail.com>2019-10-22 19:39:19 -0400
committerSam Wilkins <samwilkins333@gmail.com>2019-10-22 19:39:19 -0400
commitcee55b4a1b13909d55708eee6c364206ae7c0d4f (patch)
tree859fa90363abfe93a457f74f73e04ddf4c56bfe0 /src/server/ApiManagers/UserManager.ts
parentf42620a33f76f8fdcf7417498f3fb2c1588c064d (diff)
api managers and web socket initial refactoring
Diffstat (limited to 'src/server/ApiManagers/UserManager.ts')
-rw-r--r--src/server/ApiManagers/UserManager.ts40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/server/ApiManagers/UserManager.ts b/src/server/ApiManagers/UserManager.ts
new file mode 100644
index 000000000..bb8837dc6
--- /dev/null
+++ b/src/server/ApiManagers/UserManager.ts
@@ -0,0 +1,40 @@
+import ApiManager from "./ApiManager";
+import RouteManager, { Method } from "../RouteManager";
+import { WebSocket } from "../Websocket/Websocket";
+
+export default class UserManager extends ApiManager {
+
+ public register(router: RouteManager): void {
+ router.addSupervisedRoute({
+ method: Method.GET,
+ subscription: "/whosOnline",
+ onValidation: ({ res }) => {
+ let users: any = { active: {}, inactive: {} };
+ const now = Date.now();
+
+ const { timeMap } = WebSocket;
+ for (const user in timeMap) {
+ const time = timeMap[user];
+ const key = ((now - time) / 1000) < (60 * 5) ? "active" : "inactive";
+ users[key][user] = `Last active ${this.msToTime(now - time)} ago`;
+ }
+
+ res.send(users);
+ }
+ });
+ }
+
+ private msToTime(duration: number) {
+ let milliseconds = Math.floor((duration % 1000) / 100),
+ seconds = Math.floor((duration / 1000) % 60),
+ minutes = Math.floor((duration / (1000 * 60)) % 60),
+ hours = Math.floor((duration / (1000 * 60 * 60)) % 24);
+
+ let hoursS = (hours < 10) ? "0" + hours : hours;
+ let minutesS = (minutes < 10) ? "0" + minutes : minutes;
+ let secondsS = (seconds < 10) ? "0" + seconds : seconds;
+
+ return hoursS + ":" + minutesS + ":" + secondsS + "." + milliseconds;
+ }
+
+} \ No newline at end of file