slate/node_common/managers/slate.js

23 lines
593 B
JavaScript
Raw Normal View History

2020-10-06 01:33:37 +03:00
import * as Data from "~/node_common/data";
import * as Strings from "~/common/strings";
import * as Numbers from "~/common/numbers";
2020-10-06 01:33:37 +03:00
export const getRandomSlateElementURL = async ({ id, fallback = "" }) => {
if (Strings.isEmpty(id)) {
return fallback;
2020-10-06 01:33:37 +03:00
}
const query = await Data.getSlateById({ id, includeFiles: true, sanitize: true });
2020-10-06 01:33:37 +03:00
if (!query || query.error) {
return fallback;
2020-10-06 01:33:37 +03:00
}
if (!query.objects.length) {
return fallback;
}
const index = Numbers.getRandomInt(0, query.objects.length - 1);
return Strings.getURLfromCID(query.objects[index].cid);
2020-10-06 01:33:37 +03:00
};