Verified Commit 5ab9f823 authored by Jakob Moser's avatar Jakob Moser
Browse files

Implement using exfiltrated metadata

parent 5e778b5d
Loading
Loading
Loading
Loading
Loading
+20 −19
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@ import { Base } from "./Base.mjs"
import { Agenda } from "../pieces/Agenda.mjs"
import { getKey } from "../../account.mjs"
import { ButtonAndTextLink } from "../pieces/ButtonAndTextLink.mjs"
import { getLatestProtocolDate } from "../../gitlab.mjs"
import { getLatestProtocolMetadata } from "../../gitlab.mjs"

const items = {
    all: null,
@@ -29,7 +29,13 @@ const items = {
    },
}

let protocolDate = null
const latestProtocol = {
    meta: null,
    async fetch() {
        this.meta = await getLatestProtocolMetadata()
        m.redraw()
    },
}

export const Fachschaftssitzung = {
    oncreate() {
@@ -37,7 +43,7 @@ export const Fachschaftssitzung = {
    },
    async oninit() {
        items.fetch()
        protocolDate = await getLatestProtocolDate()
        latestProtocol.fetch()
    },
    view() {
        return m(Base, [
@@ -49,9 +55,7 @@ export const Fachschaftssitzung = {
                m(
                    "p",
                    m(ButtonAndTextLink, {
                        href:
                            protocolDate &&
                            `https://gitlab.cl.uni-heidelberg.de/-/ide/project/fachschaft/protokolle/edit/master/-/${protocolDate.slice(0, 4)}/${protocolDate}.md`,
                        href: latestProtocol.meta?.editLink,
                        icon: "fa-solid fa-file-pen",
                        name: "Aktuelles Protokoll bearbeiten",
                    }),
@@ -59,19 +63,16 @@ export const Fachschaftssitzung = {
                m(
                    "p",
                    m(ButtonAndTextLink, {
                        href:
                            protocolDate &&
                            `https://gitlab.cl.uni-heidelberg.de/api/v4/projects/1193/jobs/artifacts/master/raw/pdfs/${protocolDate}.pdf?job=convert-to-pdf`,
                        href: latestProtocol.meta?.downloadLink,
                        icon: "fa-regular fa-file-pdf",
                        name: "Protokoll als PDF herunterladen",
                    }),
                ),
                latestProtocol.meta?.containsResolution &&
                    m(
                        "p",
                        m(ButtonAndTextLink, {
                        href:
                            protocolDate &&
                            `https://gitlab.cl.uni-heidelberg.de/api/v4/projects/1193/jobs/artifacts/master/raw/pdfs/${protocolDate}_nur_Beschlüsse.pdf?job=convert-to-pdf`,
                            href: latestProtocol.meta?.resolutionDownloadLink,
                            icon: "fa-solid fa-gavel",
                            name: "Beschlüsse als PDF herunterladen",
                        }),
+51 −18
Original line number Diff line number Diff line
@@ -39,39 +39,72 @@
 * the check will always fail.
 *
 * @param {String} imageUrl A url to an image file (likely hosted on some other origin)
 * @returns The <img> object for further processing, or null if the browser cannot access it.
 */
async function canAccess(imageUrl) {
async function access(imageUrl) {
    try {
        await new Promise((resolve, reject) => {
        return await new Promise((resolve, reject) => {
            const img = document.createElement("img")
            img.addEventListener("load", resolve)
            img.addEventListener("load", resolve(img))
            img.addEventListener("error", reject)
            img.src = imageUrl
        })
        return true
    } catch {
        return false
        return null
    }
}

export async function isLoggedIn() {}
function toIsoString(year, month, day) {
    return `${year.toString().padStart(4, "0")}-${month.toString().padStart(2, "0")}-${day.toString().padStart(2, "0")}`
}

/**
 * @returns if the browser session has access to the protocols repo on GitLab
 * Return an object with metadata about the latest protocol, or null, if the metadata cannot be accessed
 * (e.g., because the user is not logged in to GitLab, is logged in but has no access to the repo, or an error occurred in the pipeline).
 *
 * An example metadata object would be:
 * {
 *   year: 2024,
 *   month: 6,
 *   day: 19,
 *   dateIso: "2024-06-19",
 *   containsResolution: true,
 *   editLink: "https://gitlab.cl.uni-heidelberg.de/-/ide/project/fachschaft/protokolle/edit/master/-/2024/2024-06-19.md",
 *   downloadLink: "https://gitlab.cl.uni-heidelberg.de/api/v4/projects/1193/jobs/artifacts/master/raw/pdfs/2024-06-19.pdf?job=convert-to-pdf",
 *   resolutionDownloadLink: "https://gitlab.cl.uni-heidelberg.de/api/v4/projects/1193/jobs/artifacts/master/raw/pdfs/2024-06-19_nur_Beschlüsse.pdf?job=convert-to-pdf"
 * }
 */
export async function canAccessProtocols() {
    return await canAccess(
export async function getLatestProtocolMetadata() {
    const img = await access(
        "https://gitlab.cl.uni-heidelberg.de/api/v4/projects/1193/jobs/artifacts/master/raw/prtcl.svg?job=create-protocol-info-svg",
    )

    if (!img) {
        return null
    }

/**
 * @returns the date of the latest protocol, e.g. "2024-06-19"
 */
export async function getLatestProtocolDate() {
    // TODO Actually fetch the information from GitLab somehow
    const now = new Date()
    const today = now.toISOString().split("T")[0]
    const year = img.width
    const month = Math.floor(img.height / 100)
    const dayAndMaybeOffset = img.height - 100 * month
    const containsResolution = dayAndMaybeOffset >= 43
    const day = containsResolution ? dayAndMaybeOffset - 42 : dayAndMaybeOffset

    const dateIso = toIsoString(year, month, day)

    return today
    const editLink = `https://gitlab.cl.uni-heidelberg.de/-/ide/project/fachschaft/protokolle/edit/master/-/${year}/${dateIso}.md`
    const downloadLink = `https://gitlab.cl.uni-heidelberg.de/api/v4/projects/1193/jobs/artifacts/master/raw/pdfs/${dateIso}.pdf?job=convert-to-pdf`
    const resolutionDownloadLink = containsResolution
        ? `https://gitlab.cl.uni-heidelberg.de/api/v4/projects/1193/jobs/artifacts/master/raw/pdfs/${dateIso}_nur_Beschlüsse.pdf?job=convert-to-pdf`
        : null

    return {
        dateIso,
        year,
        month,
        day,
        containsResolution,
        editLink,
        downloadLink,
        resolutionDownloadLink,
    }
}