diff options
author | Mohammad Amoush <47069173+mamoush34@users.noreply.github.com> | 2020-01-19 15:15:53 +0300 |
---|---|---|
committer | Mohammad Amoush <47069173+mamoush34@users.noreply.github.com> | 2020-01-19 15:15:53 +0300 |
commit | 7683e1fbb53fe683c0d04e537d89fb53d768e852 (patch) | |
tree | d81eebcd5a129550a49fdfc852b8bb6220907a1a /src/server/ApiManagers/UserManager.ts | |
parent | f4382d73eec75f7d7f4bfe6eae3fb1efa128a021 (diff) | |
parent | aff9cc02750eb032ade98d77cf9ff45677063fc8 (diff) |
Merge branch 'master' of https://github.com/browngraphicslab/Dash-Web into webcam_mohammad
Diffstat (limited to 'src/server/ApiManagers/UserManager.ts')
-rw-r--r-- | src/server/ApiManagers/UserManager.ts | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/server/ApiManagers/UserManager.ts b/src/server/ApiManagers/UserManager.ts new file mode 100644 index 000000000..f2ef22961 --- /dev/null +++ b/src/server/ApiManagers/UserManager.ts @@ -0,0 +1,71 @@ +import ApiManager, { Registration } from "./ApiManager"; +import { Method } from "../RouteManager"; +import { Database } from "../database"; +import { msToTime } from "../ActionUtilities"; + +export const timeMap: { [id: string]: number } = {}; +interface ActivityUnit { + user: string; + duration: number; +} + +export default class UserManager extends ApiManager { + + protected initialize(register: Registration): void { + + register({ + method: Method.GET, + subscription: "/getUsers", + secureHandler: async ({ res }) => { + const cursor = await Database.Instance.query({}, { email: 1, userDocumentId: 1 }, "users"); + const results = await cursor.toArray(); + res.send(results.map(user => ({ email: user.email, userDocumentId: user.userDocumentId }))); + } + }); + + register({ + method: Method.GET, + subscription: "/getUserDocumentId", + secureHandler: ({ res, user }) => res.send(user.userDocumentId) + }); + + register({ + method: Method.GET, + subscription: "/getCurrentUser", + secureHandler: ({ res, user }) => res.send(JSON.stringify(user)), + publicHandler: ({ res }) => res.send(JSON.stringify({ id: "__guest__", email: "" })) + }); + + register({ + method: Method.GET, + subscription: "/activity", + secureHandler: ({ res }) => { + const now = Date.now(); + + const activeTimes: ActivityUnit[] = []; + const inactiveTimes: ActivityUnit[] = []; + + for (const user in timeMap) { + const time = timeMap[user]; + const duration = now - time; + const target = (duration / 1000) < (60 * 5) ? activeTimes : inactiveTimes; + target.push({ user, duration }); + } + + const process = (target: { user: string, duration: number }[]) => { + const comparator = (first: ActivityUnit, second: ActivityUnit) => first.duration - second.duration; + const sorted = target.sort(comparator); + return sorted.map(({ user, duration }) => `${user} (${msToTime(duration)})`); + }; + + res.render("user_activity.pug", { + title: "User Activity", + active: process(activeTimes), + inactive: process(inactiveTimes) + }); + } + }); + + } + +}
\ No newline at end of file |