Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/database/postgres/file-use-times.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { callback2 } from "@cocalc/util/async-utils";6import { PostgreSQL } from "./types";7import { query } from "./query";89interface Options {10project_id: string;11path: string;12account_id: string; // who the request is about13user_account_id: string; // who is making the request14limit: number; // at most this many timestamps15access_times?: boolean; // if true, include access times16edit_times?: boolean; // if true, return edit times.17}1819type Response = { access_times?: number[]; edit_times?: number[] };2021export async function file_use_times(22db: PostgreSQL,23opts: Options24): Promise<Response> {25if (!opts.access_times && !opts.edit_times) {26// trivial edge case.27return {};28}29// Verify that that user has access. Throws exception if not allowed.30if (31!(await callback2(db.user_is_in_project_group.bind(db), {32account_id: opts.user_account_id,33project_id: opts.project_id,34cache: true,35}))36) {37throw Error("user does not have read access to the given project");38}3940const resp: Response = {};4142if (opts.access_times) {43// Query the file_access_log file.44const file_access_times: { time: Date }[] = await query({45db,46table: "file_access_log",47select: ["time"],48where: {49project_id: opts.project_id,50filename: opts.path,51account_id: opts.account_id,52},53one: false,54order_by: "time desc",55limit: opts.limit,56});57resp.access_times = [];58for (const d of file_access_times) {59resp.access_times.push(d.time.valueOf());60}61}6263// The patches data64if (opts.edit_times) {65const string_id = db.sha1(opts.project_id, opts.path);66const edit_times: { time: Date }[] = await query({67db,68table: "patches",69select: ["time"],70where: { string_id },71one: false,72order_by: "time desc",73limit: opts.limit,74});75resp.edit_times = [];76for (const d of edit_times) {77resp.edit_times.push(d.time.valueOf());78}79}8081return resp;82}838485