Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

api: Use a common rating value for Tenor and GIPHY.

apoorvapendse5ba158a13d82
diff --git a/api_docs/changelog.md b/api_docs/changelog.md
index ed753a7ebb1..0c5c501f07d 100644
--- a/api_docs/changelog.md
+++ b/api_docs/changelog.md
@@ -20,6 +20,17 @@ format used by the Zulip server that they are interacting with.
## Changes in Zulip 12.0
+**Feature level 442**
+
+* [`GET /events`](/api/get-events): `giphy_rating` is now used to denote
+ the common rating configured for both Tenor and GIPHY integrations.
+* [`POST /register`](/api/register-queue): Added new `tenor_api_key`
+ field, which is required to fetch GIFs using the Tenor API.
+* [`POST /register`](/api/register-queue): Renamed
+ `giphy_rating_options` to `gif_rating_options` to generalize the
+ ratings for both GIPHY and Tenor integrations. `realm_giphy_rating`
+ is now used for both the Tenor and GIPHY integrations.
+
**Feature level 441**
* [`GET /users/me/subscriptions`](/api/get-subscriptions),
diff --git a/api_docs/unmerged.d/ZF-35b8e6.md b/api_docs/unmerged.d/ZF-35b8e6.md
deleted file mode 100644
index 33ea97faf02..00000000000
--- a/api_docs/unmerged.d/ZF-35b8e6.md
+++ /dev/null
@@ -1,2 +0,0 @@
-* [`POST /register`](/api/register-queue): Added new `tenor_api_key`
- field, which is required to fetch GIFs using the Tenor API.
diff --git a/version.py b/version.py
index 1df59a82444..6066e80e271 100644
--- a/version.py
+++ b/version.py
@@ -35,7 +35,7 @@ DESKTOP_WARNING_VERSION = "5.9.3"
# new level means in api_docs/changelog.md, as well as "**Changes**"
# entries in the endpoint's documentation in `zulip.yaml`.
-API_FEATURE_LEVEL = 441
+API_FEATURE_LEVEL = 442
# Bump the minor PROVISION_VERSION to indicate that folks should provision
# only when going from an old version of the code to a newer version. Bump
diff --git a/web/src/admin.ts b/web/src/admin.ts
index 80559cac6b4..a12faedac9b 100644
--- a/web/src/admin.ts
+++ b/web/src/admin.ts
@@ -150,8 +150,8 @@ export function build_page(): void {
realm_org_type: realm.realm_org_type,
realm_available_video_chat_providers: realm.realm_available_video_chat_providers,
server_jitsi_server_url: realm.server_jitsi_server_url,
- giphy_rating_options: realm.giphy_rating_options,
- giphy_api_key_empty: realm.giphy_api_key === "",
+ gif_rating_options: realm.gif_rating_options,
+ gif_api_key_empty: realm.giphy_api_key === "" && realm.tenor_api_key === "",
realm_description: realm.realm_description,
realm_inline_image_preview: realm.realm_inline_image_preview,
server_inline_image_preview: realm.server_inline_image_preview,
diff --git a/web/src/gif_state.ts b/web/src/gif_state.ts
index c936d556b13..dd1a8217278 100644
--- a/web/src/gif_state.ts
+++ b/web/src/gif_state.ts
@@ -1,12 +1,45 @@
+import $ from "jquery";
+
+import * as blueslip from "./blueslip.ts";
import {realm} from "./state_data.ts";
+type GifRating = "pg" | "pg-13" | "r" | "g";
+
export function is_tenor_enabled(): boolean {
- return realm.tenor_api_key !== "";
+ return (
+ realm.tenor_api_key !== "" &&
+ realm.realm_giphy_rating !== realm.gif_rating_options.disabled.id
+ );
}
export function is_giphy_enabled(): boolean {
return (
realm.giphy_api_key !== "" &&
- realm.realm_giphy_rating !== realm.giphy_rating_options.disabled.id
+ realm.realm_giphy_rating !== realm.gif_rating_options.disabled.id
);
}
+
+export function get_rating(): GifRating {
+ const options = realm.gif_rating_options;
+ for (const rating of ["pg", "g", "pg-13", "r"] as const) {
+ if (options[rating]?.id === realm.realm_giphy_rating) {
+ return rating;
+ }
+ }
+
+ // The below should never run unless a server bug allowed a
+ // `gif_rating` value not present in `gif_rating_options`.
+ blueslip.error("Invalid gif_rating value: " + realm.realm_giphy_rating);
+ return "g";
+}
+
+export function update_gif_rating(): void {
+ // Updating the GIF ratings would only result in us showing/hiding
+ // the currently set GIF icon.
+ // It won't change the GIF provider without a server restart as of now.
+ if (realm.realm_giphy_rating === realm.gif_rating_options.disabled.id) {
+ $(".zulip-icon-gif").hide();
+ } else {
+ $(".zulip-icon-gif").show();
+ }
+}
diff --git a/web/src/giphy.ts b/web/src/giphy.ts
index 9a5c667a705..a0eb078c955 100644
--- a/web/src/giphy.ts
+++ b/web/src/giphy.ts
@@ -1,4 +1,4 @@
-import type {GifsResult, GiphyFetch, Rating} from "@giphy/js-fetch-api";
+import type {GifsResult, GiphyFetch} from "@giphy/js-fetch-api";
import type {IGif} from "@giphy/js-types";
import $ from "jquery";
import _ from "lodash";
@@ -7,8 +7,8 @@ import type * as tippy from "tippy.js";
import render_gif_picker_ui from "../templates/gif_picker_ui.hbs";
-import * as blueslip from "./blueslip.ts";
import * as compose_ui from "./compose_ui.ts";
+import * as gif_state from "./gif_state.ts";
import * as popover_menus from "./popover_menus.ts";
import * as rows from "./rows.ts";
import {realm} from "./state_data.ts";
@@ -32,31 +32,6 @@ export function focus_current_edit_message(): void {
$(`#edit_form_${CSS.escape(`${edit_message_id}`)} .message_edit_content`).trigger("focus");
}
-export function update_giphy_rating(): void {
- if (
- realm.realm_giphy_rating === realm.giphy_rating_options.disabled.id ||
- realm.giphy_api_key === ""
- ) {
- $(".compose-gif-icon-giphy").hide();
- } else {
- $(".compose-gif-icon-giphy").show();
- }
-}
-
-function get_rating(): Rating {
- const options = realm.giphy_rating_options;
- for (const rating of ["pg", "g", "pg-13", "r"] as const) {
- if (options[rating]?.id === realm.realm_giphy_rating) {
- return rating;
- }
- }
-
- // The below should never run unless a server bug allowed a
- // `giphy_rating` value not present in `giphy_rating_options`.
- blueslip.error("Invalid giphy_rating value: " + realm.realm_giphy_rating);
- return "g";
-}
-
async function renderGIPHYGrid(targetEl: HTMLElement): Promise<{remove: () => void}> {
const {renderGrid} = await import(/* webpackChunkName: "giphy-sdk" */ "@giphy/js-components");
const {GiphyFetch} = await import(/* webpackChunkName: "giphy-sdk" */ "@giphy/js-fetch-api");
@@ -68,7 +43,7 @@ async function renderGIPHYGrid(targetEl: HTMLElement): Promise<{remove: () => vo
const config = {
offset,
limit: 25,
- rating: get_rating(),
+ rating: gif_state.get_rating(),
// We don't pass random_id here, for privacy reasons.
};
if (search_term === "") {
diff --git a/web/src/server_events_dispatch.js b/web/src/server_events_dispatch.js
index e6ae4fb17f3..38c6ad219c3 100644
--- a/web/src/server_events_dispatch.js
+++ b/web/src/server_events_dispatch.js
@@ -21,7 +21,7 @@ import {electron_bridge} from "./electron_bridge.ts";
import * as emoji from "./emoji.ts";
import * as emoji_picker from "./emoji_picker.ts";
import * as gear_menu from "./gear_menu.ts";
-import * as giphy from "./giphy.ts";
+import * as gif_state from "./gif_state.ts";
import * as inbox_ui from "./inbox_ui.ts";
import * as information_density from "./information_density.ts";
import * as left_sidebar_navigation_area from "./left_sidebar_navigation_area.ts";
@@ -330,7 +330,7 @@ export function dispatch_normal_event(event) {
emails_restricted_to_domains: noop,
video_chat_provider: compose_call_ui.update_audio_and_video_chat_button_display,
jitsi_server_url: compose_call_ui.update_audio_and_video_chat_button_display,
- giphy_rating: giphy.update_giphy_rating,
+ giphy_rating: gif_state.update_gif_rating,
waiting_period_threshold: noop,
want_advertise_in_communities_directory: noop,
welcome_message_custom_text: noop,
diff --git a/web/src/settings_org.ts b/web/src/settings_org.ts
index ea873b1c1a0..35ad9937387 100644
--- a/web/src/settings_org.ts
+++ b/web/src/settings_org.ts
@@ -238,7 +238,7 @@ function set_video_chat_provider_dropdown(): void {
set_jitsi_server_url_dropdown();
}
-function set_giphy_rating_dropdown(): void {
+function set_gif_rating_dropdown(): void {
const rating_id = realm.realm_giphy_rating;
$("#id_realm_giphy_rating").val(rating_id);
}
@@ -1466,7 +1466,7 @@ export function build_page(): void {
set_realm_waiting_period_setting();
set_video_chat_provider_dropdown();
- set_giphy_rating_dropdown();
+ set_gif_rating_dropdown();
set_msg_edit_limit_dropdown();
set_msg_move_limit_setting("realm_move_messages_within_stream_limit_seconds");
set_msg_move_limit_setting("realm_move_messages_between_streams_limit_seconds");
diff --git a/web/src/state_data.ts b/web/src/state_data.ts
index efe4ca5a4a2..7079814e677 100644
--- a/web/src/state_data.ts
+++ b/web/src/state_data.ts
@@ -406,7 +406,7 @@ export const realm_schema = z.object({
custom_profile_field_types: custom_profile_field_types_schema,
demo_organization_scheduled_deletion_date: z.optional(z.number()),
giphy_api_key: z.string(),
- giphy_rating_options: z.intersection(
+ gif_rating_options: z.intersection(
z.record(z.string(), z.object({id: z.number(), name: z.string()})),
z.object({disabled: z.object({id: z.number(), name: z.string()})}),
),
diff --git a/web/src/tenor.ts b/web/src/tenor.ts
index efb7ac07454..d185352876a 100644
--- a/web/src/tenor.ts
+++ b/web/src/tenor.ts
@@ -9,6 +9,7 @@ import render_tenor_gif from "../templates/tenor_gif.hbs";
import * as channel from "./channel.ts";
import * as compose_ui from "./compose_ui.ts";
+import {get_rating} from "./gif_state.ts";
import * as popover_menus from "./popover_menus.ts";
import * as rows from "./rows.ts";
import * as scroll_util from "./scroll_util.ts";
@@ -16,6 +17,14 @@ import {realm} from "./state_data.ts";
import * as ui_util from "./ui_util.ts";
import {user_settings} from "./user_settings.ts";
+const tenor_rating_map = {
+ // Source: https://developers.google.com/tenor/guides/content-filtering#ContentFilter-options
+ pg: "medium",
+ g: "high",
+ r: "off",
+ "pg-13": "low",
+};
+
const tenor_result_schema = z.object({
results: z.array(
z.object({
@@ -62,11 +71,7 @@ function get_base_payload(): TenorPayload {
// for what gets actually uploaded.
media_filter: "tinygif,mediumgif",
locale: user_settings.default_language,
- // TODO: This must be configurable, preferably via admin settings
- // just like GIPHY. For that we would first have to figure out
- // how to map the GIPHY and Tenor ratings, since they don't have
- // a 1:1 alignment based on the current `realm_giphy_rating` configuration.
- contentfilter: "high",
+ contentfilter: tenor_rating_map[get_rating()],
};
}
diff --git a/web/templates/settings/organization_settings_admin.hbs b/web/templates/settings/organization_settings_admin.hbs
index 02feabc35c8..b2ac1daa1ca 100644
--- a/web/templates/settings/organization_settings_admin.hbs
+++ b/web/templates/settings/organization_settings_admin.hbs
@@ -170,11 +170,11 @@
</div>
<div class="input-group">
<label for="id_realm_giphy_rating" class="settings-field-label">
- {{t 'GIPHY integration' }}
+ {{t 'GIF integration' }}
{{> ../help_link_widget link=giphy_help_link }}
</label>
- <select name="realm_giphy_rating" class ="setting-widget prop-element settings_select bootstrap-focus-style" id="id_realm_giphy_rating" data-setting-widget-type="number" {{#if giphy_api_key_empty}}disabled{{/if}}>
- {{#each giphy_rating_options}}
+ <select name="realm_giphy_rating" class ="setting-widget prop-element settings_select bootstrap-focus-style" id="id_realm_giphy_rating" data-setting-widget-type="number" {{#if gif_api_key_empty}}disabled{{/if}}>
+ {{#each gif_rating_options}}
<option value='{{this.id}}'>{{this.name}}</option>
{{/each}}
</select>
diff --git a/web/tests/lib/example_realm.cjs b/web/tests/lib/example_realm.cjs
index 0f4349ce57a..769134d7b27 100644
--- a/web/tests/lib/example_realm.cjs
+++ b/web/tests/lib/example_realm.cjs
@@ -7,7 +7,7 @@ exports.make_realm = (opts = {}) => {
custom_profile_fields: [],
custom_profile_field_types: [],
giphy_api_key: "giphy-api-key",
- giphy_rating_options: {disabled: {id: 0, name: ""}},
+ gif_rating_options: {disabled: {id: 0, name: ""}},
max_avatar_file_size_mib: 0,
max_channel_folder_description_length: 0,
max_channel_folder_name_length: 0,
diff --git a/zerver/lib/events.py b/zerver/lib/events.py
index 00799a349d6..a972c9a396f 100644
--- a/zerver/lib/events.py
+++ b/zerver/lib/events.py
@@ -395,7 +395,7 @@ def fetch_initial_state_data(
#
# Other settings, which are just server-level settings or data
# about the version of Zulip, can be named without prefixes,
- # e.g. giphy_rating_options or development_environment.
+ # e.g. gif_rating_options or development_environment.
for property_name in Realm.property_types:
state["realm_" + property_name] = getattr(realm, property_name)
@@ -522,7 +522,7 @@ def fetch_initial_state_data(
state["server_avatar_changes_disabled"] = settings.AVATAR_CHANGES_DISABLED
state["server_name_changes_disabled"] = settings.NAME_CHANGES_DISABLED
state["server_web_public_streams_enabled"] = settings.WEB_PUBLIC_STREAMS_ENABLED
- state["giphy_rating_options"] = realm.get_giphy_rating_options()
+ state["gif_rating_options"] = realm.get_gif_rating_options()
state["server_emoji_data_url"] = emoji.data_url()
diff --git a/zerver/models/realms.py b/zerver/models/realms.py
index 8e3ea8f8daa..79e88b7071c 100644
--- a/zerver/models/realms.py
+++ b/zerver/models/realms.py
@@ -663,13 +663,15 @@ class Realm(models.Model):
JITSI_SERVER_SPECIAL_VALUES_MAP = {"default": None}
jitsi_server_url = models.URLField(null=True, default=None)
- # Please access this via get_giphy_rating_options.
- GIPHY_RATING_OPTIONS = {
+ # Please access this via get_gif_rating_options.
+ GIF_RATING_OPTIONS = {
"disabled": {
- "name": gettext_lazy("GIPHY integration disabled"),
+ "name": gettext_lazy("GIF integration disabled"),
"id": 0,
},
- # Source: https://developers.giphy.com/docs/optional-settings/#rating
+ # Source:
+ # 1. https://developers.giphy.com/docs/optional-settings/#rating
+ # 2. https://developers.google.com/tenor/guides/content-filtering#ContentFilter-options
"g": {
"name": gettext_lazy("Allow GIFs rated G (General audience)"),
"id": 1,
@@ -688,8 +690,9 @@ class Realm(models.Model):
},
}
- # maximum rating of the GIFs that will be retrieved from GIPHY
- giphy_rating = models.PositiveSmallIntegerField(default=GIPHY_RATING_OPTIONS["g"]["id"])
+ # maximum rating of the GIFs that will be retrieved.
+ # This is now used as a common rating for both Tenor and GIPHY.
+ giphy_rating = models.PositiveSmallIntegerField(default=GIF_RATING_OPTIONS["g"]["id"])
default_code_block_language = models.TextField(default="")
@@ -920,12 +923,12 @@ class Realm(models.Model):
def __str__(self) -> str:
return f"{self.string_id} {self.id}"
- def get_giphy_rating_options(self) -> dict[str, dict[str, object]]:
- """Wrapper function for GIPHY_RATING_OPTIONS that ensures evaluation
+ def get_gif_rating_options(self) -> dict[str, dict[str, object]]:
+ """Wrapper function for GIF_RATING_OPTIONS that ensures evaluation
of the lazily evaluated `name` field without modifying the original."""
return {
rating_type: {"name": str(rating["name"]), "id": rating["id"]}
- for rating_type, rating in self.GIPHY_RATING_OPTIONS.items()
+ for rating_type, rating in self.GIF_RATING_OPTIONS.items()
}
def authentication_methods_dict(self) -> dict[str, bool]:
diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml
index dfe026b7310..d1a7a3bf403 100644
--- a/zerver/openapi/zulip.yaml
+++ b/zerver/openapi/zulip.yaml
@@ -5081,9 +5081,13 @@ paths:
giphy_rating:
type: integer
description: |
- Maximum rating of the GIFs that will be retrieved from GIPHY.
+ Maximum rating of the GIFs that will be retrieved by the
+ GIPHY and Tenor integrations in this organization.
- **Changes**: New in Zulip 4.0 (feature level 55).
+ **Changes**: In Zulip 12.0 (feature level 442), is now used for
+ both the Tenor and GIPHY integrations.
+
+ New in Zulip 4.0 (feature level 55).
icon_source:
type: string
description: |
@@ -18311,7 +18315,7 @@ paths:
Developers of clients should also read the
[Tenor API TOS](https://developers.google.com/tenor/guides/api-terms).
- **Changes**: Added in Zulip 12.0 (feature level ZF-35b8e6).
+ **Changes**: New in Zulip 12.0 (feature level 442).
push_devices:
type: object
description: |
@@ -19096,9 +19100,13 @@ paths:
description: |
Present if `realm` is present in `fetch_event_types`.
- The configured GIPHY rating for the organization.
+ Maximum rating of the GIFs that will be retrieved by the
+ GIPHY and Tenor integrations in this organization.
+
+ **Changes**: In Zulip 12.0 (feature level 442), is now used for
+ both the Tenor and GIPHY integrations.
- **Changes**: New in Zulip 4.0 (feature level 55).
+ New in Zulip 4.0 (feature level 55).
realm_waiting_period_threshold:
type: integer
description: |
@@ -19692,17 +19700,23 @@ paths:
This Zulip server's configured minimum `zxcvbn` minimum guesses.
Necessary for password change UI to show whether the password
will be accepted.
- giphy_rating_options:
+ gif_rating_options:
type: object
description: |
Present if `realm` is present in `fetch_event_types`.
- Dictionary where each entry describes a valid rating
- that is configured on this server and could be selected by an
+ Dictionary where each entry describes a valid rating configuration
+ that is available on this server and could be selected by an
organization administrator.
Useful for administrative settings UI that allows changing the
allowed rating of GIFs.
+
+ **Changes**: New in Zulip 12.0 (feature level 442). Previously,
+ this was called `giphy_rating_options` and only supported the
+ original GIPHY gif picker integration.
+
+ `giphy_rating_options` was new in Zulip 4.0 (feature level 55).
additionalProperties:
description: |
`{rating_name}`: Dictionary containing the details of the
diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py
index 87bff75395c..fd14daaf144 100644
--- a/zerver/tests/test_events.py
+++ b/zerver/tests/test_events.py
@@ -4400,7 +4400,7 @@ class RealmPropertyActionTest(BaseAction):
],
jitsi_server_url=["https://jitsi1.example.com", "https://jitsi2.example.com", None],
giphy_rating=[
- Realm.GIPHY_RATING_OPTIONS["disabled"]["id"],
+ Realm.GIF_RATING_OPTIONS["disabled"]["id"],
],
default_code_block_language=["python", "javascript"],
message_content_delete_limit_seconds=[1000, 1100, 1200, None],
diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.py
index 6322aa8146f..d4e04c4057d 100644
--- a/zerver/tests/test_home.py
+++ b/zerver/tests/test_home.py
@@ -88,7 +88,7 @@ class HomeTest(ZulipTestCase):
"full_name",
"giphy_api_key",
"tenor_api_key",
- "giphy_rating_options",
+ "gif_rating_options",
"has_zoom_token",
"is_admin",
"is_guest",
diff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.py
index 56e40fc8fa8..a0aefb275a3 100644
--- a/zerver/tests/test_realm.py
+++ b/zerver/tests/test_realm.py
@@ -2081,8 +2081,8 @@ class RealmAPITest(ZulipTestCase):
],
jitsi_server_url=["https://example.jit.si"],
giphy_rating=[
- Realm.GIPHY_RATING_OPTIONS["g"]["id"],
- Realm.GIPHY_RATING_OPTIONS["r"]["id"],
+ Realm.GIF_RATING_OPTIONS["g"]["id"],
+ Realm.GIF_RATING_OPTIONS["r"]["id"],
],
message_content_delete_limit_seconds=[1000, 1100, 1200],
message_content_edit_limit_seconds=[1000, 1100, 1200],
diff --git a/zerver/views/realm.py b/zerver/views/realm.py
index 2dba4a19ef7..2a4d0d8e130 100644
--- a/zerver/views/realm.py
+++ b/zerver/views/realm.py
@@ -239,7 +239,7 @@ def update_realm(
)
)
if giphy_rating is not None and giphy_rating not in {
- p["id"] for p in Realm.GIPHY_RATING_OPTIONS.values()
+ p["id"] for p in Realm.GIF_RATING_OPTIONS.values()
}:
raise JsonableError(
_("Invalid giphy_rating {giphy_rating}").format(giphy_rating=giphy_rating)