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