Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
import assert from "minimalistic-assert";
import * as z from "zod/mini";
import {
type GifInfoUrl,
GifNetwork,
type GifProvider,
type RenderGifsCallback,
} from "./abstract_gif_network.ts";
import * as channel from "./channel.ts";
import {get_rating} from "./gif_state.ts";
import {realm} from "./state_data.ts";
import {user_settings} from "./user_settings.ts";
const BASE_URL = "https://api.klipy.com/v2";
const klipy_rating_map = {
// Source: https://docs.klipy.com/migrate-from-tenor/content-filtering
pg: "medium",
g: "high",
r: "off",
"pg-13": "low",
};
const klipy_result_schema = z.object({
results: z.array(
z.object({
media_formats: z.object({
tinygif: z.object({
url: z.url(),
}),
mediumgif: z.object({
url: z.url(),
}),
}),
}),
),
// This denotes the identifier to use for the next API call
// to fetch the next set of results for the current query.
next: z.string(),
});
export type KlipyPayload = {
key: string;
limit: string;
media_filter: string;
locale: string;
contentfilter: string;
pos?: string | number | undefined;
q?: string;
};
export class KlipyNetwork extends GifNetwork {
is_loading_more = false;
next_pos_identifier: string | number | undefined;
abandoned = false;
get_provider(): GifProvider {
return "klipy";
}
is_loading_more_gifs(): boolean {
return this.is_loading_more;
}
abandon(): void {
this.abandoned = true;
}
ask_for_default_gifs(next_page: boolean, render_gifs_callback: RenderGifsCallback): void {
assert(!this.abandoned);
const data = get_base_payload();
this.ask_for_gifs(`${BASE_URL}/featured`, data, next_page, render_gifs_callback);
}
ask_for_search_gifs(
search_term: string,
next_page: boolean,
render_gifs_callback: RenderGifsCallback,
): void {
assert(!this.abandoned);
const data: KlipyPayload = {
q: search_term,
...get_base_payload(),
};
this.ask_for_gifs(`${BASE_URL}/search`, data, next_page, render_gifs_callback);
}
ask_for_gifs(
url: string,
data: KlipyPayload,
next_page = false,
render_gifs_callback: RenderGifsCallback,
): void {
assert(!this.abandoned);
if (next_page) {
this.is_loading_more = true;
data = {...data, pos: this.next_pos_identifier};
}
void channel.get({
url,
data,
success: (raw_klipy_result) => {
// We don't want to have this code run after the caller
// abandons its network object, to avoid all sorts of weird
// bugs.
if (this.abandoned) {
return;
}
const parsed_data = klipy_result_schema.parse(raw_klipy_result);
const urls: GifInfoUrl[] = parsed_data.results.map((result) => ({
preview_url: result.media_formats.tinygif.url,
insert_url: result.media_formats.mediumgif.url,
}));
this.next_pos_identifier = parsed_data.next;
this.is_loading_more = false;
render_gifs_callback(urls, next_page);
},
});
}
}
// Klipy expects locale in xx_YY format (ISO 639-1 language code +
// ISO 3166-1 country code, e.g. en_US), but user_settings.default_language
// uses Django's xx-yy format (e.g. en-gb).
function to_klipy_locale(language: string): string {
const locale = new Intl.Locale(language).maximize();
return `${locale.language}_${locale.region}`;
}
function get_base_payload(): KlipyPayload {
return {
key: realm.klipy_api_key,
limit: "15",
// We use the tinygif size for the picker UI, and the mediumgif size
// for what gets actually uploaded.
media_filter: "tinygif,mediumgif",
locale: to_klipy_locale(user_settings.default_language),
contentfilter: klipy_rating_map[get_rating()],
};
}