mirror of
https://github.com/lux-tel/session-desktop.git
synced 2026-10-30 20:33:24 +00:00
55 lines
1.8 KiB
TypeScript
55 lines
1.8 KiB
TypeScript
import AbortController from 'abort-controller';
|
|
import { isString, toNumber } from 'lodash';
|
|
import { batchFirstSubIsSuccess, batchGlobalIsSuccess, sogsBatchSend } from './sogsV3BatchPoll';
|
|
import { uploadFileToRoomSogs3 } from './sogsV3SendFile';
|
|
import { OpenGroupRequestCommonType } from '../../../../data/types';
|
|
import { DURATION } from '../../../constants';
|
|
|
|
/**
|
|
* This function does a double request to the sogs.
|
|
* - First request uploads the file content to the server and gets the associated fileId.
|
|
* - Second request set the preview/avatar image of that room to the just got fileId.
|
|
*
|
|
* @param fileContent the content of the image to upload as new sogs room image. Be sure to resize it to not have something too big (500x500 jpeg should be enough)
|
|
* @param roomInfos the room details including the
|
|
* @returns the fileId and the full url of the new avatar. To be used for redownload and to set the avatar locally once it is updated.
|
|
*/
|
|
export const uploadImageForRoomSogsV3 = async (
|
|
fileContent: Uint8Array,
|
|
roomInfos: OpenGroupRequestCommonType
|
|
): Promise<{ fileUrl: string } | null> => {
|
|
if (!fileContent || !fileContent.length) {
|
|
return null;
|
|
}
|
|
|
|
const result = await uploadFileToRoomSogs3(fileContent, roomInfos);
|
|
if (!result || !isString(result.fileId)) {
|
|
return null;
|
|
}
|
|
const { fileUrl } = result;
|
|
if (!fileContent.length) {
|
|
return null;
|
|
}
|
|
|
|
const batchResult = await sogsBatchSend(
|
|
roomInfos.serverUrl,
|
|
new Set([roomInfos.roomId]),
|
|
new AbortController().signal,
|
|
[
|
|
{
|
|
type: 'updateRoom',
|
|
updateRoom: { roomId: roomInfos.roomId, imageId: toNumber(result.fileId) },
|
|
},
|
|
],
|
|
'batch',
|
|
30 * DURATION.SECONDS // longer time for image upload
|
|
);
|
|
|
|
if (!batchGlobalIsSuccess(batchResult) || !batchFirstSubIsSuccess(batchResult)) {
|
|
return null;
|
|
}
|
|
return {
|
|
fileUrl,
|
|
};
|
|
};
|