43 lines
1.6 KiB
TypeScript
43 lines
1.6 KiB
TypeScript
// Metro module 13533 (f13534).
|
|
import UserStore from "../../../../stores/UserStore";
|
|
import { RPC_LOCAL_SCOPE } from "../../Constants";
|
|
import { RPCErrors, RPCCommands } from "../../../../Constants";
|
|
import createRpcJoiSchemaObject from "../../helpers/createRpcJoiSchemaObject";
|
|
import AvatarUtils from "../../../../utils/AvatarUtils";
|
|
import RPCError from "../../RPCError";
|
|
import { readFileAsBase64 } from "../../../../utils/ImageUtils";
|
|
const r21 = {};
|
|
r21[RPCCommands.GET_IMAGE] = {
|
|
scope: RPC_LOCAL_SCOPE,
|
|
validation: (a0) => createRpcJoiSchemaObject(a0).required().keys({
|
|
type: a0.string().required().valid(["user"]),
|
|
id: a0.string().required(),
|
|
format: a0.string().required().valid(["png", "webp", "jpg"]),
|
|
size: a0.number().required().valid([16, 32, 64, 128, 256, 512, 1024])
|
|
}),
|
|
handler: ({ args }) => {
|
|
const { type, id, format = "png", size = 128 } = args;
|
|
let r6 = void 0;
|
|
if (type === "user") {
|
|
const user = UserStore.getUser(id);
|
|
if (user == null) {
|
|
throw new RPCError({
|
|
errorCode: RPCErrors.INVALID_USER
|
|
}, `Invalid user id: ${id}`);
|
|
}
|
|
const userAvatarURL = AvatarUtils.getUserAvatarURL(user, false, size, format);
|
|
const CDN_HOST = window.GLOBAL_ENV.CDN_HOST;
|
|
r6 = CDN_HOST != null && userAvatarURL.indexOf(CDN_HOST) !== -1 ? userAvatarURL + "&_=" : userAvatarURL;
|
|
}
|
|
if (r6 == null) {
|
|
throw new RPCError({
|
|
errorCode: RPCErrors.INVALID_COMMAND
|
|
}, "No valid type.");
|
|
}
|
|
return fetch(r6).then((result) => result.blob()).then((result) => readFileAsBase64(result)).then((result) => ({
|
|
data_url: result
|
|
}));
|
|
}
|
|
};
|
|
export default r21;
|