mirror of
https://github.com/filecoin-project/slate.git
synced 2024-12-02 08:56:02 +03:00
53 lines
1.6 KiB
JavaScript
53 lines
1.6 KiB
JavaScript
import * as Strings from "~/common/strings";
|
|
import * as Data from "~/node_common/data";
|
|
import * as SearchManager from "~/node_common/managers/search";
|
|
import * as ViewerManager from "~/node_common/managers/viewer";
|
|
import * as RequestUtilities from "~/node_common/request-utilities";
|
|
|
|
export default async (req, res) => {
|
|
const userInfo = await RequestUtilities.checkAuthorizationExternal(req, res);
|
|
if (!userInfo) return;
|
|
const { id, key, user } = userInfo;
|
|
|
|
if (!req.body?.data?.id) {
|
|
return res.status(500).send({ decorator: "NO_FILE_ID_PROVIDED", error: true });
|
|
}
|
|
|
|
//NOTE(martina): cleans the input to remove fields they should not be changing like ownerId, createdAt, filename, size, type etc.
|
|
let updates = {
|
|
id: req.body.data.id,
|
|
data: {
|
|
name: req.body.data.data?.name,
|
|
body: req.body.data.data?.body,
|
|
source: req.body.data.data?.source,
|
|
author: req.body.data.data?.author,
|
|
},
|
|
};
|
|
|
|
const file = await Data.getFileById({ id: updates.id });
|
|
|
|
if (file.ownerId !== user.id) {
|
|
return res.status(400).send({
|
|
decorator: "NOT_FILE_OWNER_UPDATE_NOT_PERMITTED",
|
|
error: true,
|
|
});
|
|
}
|
|
|
|
let response = await Data.updateFileById(updates);
|
|
|
|
if (!response || response.error) {
|
|
return res.status(500).send({ decorator: "UPDATE_FILE_FAILED", error: true });
|
|
}
|
|
|
|
if (response.isPublic) {
|
|
SearchManager.updateFile(response, "EDIT");
|
|
}
|
|
|
|
ViewerManager.hydratePartial(user.id, { library: true, slates: true });
|
|
|
|
return res.status(200).send({
|
|
decorator: "UPDATE_FILE",
|
|
file: response,
|
|
});
|
|
};
|