Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CORL-3195]: update tenor search #4679

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 35 additions & 26 deletions server/src/core/server/app/handlers/api/tenor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fetch from "node-fetch";

import { SearchPayload } from "coral-common/common/lib/types/tenor";
import { AppOptions } from "coral-server/app/";
import { WrappedInternalError } from "coral-server/errors";
import { RequestHandler, TenantCoralRequest } from "coral-server/types/express";

const SEARCH_LIMIT = 32;
Expand Down Expand Up @@ -58,7 +59,7 @@ export const tenorSearchHandler =

const result = schema.validate(req.query);
if (result.error || result.errors) {
res.status(400).send(result.errors);
res.status(400).send(result.error ?? result.errors);
return;
}

Expand Down Expand Up @@ -99,33 +100,41 @@ export const tenorSearchHandler =
url.searchParams.set("pos", params.pos);
}

const response = await fetch(url.toString(), {
method: "GET",
});

if (!response.ok) {
res.status(500).send({
results: [],
try {
const response = await fetch(url.toString(), {
method: "GET",
});
return;
}

const json = (await response.json()) as SearchPayload;
if (!json) {
res.status(500).send({
results: [],
if (!response.ok) {
res.status(500).send({
results: [],
});
return;
}
const json = (await response.json()) as SearchPayload;
if (!json) {
res.status(500).send({
results: [],
});
return;
}

res.status(200).send({
results: json.results.map((r) => {
return {
id: r.id,
title: r.title,
url: r.media_formats.gif.url,
preview: r.media_formats.nanogif.url,
};
}),
next: json.next,
});
} 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]");
}
throw new WrappedInternalError(e as Error, "tenor search error");
}

res.status(200).send({
results: json.results.map((r) => {
return {
id: r.id,
title: r.title,
url: r.media_formats.gif.url,
preview: r.media_formats.nanogif.url,
};
}),
next: json.next,
});
};
Loading