Skip to content

Commit

Permalink
Merge pull request #4689 from coralproject/develop
Browse files Browse the repository at this point in the history
v9.5.1
  • Loading branch information
tessalt authored Nov 1, 2024
2 parents 2d14038 + 7a066b2 commit 62e6444
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 18 deletions.
8 changes: 4 additions & 4 deletions server/src/core/server/app/handlers/api/tenor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ export const tenorSearchHandler =
return;
}

const gifsEnabled = tenant.media?.gifs.enabled ?? false;
const gifsEnabled = tenant.media?.gifs?.enabled ?? false;
if (!gifsEnabled) {
res.status(200).send({
results: [],
});
return;
}

const apiKey = tenant.media?.gifs.key ?? null;
const apiKey = tenant.media?.gifs?.key ?? null;
if (!apiKey || apiKey.length === 0) {
res.status(200).send({
results: [],
Expand All @@ -86,7 +86,7 @@ export const tenorSearchHandler =
}

const contentFilter = convertGiphyContentRatingToTenorLevel(
tenant.media?.gifs.maxRating
tenant.media?.gifs?.maxRating
);

const url = new URL(TENOR_SEARCH_URL);
Expand Down Expand Up @@ -133,7 +133,7 @@ export const tenorSearchHandler =
} catch (e) {
// Ensure that the API key doesn't get leaked to the logs by accident.
if (e.message) {
e.message = e.message.replace(tenant.media?.gifs.key, "[Sensitive]");
e.message = e.message.replace(tenant.media?.gifs?.key, "[Sensitive]");
}
throw new WrappedInternalError(e as Error, "tenor search error");
}
Expand Down
6 changes: 6 additions & 0 deletions server/src/core/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ const config = convict({
env: "MONGODB_ARCHIVE_URI",
sensitive: true,
},
mongodb_max_pool_size: {
doc: "Max pool size for the MongoDB driver.",
format: Number,
default: 50,
env: "MONGODB_MAX_POOL_SIZE",
},
redis: {
doc: "The Redis database to connect to.",
format: "redis-uri",
Expand Down
6 changes: 4 additions & 2 deletions server/src/core/server/data/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,11 @@ export function isArchivingEnabled(config: Config): boolean {
export async function createMongoContext(
config: Config
): Promise<MongoContext> {
const maxPoolSize = config.get("mongodb_max_pool_size");

// Setup MongoDB.
const liveURI = config.get("mongodb");
const live = (await createMongoDB(liveURI)).db;
const live = (await createMongoDB(liveURI, maxPoolSize)).db;

// If we have an archive URI, use it, otherwise, default
// to using the live database
Expand All @@ -154,7 +156,7 @@ export async function createMongoContext(
) {
archive = live;
} else {
archive = (await createMongoDB(archiveURI)).db;
archive = (await createMongoDB(archiveURI, maxPoolSize)).db;
}

return new MongoContextImpl(live, archive);
Expand Down
4 changes: 2 additions & 2 deletions server/src/core/server/models/tenant/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ export function supportsMediaType(
return !!tenant.media?.youtube.enabled;
case "giphy":
return (
!!tenant.media?.gifs.enabled &&
!!tenant.media?.gifs?.enabled &&
!!tenant.media.gifs.key &&
tenant.media.gifs.provider === GQLGIF_MEDIA_SOURCE.GIPHY
);
case "tenor":
return (
!!tenant.media?.gifs.enabled &&
!!tenant.media?.gifs?.enabled &&
!!tenant.media.gifs.key &&
tenant.media.gifs.provider === GQLGIF_MEDIA_SOURCE.TENOR
);
Expand Down
5 changes: 4 additions & 1 deletion server/src/core/server/services/comments/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ async function attachTenorMedia(
video: data.media_formats.mp4.url,
};
} catch (err) {
throw new WrappedInternalError(err as Error, "cannot attach Tenor Media");
if (!(err instanceof Error)) {
throw new Error("cannot attach Tenor Media");
}
throw new WrappedInternalError(err, "cannot attach Tenor Media");
}
}

Expand Down
10 changes: 5 additions & 5 deletions server/src/core/server/services/giphy/giphy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const GiphyRetrieveResponseSchema = Joi.object().keys({

export function ratingIsAllowed(rating: string, tenant: Tenant) {
const compareRating = rating.toLowerCase();
const maxRating = tenant.media?.gifs.maxRating || "g";
const maxRating = tenant.media?.gifs?.maxRating || "g";

const compareIndex = RATINGS_ORDER.indexOf(compareRating);
const maxIndex = RATINGS_ORDER.indexOf(maxRating);
Expand Down Expand Up @@ -98,7 +98,7 @@ export async function searchGiphy(
offset: string,
tenant: Tenant
): Promise<GiphyGifSearchResponse> {
if (!supportsMediaType(tenant, "giphy") || !tenant.media.gifs.key) {
if (!supportsMediaType(tenant, "giphy") || !tenant.media?.gifs?.key) {
throw new InternalError("Giphy was not enabled");
}

Expand Down Expand Up @@ -137,12 +137,12 @@ export async function retrieveFromGiphy(
tenant: Tenant,
id: string
): Promise<GiphyGifRetrieveResponse> {
if (!supportsMediaType(tenant, "giphy") || !tenant.media.gifs.key) {
if (!supportsMediaType(tenant, "giphy") || !tenant.media?.gifs?.key) {
throw new InternalError("Giphy was not enabled");
}

const url = new URL(`${GIPHY_FETCH}/${id}`);
url.searchParams.set("api_key", tenant.media.gifs.key);
url.searchParams.set("api_key", tenant.media?.gifs?.key);

try {
const res = await fetch(url.toString());
Expand All @@ -158,7 +158,7 @@ export async function retrieveFromGiphy(
} catch (err) {
// Ensure that the API key doesn't get leaked to the logs by accident.
if (err.message) {
err.message = err.message.replace(tenant.media.gifs.key, "[Sensitive]");
err.message = err.message.replace(tenant.media?.gifs?.key, "[Sensitive]");
}

// Rethrow the error.
Expand Down
11 changes: 8 additions & 3 deletions server/src/core/server/services/mongodb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import { Db, MongoClient } from "mongodb";
import { WrappedInternalError } from "coral-server/errors";
import logger from "coral-server/logger";

async function createMongoClient(mongoURI: string): Promise<MongoClient> {
async function createMongoClient(
mongoURI: string,
maxPoolSize = 50
): Promise<MongoClient> {
try {
return await MongoClient.connect(mongoURI, {
// believe we don't need this since the new driver only uses
// the new URL parser. But am leaving this here in case we have
// issues with URL's and want to investigate into it.
// useNewUrlParser: true,
ignoreUndefined: true,
maxPoolSize,
});
} catch (err) {
throw new WrappedInternalError(
Expand Down Expand Up @@ -45,10 +49,11 @@ interface CreateMongoDbResult {
* @param config application configuration.
*/
export async function createMongoDB(
mongoURI: string
mongoURI: string,
maxPoolSize?: number
): Promise<CreateMongoDbResult> {
// Connect and create a client for MongoDB.
const client = await createMongoClient(mongoURI);
const client = await createMongoClient(mongoURI, maxPoolSize);

logger.info("mongodb has connected");

Expand Down
2 changes: 1 addition & 1 deletion server/src/core/server/services/tenor/tenor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function retrieveFromTenor(
tenant: Tenant,
id: string
): Promise<FetchPayload> {
if (!supportsMediaType(tenant, "tenor") || !tenant.media.gifs.key) {
if (!supportsMediaType(tenant, "tenor") || !tenant.media?.gifs?.key) {
throw new InternalError("Tenor was not enabled");
}

Expand Down

0 comments on commit 62e6444

Please sign in to comment.