slate/scripts/repost-migration.js

98 lines
2.6 KiB
JavaScript
Raw Normal View History

2021-06-01 06:38:38 +03:00
import configs from "~/knexfile";
import knex from "knex";
import { v4 as uuid } from "uuid";
import * as Logging from "~/common/logging";
2021-06-01 06:38:38 +03:00
import * as Utilities from "~/node_common/utilities";
import * as Data from "~/node_common/data";
import * as Strings from "~/common/strings";
import * as Constants from "~/common/constants";
const envConfig = configs["production"];
2021-06-01 06:38:38 +03:00
const DB = knex(envConfig);
Logging.log(`RUNNING: files-migration.js`);
2021-06-01 06:38:38 +03:00
const saveCopyReposts = async () => {
let repostedFiles = await DB.column(
"users.id",
"users.data",
"files.cid",
"files.filename",
"slates.isPublic",
{ fileData: "files.data" },
{ fileId: "files.id" }
)
.from("slate_files")
.join("slates", "slates.id", "=", "slate_files.slateId")
.join("files", "files.id", "=", "slate_files.fileId")
.join("users", "users.id", "=", "slates.ownerId")
.whereRaw("?? != ??", ["files.ownerId", "slates.ownerId"]);
// Logging.log(repostedFiles.length);
2021-06-01 06:38:38 +03:00
for (let item of repostedFiles) {
Logging.log(item);
// continue;
2021-06-01 06:38:38 +03:00
let user = { data: item.data };
let { buckets, bucketKey, bucketRoot } = await Utilities.getBucketAPIFromUserToken({
user,
});
try {
let response = await Utilities.addExistingCIDToData({
buckets,
key: bucketKey,
path: bucketRoot?.path,
2021-06-01 06:38:38 +03:00
cid: item.cid,
});
} catch (e) {
Logging.log(e);
2021-06-01 06:38:38 +03:00
}
const duplicateFiles = await Data.getFilesByCids({
ownerId: item.id,
cids: [item.cid],
});
if (duplicateFiles.length) {
if (!duplicateFiles[0].isPublic && item.isPublic) {
Logging.log("UPDATE PUBLIC FOR FILE");
2021-06-01 06:38:38 +03:00
await DB.from("files").where("id", item.fileId).update({ isPublic: true });
}
} else {
const file = {
cid: item.cid,
ownerId: item.id,
filename: item.filename,
isPublic: item.isPublic,
data: item.fileData,
};
await DB.insert(file).into("files");
}
}
};
const removeReposts = async () => {
let repostedFiles = await DB.from("slate_files")
.whereIn("id", function () {
this.select("slate_files.id")
.from("slate_files")
.join("slates", "slates.id", "=", "slate_files.slateId")
.join("files", "files.id", "=", "slate_files.fileId")
.whereRaw("?? != ??", ["files.ownerId", "slates.ownerId"]);
})
.del()
.returning("*");
Logging.log(repostedFiles);
2021-06-01 06:38:38 +03:00
};
const runScript = async () => {
await saveCopyReposts();
// await removeReposts();
Logging.log("Finished running. Hit CTRL + C to quit");
2021-06-01 06:38:38 +03:00
};
runScript();