Skip to content

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

DEV: Incorporate the discourse-livestream plugin into discourse-calendar (#40466)

Martin Brennan35520fffa55d
diff --git a/db/structure.sql b/db/structure.sql
index 977f118e283..80b64f55555 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -6179,6 +6179,38 @@ CREATE SEQUENCE public.linked_topics_id_seq
ALTER SEQUENCE public.linked_topics_id_seq OWNED BY public.linked_topics.id;
+--
+-- Name: livestream_topic_chat_channels; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE public.livestream_topic_chat_channels (
+ id bigint NOT NULL,
+ topic_id bigint NOT NULL,
+ chat_channel_id bigint NOT NULL,
+ created_at timestamp(6) without time zone NOT NULL,
+ updated_at timestamp(6) without time zone NOT NULL
+);
+
+
+--
+-- Name: livestream_topic_chat_channels_id_seq; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE public.livestream_topic_chat_channels_id_seq
+ START WITH 1
+ INCREMENT BY 1
+ NO MINVALUE
+ NO MAXVALUE
+ CACHE 1;
+
+
+--
+-- Name: livestream_topic_chat_channels_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
+--
+
+ALTER SEQUENCE public.livestream_topic_chat_channels_id_seq OWNED BY public.livestream_topic_chat_channels.id;
+
+
--
-- Name: llm_credit_allocations; Type: TABLE; Schema: public; Owner: -
--
@@ -12891,6 +12923,13 @@ ALTER TABLE ONLY public.javascript_caches ALTER COLUMN id SET DEFAULT nextval('p
ALTER TABLE ONLY public.linked_topics ALTER COLUMN id SET DEFAULT nextval('public.linked_topics_id_seq'::regclass);
+--
+-- Name: livestream_topic_chat_channels id; Type: DEFAULT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY public.livestream_topic_chat_channels ALTER COLUMN id SET DEFAULT nextval('public.livestream_topic_chat_channels_id_seq'::regclass);
+
+
--
-- Name: llm_credit_allocations id; Type: DEFAULT; Schema: public; Owner: -
--
@@ -15198,6 +15237,14 @@ ALTER TABLE ONLY public.linked_topics
ADD CONSTRAINT linked_topics_pkey PRIMARY KEY (id);
+--
+-- Name: livestream_topic_chat_channels livestream_topic_chat_channels_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY public.livestream_topic_chat_channels
+ ADD CONSTRAINT livestream_topic_chat_channels_pkey PRIMARY KEY (id);
+
+
--
-- Name: llm_credit_allocations llm_credit_allocations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
@@ -21576,6 +21623,13 @@ CREATE UNIQUE INDEX unique_index_categories_on_name ON public.categories USING b
CREATE UNIQUE INDEX unique_index_categories_on_slug ON public.categories USING btree (COALESCE(parent_category_id, '-1'::integer), lower((slug)::text)) WHERE ((slug)::text <> ''::text);
+--
+-- Name: unique_livestream_topic_chat_channels; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE UNIQUE INDEX unique_livestream_topic_chat_channels ON public.livestream_topic_chat_channels USING btree (topic_id, chat_channel_id);
+
+
--
-- Name: unique_post_links; Type: INDEX; Schema: public; Owner: -
--
@@ -21861,6 +21915,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20260604052235'),
('20260603115312'),
('20260603115200'),
+('20260603013342'),
('20260602104726'),
('20260601043020'),
('20260528074731'),
diff --git a/plugins/discourse-calendar/app/models/discourse_calendar/livestream/topic_chat_channel.rb b/plugins/discourse-calendar/app/models/discourse_calendar/livestream/topic_chat_channel.rb
new file mode 100644
index 00000000000..ddf944e9940
--- /dev/null
+++ b/plugins/discourse-calendar/app/models/discourse_calendar/livestream/topic_chat_channel.rb
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+
+module DiscourseCalendar
+ module Livestream
+ class TopicChatChannel < ActiveRecord::Base
+ self.table_name = "livestream_topic_chat_channels"
+ belongs_to :topic
+ belongs_to :chat_channel, class_name: "Chat::Channel", dependent: :destroy
+ end
+ end
+end
+
+# == Schema Information
+#
+# Table name: livestream_topic_chat_channels
+#
+# id :bigint not null, primary key
+# created_at :datetime not null
+# updated_at :datetime not null
+# chat_channel_id :bigint not null
+# topic_id :bigint not null
+#
+# Indexes
+#
+# unique_livestream_topic_chat_channels (topic_id,chat_channel_id) UNIQUE
+#
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/components/livestream/embeddable-chat-channel.gjs b/plugins/discourse-calendar/assets/javascripts/discourse/components/livestream/embeddable-chat-channel.gjs
new file mode 100644
index 00000000000..5d3e5dd3e7d
--- /dev/null
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/components/livestream/embeddable-chat-channel.gjs
@@ -0,0 +1,83 @@
+import Component from "@glimmer/component";
+import { tracked } from "@glimmer/tracking";
+import { array } from "@ember/helper";
+import { service } from "@ember/service";
+import { modifier } from "ember-modifier";
+import { bind } from "discourse/lib/decorators";
+import DButton from "discourse/ui-kit/d-button";
+import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";
+import ChatChannel from "discourse/plugins/chat/discourse/components/chat-channel";
+
+export default class EmbedableChatChannel extends Component {
+ @service chatChannelsManager;
+ @service currentUser;
+ @service embeddableChat;
+ @service messageBus;
+
+ @tracked activeChannel;
+
+ updateChannel = modifier(async () => {
+ if (this.args.chatChannelId === this.activeChannel?.id) {
+ return;
+ }
+
+ this.activeChannel = await this.chatChannelsManager.find(
+ this.args.chatChannelId
+ );
+ });
+
+ constructor() {
+ super(...arguments);
+ this.messageBus.subscribe(this.messageBusChannel, this.onMessage);
+ }
+
+ willDestroy() {
+ super.willDestroy(...arguments);
+ this.messageBus.unsubscribe(this.messageBusChannel, this.onMessage);
+ }
+
+ get messageBusChannel() {
+ return `/discourse-calendar/livestream/chat-status/${this.currentUser.id}`;
+ }
+
+ @bind
+ async onMessage(message) {
+ const membership = JSON.parse(message).user_channel_membership;
+
+ if (membership.chat_channel_id !== this.activeChannel?.id) {
+ return;
+ }
+
+ this.activeChannel.currentUserMembership = membership;
+ }
+
+ <template>
+ <div
+ id="custom-chat-container"
+ class={{dConcatClass
+ (if this.embeddableChat.isMobileChatVisible "mobile")
+ (unless this.embeddableChat.isMobileModal "no-modal-mobile")
+ }}
+ {{this.updateChannel}}
+ >
+ {{#unless this.embeddableChat.isMobileModal}}
+ <div class="c-navbar-container livestream-chat-close">
+
+ <DButton
+ @icon="xmark"
+ @action={{this.embeddableChat.toggleChatVisibility}}
+ @title="chat.close"
+ class="btn-transparent no-text c-navbar__close-drawer-button"
+ />
+ </div>
+ {{/unless}}
+ <div class="chat-drawer">
+ {{#if this.activeChannel}}
+ {{#each (array this.activeChannel) as |channel|}}
+ <ChatChannel @channel={{channel}} />
+ {{/each}}
+ {{/if}}
+ </div>
+ </div>
+ </template>
+}
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/components/livestream/mobile-livestream-chat-icon.gjs b/plugins/discourse-calendar/assets/javascripts/discourse/components/livestream/mobile-livestream-chat-icon.gjs
new file mode 100644
index 00000000000..cce64be3654
--- /dev/null
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/components/livestream/mobile-livestream-chat-icon.gjs
@@ -0,0 +1,31 @@
+import Component from "@glimmer/component";
+import { action } from "@ember/object";
+import { service } from "@ember/service";
+import DButton from "discourse/ui-kit/d-button";
+import MobileEmbeddableChatModal from "./modal/mobile-embeddable-chat-modal";
+
+export default class MobileLivestreamChatIcon extends Component {
+ @service modal;
+ @service embeddableChat;
+ @service siteSettings;
+
+ @action
+ openLivestreamChat() {
+ if (this.siteSettings.livestream_enable_modal_chat_on_mobile) {
+ this.modal.show(MobileEmbeddableChatModal);
+ } else {
+ this.embeddableChat.toggleChatVisibility();
+ }
+ }
+
+ <template>
+ <li class="header-dropdown-toggle livestream-header-icon">
+ <DButton
+ @icon="comments"
+ class="icon btn-flat"
+ tabindex="0"
+ @action={{this.openLivestreamChat}}
+ />
+ </li>
+ </template>
+}
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/components/livestream/modal/mobile-embeddable-chat-modal.gjs b/plugins/discourse-calendar/assets/javascripts/discourse/components/livestream/modal/mobile-embeddable-chat-modal.gjs
new file mode 100644
index 00000000000..192fadeaa08
--- /dev/null
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/components/livestream/modal/mobile-embeddable-chat-modal.gjs
@@ -0,0 +1,39 @@
+import Component from "@glimmer/component";
+import { inject as controller } from "@ember/controller";
+import didUpdate from "@ember/render-modifiers/modifiers/did-update";
+import { service } from "@ember/service";
+import DModal from "discourse/ui-kit/d-modal";
+import EmbeddableChatChannel from "../embeddable-chat-channel";
+
+export default class MobileEmbeddableChatModal extends Component {
+ @service embeddableChat;
+ @service capabilities;
+ @controller("topic") topicController;
+
+ checkAndCloseModal = () => {
+ if (this.capabilities.viewport.lg) {
+ this.args.closeModal();
+ }
+ };
+
+ get shouldRender() {
+ return this.embeddableChat.canRenderChatChannel(this.topicController, true);
+ }
+
+ <template>
+ <DModal
+ @closeModal={{@closeModal}}
+ class="livestream-chat-modal"
+ @hideHeader={{true}}
+ {{didUpdate this.checkAndCloseModal this.capabilities.viewport.lg}}
+ >
+ <:body>
+ {{#if this.shouldRender}}
+ <EmbeddableChatChannel
+ @chatChannelId={{this.embeddableChat.chatChannelId}}
+ />
+ {{/if}}
+ </:body>
+ </DModal>
+ </template>
+}
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/components/livestream/responsive-livestream-chat-icon.gjs b/plugins/discourse-calendar/assets/javascripts/discourse/components/livestream/responsive-livestream-chat-icon.gjs
new file mode 100644
index 00000000000..f30321c82d3
--- /dev/null
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/components/livestream/responsive-livestream-chat-icon.gjs
@@ -0,0 +1,24 @@
+import Component from "@glimmer/component";
+import { inject as controller } from "@ember/controller";
+import { service } from "@ember/service";
+import MobileLivestreamChatIcon from "./mobile-livestream-chat-icon";
+
+export default class ResponsiveLivestreamChatIcon extends Component {
+ @service capabilities;
+ @service siteSettings;
+ @controller("topic") topicController;
+
+ get shouldShow() {
+ return (
+ this.siteSettings.livestream_enabled &&
+ !this.capabilities.viewport.lg &&
+ this.topicController?.model?.chat_channel_id
+ );
+ }
+
+ <template>
+ {{#if this.shouldShow}}
+ <MobileLivestreamChatIcon />
+ {{/if}}
+ </template>
+}
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/connectors/before-main-outlet/embeddable-chat-channel-connector.gjs b/plugins/discourse-calendar/assets/javascripts/discourse/connectors/before-main-outlet/embeddable-chat-channel-connector.gjs
new file mode 100644
index 00000000000..6f3cbc0b72f
--- /dev/null
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/connectors/before-main-outlet/embeddable-chat-channel-connector.gjs
@@ -0,0 +1,34 @@
+import Component from "@glimmer/component";
+import { inject as controller } from "@ember/controller";
+import { service } from "@ember/service";
+import EmbeddableChatChannel from "../../components/livestream/embeddable-chat-channel";
+
+export default class EmbedableChatChannelConnector extends Component {
+ @service embeddableChat;
+ @service siteSettings;
+ @service capabilities;
+ @controller("topic") topicController;
+
+ get shouldRender() {
+ const mobileViewport =
+ !this.siteSettings.livestream_enable_modal_chat_on_mobile &&
+ !this.capabilities.viewport.lg;
+
+ if (!this.siteSettings.livestream_enabled) {
+ return false;
+ }
+
+ return this.embeddableChat.canRenderChatChannel(
+ this.topicController,
+ mobileViewport
+ );
+ }
+
+ <template>
+ {{#if this.shouldRender}}
+ <EmbeddableChatChannel
+ @chatChannelId={{this.embeddableChat.chatChannelId}}
+ />
+ {{/if}}
+ </template>
+}
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/connectors/chat-join-channel-button/join-channel-message.gjs b/plugins/discourse-calendar/assets/javascripts/discourse/connectors/chat-join-channel-button/join-channel-message.gjs
new file mode 100644
index 00000000000..9d2cf2d8700
--- /dev/null
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/connectors/chat-join-channel-button/join-channel-message.gjs
@@ -0,0 +1,44 @@
+import Component from "@glimmer/component";
+import { inject as controller } from "@ember/controller";
+import { service } from "@ember/service";
+import DButton from "discourse/ui-kit/d-button";
+import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";
+import { i18n } from "discourse-i18n";
+
+export default class JoinChannelMessage extends Component {
+ @service embeddableChat;
+ @service siteSettings;
+ @controller("topic") topicController;
+
+ get shouldRenderJoinText() {
+ const topic = this.topicController?.model;
+ return (
+ this.siteSettings.livestream_enabled &&
+ topic?.chat_channel_id &&
+ this.embeddableChat.topicHasLivestreamTag(topic)
+ );
+ }
+
+ <template>
+ {{#if this.shouldRenderJoinText}}
+ <h2>
+ {{i18n "discourse_calendar.livestream.chat.join_channel_header"}}
+ </h2>
+ <p>
+ {{i18n "discourse_calendar.livestream.chat.join_channel_message"}}
+ </p>
+ {{else}}
+ <DButton
+ @action={{@outletArgs.onJoinChannel}}
+ @translatedLabel={{@outletArgs.label}}
+ @translatedTitle={{@outletArgs.options.joinTitle}}
+ @icon={{@outletArgs.options.joinIcon}}
+ @disabled={{@outletArgs.isLoading}}
+ class={{dConcatClass
+ "toggle-channel-membership-button -join"
+ @outletArgs.options.joinClass
+ }}
+ />
+ {{/if}}
+ </template>
+}
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/initializers/livestream-chat-overrides.js b/plugins/discourse-calendar/assets/javascripts/discourse/initializers/livestream-chat-overrides.js
new file mode 100644
index 00000000000..b5ec88f9e67
--- /dev/null
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/initializers/livestream-chat-overrides.js
@@ -0,0 +1,113 @@
+import { withPluginApi } from "discourse/lib/plugin-api";
+import ResponsiveLivestreamChatIcon from "../components/livestream/responsive-livestream-chat-icon";
+
+function showCustomBBCode(isGoing = false) {
+ // show the content within the [preview] tag if the user is not going to the event
+ document.querySelectorAll(".cooked .preview").forEach((e) => {
+ e.style.setProperty("display", !isGoing ? "block" : "none", "important");
+ });
+
+ // show the content within the [hidden] tag if the user is going to the event
+ document.querySelectorAll(".cooked .hidden").forEach((e) => {
+ e.style.setProperty("display", isGoing ? "block" : "none", "important");
+ });
+}
+
+async function onAcceptInvite({ status }) {
+ if (status === "going") {
+ showCustomBBCode(true);
+ document.body.classList.add("confirmed-event-assistance");
+ } else if (status !== "going") {
+ showCustomBBCode(false);
+ document.body.classList.remove("confirmed-event-assistance");
+ }
+}
+
+function overrideChat(api, container) {
+ const siteSettings = container.lookup("service:site-settings");
+ const currentUser = api.getCurrentUser();
+ const chatService = container.lookup("service:chat");
+ const appEvents = container.lookup("service:appEvents");
+
+ if (
+ !siteSettings.livestream_enabled ||
+ !currentUser ||
+ !siteSettings.chat_enabled ||
+ !chatService.userCanChat
+ ) {
+ return;
+ }
+
+ const events = [
+ "calendar:update-invitee-status",
+ "calendar:create-invitee-status",
+ "calendar:invitee-left-event",
+ ];
+
+ events.forEach((event) => {
+ appEvents.on(event, (data) => {
+ onAcceptInvite({
+ ...data,
+ });
+ });
+ });
+
+ api.headerIcons.add("livestream", ResponsiveLivestreamChatIcon, {
+ before: "chat",
+ });
+
+ api.onPageChange((url) => {
+ const store = container.lookup("service:store");
+ const topic = container.lookup("controller:topic");
+ const allowedPaths =
+ siteSettings.livestream_embeddable_chat_allowed_paths.split("|");
+
+ // non livestream topics
+ if (
+ allowedPaths.every(
+ (path) => !url.includes(path) && !url.startsWith(path)
+ ) ||
+ !topic?.model?.chat_channel_id
+ ) {
+ return false;
+ }
+
+ updateEventStylesByStatus(topic, store, currentUser);
+ });
+}
+
+async function updateEventStylesByStatus(topic, store, currentUser) {
+ let isGoing;
+ try {
+ const topicPost = parseInt(
+ document.getElementById("post_1").dataset.postId,
+ 10
+ );
+ const attendees = await store.findAll("discourse-post-event-invitee", {
+ undefined,
+ post_id: topicPost,
+ type: "going",
+ });
+ isGoing = attendees.content.some(
+ (attendee) => attendee.user.id === currentUser.id
+ );
+ showCustomBBCode(isGoing);
+ } catch {
+ // no event found
+ } finally {
+ if (!isGoing) {
+ document.body.classList.remove("confirmed-event-assistance");
+ } else {
+ document.body.classList.add("confirmed-event-assistance");
+ }
+ }
+}
+
+export default {
+ name: "discourse-calendar-livestream-chat-sidebar",
+ initialize(container) {
+ withPluginApi((api) => {
+ overrideChat(api, container);
+ });
+ },
+};
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/lib/discourse-markdown/livestream-preview.js b/plugins/discourse-calendar/assets/javascripts/discourse/lib/discourse-markdown/livestream-preview.js
new file mode 100644
index 00000000000..bf6bba8d5c5
--- /dev/null
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/lib/discourse-markdown/livestream-preview.js
@@ -0,0 +1,47 @@
+function setupMarkdownIt(helper) {
+ helper.registerOptions((opts, siteSettings) => {
+ opts.features["livestream-preview"] = !!siteSettings.livestream_enabled;
+ });
+
+ helper.registerPlugin((md) => {
+ // Gated on livestream_enabled so that when it is disabled the standalone
+ // discourse-livestream plugin (if installed) owns these bbcode tags
+ // instead, avoiding duplicate registration.
+ if (!md.options.discourse.features["livestream-preview"]) {
+ return;
+ }
+
+ md.inline.bbcode.ruler.push("preview", {
+ tag: "preview",
+ wrap: "span.preview",
+ });
+
+ md.block.bbcode.ruler.push("preview", {
+ tag: "preview",
+ wrap: "div.preview",
+ });
+
+ md.inline.bbcode.ruler.push("hidden", {
+ tag: "hidden",
+ wrap: "span.hidden",
+ });
+
+ md.block.bbcode.ruler.push("hidden", {
+ tag: "hidden",
+ wrap: "div.hidden",
+ });
+ });
+}
+
+export function setup(helper) {
+ helper.allowList([
+ "span.preview",
+ "div.preview",
+ "span.hidden",
+ "div.hidden",
+ ]);
+
+ if (helper.markdownIt) {
+ setupMarkdownIt(helper);
+ }
+}
diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/services/embeddable-chat.gjs b/plugins/discourse-calendar/assets/javascripts/discourse/services/embeddable-chat.gjs
new file mode 100644
index 00000000000..90c8b3136e2
--- /dev/null
+++ b/plugins/discourse-calendar/assets/javascripts/discourse/services/embeddable-chat.gjs
@@ -0,0 +1,71 @@
+import { tracked } from "@glimmer/tracking";
+import { action } from "@ember/object";
+import { service } from "@ember/service";
+import Chat from "discourse/plugins/chat/discourse/services/chat";
+
+export const LIVESTREAM_TAG_NAME = "livestream";
+
+export default class EmbeddableChat extends Chat {
+ @service siteSettings;
+ @service router;
+ @service currentUser;
+ @service capabilities;
+
+ @tracked isMobileChatVisible = false;
+
+ canRenderChatChannel(topicController, mobileViewAllowed = false) {
+ this.topicController = topicController;
+ if (
+ this.siteSettings.livestream_enabled &&
+ this.isMobileViewport === mobileViewAllowed &&
+ this.siteSettings.chat_enabled &&
+ this.currentUser &&
+ this.userCanChat
+ ) {
+ const allowedPaths =
+ this.siteSettings.livestream_embeddable_chat_allowed_paths.split("|");
+ const withinPathsAllowed = allowedPaths.some(
+ (path) =>
+ this.router.currentURL.includes(path) ||
+ this.router.currentURL.startsWith(path)
+ );
+
+ if (withinPathsAllowed && this.topicController?.model?.chat_channel_id) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ @action
+ toggleChatVisibility() {
+ this.isMobileChatVisible = !this.isMobileChatVisible;
+ }
+
+ topicHasLivestreamTag(topic) {
+ return (
+ // TODO(https://github.com/discourse/discourse/pull/36678): The string check can be
+ // removed using .discourse-compatibility once the PR is merged.
+ topic?.tags?.some?.((tag) => {
+ const tagName = typeof tag === "string" ? tag : tag.name;
+ return tagName === LIVESTREAM_TAG_NAME;
+ }) || false
+ );
+ }
+
+ get isMobileModal() {
+ return (
+ this.siteSettings.livestream_enable_modal_chat_on_mobile &&
+ this.isMobileViewport
+ );
+ }
+
+ get isMobileViewport() {
+ return !this.capabilities.viewport.lg;
+ }
+
+ get chatChannelId() {
+ return this.topicController?.model?.chat_channel_id;
+ }
+}
diff --git a/plugins/discourse-calendar/assets/stylesheets/common/livestream.scss b/plugins/discourse-calendar/assets/stylesheets/common/livestream.scss
new file mode 100644
index 00000000000..24e8126897b
--- /dev/null
+++ b/plugins/discourse-calendar/assets/stylesheets/common/livestream.scss
@@ -0,0 +1,40 @@
+body.chat-enabled.tag-livestream.confirmed-event-assistance {
+ #post_1 {
+ .topic-avatar {
+ display: none;
+ }
+
+ .topic-meta-data {
+ margin: 0;
+ }
+ }
+
+ .topic-navigation {
+ display: none;
+ }
+
+ .container.posts {
+ width: 100%;
+
+ .topic-body {
+ width: 100%;
+ margin-right: 1em;
+ }
+ }
+}
+
+body.chat-enabled.tag-livestream .chat-channel-preview-card {
+ .chat-channel-preview-card__browse-all {
+ display: none;
+ }
+
+ .chat-channel-title {
+ display: none;
+ }
+}
+
+.livestream-chat-modal {
+ .chat-drawer {
+ width: 100%;
+ }
+}
diff --git a/plugins/discourse-calendar/assets/stylesheets/desktop/livestream.scss b/plugins/discourse-calendar/assets/stylesheets/desktop/livestream.scss
new file mode 100644
index 00000000000..d38d91a9199
--- /dev/null
+++ b/plugins/discourse-calendar/assets/stylesheets/desktop/livestream.scss
@@ -0,0 +1,42 @@
+.tag-livestream.chat-enabled.archetype-regular {
+ // we need to fullscreen the header icons to match the chat panel
+ // being on the right side of the screen
+ .d-header-wrap .wrap {
+ margin: 0;
+ max-width: 100%;
+ }
+
+ #main-outlet-wrapper {
+ grid-template-areas:
+ "sidebar content livestream"
+ "sidebar below-content livestream";
+ margin: 0;
+ padding-right: 0;
+ max-width: 100%;
+ }
+
+ #custom-chat-container {
+ height: calc(
+ var(--composer-vh, var(--1dvh)) * 100 - var(--header-offset, 0px)
+ ) !important;
+ grid-area: livestream;
+ border-left: 2px solid var(--primary-very-low);
+ display: flex;
+ position: sticky;
+ top: var(--header-offset);
+ background: var(--d-content-background, var(--secondary));
+
+ --1dvh: 1vh;
+ align-self: start;
+ overflow-y: auto;
+ margin-left: 1em;
+
+ .livestream-chat-close {
+ display: none;
+ }
+
+ .chat-drawer {
+ height: 100%;
+ }
+ }
+}
diff --git a/plugins/discourse-calendar/assets/stylesheets/mobile/livestream.scss b/plugins/discourse-calendar/assets/stylesheets/mobile/livestream.scss
new file mode 100644
index 00000000000..9a695817cf7
--- /dev/null
+++ b/plugins/discourse-calendar/assets/stylesheets/mobile/livestream.scss
@@ -0,0 +1,168 @@
+body.tag-livestream.chat-enabled {
+ .d-header-icons {
+ .livestream-header-icon {
+ position: relative;
+
+ &::after {
+ content: "";
+ position: absolute;
+ top: 9px;
+ right: 16px;
+ width: 8px;
+ height: 8px;
+ border-radius: 50%;
+ background-color: rgb(255, 0, 0);
+ animation: pulse-circle 3s infinite;
+ }
+
+ @keyframes pulse-circle {
+ 0% {
+ transform: scale(1);
+ background-color: rgb(255, 0, 0, 0.9);
+ }
+
+ 50% {
+ transform: scale(1.3);
+ background-color: rgb(255, 0, 0, 0.75);
+ }
+
+ 100% {
+ transform: scale(1);
+ background-color: rgb(255, 0, 0, 0.9);
+ }
+ }
+ }
+ }
+
+ #custom-chat-container {
+ .livestream-chat-close {
+ display: none;
+ }
+
+ .chat-composer__wrapper {
+ border-top: 1px solid var(--primary-200);
+ background-color: var(--secondary);
+ }
+ }
+
+ #custom-chat-container.no-modal-mobile {
+ display: none;
+ }
+
+ #custom-chat-container.mobile.no-modal-mobile {
+ display: flex;
+ flex-direction: column;
+ position: fixed;
+ bottom: 0;
+ left: 0;
+ right: 0;
+ width: 100%;
+ max-height: 50%;
+ height: 400px;
+ background: var(--d-content-background, var(--secondary));
+ z-index: 50;
+ resize: both;
+ border-top: 1px solid var(--primary-low);
+
+ html.keyboard-visible & {
+ max-height: none;
+ height: calc(var(--composer-vh, 1vh) * 100 - var(--header-offset, 0px));
+ }
+
+ // Fix for safari keyboard popup based on core: discourse/app/assets/stylesheets/common/base/compose.scss
+ html.keyboard-visible.ios-device & {
+ top: calc(var(--composer-vh, 1vh) * 100);
+ transform: translateY(-100%);
+ bottom: unset;
+ }
+
+ .c-navbar-container {
+ display: flex;
+ padding-top: 0.3rem;
+ padding-bottom: 0.3rem;
+ width: 100%;
+ height: 2rem;
+ }
+
+ .chat-drawer {
+ width: 100%;
+ height: 100%;
+
+ .chat-channel {
+ height: 100%;
+
+ .chat-messages-scroller {
+ width: 100%;
+ }
+
+ .chat-composer__wrapper {
+ position: relative;
+ bottom: 0;
+ }
+ }
+
+ html.keyboard-visible & {
+ min-height: 200px !important; // avoid losing the text box when virtual keyboard is visible
+ }
+ }
+
+ .c-navbar__close-drawer-button {
+ position: absolute;
+ right: 0;
+ top: 50%;
+ transform: translateY(-50%);
+ }
+
+ .chat-composer__wrapper {
+ position: fixed;
+ bottom: 0;
+ background: var(--d-content-background, var(--secondary));
+ width: 100%;
+
+ #channel-composer {
+ height: auto !important;
+ }
+ }
+ }
+
+ .livestream-chat-modal {
+ // don't blur the background when the livestream chat modal is open
+ & ~ .d-modal__backdrop {
+ background-color: unset;
+ animation: none;
+ bottom: 0;
+ }
+
+ .d-modal__container {
+ height: 400px;
+ }
+
+ .d-modal__body {
+ flex-grow: 1;
+ padding: 1em 0.4em 0.4em 0.4em;
+ overflow: hidden;
+ }
+
+ #custom-chat-container {
+ width: 100%;
+ height: 100%;
+
+ .chat-drawer {
+ height: 100%;
+ width: 100%;
+
+ .chat-channel {
+ height: 100%;
+ }
+ }
+
+ .chat-channel-preview-card p {
+ margin-bottom: 0;
+ }
+ }
+ }
+}
+
+body:not(.tag-livestream) .d-header-icons .livestream-header-icon {
+ display: none;
+}
diff --git a/plugins/discourse-calendar/config/locales/client.en.yml b/plugins/discourse-calendar/config/locales/client.en.yml
index 0e26f53ea6d..6ef56cb2fd2 100644
--- a/plugins/discourse-calendar/config/locales/client.en.yml
+++ b/plugins/discourse-calendar/config/locales/client.en.yml
@@ -27,6 +27,11 @@ en:
topic_id:
label: Topic ID
discourse_calendar:
+ livestream:
+ chat:
+ going: "Click Going"
+ join_channel_header: "🌟 Join the Buzz! 🌟"
+ join_channel_message: "Confirm your attendance and unlock our event chat. Dive into discussions, share your excitement, and connect with fellow attendees!"
invite_user_notification: "%{username} invited you to: %{description}"
preferences:
all_events: "Discourse Calendar - All Events"
diff --git a/plugins/discourse-calendar/config/locales/server.en.yml b/plugins/discourse-calendar/config/locales/server.en.yml
index bcc190e3f37..084bd4e19e2 100644
--- a/plugins/discourse-calendar/config/locales/server.en.yml
+++ b/plugins/discourse-calendar/config/locales/server.en.yml
@@ -70,6 +70,10 @@ en:
sidebar_show_upcoming_events: "Show upcoming events link in the sidebar under 'More'."
calendar_first_day_of_week: "Set the first day of the week for calendar display. Options are Saturday, Sunday, or Monday."
enable_events_category_type_setup: "Enables the Events category type option during category creation setup."
+ livestream_enabled: "Enable livestream chat for topics tagged 'livestream'."
+ livestream_chat_allowed_groups: "Only allowlisted groups can chat in the livestream channel."
+ livestream_embeddable_chat_allowed_paths: "Allowed paths for the embeddable livestream chat."
+ livestream_enable_modal_chat_on_mobile: "Show livestream chat in a modal on mobile."
discourse_calendar:
category_type:
diff --git a/plugins/discourse-calendar/config/settings.yml b/plugins/discourse-calendar/config/settings.yml
index 68ff195bd56..4cf75914313 100644
--- a/plugins/discourse-calendar/config/settings.yml
+++ b/plugins/discourse-calendar/config/settings.yml
@@ -139,3 +139,21 @@ discourse_calendar:
status: "alpha"
impact: "feature,staff"
learn_more_url: "https://meta.discourse.org/t/-/401309"
+ livestream_enabled:
+ default: false
+ client: true
+ hidden: true
+ livestream_embeddable_chat_allowed_paths:
+ type: list
+ default: "/t"
+ client: true
+ hidden: true
+ livestream_enable_modal_chat_on_mobile:
+ default: true
+ client: true
+ hidden: true
+ livestream_chat_allowed_groups:
+ default: "11" # default group trust_level_1
+ client: true
+ type: group_list
+ hidden: true
\ No newline at end of file
diff --git a/plugins/discourse-calendar/db/migrate/20260603013342_add_livestream_tables_to_calendar.rb b/plugins/discourse-calendar/db/migrate/20260603013342_add_livestream_tables_to_calendar.rb
new file mode 100644
index 00000000000..a95500cddfb
--- /dev/null
+++ b/plugins/discourse-calendar/db/migrate/20260603013342_add_livestream_tables_to_calendar.rb
@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+class AddLivestreamTablesToCalendar < ActiveRecord::Migration[8.0]
+ # NOTE: This migration is a no-op for sites that had previously
+ # installed the discourse-livestream plugin.
+ def up
+ if !table_exists?(:livestream_topic_chat_channels)
+ create_table :livestream_topic_chat_channels do |t|
+ t.bigint :topic_id, null: false
+ t.bigint :chat_channel_id, null: false
+ t.timestamps
+ end
+
+ add_index :livestream_topic_chat_channels,
+ %i[topic_id chat_channel_id],
+ unique: true,
+ name: "unique_livestream_topic_chat_channels"
+ end
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/plugins/discourse-calendar/jobs/regular/livestream/recalculate_user_channel_memberships.rb b/plugins/discourse-calendar/jobs/regular/livestream/recalculate_user_channel_memberships.rb
new file mode 100644
index 00000000000..26768fbb0ed
--- /dev/null
+++ b/plugins/discourse-calendar/jobs/regular/livestream/recalculate_user_channel_memberships.rb
@@ -0,0 +1,50 @@
+# frozen_string_literal: true
+
+module Jobs
+ class LivestreamRecalculateUserChannelMemberships < ::Jobs::Base
+ def execute
+ if !SiteSetting.calendar_enabled || !SiteSetting.discourse_post_event_enabled ||
+ !SiteSetting.livestream_enabled
+ return
+ end
+
+ going = DiscoursePostEvent::Invitee.statuses[:going]
+ query = <<~SQL
+ WITH attending_users AS (
+ SELECT u.*
+ FROM discourse_post_event_invitees dpei
+ JOIN users u ON dpei.user_id = u.id
+ WHERE dpei.status = :going
+ )
+ SELECT uccm.*
+ FROM livestream_topic_chat_channels ltcc
+ JOIN chat_channels cc ON cc.id = ltcc.chat_channel_id
+ JOIN user_chat_channel_memberships uccm ON cc.id = uccm.chat_channel_id
+ JOIN attending_users au ON au.id = uccm.user_id
+ SQL
+ memberships = ::Chat::UserChatChannelMembership.find_by_sql([query, { going: going }])
+
+ memberships.each do |membership|
+ user = membership.user
+ ActiveRecord::Base.transaction do
+ is_user_allowed_in_livestream_chat = user_allowed_in_livestream_chat?(user)
+ if membership.following != is_user_allowed_in_livestream_chat
+ membership.update!(following: is_user_allowed_in_livestream_chat)
+ ::Chat::ChannelMembershipManager.new(membership.chat_channel).recalculate_user_count
+ end
+ end
+
+ ::DiscourseCalendar::Livestream.publish_livestream_chat_status(
+ membership.reload,
+ user: user,
+ )
+ end
+ end
+
+ private
+
+ def user_allowed_in_livestream_chat?(user)
+ user.in_any_groups?(SiteSetting.livestream_chat_allowed_groups_map)
+ end
+ end
+end
diff --git a/plugins/discourse-calendar/lib/discourse_calendar/livestream/chat_channel_extension.rb b/plugins/discourse-calendar/lib/discourse_calendar/livestream/chat_channel_extension.rb
new file mode 100644
index 00000000000..aee67d01445
--- /dev/null
+++ b/plugins/discourse-calendar/lib/discourse_calendar/livestream/chat_channel_extension.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+module DiscourseCalendar
+ module Livestream
+ module ChatChannelExtension
+ extend ActiveSupport::Concern
+
+ prepended do
+ has_one :livestream_topic_chat_channel,
+ class_name: "DiscourseCalendar::Livestream::TopicChatChannel",
+ dependent: :destroy,
+ foreign_key: :chat_channel_id
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-calendar/lib/discourse_calendar/livestream/topic_extension.rb b/plugins/discourse-calendar/lib/discourse_calendar/livestream/topic_extension.rb
new file mode 100644
index 00000000000..82a83e9b5d1
--- /dev/null
+++ b/plugins/discourse-calendar/lib/discourse_calendar/livestream/topic_extension.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+module DiscourseCalendar
+ module Livestream
+ module TopicExtension
+ extend ActiveSupport::Concern
+
+ prepended do
+ has_one :topic_chat_channel,
+ class_name: "DiscourseCalendar::Livestream::TopicChatChannel",
+ dependent: :destroy
+
+ after_update do
+ chat_channel = topic_chat_channel&.chat_channel
+
+ next if chat_channel.nil?
+ next if chat_channel.chatable_id == category_id
+
+ chat_channel.update!(chatable_id: category_id)
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-calendar/plugin.rb b/plugins/discourse-calendar/plugin.rb
index 51a6cafe2c4..0ae32525c8b 100644
--- a/plugins/discourse-calendar/plugin.rb
+++ b/plugins/discourse-calendar/plugin.rb
@@ -33,6 +33,9 @@ register_asset "stylesheets/mobile/discourse-post-event.scss", :mobile
register_asset "stylesheets/colors.scss", :color_definitions
register_asset "stylesheets/common/user-preferences.scss"
register_asset "stylesheets/common/upcoming-events-list.scss"
+register_asset "stylesheets/common/livestream.scss"
+register_asset "stylesheets/desktop/livestream.scss", :desktop
+register_asset "stylesheets/mobile/livestream.scss", :mobile
register_svg_icon "calendar-day"
register_svg_icon "clock"
register_svg_icon "file-csv"
@@ -59,6 +62,41 @@ module ::DiscourseCalendar
# List of groups
GROUP_TIMEZONES_CUSTOM_FIELD = "group-timezones"
+ module Livestream
+ LIVESTREAM_CHAT_STATUS_MESSAGE_BUS_CHANNEL = "/discourse-calendar/livestream/chat-status"
+
+ def self.handle_topic_chat_channel_creation(topic)
+ return if topic.category.blank?
+ return if DiscourseCalendar::Livestream::TopicChatChannel.exists?(topic_id: topic.id)
+ return if topic.tags.blank? || topic.tags.none? { |tag| tag.name == "livestream" }
+
+ channel =
+ Chat::Channel.create!(
+ chatable_id: topic.category.id,
+ chatable_type: "Category",
+ name: topic.title,
+ status: Chat::Channel.statuses[:open],
+ type: "CategoryChannel",
+ allow_channel_wide_mentions: true,
+ )
+
+ DiscourseCalendar::Livestream::TopicChatChannel.create!(topic: topic, chat_channel: channel)
+ channel.user_chat_channel_memberships.create!(user: topic.user, following: false)
+ end
+
+ def self.livestream_chat_status_channel(user_id)
+ "#{LIVESTREAM_CHAT_STATUS_MESSAGE_BUS_CHANNEL}/#{user_id}"
+ end
+
+ def self.publish_livestream_chat_status(membership, user:)
+ MessageBus.publish(
+ livestream_chat_status_channel(user.id),
+ Chat::UserChannelMembershipSerializer.new(membership, scope: user.guardian).to_json,
+ user_ids: [user.id],
+ )
+ end
+ end
+
def self.users_on_holiday
PluginStore.get(PLUGIN_NAME, USERS_ON_HOLIDAY_KEY) || []
end
@@ -78,6 +116,8 @@ module ::DiscoursePostEvent
end
require_relative "lib/discourse_calendar/engine"
+require_relative "lib/discourse_calendar/livestream/topic_extension"
+require_relative "lib/discourse_calendar/livestream/chat_channel_extension"
Dir
.glob(File.expand_path("../lib/discourse_calendar/site_settings/*.rb", __FILE__))
@@ -133,6 +173,7 @@ after_initialize do
require_relative "jobs/regular/discourse_post_event/bulk_invite"
require_relative "jobs/regular/discourse_post_event/bump_topic"
require_relative "jobs/regular/discourse_post_event/send_reminder"
+ require_relative "jobs/regular/livestream/recalculate_user_channel_memberships"
require_relative "lib/discourse_post_event/engine"
require_relative "lib/discourse_post_event/event_finder"
require_relative "lib/discourse_post_event/event_parser"
@@ -180,6 +221,8 @@ after_initialize do
Jobs::ExportCsvFile.prepend(DiscoursePostEvent::ExportPostEventCsvReportExtension)
Post.prepend(DiscoursePostEvent::PostExtension)
::WebHook.prepend(DiscoursePostEvent::WebHookExtension)
+ Topic.prepend(DiscourseCalendar::Livestream::TopicExtension)
+ Chat::Channel.prepend(DiscourseCalendar::Livestream::ChatChannelExtension)
end
add_to_class(:user, :can_create_discourse_post_event?) do
@@ -802,4 +845,62 @@ after_initialize do
)
end
end
+
+ # DISCOURSE LIVESTREAM
+
+ add_to_serializer(
+ :topic_view,
+ :chat_channel_id,
+ include_condition: -> { SiteSetting.livestream_enabled },
+ ) do
+ return nil if object.topic.topic_chat_channel.blank?
+ object.topic.topic_chat_channel.chat_channel_id
+ end
+
+ on(:post_edited) do |post, _, _|
+ if SiteSetting.livestream_enabled
+ DiscourseCalendar::Livestream.handle_topic_chat_channel_creation(post.topic)
+ end
+ end
+ on(:topic_created) do |topic, _, _|
+ if SiteSetting.livestream_enabled
+ DiscourseCalendar::Livestream.handle_topic_chat_channel_creation(topic)
+ end
+ end
+ on(:chat_channel_trashed) do |channel, user|
+ if SiteSetting.livestream_enabled
+ # If the chat channel is deleted, delete the related TopicChatChannel record
+ DiscourseCalendar::Livestream::TopicChatChannel.where(chat_channel_id: channel.id).destroy_all
+ end
+ end
+
+ on(:discourse_calendar_post_event_invitee_status_changed) do |invitee|
+ next if !SiteSetting.livestream_enabled
+
+ topic = invitee.event.post.topic
+ topic_chat_channel = topic.topic_chat_channel
+
+ next if !topic_chat_channel
+
+ user = User.find(invitee.user_id)
+ channel = topic_chat_channel.chat_channel
+ manager = Chat::ChannelMembershipManager.new(channel)
+
+ user_allowed_in_chat = user.in_any_groups?(SiteSetting.livestream_chat_allowed_groups_map)
+
+ membership =
+ if invitee.status == DiscoursePostEvent::Invitee.statuses[:going]
+ user_allowed_in_chat ? manager.follow(user) : manager.unfollow(user)
+ else
+ manager.unfollow(user)
+ end
+
+ DiscourseCalendar::Livestream.publish_livestream_chat_status(membership, user: user)
+ end
+
+ on(:site_setting_changed) do |name, old_val, new_val|
+ if name == :livestream_chat_allowed_groups && SiteSetting.livestream_enabled
+ Jobs::LivestreamRecalculateUserChannelMemberships.new.execute
+ end
+ end
end
diff --git a/plugins/discourse-calendar/spec/fabricators/post_event_invitee_fabricator.rb b/plugins/discourse-calendar/spec/fabricators/post_event_invitee_fabricator.rb
new file mode 100644
index 00000000000..97c9a9999a6
--- /dev/null
+++ b/plugins/discourse-calendar/spec/fabricators/post_event_invitee_fabricator.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+Fabricator(:post_event_invitee, from: "DiscoursePostEvent::Invitee") do
+ event
+ user
+ status { |attrs| attrs[:status] || nil }
+end
diff --git a/plugins/discourse-calendar/spec/fabricators/topic_chat_channel_fabricator.rb b/plugins/discourse-calendar/spec/fabricators/topic_chat_channel_fabricator.rb
new file mode 100644
index 00000000000..f930776e375
--- /dev/null
+++ b/plugins/discourse-calendar/spec/fabricators/topic_chat_channel_fabricator.rb
@@ -0,0 +1,6 @@
+# frozen_string_literal: true
+
+Fabricator(:topic_chat_channel, from: "DiscourseCalendar::Livestream::TopicChatChannel") do
+ topic
+ chat_channel
+end
diff --git a/plugins/discourse-calendar/spec/jobs/livestream/recalculate_user_channel_memberships_spec.rb b/plugins/discourse-calendar/spec/jobs/livestream/recalculate_user_channel_memberships_spec.rb
new file mode 100644
index 00000000000..1767abb3544
--- /dev/null
+++ b/plugins/discourse-calendar/spec/jobs/livestream/recalculate_user_channel_memberships_spec.rb
@@ -0,0 +1,122 @@
+# frozen_string_literal: true
+
+RSpec.describe Jobs::LivestreamRecalculateUserChannelMemberships do
+ def run_job
+ Jobs::LivestreamRecalculateUserChannelMemberships.new.execute
+ end
+
+ fab!(:admin)
+ fab!(:user) { Fabricate(:user, trust_level: TrustLevel[1]) }
+
+ fab!(:livestream_channel1, :category_channel)
+ fab!(:livestream_channel2, :category_channel)
+
+ fab!(:normal_channel1, :category_channel)
+ fab!(:normal_channel2, :category_channel)
+
+ fab!(:topic1) { Fabricate(:topic, user: user) }
+ fab!(:topic2) { Fabricate(:topic, user: user) }
+
+ fab!(:topic_chat_channel1) do
+ Fabricate(:topic_chat_channel, topic: topic1, chat_channel: livestream_channel1)
+ end
+ fab!(:topic_chat_channel2) do
+ Fabricate(:topic_chat_channel, topic: topic2, chat_channel: livestream_channel2)
+ end
+
+ fab!(:first_post1) { Fabricate(:post, topic: topic1) }
+ fab!(:first_post2) { Fabricate(:post, topic: topic2) }
+
+ fab!(:normal_membership1) do
+ Fabricate(
+ :user_chat_channel_membership,
+ user: user,
+ chat_channel: normal_channel1,
+ following: true,
+ )
+ end
+
+ fab!(:normal_membership2) do
+ Fabricate(
+ :user_chat_channel_membership,
+ user: user,
+ chat_channel: normal_channel2,
+ following: false,
+ )
+ end
+
+ fab!(:event1) do
+ Fabricate(
+ :event,
+ post: first_post1,
+ original_starts_at: Time.now + 1.hour,
+ original_ends_at: Time.now + 2.hours,
+ )
+ end
+
+ fab!(:event2) do
+ Fabricate(
+ :event,
+ post: first_post2,
+ original_starts_at: Time.now + 1.hour,
+ original_ends_at: Time.now + 2.hours,
+ )
+ end
+
+ fab!(:livestream_membership1) do
+ Fabricate(:user_chat_channel_membership, user: user, chat_channel: livestream_channel1)
+ end
+
+ fab!(:livestream_membership2) do
+ Fabricate(:user_chat_channel_membership, user: user, chat_channel: livestream_channel2)
+ end
+
+ fab!(:post_event_invitee1) do
+ Fabricate(
+ :post_event_invitee,
+ event: event1,
+ user: user,
+ status: DiscoursePostEvent::Invitee.statuses[:going],
+ )
+ end
+
+ fab!(:post_event_invitee2) do
+ Fabricate(
+ :post_event_invitee,
+ event: event2,
+ user: user,
+ status: DiscoursePostEvent::Invitee.statuses[:going],
+ )
+ end
+ before do
+ SiteSetting.livestream_enabled = true
+ SiteSetting.calendar_enabled = true
+ SiteSetting.discourse_post_event_enabled = true
+ end
+
+ describe "when user is not in the allow list" do
+ before { SiteSetting.livestream_chat_allowed_groups = Group::AUTO_GROUPS[:trust_level_4].to_s }
+
+ it "unfollows all livestream channels" do
+ run_job
+
+ expect(livestream_membership1.following).to eq(false)
+ expect(livestream_membership2.following).to eq(false)
+ expect(normal_membership1.following).to eq(true)
+ expect(normal_membership2.following).to eq(false)
+ expect(Chat::UserChatChannelMembership.count).to eq(4)
+ end
+
+ it "publishes membership changes only to the affected user's message bus channel" do
+ messages = MessageBus.track_publish { run_job }
+
+ expect(messages).not_to be_empty
+ expect(messages).to all(
+ have_attributes(
+ channel: "/discourse-calendar/livestream/chat-status/#{user.id}",
+ user_ids: [user.id],
+ ),
+ )
+ end
+ end
+end
diff --git a/plugins/discourse-calendar/spec/lib/discourse_calendar/livestream/handle_topic_chat_channel_creation_spec.rb b/plugins/discourse-calendar/spec/lib/discourse_calendar/livestream/handle_topic_chat_channel_creation_spec.rb
new file mode 100644
index 00000000000..22e777de35d
--- /dev/null
+++ b/plugins/discourse-calendar/spec/lib/discourse_calendar/livestream/handle_topic_chat_channel_creation_spec.rb
@@ -0,0 +1,119 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseCalendar::Livestream do
+ describe ".handle_topic_chat_channel_creation" do
+ let(:topic) { Fabricate(:topic) }
+
+ context "when the topic has no category" do
+ it "does not create a chat channel" do
+ described_class.handle_topic_chat_channel_creation(topic)
+
+ expect(Chat::Channel.count).to eq(0)
+ end
+ end
+
+ context "when the topic has a category" do
+ let(:category) { Fabricate(:category) }
+
+ before { topic.update!(category: category) }
+
+ context "when the topic has no 'livestream' tag" do
+ it "does not create a chat channel" do
+ described_class.handle_topic_chat_channel_creation(topic)
+
+ expect(Chat::Channel.count).to eq(0)
+ end
+ end
+
+ context "when the topic has a 'livestream' tag" do
+ let(:tag) { Fabricate(:tag, name: "livestream") }
+
+ before do
+ SiteSetting.calendar_enabled = true
+ SiteSetting.livestream_enabled = true
+ topic.tags << tag
+ end
+
+ it "creates a chat channel" do
+ described_class.handle_topic_chat_channel_creation(topic)
+
+ expect(Chat::Channel.count).to eq(1)
+ expect(Chat::Channel.first.chatable).to eq(category)
+ end
+
+ it "creates a topic chat channel" do
+ described_class.handle_topic_chat_channel_creation(topic)
+
+ expect(DiscourseCalendar::Livestream::TopicChatChannel.count).to eq(1)
+ expect(DiscourseCalendar::Livestream::TopicChatChannel.first.topic).to eq(topic)
+ end
+
+ it "creates a user chat channel membership" do
+ described_class.handle_topic_chat_channel_creation(topic)
+
+ expect(Chat::UserChatChannelMembership.count).to eq(1)
+ expect(Chat::UserChatChannelMembership.first.user).to eq(topic.user)
+ end
+
+ it "deletes the chat channel and the topic chat channel when the topic is destroyed" do
+ described_class.handle_topic_chat_channel_creation(topic)
+
+ expect(Chat::Channel.count).to eq(1)
+
+ topic.destroy!
+
+ expect(Chat::Channel.count).to eq(0)
+ expect(DiscourseCalendar::Livestream::TopicChatChannel.count).to eq(0)
+ end
+
+ it "deletes the topic chat channel when the chat channel is destroyed" do
+ described_class.handle_topic_chat_channel_creation(topic)
+
+ expect(DiscourseCalendar::Livestream::TopicChatChannel.count).to eq(1)
+
+ Chat::Channel.first.destroy!
+
+ expect(DiscourseCalendar::Livestream::TopicChatChannel.count).to eq(0)
+ end
+
+ it "deletes the topic chat channel when the chat channel is soft deleted" do
+ described_class.handle_topic_chat_channel_creation(topic)
+ expect(DiscourseCalendar::Livestream::TopicChatChannel.count).to eq(1)
+ expect(Chat::Channel.count).to eq(1)
+
+ chat_channel = Chat::Channel.first
+ Chat::TrashChannel.call(
+ guardian: Guardian.new(Fabricate(:admin)),
+ params: {
+ channel_id: chat_channel.id,
+ },
+ )
+ expect(chat_channel.reload).to be_trashed
+ expect(DiscourseCalendar::Livestream::TopicChatChannel.count).to eq(0)
+ end
+
+ it "deletes the chat channel when topic chat channel is destroyed" do
+ described_class.handle_topic_chat_channel_creation(topic)
+
+ expect(DiscourseCalendar::Livestream::TopicChatChannel.count).to eq(1)
+
+ DiscourseCalendar::Livestream::TopicChatChannel.first.destroy!
+
+ expect(Chat::Channel.count).to eq(0)
+ end
+
+ it "updated the chat channel when the topic category is updated" do
+ described_class.handle_topic_chat_channel_creation(topic)
+
+ expect(Chat::Channel.first.chatable).to eq(category)
+
+ new_category = Fabricate(:category)
+
+ topic.update!(category: new_category)
+
+ expect(Chat::Channel.first.chatable).to eq(new_category)
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-calendar/spec/system/desktop_topic_livestream_authenticated_spec.rb b/plugins/discourse-calendar/spec/system/desktop_topic_livestream_authenticated_spec.rb
new file mode 100644
index 00000000000..53590c5fcda
--- /dev/null
+++ b/plugins/discourse-calendar/spec/system/desktop_topic_livestream_authenticated_spec.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+describe "Discourse Livestream - Topic Livestream - Desktop - Authenticated" do
+ fab!(:current_user) { Fabricate(:user, refresh_auto_groups: true) }
+ fab!(:category)
+ fab!(:livestream_tag) { Fabricate(:tag, name: "livestream") }
+ let(:topic_page) { PageObjects::Pages::Topic.new }
+ let(:composer) { PageObjects::Components::Composer.new }
+ let(:topic_livestream) { PageObjects::Pages::TopicLivestream.new }
+
+ before do
+ SiteSetting.calendar_enabled = true
+ SiteSetting.chat_enabled = true
+ SiteSetting.livestream_enabled = true
+ sign_in(current_user)
+ end
+
+ context "when in a topic view" do
+ it "creates a chat channel for livestream topics" do
+ topic_livestream.create_livestream_topic(composer, topic_page, livestream_tag)
+
+ expect(topic_page).to have_css("#custom-chat-container")
+ expect(topic_page).to have_css("#custom-chat-container .chat-channel-preview-card")
+ expect(topic_page).to have_text(
+ I18n.t("js.discourse_calendar.livestream.chat.join_channel_header"),
+ )
+ end
+
+ it "does not create a chat channel for regular topics" do
+ topic_livestream.create_regular_topic(composer, topic_page)
+
+ expect(topic_page).not_to have_css("#custom-chat-container")
+ expect(topic_page).not_to have_text(
+ I18n.t("js.discourse_calendar.livestream.chat.join_channel_header"),
+ )
+ end
+ end
+end
diff --git a/plugins/discourse-calendar/spec/system/livestream_calendar_event_spec.rb b/plugins/discourse-calendar/spec/system/livestream_calendar_event_spec.rb
new file mode 100644
index 00000000000..d14a7d4eb64
--- /dev/null
+++ b/plugins/discourse-calendar/spec/system/livestream_calendar_event_spec.rb
@@ -0,0 +1,43 @@
+# frozen_string_literal: true
+
+describe "Discourse Livestream - Topic Livestream with events - Authenticated" do
+ fab!(:group)
+ fab!(:current_user) { Fabricate(:user, trust_level: 1, groups: [group]) }
+ fab!(:category)
+ fab!(:livestream_tag) { Fabricate(:tag, name: "livestream") }
+ let(:topic_page) { PageObjects::Pages::Topic.new }
+ let(:composer) { PageObjects::Components::Composer.new }
+ let(:topic_livestream) { PageObjects::Pages::TopicLivestream.new }
+
+ before do
+ SiteSetting.calendar_enabled = true
+ SiteSetting.chat_enabled = true
+ SiteSetting.livestream_enabled = true
+ SiteSetting.discourse_post_event_enabled = true
+ SiteSetting.discourse_post_event_allowed_on_groups = group.id.to_s
+ SiteSetting.livestream_chat_allowed_groups = group.id.to_s
+ sign_in(current_user)
+ end
+
+ context "when in a event topic view" do
+ it "clicks going to join the chat channel for livestream topics" do
+ topic_livestream.create_livestream_event_topic(composer, topic_page, livestream_tag)
+
+ find(".going-button", wait: 25).click
+ expect(topic_page).to have_css(".confirmed-event-assistance", wait: 25)
+
+ find(".not-going-button").click
+ expect(topic_page).not_to have_css(".confirmed-event-assistance", wait: 25)
+ end
+
+ it "clicks going to join the chat channel for livestream topics" do
+ topic_livestream.create_normal_event_topic(composer, topic_page)
+
+ find(".going-button", wait: 25).click
+ expect(topic_page).to have_css(".confirmed-event-assistance", wait: 25)
+
+ find(".not-going-button").click
+ expect(topic_page).not_to have_css(".confirmed-event-assistance", wait: 25)
+ end
+ end
+end
diff --git a/plugins/discourse-calendar/spec/system/mobile_topic_livestream_authenticated_spec.rb b/plugins/discourse-calendar/spec/system/mobile_topic_livestream_authenticated_spec.rb
new file mode 100644
index 00000000000..b8cc4f7e87a
--- /dev/null
+++ b/plugins/discourse-calendar/spec/system/mobile_topic_livestream_authenticated_spec.rb
@@ -0,0 +1,72 @@
+# frozen_string_literal: true
+
+describe "Discourse Livestream - Topic Livestream - Mobile - Authenticated", mobile: true do
+ fab!(:admin)
+ fab!(:current_user) { Fabricate(:user, trust_level: TrustLevel[3]) }
+ fab!(:livestream_tag) { Fabricate(:tag, name: "livestream") }
+ fab!(:category)
+ let(:topic_page) { PageObjects::Pages::Topic.new }
+ let(:composer) { PageObjects::Components::Composer.new }
+ let(:topic_livestream) { PageObjects::Pages::TopicLivestream.new }
+
+ before do
+ SiteSetting.calendar_enabled = true
+ SiteSetting.chat_enabled = true
+ SiteSetting.livestream_enabled = true
+ SiteSetting.discourse_post_event_enabled = true
+ sign_in(admin)
+ end
+
+ context "when in a topic view" do
+ it "does not display livestream chat icon on regular topics" do
+ topic_livestream.create_regular_topic(composer, topic_page)
+ expect(topic_page).not_to have_css(".livestream-header-icon")
+ end
+
+ it "displays the livestream chat icon on livestream topics" do
+ topic_livestream.create_livestream_topic(composer, topic_page, livestream_tag)
+
+ expect(topic_page).to have_css(".chat-header-icon")
+
+ expect(topic_page).to have_css(".livestream-header-icon")
+ end
+
+ it "opens the chat channel and displays the preview after clicking the header icon" do
+ topic_livestream.create_livestream_topic(composer, topic_page, livestream_tag)
+
+ find(".livestream-header-icon").click
+ expect(topic_page).to have_css("#custom-chat-container")
+ expect(topic_page).to have_css(".chat-channel-preview-card")
+ end
+
+ context "when user in allowlisted group" do
+ it "opens the chat channel and allows to chat after clicking the header icon" do
+ SiteSetting.livestream_chat_allowed_groups = Group::AUTO_GROUPS[:admins].to_s
+ topic_livestream.create_livestream_event_topic(composer, topic_page, livestream_tag)
+
+ find(".going-button").click
+ find(".livestream-header-icon").click
+ expect(topic_page).to have_css("#custom-chat-container")
+ expect(topic_page).to have_css(".chat-drawer")
+
+ expect(topic_page).to have_css(".chat-composer")
+ end
+ end
+
+ context "when user not in allowlisted group" do
+ fab!(:group)
+
+ it "opens the chat channel and allows to chat after clicking the header icon" do
+ SiteSetting.livestream_chat_allowed_groups = group.id.to_s
+ topic_livestream.create_livestream_event_topic(composer, topic_page, livestream_tag)
+
+ find(".going-button").click
+ find(".livestream-header-icon").click
+ expect(topic_page).to have_css("#custom-chat-container")
+ expect(topic_page).to have_css(".chat-drawer")
+
+ expect(topic_page).not_to have_css(".chat-composer")
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-calendar/spec/system/page_objects/discourse_calendar/livestream/topic_livestream.rb b/plugins/discourse-calendar/spec/system/page_objects/discourse_calendar/livestream/topic_livestream.rb
new file mode 100644
index 00000000000..4b56875fdb5
--- /dev/null
+++ b/plugins/discourse-calendar/spec/system/page_objects/discourse_calendar/livestream/topic_livestream.rb
@@ -0,0 +1,61 @@
+# frozen_string_literal: true
+
+module PageObjects
+ module Pages
+ class TopicLivestream < PageObjects::Pages::Base
+ def create_livestream_topic(composer, topic_page, tag)
+ visit("/latest")
+ topic_page.open_new_topic
+
+ composer.fill_title("Creating a livestream topic")
+ tag_chooser = PageObjects::Components::SelectKit.new(".composer-fields .mini-tag-chooser")
+ tag_chooser.expand
+ tag_chooser.select_row_by_name(tag.name)
+ tag_chooser.collapse
+
+ composer.fill_content("The content for my livestream topic")
+ composer.create
+ end
+
+ def create_regular_topic(composer, topic_page)
+ visit("/latest")
+ topic_page.open_new_topic
+
+ composer.fill_title("Creating a regular topic")
+ composer.fill_content("The content for my regular topic")
+ composer.create
+ end
+
+ def create_livestream_event_topic(composer, topic_page, tag)
+ visit("/latest")
+ topic_page.open_new_topic
+
+ composer.fill_title("Creating a livestream event topic")
+ tag_chooser = PageObjects::Components::SelectKit.new(".composer-fields .mini-tag-chooser")
+ tag_chooser.expand
+ tag_chooser.select_row_by_name(tag.name)
+ tag_chooser.collapse
+
+ tomorrow = 1.day.from_now.strftime("%Y-%m-%d")
+ composer.fill_content <<~MD
+ [event start="#{tomorrow} 13:37" status="public"]
+ [/event]
+ MD
+ composer.create
+ end
+
+ def create_normal_event_topic(composer, topic_page)
+ visit("/latest")
+ topic_page.open_new_topic
+
+ composer.fill_title("Creating a normal event topic")
+ tomorrow = 1.day.from_now.strftime("%Y-%m-%d")
+ composer.fill_content <<~MD
+ [event start="#{tomorrow} 13:37" status="public"]
+ [/event]
+ MD
+ composer.create
+ end
+ end
+ end
+end