The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
GDGT-3137: accept emoji up to 10 code points as a comment reaction (#82652)
Bartosz Rybacki0e838525574f
diff --git a/resources/migrations/065/20260917_comment_reaction_emoji_h2_width.yaml b/resources/migrations/065/20260917_comment_reaction_emoji_h2_width.yamlnew file mode 100644index 00000000000..5f8a88947c7--- /dev/null+++ b/resources/migrations/065/20260917_comment_reaction_emoji_h2_width.yaml@@ -0,0 +1,24 @@+## Widen comment_reaction.emoji on H2 only.+##+## The API allows 1 to 10 Unicode code points in a reaction (metabase.comments.schema/reaction-emoji). Postgres and+## MySQL/MariaDB count varchar(10) in code points, so their column already fits. H2 counts UTF-16 units, and 10 code+## points can take 20 units. varchar(20) on H2 is the same limit in H2's unit.+##+## Do not widen the other engines to match. A plain modifyDataType on MySQL/MariaDB drops the utf8mb4_bin collation+## set by v63.2026-07-06T00:00:00, and distinct emoji collapse into one reaction again.+##+## No rollback: narrowing back to varchar(10) fails once a long reaction is stored, and the wider column does not+## affect older code (same as v62.2026-05-26T00:00:00).++databaseChangeLog:+ - changeSet:+ id: v65.xcewmy+ author: brybacki+ dbms: h2+ comment: Widen comment_reaction.emoji to 20 UTF-16 units on H2, the equivalent of 10 code points elsewhere+ changes:+ - modifyDataType:+ tableName: comment_reaction+ columnName: emoji+ newDataType: varchar(20)+ rollback: []diff --git a/src/metabase/comments/api.clj b/src/metabase/comments/api.cljindex 28ea5ca6e6d..857d7541fbd 100644--- a/src/metabase/comments/api.clj+++ b/src/metabase/comments/api.clj@@ -285,7 +285,7 @@"Toggle a reaction on a comment"[{:keys [comment-id]} :- [:map {:closed true} [:comment-id ms/PositiveInt]]_query-params- {:keys [emoji]} :- [:map {:closed true} [:emoji [:string {:min 1 :max 10}]]]]+ {:keys [emoji]} :- [:map {:closed true} [:emoji ::comments.schema/reaction-emoji]]](let [comment (api/check-404 (comments.db/comment-by-id comment-id))](api/check-400 (not (:deleted_at comment))"Cannot react to deleted comments")diff --git a/src/metabase/comments/schema.clj b/src/metabase/comments/schema.cljindex ba4dd2dfe54..e863939a58c 100644--- a/src/metabase/comments/schema.clj+++ b/src/metabase/comments/schema.clj@@ -4,10 +4,13 @@[metabase.lib.core :as lib][metabase.lib.schema.id :as lib.schema.id][metabase.lib.schema.literal :as lib.schema.literal]+ [metabase.util.i18n :refer [deferred-tru]][metabase.util.malli :as mu][metabase.util.malli.registry :as mr][metabase.util.malli.schema :as ms]))+(set! *warn-on-reflection* true)+(mr/def ::prose-mirror-node.attrs"The `attrs` of a ProseMirror node in a comment: the ones this code reads by name, and whatever else the editor putthere. Every key, declared or not, is a string, so the map never mixes keyword and string keys."@@ -101,6 +104,25 @@[:map {:closed true}[:id ms/PositiveInt]]])+;; The `emoji` of a reaction: a string of 1 to 10 Unicode code points.+;;+;; The limit counts code points, not UTF-16 units, because `comment_reaction.emoji` is `varchar(10)` on Postgres and+;; MySQL, and those count code points.+;;+;; No docstring on purpose: `mr/def` would store it as the `:description`, which is the text a 400 response shows+;; under `:errors`. `with-api-error-message` sets that text instead, localized.+(mr/def ::reaction-emoji+ (mu/with-api-error-message+ [:and+ ;; Documentation only: Malli ignores these when validating. JSON Schema counts maxLength in code points, the same+ ;; unit the :fn below enforces.+ {:json-schema/minLength 1+ :json-schema/maxLength 10}+ :string+ [:fn {:error/message "must be 1 to 10 Unicode code points"}+ (fn [^String s] (<= 1 (.codePointCount s 0 (.length s)) 10))]]+ (deferred-tru "Emoji must be 1 to 10 Unicode code points.")))+(mr/def ::comment-reaction.update"What an update (or insert) of a CommentReaction accepts: every column of `:comment_reaction` except `id`, all optional."[:map {:closed true}diff --git a/test/metabase/comments/api_test.clj b/test/metabase/comments/api_test.cljindex e1326135da5..0e132f49320 100644--- a/test/metabase/comments/api_test.clj+++ b/test/metabase/comments/api_test.clj@@ -11,6 +11,8 @@[metabase.util.performance :refer [dropv]][toucan2.core :as t2]))+(set! *warn-on-reflection* true)+(defn- relaxed-re [& s](re-pattern (str "(?s).*" (str/join ".*" s) ".*")))@@ -346,6 +348,45 @@:target_type "document":target_id doc-id)))))))+(defn- reactions-of+ "The reactions on the Comment with `comment-id` on the Document with `doc-id`, as a set of `{:emoji :count}` maps."+ [doc-id comment-id]+ (let [{:keys [comments]} (mt/user-http-request :rasta :get 200 "comment/"+ :target_type "document" :target_id doc-id)]+ (->> comments (filter #(= comment-id (:id %))) first :reactions+ (map #(select-keys % [:emoji :count])) set)))++(deftest long-emoji-reaction-test+ ;; The limit is 10 Unicode code points, not 10 UTF-16 units: the England flag is 7 code points in 14 units, the+ ;; kiss with skin tones 10 in 15. See ::comments.schema/reaction-emoji.+ (testing "POST /api/comment/:comment-id/reaction accepts emoji longer than 10 UTF-16 units"+ (let [england "\uD83C\uDFF4\uDB40\uDC67\uDB40\uDC62\uDB40\uDC65\uDB40\uDC6E\uDB40\uDC67\uDB40\uDC7F"+ kiss "\uD83D\uDC69\uD83C\uDFFD\u200D\u2764\uFE0F\u200D\uD83D\uDC8B\u200D\uD83D\uDC68\uD83C\uDFFC"]+ (is (= [14 7] [(count england) (.codePointCount england 0 (count england))]))+ (is (= [15 10] [(count kiss) (.codePointCount kiss 0 (count kiss))]))+ (mt/with-temp [:model/Document {doc-id :id} {}+ :model/Comment {comment-id :id} {:target_id doc-id}]+ (mt/with-model-cleanup [:model/CommentReaction]+ (is (=? {:reacted true}+ (mt/user-http-request :rasta :post 200 (str "comment/" comment-id "/reaction") {:emoji england})))+ (is (=? {:reacted true}+ (mt/user-http-request :rasta :post 200 (str "comment/" comment-id "/reaction") {:emoji kiss})))+ (is (= #{{:emoji england :count 1} {:emoji kiss :count 1}}+ (reactions-of doc-id comment-id))))))))++(deftest too-long-emoji-reaction-test+ ;; The family of four with light skin tone is 11 code points in 19 UTF-16 units. It is a valid ZWJ sequence that the+ ;; picker does not offer.+ (testing "POST /api/comment/:comment-id/reaction rejects more than 10 code points"+ (let [family "\uD83D\uDC68\uD83C\uDFFB\u200D\uD83D\uDC69\uD83C\uDFFB\u200D\uD83D\uDC67\uD83C\uDFFB\u200D\uD83D\uDC66\uD83C\uDFFB"]+ (is (= [19 11] [(count family) (.codePointCount family 0 (count family))]))+ (mt/with-temp [:model/Document {doc-id :id} {}+ :model/Comment {comment-id :id} {:target_id doc-id}]+ (is (=? {:errors {:emoji "Emoji must be 1 to 10 Unicode code points."}+ :specific-errors {:emoji [(str "must be 1 to 10 Unicode code points, received: " (pr-str family))]}}+ (mt/user-http-request :rasta :post 400 (str "comment/" comment-id "/reaction")+ {:emoji family})))))))+(deftest multiple-emoji-reactions-test;; Relies on comment_reaction.emoji using an exact (not linguistic) collation on MySQL/MariaDB -- see;; migration v63.2026-07-06T00:00:00. Under the table's original utf8mb4_unicode_ci collation, distinct@@ -357,11 +398,8 @@(mt/with-model-cleanup [:model/CommentReaction](mt/user-http-request :rasta :post 200 (str "comment/" comment-id "/reaction") {:emoji "π"})(mt/user-http-request :rasta :post 200 (str "comment/" comment-id "/reaction") {:emoji "π"})- (let [{:keys [comments]} (mt/user-http-request :rasta :get 200 "comment/"- :target_type "document" :target_id doc-id)- reactions (->> comments (filter #(= comment-id (:id %))) first :reactions- (map #(select-keys % [:emoji :count])) set)]- (is (= #{{:emoji "π" :count 1} {:emoji "π" :count 1}} reactions)))))))+ (is (= #{{:emoji "π" :count 1} {:emoji "π" :count 1}}+ (reactions-of doc-id comment-id)))))))(deftest multi-user-reaction-aggregation-test(testing "reactions from different users on the same emoji aggregate, and one user removing theirs doesn't affect the other's"