Skip to content

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

FEATURE: Add oneboxes for Live room links (#44936)

Keegan George97a5569ac79d
diff --git a/plugins/live/assets/javascripts/discourse/components/live/room-onebox-presence.gjs b/plugins/live/assets/javascripts/discourse/components/live/room-onebox-presence.gjs
new file mode 100644
index 00000000000..c13045a2dba
--- /dev/null
+++ b/plugins/live/assets/javascripts/discourse/components/live/room-onebox-presence.gjs
@@ -0,0 +1,55 @@
+import Component from "@glimmer/component";
+import { service } from "@ember/service";
+import dAvatar from "discourse/ui-kit/helpers/d-avatar";
+import { i18n } from "discourse-i18n";
+
+const MAX_AVATARS = 5;
+
+// Who is in a room right now, rendered into its onebox card. The card itself
+// is baked into the post, so this reads the live-rooms directory instead:
+// it is scoped to the viewer and kept current over message bus, so the row
+// updates as people join and leave.
+export default class LiveRoomOneboxPresence extends Component {
+ @service liveRooms;
+
+ get room() {
+ // Reading the tracked list subscribes this getter to directory updates;
+ // the slug index itself is not tracked.
+ return (
+ this.liveRooms.rooms && this.liveRooms.roomBySlug(this.args.data.slug)
+ );
+ }
+
+ get participants() {
+ return this.room?.active_participants || [];
+ }
+
+ get shownParticipants() {
+ return this.participants.slice(0, MAX_AVATARS);
+ }
+
+ get label() {
+ return i18n("live.onebox.here_now", { count: this.participants.length });
+ }
+
+ <template>
+ {{#if this.participants.length}}
+ <div class="live-room-onebox__presence">
+ <span aria-hidden="true" class="live-room-onebox__presence-dot"></span>
+ <span class="live-room-onebox__presence-avatars">
+ {{#each this.shownParticipants as |participant|}}
+ <a
+ aria-hidden="true"
+ class="trigger-user-card"
+ data-user-card={{participant.username}}
+ tabindex="-1"
+ >
+ {{dAvatar participant imageSize="tiny"}}
+ </a>
+ {{/each}}
+ </span>
+ <span class="live-room-onebox__presence-label">{{this.label}}</span>
+ </div>
+ {{/if}}
+ </template>
+}
diff --git a/plugins/live/assets/javascripts/discourse/initializers/live-room-onebox-presence.js b/plugins/live/assets/javascripts/discourse/initializers/live-room-onebox-presence.js
new file mode 100644
index 00000000000..eedf6cd1f7e
--- /dev/null
+++ b/plugins/live/assets/javascripts/discourse/initializers/live-room-onebox-presence.js
@@ -0,0 +1,59 @@
+import { withPluginApi } from "discourse/lib/plugin-api";
+import LiveRoomOneboxPresence from "../components/live/room-onebox-presence.gjs";
+
+const ROOM_SLUG_PATTERN = /\/live\/r\/([^/?#]+)/;
+
+// Reads the slug from the card's link rather than a data attribute, so cards
+// baked before presence existed get it without a rebake. The host isn't
+// checked: the server only builds these cards for links it routes as local,
+// which may use another of the site's hostnames, and the sanitizer keeps
+// authors from writing one by hand.
+export function roomSlug(card) {
+ // Read the attribute, not the `href` property: cooked HTML can be decorated
+ // in a detached document, where a relative link isn't resolved.
+ const href = card
+ .querySelector(".live-room-onebox__title a")
+ ?.getAttribute("href");
+ if (!href || !URL.canParse(href, window.location.origin)) {
+ return null;
+ }
+
+ const match = new URL(href, window.location.origin).pathname.match(
+ ROOM_SLUG_PATTERN
+ );
+ return match ? decodeURIComponent(match[1]) : null;
+}
+
+function decorateRoomOneboxes(element, helper) {
+ element.querySelectorAll("aside.live-room-onebox").forEach((card) => {
+ const slug = roomSlug(card);
+ const body = card.querySelector(".live-room-onebox__body");
+ if (
+ !slug ||
+ !body ||
+ body.querySelector(".live-room-onebox__presence-mount")
+ ) {
+ return;
+ }
+
+ const mount = document.createElement("div");
+ mount.className = "live-room-onebox__presence-mount";
+ body.appendChild(mount);
+ helper.renderGlimmer(mount, LiveRoomOneboxPresence, { slug });
+ });
+}
+
+export default {
+ name: "live-room-onebox-presence",
+
+ initialize(owner) {
+ if (!owner.lookup("service:site-settings").live_enabled) {
+ return;
+ }
+
+ withPluginApi((api) => {
+ api.decorateCookedElement(decorateRoomOneboxes);
+ api.decorateChatMessage?.(decorateRoomOneboxes);
+ });
+ },
+};
diff --git a/plugins/live/assets/stylesheets/common/live-onebox.scss b/plugins/live/assets/stylesheets/common/live-onebox.scss
new file mode 100644
index 00000000000..86ec3c0fef1
--- /dev/null
+++ b/plugins/live/assets/stylesheets/common/live-onebox.scss
@@ -0,0 +1,80 @@
+aside.onebox.live-room-onebox {
+ // Specific enough to beat the generic onebox heading margins, including
+ // the extra top margin chat adds to onebox headings on narrow screens.
+ .onebox-body h3.live-room-onebox__title {
+ margin-block: 0 var(--space-1);
+
+ a {
+ display: inline-flex;
+ align-items: center;
+ gap: var(--space-2);
+ }
+
+ .d-icon {
+ color: var(--primary-medium);
+ }
+ }
+
+ .live-room-onebox__type {
+ font-size: var(--font-down-1);
+ color: var(--primary-medium);
+ }
+
+ .live-room-onebox__description {
+ margin-top: var(--space-2);
+ }
+
+ .live-room-onebox__presence {
+ display: flex;
+ flex-wrap: wrap;
+ align-items: center;
+ gap: var(--space-2);
+ margin-top: var(--space-3);
+ font-size: var(--font-down-1);
+ color: var(--primary-medium);
+ }
+
+ .live-room-onebox__presence-dot {
+ width: 0.5em;
+ height: 0.5em;
+ border-radius: 50%;
+ background-color: var(--success);
+ }
+
+ .live-room-onebox__presence-avatars {
+ display: flex;
+ gap: var(--space-1);
+
+ .avatar {
+ display: block;
+ }
+ }
+}
+
+// Font Awesome microphone-lines and podcast, the icons the room cards and
+// room hashtags use.
+a.inline-onebox.live-inline-onebox-room,
+a.inline-onebox.live-inline-onebox-stage {
+ &::before {
+ content: "";
+ display: inline-block;
+ width: var(--live-inline-onebox-icon-width, 0.875em);
+ height: 1em;
+ margin-right: 0.25em;
+ vertical-align: -0.125em;
+ background-color: currentcolor;
+ mask-image: var(--live-inline-onebox-icon);
+ mask-position: center;
+ mask-repeat: no-repeat;
+ mask-size: contain;
+ }
+}
+
+a.inline-onebox.live-inline-onebox-room {
+ --live-inline-onebox-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 384 512'%3E%3Cpath d='M96 96c0-53 43-96 96-96 50.3 0 91.6 38.7 95.7 88L232 88c-13.3 0-24 10.7-24 24s10.7 24 24 24l56 0 0 48-56 0c-13.3 0-24 10.7-24 24s10.7 24 24 24l55.7 0c-4.1 49.3-45.3 88-95.7 88-53 0-96-43-96-96L96 96zM24 160c13.3 0 24 10.7 24 24l0 40c0 79.5 64.5 144 144 144s144-64.5 144-144l0-40c0-13.3 10.7-24 24-24s24 10.7 24 24l0 40c0 97.9-73.3 178.7-168 190.5l0 49.5 48 0c13.3 0 24 10.7 24 24s-10.7 24-24 24l-144 0c-13.3 0-24-10.7-24-24s10.7-24 24-24l48 0 0-49.5C73.3 402.7 0 321.9 0 224l0-40c0-13.3 10.7-24 24-24z'/%3E%3C/svg%3E");
+}
+
+a.inline-onebox.live-inline-onebox-stage {
+ --live-inline-onebox-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 448 512'%3E%3Cpath d='M336 359.8c39.1-32.3 64-81.1 64-135.8 0-97.2-78.8-176-176-176S48 126.8 48 224C48 278.7 72.9 327.5 112 359.8 112.4 377.4 115.2 400.2 118.4 421.6 48 383.9 0 309.5 0 224 0 100.3 100.3 0 224 0S448 100.3 448 224c0 85.6-48 159.9-118.5 197.6 3.3-21.4 6-44.2 6.4-61.8zm-14-53.4c-8.3-12.6-19.2-21.6-30.4-27.8-2.1-1.1-4.2-2.2-6.3-3.2 11.7-13.9 18.8-31.9 18.8-51.5 0-44.2-35.8-80-80-80s-80 35.8-80 80c0 19.6 7.1 37.6 18.8 51.5-2.1 1-4.2 2-6.3 3.2-11.2 6.2-22.1 15.2-30.4 27.8-18.8-22.3-30.1-51-30.1-82.4 0-70.7 57.3-128 128-128s128 57.3 128 128c0 31.4-11.3 60.2-30.1 82.4zM224 312c32.9 0 64 8.6 64 43.8 0 33-12.9 104.1-20.6 132.9-5.1 19-24.5 23.4-43.4 23.4s-38.2-4.4-43.4-23.4c-7.8-28.5-20.6-99.7-20.6-132.8 0-35.1 31.1-43.8 64-43.8zm0-128a40 40 0 1 1 0 80 40 40 0 1 1 0-80z'/%3E%3C/svg%3E");
+ --live-inline-onebox-icon-width: 1em;
+}
diff --git a/plugins/live/config/locales/client.en.yml b/plugins/live/config/locales/client.en.yml
index 39a0734d8d8..9d3004a4873 100644
--- a/plugins/live/config/locales/client.en.yml
+++ b/plugins/live/config/locales/client.en.yml
@@ -5,6 +5,10 @@ en:
participants:
one: "%{formatted_number} person joined voice chats"
other: "%{formatted_number} people joined voice chats"
+ onebox:
+ here_now:
+ one: "%{count} person here now"
+ other: "%{count} people here now"
sidebar:
title: "Live rooms"
create: "Create room"
diff --git a/plugins/live/config/locales/server.en.yml b/plugins/live/config/locales/server.en.yml
index b08683433bc..347c6a1f181 100644
--- a/plugins/live/config/locales/server.en.yml
+++ b/plugins/live/config/locales/server.en.yml
@@ -67,6 +67,11 @@ en:
chat:
default_thread_title: "Voice chat in %{room}"
default_thread_opener: "In %{room} - %{message}"
+ onebox:
+ inline_to_room: "Live: %{room_name}"
+ room_type:
+ open: "Live room"
+ stage: "Live stage"
user_status:
in_room: "In %{room_name}"
in_private_room: "In a live room"
diff --git a/plugins/live/lib/live.rb b/plugins/live/lib/live.rb
index bc9a4986c26..f62f7502367 100644
--- a/plugins/live/lib/live.rb
+++ b/plugins/live/lib/live.rb
@@ -57,7 +57,9 @@ require_relative "live/livekit/room_service_client"
require_relative "live/livekit/agent_dispatch_client"
require_relative "live/livekit/cloud_agent_client"
require_relative "live/livekit/webhook_verifier"
+require_relative "live/inline_onebox_handler"
require_relative "live/media_entitlements"
+require_relative "live/onebox_handler"
require_relative "live/room_hashtag_data_source"
require_relative "live/user_status_manager"
require_relative "../app/services/live/agent_manager"
diff --git a/plugins/live/lib/live/inline_onebox_handler.rb b/plugins/live/lib/live/inline_onebox_handler.rb
new file mode 100644
index 00000000000..a6b2343dc33
--- /dev/null
+++ b/plugins/live/lib/live/inline_onebox_handler.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+module Live
+ class InlineOneboxHandler
+ def self.handle(url, route)
+ return unless room = Live::OneboxHandler.oneboxable_room(route)
+
+ # Inline oneboxes carry text only, so live-onebox.scss draws the room
+ # icon from this class. It must stay a single class: the composer
+ # preview applies it with classList.add, which rejects spaces.
+ {
+ url: url,
+ title: I18n.t("live.onebox.inline_to_room", room_name: room.name),
+ css_class: room.stage? ? "live-inline-onebox-stage" : "live-inline-onebox-room",
+ }
+ end
+ end
+end
diff --git a/plugins/live/lib/live/onebox_handler.rb b/plugins/live/lib/live/onebox_handler.rb
new file mode 100644
index 00000000000..73080065b4b
--- /dev/null
+++ b/plugins/live/lib/live/onebox_handler.rb
@@ -0,0 +1,44 @@
+# frozen_string_literal: true
+
+module Live
+ class OneboxHandler
+ TEMPLATE_PATH = File.expand_path("../onebox/templates/live_room.mustache", __dir__)
+ private_constant :TEMPLATE_PATH
+
+ def self.handle(url, route)
+ return unless room = oneboxable_room(route)
+
+ args = {
+ url: url,
+ name: room.name,
+ icon: room.stage? ? "podcast" : "microphone-lines",
+ room_type_label: I18n.t("live.onebox.room_type.#{room.room_type_name}"),
+ description:
+ if room.cooked_description.present?
+ PrettyText.excerpt(room.cooked_description, SiteSetting.post_onebox_maxlength)
+ end,
+ }
+
+ Mustache.render(template, args)
+ end
+
+ # Oneboxes are cached and baked into cooked posts that anyone may read,
+ # so a room only previews when an anonymous visitor could see it.
+ def self.oneboxable_room(route)
+ return if route[:slug].blank?
+
+ room = Live::Room.find_by(slug: route[:slug])
+ return if room.blank? || room.ephemeral?
+ return unless Guardian.new.can_see_live_room?(room)
+
+ room
+ end
+
+ def self.template
+ return File.read(TEMPLATE_PATH) if Rails.env.development?
+
+ @template ||= File.read(TEMPLATE_PATH)
+ end
+ private_class_method :template
+ end
+end
diff --git a/plugins/live/lib/onebox/templates/live_room.mustache b/plugins/live/lib/onebox/templates/live_room.mustache
new file mode 100644
index 00000000000..b35547b5a21
--- /dev/null
+++ b/plugins/live/lib/onebox/templates/live_room.mustache
@@ -0,0 +1,16 @@
+<aside class="onebox live-room-onebox">
+ <article class="onebox-body live-room-onebox__body">
+ <h3 class="live-room-onebox__title">
+ <a href="{{url}}">
+ <svg class="fa d-icon d-icon-{{icon}} svg-icon svg-string" xmlns="http://www.w3.org/2000/svg">
+ <use href="#{{icon}}"></use>
+ </svg>
+ <span class="live-room-onebox__name">{{name}}</span>
+ </a>
+ </h3>
+ <div class="live-room-onebox__type">{{room_type_label}}</div>
+ {{#description}}
+ <div class="live-room-onebox__description">{{{description}}}</div>
+ {{/description}}
+ </article>
+</aside>
diff --git a/plugins/live/plugin.rb b/plugins/live/plugin.rb
index f4e0fd1097f..6be028ca282 100644
--- a/plugins/live/plugin.rb
+++ b/plugins/live/plugin.rb
@@ -91,6 +91,7 @@ register_svg_icon "far-file-lines"
register_asset "stylesheets/common/live.scss"
register_asset "stylesheets/common/live-room-page.scss"
register_asset "stylesheets/common/live-chat.scss"
+register_asset "stylesheets/common/live-onebox.scss"
register_asset "stylesheets/common/live-admin.scss", :admin
add_admin_route "live.admin.title", "live", use_new_show_route: true
@@ -131,6 +132,14 @@ after_initialize do
register_hashtag_type_priority_for_context("room", "topic-composer", 5)
refresh_chat_hashtag_configurations
+ Oneboxer.register_local_handler("live/page") do |url, route|
+ Live::OneboxHandler.handle(url, route)
+ end
+
+ InlineOneboxer.register_local_handler("live/page") do |url, route|
+ Live::InlineOneboxHandler.handle(url, route)
+ end
+
# Gates the user-card call button: the viewer needs the direct-call
# permission and the target must be callable by them — live-room access,
# not the viewer themselves or a bot, and not screened out by the target's
diff --git a/plugins/live/spec/lib/live/onebox_handler_spec.rb b/plugins/live/spec/lib/live/onebox_handler_spec.rb
new file mode 100644
index 00000000000..8ba93a8a871
--- /dev/null
+++ b/plugins/live/spec/lib/live/onebox_handler_spec.rb
@@ -0,0 +1,132 @@
+# frozen_string_literal: true
+
+require "rails_helper"
+
+RSpec.describe Live::OneboxHandler do
+ fab!(:room) do
+ Fabricate(
+ :live_room,
+ name: "Watercooler",
+ description: "Drop in & **chat** <script>alert(1)</script>",
+ public: true,
+ )
+ end
+ fab!(:stage_room) do
+ Fabricate(:live_room, name: "Town Hall", public: true, room_type: Live::Room::ROOM_TYPE_STAGE)
+ end
+ fab!(:private_room) { Fabricate(:live_room, name: "Secret Sessions") }
+ fab!(:ephemeral_room) { Fabricate(:live_ephemeral_room, name: "Call", public: true) }
+
+ before { SiteSetting.live_enabled = true }
+
+ def room_url(room, suffix = "")
+ "#{Discourse.base_url}/live/r/#{room.slug}#{suffix}"
+ end
+
+ def preview(url)
+ Nokogiri::HTML5.fragment(Oneboxer.preview(url, invalidate_oneboxes: true))
+ end
+
+ it "renders a card for a public room" do
+ html = preview(room_url(room))
+
+ expect(html.at_css(".live-room-onebox__title a")["href"]).to eq(room_url(room))
+ expect(html.at_css(".live-room-onebox__name").text).to eq("Watercooler")
+ expect(html.at_css(".live-room-onebox__title use")["href"]).to eq("#microphone-lines")
+ expect(html.at_css(".live-room-onebox__type").text).to eq(I18n.t("live.onebox.room_type.open"))
+ expect(html.at_css(".live-room-onebox__description").text).to eq("Drop in & chat")
+ expect(html.at_css("script")).to be_nil
+ end
+
+ it "escapes markup in the room name and description" do
+ room.update!(
+ name: "<img src=x onerror=alert(1)>",
+ description: "`<img src=x onerror=alert(2)>` and &lt;b&gt;bold&lt;/b&gt;",
+ )
+
+ html = preview(room_url(room))
+
+ expect(html.at_css(".live-room-onebox__name").text).to eq("<img src=x onerror=alert(1)>")
+ expect(html.at_css(".live-room-onebox__description").text).to eq(
+ "<img src=x onerror=alert(2)> and <b>bold</b>",
+ )
+ expect(html.css("img, b")).to be_empty
+ end
+
+ it "omits the description when the room has none" do
+ html = preview(room_url(stage_room))
+
+ expect(html.at_css(".live-room-onebox__title use")["href"]).to eq("#podcast")
+ expect(html.at_css(".live-room-onebox__type").text).to eq(I18n.t("live.onebox.room_type.stage"))
+ expect(html.at_css(".live-room-onebox__description")).to be_nil
+ end
+
+ it "renders a card for an invite link" do
+ url = room_url(room, "/invited-by/#{Fabricate(:user).username}")
+
+ expect(preview(url).at_css(".live-room-onebox__title a")["href"]).to eq(url)
+ end
+
+ it "falls back to a plain link for a room an anonymous visitor cannot see" do
+ [private_room, ephemeral_room].each do |hidden_room|
+ expect(preview(room_url(hidden_room)).at_css(".live-room-onebox")).to be_nil
+ end
+
+ expect(preview("#{Discourse.base_url}/live/r/missing").at_css(".live-room-onebox")).to be_nil
+ end
+
+ it "falls back to a plain link when Live is closed to anonymous visitors" do
+ SiteSetting.live_allowed_groups = Group::AUTO_GROUPS[:trust_level_0].to_s
+
+ expect(preview(room_url(room)).at_css(".live-room-onebox")).to be_nil
+ end
+
+ it "falls back to a plain link when Live is disabled" do
+ SiteSetting.live_enabled = false
+
+ expect(preview(room_url(room)).at_css(".live-room-onebox")).to be_nil
+ end
+
+ describe "inline oneboxes" do
+ it "titles a public room link with the room name and an icon class" do
+ onebox = InlineOneboxer.lookup(room_url(room), skip_cache: true)
+
+ expect(onebox).to eq(
+ url: room_url(room),
+ title: I18n.t("live.onebox.inline_to_room", room_name: "Watercooler"),
+ css_class: "live-inline-onebox-room",
+ )
+ end
+
+ it "uses the stage icon class for stage rooms" do
+ onebox = InlineOneboxer.lookup(room_url(stage_room), skip_cache: true)
+
+ expect(onebox[:css_class]).to eq("live-inline-onebox-stage")
+ end
+
+ it "leaves links to hidden rooms alone" do
+ expect(InlineOneboxer.lookup(room_url(private_room), skip_cache: true)).to be_nil
+ end
+ end
+
+ describe "chat messages", if: defined?(::Chat) do
+ before { SiteSetting.chat_enabled = true }
+
+ it "renders a card and an inline onebox" do
+ message =
+ Fabricate(
+ :chat_message,
+ message: "#{room_url(room)}\n\nJoin us in #{room_url(stage_room)} later",
+ )
+
+ processor = Chat::MessageProcessor.new(message, invalidate_oneboxes: true)
+ processor.run!
+ html = Nokogiri::HTML5.fragment(processor.html)
+
+ expect(html.at_css(".live-room-onebox .live-room-onebox__name").text).to eq("Watercooler")
+ expect(html.at_css("a.inline-onebox.live-inline-onebox-stage").text).to eq(
+ I18n.t("live.onebox.inline_to_room", room_name: "Town Hall"),
+ )
+ end
+ end
+end
diff --git a/plugins/live/spec/system/live_room_oneboxes_spec.rb b/plugins/live/spec/system/live_room_oneboxes_spec.rb
new file mode 100644
index 00000000000..6d9e5e4c0ff
--- /dev/null
+++ b/plugins/live/spec/system/live_room_oneboxes_spec.rb
@@ -0,0 +1,60 @@
+# frozen_string_literal: true
+
+require_relative "page_objects/components/live_room_onebox"
+
+describe "Live room oneboxes" do
+ let(:topic_page) { PageObjects::Pages::Topic.new }
+ let(:onebox) { PageObjects::Components::LiveRoomOnebox.new(room) }
+
+ fab!(:user)
+ fab!(:participants) { Fabricate.times(2, :user) }
+ fab!(:latecomer, :user)
+ fab!(:room) do
+ Fabricate(:live_room, name: "Watercooler", description: "Drop in and chat", public: true)
+ end
+ fab!(:stage_room) do
+ Fabricate(:live_room, name: "Town Hall", public: true, room_type: Live::Room::ROOM_TYPE_STAGE)
+ end
+ # Built per example rather than with fab!, so the links use the system test
+ # server's host and are recognized as local.
+ let!(:post) do
+ Fabricate(
+ :post,
+ raw:
+ "#{Discourse.base_url}/live/r/#{room.slug}\n\nSee you at #{Discourse.base_url}/live/r/#{stage_room.slug} later",
+ )
+ end
+
+ before do
+ SiteSetting.live_enabled = true
+ SiteSetting.live_mesh_privacy_warning_enabled = false
+ SiteSetting.live_allowed_groups =
+ "#{Group::AUTO_GROUPS[:anonymous_users]}|#{Group::AUTO_GROUPS[:logged_in_users]}"
+
+ Jobs.run_immediately!
+ post.rebake!
+
+ participants.each { |participant| Live::ParticipantTracker.add(room.id, participant.id) }
+ sign_in(user)
+ end
+
+ after { Live::ParticipantTracker.clear(room.id) }
+
+ it "renders the room card, the inline link and who is in the room" do
+ topic_page.visit_topic(post.topic)
+
+ expect(onebox).to have_name("Watercooler")
+ expect(onebox).to have_type("Live room")
+ expect(onebox).to have_presence(count: 2)
+
+ expect(page).to have_css(
+ "a.inline-onebox.live-inline-onebox-stage",
+ text: I18n.t("live.onebox.inline_to_room", room_name: "Town Hall"),
+ )
+
+ Live::ParticipantTracker.add(room.id, latecomer.id)
+ Live::RoomBroadcaster.new(room).publish_participants
+
+ expect(onebox).to have_presence(count: 3)
+ end
+end
diff --git a/plugins/live/spec/system/page_objects/components/live_room_onebox.rb b/plugins/live/spec/system/page_objects/components/live_room_onebox.rb
new file mode 100644
index 00000000000..c7ba094f8bb
--- /dev/null
+++ b/plugins/live/spec/system/page_objects/components/live_room_onebox.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+module PageObjects
+ module Components
+ class LiveRoomOnebox < PageObjects::Components::Base
+ SELECTOR = "aside.onebox.live-room-onebox"
+
+ def initialize(room)
+ @room = room
+ end
+
+ def card
+ find("#{SELECTOR}:has(a[href$='/live/r/#{@room.slug}'])")
+ end
+
+ def has_name?(name)
+ card.has_css?(".live-room-onebox__name", text: name)
+ end
+
+ def has_type?(label)
+ card.has_css?(".live-room-onebox__type", text: label)
+ end
+
+ def has_presence?(count:)
+ card.has_css?(
+ ".live-room-onebox__presence",
+ text: I18n.t("js.live.onebox.here_now", count:),
+ ) && card.has_css?(".live-room-onebox__presence-avatars .avatar", count: [count, 5].min)
+ end
+
+ def has_no_presence?
+ card.has_no_css?(".live-room-onebox__presence")
+ end
+ end
+ end
+end
diff --git a/plugins/live/test/javascripts/integration/components/live/room-onebox-presence-test.gjs b/plugins/live/test/javascripts/integration/components/live/room-onebox-presence-test.gjs
new file mode 100644
index 00000000000..1573ad570da
--- /dev/null
+++ b/plugins/live/test/javascripts/integration/components/live/room-onebox-presence-test.gjs
@@ -0,0 +1,76 @@
+import { tracked } from "@glimmer/tracking";
+import Service from "@ember/service";
+import { render, settled } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import LiveRoomOneboxPresence from "discourse/plugins/live/discourse/components/live/room-onebox-presence";
+
+class LiveRoomsStub extends Service {
+ @tracked rooms = [];
+
+ roomBySlug(slug) {
+ return this.rooms.find((room) => room.slug === slug);
+ }
+}
+
+function participant(id) {
+ return {
+ id,
+ username: `user${id}`,
+ avatar_template: "/letter_avatar_proxy/v4/letter/u/aaaaaa/{size}.png",
+ };
+}
+
+module("Integration | Component | Live | RoomOneboxPresence", function (hooks) {
+ setupRenderingTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.owner.unregister("service:live-rooms");
+ this.owner.register("service:live-rooms", LiveRoomsStub);
+ this.liveRooms = this.owner.lookup("service:live-rooms");
+ this.data = { slug: "watercooler" };
+ });
+
+ test("renders nothing while the room is empty or unknown", async function (assert) {
+ await render(
+ <template><LiveRoomOneboxPresence @data={{this.data}} /></template>
+ );
+ assert.dom(".live-room-onebox__presence").doesNotExist("unknown room");
+
+ this.liveRooms.rooms = [{ slug: "watercooler", active_participants: [] }];
+ await settled();
+ assert.dom(".live-room-onebox__presence").doesNotExist("empty room");
+ });
+
+ test("shows who is here and follows directory updates", async function (assert) {
+ this.liveRooms.rooms = [
+ { slug: "watercooler", active_participants: [participant(1)] },
+ ];
+
+ await render(
+ <template><LiveRoomOneboxPresence @data={{this.data}} /></template>
+ );
+
+ assert
+ .dom(".live-room-onebox__presence-label")
+ .hasText("1 person here now");
+ assert
+ .dom(".live-room-onebox__presence-avatars .avatar")
+ .exists({ count: 1 });
+
+ this.liveRooms.rooms = [
+ {
+ slug: "watercooler",
+ active_participants: [1, 2, 3, 4, 5, 6, 7].map(participant),
+ },
+ ];
+ await settled();
+
+ assert
+ .dom(".live-room-onebox__presence-label")
+ .hasText("7 people here now");
+ assert
+ .dom(".live-room-onebox__presence-avatars .avatar")
+ .exists({ count: 5 }, "caps the avatars");
+ });
+});
diff --git a/plugins/live/test/javascripts/unit/lib/room-onebox-slug-test.js b/plugins/live/test/javascripts/unit/lib/room-onebox-slug-test.js
new file mode 100644
index 00000000000..b64dce34049
--- /dev/null
+++ b/plugins/live/test/javascripts/unit/lib/room-onebox-slug-test.js
@@ -0,0 +1,40 @@
+import { module, test } from "qunit";
+import { roomSlug } from "discourse/plugins/live/discourse/initializers/live-room-onebox-presence";
+
+function card(href) {
+ const element = document.createElement("aside");
+ element.innerHTML = `<h3 class="live-room-onebox__title"><a href="${href}">Room</a></h3>`;
+ return element;
+}
+
+module("Live | Unit | Lib | room onebox slug", function () {
+ const origin = window.location.origin;
+
+ test("reads the slug from room and invite links on this site", function (assert) {
+ assert.strictEqual(
+ roomSlug(card(`${origin}/live/r/watercooler`)),
+ "watercooler"
+ );
+ assert.strictEqual(
+ roomSlug(card(`${origin}/live/r/town-hall/invited-by/sam?x=1`)),
+ "town-hall"
+ );
+ assert.strictEqual(
+ roomSlug(card(`${origin}/forum/live/r/watercooler`)),
+ "watercooler",
+ "subfolder installs"
+ );
+ assert.strictEqual(
+ roomSlug(card("/live/r/watercooler")),
+ "watercooler",
+ "relative links"
+ );
+ });
+
+ test("ignores other links", function (assert) {
+ assert.strictEqual(roomSlug(card(`${origin}/t/some-topic/1`)), null);
+ assert.strictEqual(roomSlug(card(`${origin}/live/rooms`)), null);
+ assert.strictEqual(roomSlug(document.createElement("aside")), null);
+ assert.strictEqual(roomSlug(card("http://[")), null, "malformed links");
+ });
+});