add a way to fetch roomDetails and preview but providing the serverPubkey

as the room might not be saved yet on the db, we have to provided the
pubkey to the request
pull/1576/head
Audric Ackermann 4 years ago
parent 8308879ff8
commit 26e22191e8
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -26,6 +26,7 @@ export type OpenGroupV2Request = {
queryParams?: Record<string, string>; queryParams?: Record<string, string>;
headers?: Record<string, string>; headers?: Record<string, string>;
isAuthRequired: boolean; isAuthRequired: boolean;
serverPublicKey?: string; // if not provided, a db called will be made to try to get it.
}; };
export type OpenGroupV2CompactPollRequest = { export type OpenGroupV2CompactPollRequest = {

@ -1,5 +1,9 @@
import _ from 'lodash'; import _ from 'lodash';
import { getV2OpenGroupRoomByRoomId, saveV2OpenGroupRoom } from '../../data/opengroups'; import {
getV2OpenGroupRoomByRoomId,
OpenGroupV2Room,
saveV2OpenGroupRoom,
} from '../../data/opengroups';
import { ConversationController } from '../../session/conversations'; import { ConversationController } from '../../session/conversations';
import { sendViaOnion } from '../../session/onions/onionSend'; import { sendViaOnion } from '../../session/onions/onionSend';
import { PubKey } from '../../session/types'; import { PubKey } from '../../session/types';
@ -46,12 +50,18 @@ async function sendOpenGroupV2Request(request: OpenGroupV2Request): Promise<Obje
body = JSON.stringify(request.queryParams); body = JSON.stringify(request.queryParams);
} }
const roomDetails = await getV2OpenGroupRoomByRoomId({ let serverPubKey: string;
serverUrl: request.server, if (!request.serverPublicKey) {
roomId: request.room, const roomDetails = await getV2OpenGroupRoomByRoomId({
}); serverUrl: request.server,
if (!roomDetails?.serverPublicKey) { roomId: request.room,
throw new Error('PublicKey not found for this server.'); });
if (!roomDetails?.serverPublicKey) {
throw new Error('PublicKey not found for this server.');
}
serverPubKey = roomDetails.serverPublicKey;
} else {
serverPubKey = request.serverPublicKey;
} }
// Because auth happens on a per-room basis, we need both to make an authenticated request // Because auth happens on a per-room basis, we need both to make an authenticated request
if (request.isAuthRequired && request.room) { if (request.isAuthRequired && request.room) {
@ -68,7 +78,7 @@ async function sendOpenGroupV2Request(request: OpenGroupV2Request): Promise<Obje
} }
headers.Authorization = token; headers.Authorization = token;
const res = await sendViaOnion( const res = await sendViaOnion(
roomDetails.serverPublicKey, serverPubKey,
builtUrl, builtUrl,
{ {
method: request.method, method: request.method,
@ -88,6 +98,14 @@ async function sendOpenGroupV2Request(request: OpenGroupV2Request): Promise<Obje
// Note that a 403 has a different meaning; it means that // Note that a 403 has a different meaning; it means that
// we provided a valid token but it doesn't have a high enough permission level for the route in question. // we provided a valid token but it doesn't have a high enough permission level for the route in question.
if (statusCode === 401) { if (statusCode === 401) {
const roomDetails = await getV2OpenGroupRoomByRoomId({
serverUrl: request.server,
roomId: request.room,
});
if (!roomDetails) {
window.log.warn('Got 401, but this room does not exist');
return null;
}
roomDetails.token = undefined; roomDetails.token = undefined;
// we might need to retry doing the request here, but how to make sure we don't retry indefinetely? // we might need to retry doing the request here, but how to make sure we don't retry indefinetely?
await saveV2OpenGroupRoom(roomDetails); await saveV2OpenGroupRoom(roomDetails);
@ -95,7 +113,7 @@ async function sendOpenGroupV2Request(request: OpenGroupV2Request): Promise<Obje
return res as object; return res as object;
} else { } else {
// no need for auth, just do the onion request // no need for auth, just do the onion request
const res = await sendViaOnion(roomDetails.serverPublicKey, builtUrl, { const res = await sendViaOnion(serverPubKey, builtUrl, {
method: request.method, method: request.method,
headers, headers,
body, body,
@ -418,7 +436,7 @@ export const deleteSingleMessage = async (
return isOk; return isOk;
}; };
export const getAllRoomInfos = async (roomInfos: OpenGroupRequestCommonType) => { export const getAllRoomInfos = async (roomInfos: OpenGroupV2Room) => {
// room should not be required here // room should not be required here
const request: OpenGroupV2Request = { const request: OpenGroupV2Request = {
method: 'GET', method: 'GET',
@ -426,6 +444,7 @@ export const getAllRoomInfos = async (roomInfos: OpenGroupRequestCommonType) =>
server: roomInfos.serverUrl, server: roomInfos.serverUrl,
isAuthRequired: false, isAuthRequired: false,
endpoint: 'rooms', endpoint: 'rooms',
serverPublicKey: roomInfos.serverPublicKey,
}; };
const result = await sendOpenGroupV2Request(request); const result = await sendOpenGroupV2Request(request);
const statusCode = parseStatusCodeFromOnionRequest(result); const statusCode = parseStatusCodeFromOnionRequest(result);
@ -535,7 +554,7 @@ export const downloadFileOpenGroupV2ByUrl = async (
* It can be used directly, or saved on the attachments directory if needed, but this function does not handle it * It can be used directly, or saved on the attachments directory if needed, but this function does not handle it
*/ */
export const downloadPreviewOpenGroupV2 = async ( export const downloadPreviewOpenGroupV2 = async (
roomInfos: OpenGroupRequestCommonType roomInfos: OpenGroupV2Room
): Promise<string | null> => { ): Promise<string | null> => {
const request: OpenGroupV2Request = { const request: OpenGroupV2Request = {
method: 'GET', method: 'GET',
@ -543,6 +562,7 @@ export const downloadPreviewOpenGroupV2 = async (
server: roomInfos.serverUrl, server: roomInfos.serverUrl,
isAuthRequired: false, isAuthRequired: false,
endpoint: `rooms/${roomInfos.roomId}/image`, endpoint: `rooms/${roomInfos.roomId}/image`,
serverPublicKey: roomInfos.serverPublicKey,
}; };
const result = await sendOpenGroupV2Request(request); const result = await sendOpenGroupV2Request(request);

Loading…
Cancel
Save