Skip to content

A platform for community discussion. Free, open, simple.

UX: Wrap the live reactions picker and allow any emoji when the site does (#45231)

Rafael dos Santos Silvaf34710d1e1be
diff --git a/plugins/live/app/controllers/live/rooms_controller.rb b/plugins/live/app/controllers/live/rooms_controller.rb
index e3cb88f37c6..52d01f20971 100644
--- a/plugins/live/app/controllers/live/rooms_controller.rb
+++ b/plugins/live/app/controllers/live/rooms_controller.rb
@@ -639,11 +639,12 @@ module Live
guardian.ensure_can_join_live_room!(@room)
ensure_participant_session!
+ # Before validation, which may look the emoji up in shared caches.
+ RateLimiter.new(current_user, "live-reactions", REACTIONS_PER_USER, 10.seconds).performed!
+
emoji = params.require(:emoji)
raise Discourse::InvalidParameters.new(:emoji) if !Live::Reactions.valid?(emoji)
- RateLimiter.new(current_user, "live-reactions", REACTIONS_PER_USER, 10.seconds).performed!
-
Live::RoomBroadcaster.publish_reaction(@room, current_user, emoji)
head :no_content
diff --git a/plugins/live/assets/javascripts/discourse/components/live/reaction-picker.gjs b/plugins/live/assets/javascripts/discourse/components/live/reaction-picker.gjs
index f971a0a5079..86fdfdfe484 100644
--- a/plugins/live/assets/javascripts/discourse/components/live/reaction-picker.gjs
+++ b/plugins/live/assets/javascripts/discourse/components/live/reaction-picker.gjs
@@ -2,34 +2,77 @@ import Component from "@glimmer/component";
import { fn } from "@ember/helper";
import { action } from "@ember/object";
import { service } from "@ember/service";
+import { trustHTML } from "@ember/template";
+import EmojiPickerDetached from "discourse/components/emoji-picker/detached";
import DMenu from "discourse/float-kit/components/d-menu";
import DButton from "discourse/ui-kit/d-button";
import dEmoji from "discourse/ui-kit/helpers/d-emoji";
import { i18n } from "discourse-i18n";
+import { reactionGridColumns } from "../../lib/live/reaction-grid.js";
export default class LiveReactionPicker extends Component {
+ @service capabilities;
@service liveReactions;
+ @service menu;
@service site;
@service siteSettings;
+ #menuApi = null;
+
get emojis() {
return this.siteSettings.enable_emoji
? (this.site.live_reaction_emojis ?? [])
: [];
}
+ get allowAnyEmoji() {
+ return !!this.site.live_reactions_allow_any_emoji;
+ }
+
+ get gridStyle() {
+ const cells = this.emojis.length + (this.allowAnyEmoji ? 1 : 0);
+ const columns = reactionGridColumns(cells, {
+ wide: this.capabilities.viewport.sm,
+ });
+ return trustHTML(`--live-reaction-columns: ${columns}`);
+ }
+
get title() {
return this.liveReactions.rateLimited
? i18n("live.reactions.rate_limited")
: i18n("live.reactions.title");
}
+ @action
+ registerMenu(api) {
+ this.#menuApi = api;
+ }
+
// The menu stays open so a burst of applause doesn't need a reopen per clap.
@action
react(emoji) {
this.liveReactions.send(this.args.room.id, emoji);
}
+ // Swaps our menu for the full picker rather than nesting it: a click inside
+ // a nested float counts as outside ours, which would tear the picker down.
+ @action
+ async openEmojiPicker() {
+ const trigger = this.#menuApi?.trigger;
+ await this.#menuApi?.close();
+
+ await this.menu.show(trigger, {
+ identifier: "emoji-picker",
+ groupIdentifier: "emoji-picker",
+ component: EmojiPickerDetached,
+ modalForMobile: true,
+ data: {
+ context: "live-reactions",
+ didSelectEmoji: this.react,
+ },
+ });
+ }
+
<template>
{{#if this.emojis.length}}
<DMenu
@@ -37,12 +80,13 @@ export default class LiveReactionPicker extends Component {
@disabled={{this.liveReactions.rateLimited}}
@icon="far-face-smile"
@identifier="live-reaction-menu"
+ @onRegisterApi={{this.registerMenu}}
@placement="top"
@title={{this.title}}
@triggerClass="btn-default live-reaction-picker__trigger"
>
<:content>
- <div class="live-reaction-picker">
+ <div class="live-reaction-picker" style={{this.gridStyle}}>
{{#each this.emojis as |emoji|}}
<DButton
class="btn-transparent live-reaction-picker__emoji"
@@ -53,6 +97,14 @@ export default class LiveReactionPicker extends Component {
{{dEmoji emoji}}
</DButton>
{{/each}}
+ {{#if this.allowAnyEmoji}}
+ <DButton
+ class="btn-transparent live-reaction-picker__more"
+ @action={{this.openEmojiPicker}}
+ @icon="discourse-emojis"
+ @title="live.reactions.more"
+ />
+ {{/if}}
</div>
</:content>
</DMenu>
diff --git a/plugins/live/assets/javascripts/discourse/lib/live/reaction-grid.js b/plugins/live/assets/javascripts/discourse/lib/live/reaction-grid.js
new file mode 100644
index 00000000000..618809e29e7
--- /dev/null
+++ b/plugins/live/assets/javascripts/discourse/lib/live/reaction-grid.js
@@ -0,0 +1,22 @@
+// Mirrors the post reactions picker so both lay a given set out the same way:
+// below five cells it's a single row, otherwise it wraps at 5 to 7 columns
+// (8 on wider screens), preferring a full or fuller last row.
+export function reactionGridColumns(count, { wide = false } = {}) {
+ const candidates = wide ? [5, 6, 7, 8] : [5, 6, 7];
+
+ if (count < candidates[0]) {
+ return count;
+ }
+
+ let columns = candidates[0];
+ for (const candidate of candidates) {
+ const rest = count % candidate;
+ if (rest === 0) {
+ return candidate;
+ }
+ if (rest > count % (candidate - 1)) {
+ columns = candidate;
+ }
+ }
+ return columns;
+}
diff --git a/plugins/live/assets/stylesheets/common/live-room-page.scss b/plugins/live/assets/stylesheets/common/live-room-page.scss
index 2e745d2460a..f2ef675c475 100644
--- a/plugins/live/assets/stylesheets/common/live-room-page.scss
+++ b/plugins/live/assets/stylesheets/common/live-room-page.scss
@@ -766,13 +766,22 @@ body.has-sidebar-page #main-outlet-wrapper:has(.live-room-header) {
}
.live-reaction-picker {
- display: flex;
+ display: grid;
+ grid-template-columns: repeat(var(--live-reaction-columns, 7), auto);
gap: var(--space-1);
padding: var(--space-1);
- &__emoji {
+ &__emoji,
+ &__more {
padding: var(--space-1);
+ }
+ &__more .d-icon {
+ width: 1.75rem;
+ height: 1.75rem;
+ }
+
+ &__emoji {
.emoji {
width: 1.75rem;
height: 1.75rem;
diff --git a/plugins/live/config/locales/client.en.yml b/plugins/live/config/locales/client.en.yml
index 90710c00f49..b6a3129ba74 100644
--- a/plugins/live/config/locales/client.en.yml
+++ b/plugins/live/config/locales/client.en.yml
@@ -16,6 +16,7 @@ en:
reactions:
title: "Send a reaction"
rate_limited: "You're sending reactions too quickly. Try again in a moment."
+ more: "React with any emoji"
room:
join: "Join live room"
leave: "Leave room"
diff --git a/plugins/live/lib/live/reactions.rb b/plugins/live/lib/live/reactions.rb
index ad54c76201a..32257614b0e 100644
--- a/plugins/live/lib/live/reactions.rb
+++ b/plugins/live/lib/live/reactions.rb
@@ -8,6 +8,10 @@ module Live
ENABLED_SETTING = :discourse_reactions_enabled
LIST_SETTING = :discourse_reactions_enabled_reactions
MAIN_SETTING = :discourse_reactions_reaction_for_like
+ ANY_EMOJI_SETTING = :discourse_reactions_allow_any_emoji
+
+ # The characters custom emoji names are sanitized to, plus a skin tone.
+ SHORTCODE = /\A[a-z0-9_+\-]{1,100}(?::t[1-6])?\z/
# Used only when the post reactions settings aren't registered at all.
FALLBACK = %w[heart +1 laughing open_mouth clap confetti_ball hugs].freeze
@@ -15,18 +19,25 @@ module Live
def self.emojis
return FALLBACK if !SiteSetting.has_setting?(LIST_SETTING)
- read =
- if SiteSetting.get(ENABLED_SETTING)
- ->(name) { SiteSetting.get(name) }
- else
- ->(name) { SiteSetting.defaults[name] }
- end
+ [setting(MAIN_SETTING), *setting(LIST_SETTING).to_s.split("|")].compact_blank.uniq
+ end
- [read.(MAIN_SETTING), *read.(LIST_SETTING).to_s.split("|")].compact_blank.uniq
+ def self.allow_any_emoji?
+ SiteSetting.has_setting?(ANY_EMOJI_SETTING) && !!setting(ANY_EMOJI_SETTING)
end
def self.valid?(emoji)
- emojis.include?(emoji)
+ return false if !emoji.is_a?(String)
+ return true if emojis.include?(emoji)
+
+ # Lookups key the shared emoji caches, misses included, so only
+ # shortcode-shaped names may reach them.
+ allow_any_emoji? && emoji.match?(SHORTCODE) && Emoji.exists?(emoji)
+ end
+
+ def self.setting(name)
+ SiteSetting.get(ENABLED_SETTING) ? SiteSetting.get(name) : SiteSetting.defaults[name]
end
+ private_class_method :setting
end
end
diff --git a/plugins/live/plugin.rb b/plugins/live/plugin.rb
index 7c3ae8f69bf..66253c161e1 100644
--- a/plugins/live/plugin.rb
+++ b/plugins/live/plugin.rb
@@ -176,6 +176,7 @@ after_initialize do
end
add_to_serializer(:site, :live_reaction_emojis) { Live::Reactions.emojis }
+ add_to_serializer(:site, :live_reactions_allow_any_emoji) { Live::Reactions.allow_any_emoji? }
Live::DefaultRoomSeeder.ensure! if SiteSetting.live_enabled?
Live::AgentBot.ensure! if SiteSetting.live_enabled?
diff --git a/plugins/live/spec/lib/live/reactions_spec.rb b/plugins/live/spec/lib/live/reactions_spec.rb
index dc10555be0d..d0441743130 100644
--- a/plugins/live/spec/lib/live/reactions_spec.rb
+++ b/plugins/live/spec/lib/live/reactions_spec.rb
@@ -23,4 +23,44 @@ RSpec.describe Live::Reactions do
)
end
end
+
+ describe ".valid?" do
+ before do
+ SiteSetting.discourse_reactions_enabled = true
+ SiteSetting.discourse_reactions_reaction_for_like = "heart"
+ SiteSetting.discourse_reactions_enabled_reactions = "clap"
+ end
+
+ it "accepts only the reaction set when any emoji isn't allowed" do
+ SiteSetting.discourse_reactions_allow_any_emoji = false
+
+ expect(%w[heart clap rocket].map { described_class.valid?(_1) }).to eq([true, true, false])
+ end
+
+ it "accepts any existing emoji, toned ones included, when any emoji is allowed" do
+ SiteSetting.discourse_reactions_allow_any_emoji = true
+
+ expect(%w[rocket clap:t3 not_an_emoji].map { described_class.valid?(_1) }).to eq(
+ [true, true, false],
+ )
+ end
+
+ it "rejects non-strings and names that aren't shortcode-shaped when any emoji is allowed" do
+ SiteSetting.discourse_reactions_allow_any_emoji = true
+
+ expect(
+ [["rocket"], { "a" => "b" }, "Rocket", "rocket!", "a" * 101].map do
+ described_class.valid?(_1)
+ end,
+ ).to all(eq(false))
+ end
+
+ it "ignores the any emoji setting while post reactions are disabled" do
+ SiteSetting.discourse_reactions_allow_any_emoji = true
+ SiteSetting.discourse_reactions_enabled = false
+
+ expect(described_class.allow_any_emoji?).to eq(false)
+ expect(described_class.valid?("rocket")).to eq(false)
+ end
+ end
end
diff --git a/plugins/live/spec/requests/live/rooms_controller_reactions_spec.rb b/plugins/live/spec/requests/live/rooms_controller_reactions_spec.rb
index feb7630b86b..c2473ae807c 100644
--- a/plugins/live/spec/requests/live/rooms_controller_reactions_spec.rb
+++ b/plugins/live/spec/requests/live/rooms_controller_reactions_spec.rb
@@ -74,6 +74,41 @@ RSpec.describe Live::RoomsController do
expect(messages).to be_empty
end
+ it "returns 400 for a non-string emoji when any emoji is allowed" do
+ SiteSetting.discourse_reactions_allow_any_emoji = true
+ sign_in(participant)
+
+ post "/live/rooms/#{room.id}/reaction.json",
+ params: {
+ emoji: ["rocket"],
+ participant_session_id: @session_id,
+ }
+
+ expect(response.status).to eq(400)
+ end
+
+ it "counts rejected emoji against the rate limit" do
+ RateLimiter.enable
+ sign_in(participant)
+
+ Live::RoomsController::REACTIONS_PER_USER.times do
+ post "/live/rooms/#{room.id}/reaction.json",
+ params: {
+ emoji: "poop",
+ participant_session_id: @session_id,
+ }
+ end
+ expect(response.status).to eq(400)
+
+ post "/live/rooms/#{room.id}/reaction.json",
+ params: {
+ emoji: "poop",
+ participant_session_id: @session_id,
+ }
+
+ expect(response.status).to eq(429)
+ end
+
it "returns 403 for a user who hasn't joined the call" do
sign_in(outsider)
diff --git a/plugins/live/test/javascripts/unit/lib/reaction-grid-test.js b/plugins/live/test/javascripts/unit/lib/reaction-grid-test.js
new file mode 100644
index 00000000000..2c2eaef65a5
--- /dev/null
+++ b/plugins/live/test/javascripts/unit/lib/reaction-grid-test.js
@@ -0,0 +1,27 @@
+import { module, test } from "qunit";
+import { reactionGridColumns } from "discourse/plugins/live/discourse/lib/live/reaction-grid";
+
+module("Live | Unit | Lib | reaction-grid", function () {
+ test("keeps a short set on one row", function (assert) {
+ assert.strictEqual(reactionGridColumns(3), 3);
+ assert.strictEqual(reactionGridColumns(4), 4);
+ });
+
+ test("wraps at a column count that fills the rows evenly", function (assert) {
+ assert.strictEqual(reactionGridColumns(10), 5);
+ assert.strictEqual(reactionGridColumns(12), 6);
+ assert.strictEqual(reactionGridColumns(14), 7);
+ });
+
+ test("prefers a fuller last row when nothing divides evenly", function (assert) {
+ // 9 → rests 4 (5), 3 (6), 2 (7): five columns leaves the fullest last row.
+ assert.strictEqual(reactionGridColumns(9), 5);
+ // 13 → rests 3 (5), 1 (6), 6 (7): seven columns leaves the fullest last row.
+ assert.strictEqual(reactionGridColumns(13), 7);
+ });
+
+ test("allows eight columns only on wider screens", function (assert) {
+ assert.strictEqual(reactionGridColumns(16), 6);
+ assert.strictEqual(reactionGridColumns(16, { wide: true }), 8);
+ });
+});