ember-alexandria
Version:
Document management frontend for the alexandria backend
53 lines (43 loc) • 1.63 kB
JavaScript
import { service } from "@ember/service";
import Model, { attr, belongsTo, hasMany } from "@ember-data/model";
import { trackedFunction } from "reactiveweb/function";
export default class DocumentModel extends Model {
title;
description;
metainfo;
createdAt;
createdByUser;
createdByGroup;
modifiedAt;
modifiedByUser;
modifiedByGroup;
date;
content; // needed for upload
("category", { inverse: "documents", async: true }) category;
("tag", { inverse: "documents", async: true }) tags;
("mark", { inverse: "documents", async: true }) marks;
("file", { inverse: "document", async: true }) files;
("alexandria-config") config;
thumbnail = trackedFunction(this, async () => {
// get the thumbnail of the latest file, because thumbnails
// might be generated out of order
const thumbnails = await this.latestFile.value?.renderings;
if (!thumbnails?.length) {
return undefined;
}
return thumbnails.toArray().sort(
// convert to array to not mutate the original
(a, b) => new Date(b.createdAt) - new Date(a.createdAt),
)[0].downloadUrl;
// no need to check for expired URLs here, because the browser caches the images when they are loaded
});
latestFile = trackedFunction(this, async () => {
const files = await this.files;
if (!files.length) {
return undefined;
}
return files
.filter((file) => file.variant === "original")
.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))[0];
});
}