Skip to content

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

FEATURE: Add hosts to post events (#43129)

Gabriel Grubba542f29bf17af
diff --git a/db/structure.sql b/db/structure.sql
index d49e6c5d827..0fb7b805fc0 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -4478,6 +4478,39 @@ CREATE SEQUENCE public.discourse_post_event_events_id_seq
ALTER SEQUENCE public.discourse_post_event_events_id_seq OWNED BY public.discourse_post_event_events.id;
+--
+-- Name: discourse_post_event_hosts; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE public.discourse_post_event_hosts (
+ id bigint NOT NULL,
+ post_id bigint NOT NULL,
+ user_id integer NOT NULL,
+ "position" integer DEFAULT 0 NOT NULL,
+ created_at timestamp(6) without time zone NOT NULL,
+ updated_at timestamp(6) without time zone NOT NULL
+);
+
+
+--
+-- Name: discourse_post_event_hosts_id_seq; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE public.discourse_post_event_hosts_id_seq
+ START WITH 1
+ INCREMENT BY 1
+ NO MINVALUE
+ NO MAXVALUE
+ CACHE 1;
+
+
+--
+-- Name: discourse_post_event_hosts_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
+--
+
+ALTER SEQUENCE public.discourse_post_event_hosts_id_seq OWNED BY public.discourse_post_event_hosts.id;
+
+
--
-- Name: discourse_post_event_invitees; Type: TABLE; Schema: public; Owner: -
--
@@ -13833,6 +13866,13 @@ ALTER TABLE ONLY public.discourse_kanban_columns ALTER COLUMN id SET DEFAULT nex
ALTER TABLE ONLY public.discourse_post_event_events ALTER COLUMN id SET DEFAULT nextval('public.discourse_post_event_events_id_seq'::regclass);
+--
+-- Name: discourse_post_event_hosts id; Type: DEFAULT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY public.discourse_post_event_hosts ALTER COLUMN id SET DEFAULT nextval('public.discourse_post_event_hosts_id_seq'::regclass);
+
+
--
-- Name: discourse_post_event_invitees id; Type: DEFAULT; Schema: public; Owner: -
--
@@ -16317,6 +16357,14 @@ ALTER TABLE ONLY public.discourse_post_event_events
ADD CONSTRAINT discourse_post_event_events_pkey PRIMARY KEY (id);
+--
+-- Name: discourse_post_event_hosts discourse_post_event_hosts_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY public.discourse_post_event_hosts
+ ADD CONSTRAINT discourse_post_event_hosts_pkey PRIMARY KEY (id);
+
+
--
-- Name: discourse_post_event_invitees discourse_post_event_invitees_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
@@ -20479,6 +20527,20 @@ CREATE UNIQUE INDEX index_discourse_kanban_card_histories_one_view_per_user_day
CREATE INDEX index_discourse_post_event_events_on_image_upload_id ON public.discourse_post_event_events USING btree (image_upload_id);
+--
+-- Name: index_discourse_post_event_hosts_on_post_id_and_user_id; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE UNIQUE INDEX index_discourse_post_event_hosts_on_post_id_and_user_id ON public.discourse_post_event_hosts USING btree (post_id, user_id);
+
+
+--
+-- Name: index_discourse_post_event_hosts_on_user_id; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX index_discourse_post_event_hosts_on_user_id ON public.discourse_post_event_hosts USING btree (user_id);
+
+
--
-- Name: index_discourse_reactions_reaction_users_on_reaction_id; Type: INDEX; Schema: public; Owner: -
--
@@ -24344,6 +24406,7 @@ ALTER TABLE ONLY public.ad_plugin_house_ads_groups
SET search_path TO "$user", public;
INSERT INTO "schema_migrations" (version) VALUES
+('20260902150024'),
('20260901020329'),
('20260831162602'),
('20260828145150'),
diff --git a/plugins/discourse-events/app/models/discourse_events/events/event.rb b/plugins/discourse-events/app/models/discourse_events/events/event.rb
index 32e27b80963..b5c60994565 100644
--- a/plugins/discourse-events/app/models/discourse_events/events/event.rb
+++ b/plugins/discourse-events/app/models/discourse_events/events/event.rb
@@ -11,6 +11,7 @@ module DiscourseEvents
MIN_NAME_LENGTH = 5
MAX_NAME_LENGTH = 255
MAX_DESCRIPTION_LENGTH = 1000
+ MAX_HOSTS = 10
DEFAULT_TIMEZONE = "UTC"
self.table_name = "discourse_post_event_events"
@@ -20,6 +21,8 @@ module DiscourseEvents
# this is a cross plugin dependency, only called if chat is enabled
belongs_to :chat_channel, class_name: "Chat::Channel"
has_many :invitees, foreign_key: :post_id, dependent: :delete_all
+ has_many :event_hosts, -> { order(:position) }, foreign_key: :post_id, dependent: :delete_all
+ has_many :hosts, through: :event_hosts, source: :user
belongs_to :post, foreign_key: :id
belongs_to :image_upload, class_name: "Upload", optional: true
has_many :upload_references, as: :target, dependent: :destroy
@@ -31,6 +34,7 @@ module DiscourseEvents
before_save :chat_channel_sync
# prepend so it runs before `dependent: :delete_all` wipes the invitees
before_destroy :reset_invitees_topic_tracking, prepend: true
+ after_save :sync_hosts
after_commit :create_livestream_chat_channel, on: %i[create update]
after_commit :enqueue_warm_livestream_onebox, on: %i[create update]
after_commit :destroy_topic_custom_field, on: %i[destroy]
@@ -60,11 +64,22 @@ module DiscourseEvents
validate :ends_before_start
validate :allowed_custom_fields
validate :url_is_a_uri, if: :url_changed?
+ validate :hosts_are_valid
def self.attributes_protected_by_default
super - %w[id]
end
+ # Hosts are set as a list of user ids so that `update_with_params!` can
+ # carry them alongside the column attributes. `sync_hosts` applies them.
+ def host_user_ids
+ defined?(@host_user_ids) ? @host_user_ids : event_hosts.map(&:user_id)
+ end
+
+ def host_user_ids=(ids)
+ @host_user_ids = Array(ids).map(&:to_i).uniq
+ end
+
def reset_invalid_livestream
return unless livestream?
@@ -250,6 +265,24 @@ module DiscourseEvents
end
end
+ def hosts_are_valid
+ ids = host_user_ids
+ return if ids.blank?
+
+ if ids.length > MAX_HOSTS
+ errors.add(
+ :base,
+ I18n.t("discourse_post_event.errors.models.event.too_many_hosts", count: MAX_HOSTS),
+ )
+ return
+ end
+
+ known_ids = User.human_users.not_staged.where(id: ids).pluck(:id)
+ return if (ids - known_ids).empty?
+
+ errors.add(:base, I18n.t("discourse_post_event.errors.models.event.invalid_hosts"))
+ end
+
def ends_before_start
if original_starts_at && original_ends_at && original_starts_at >= original_ends_at
errors.add(
@@ -615,6 +648,40 @@ module DiscourseEvents
private
+ # Diff-replaces the host rows so `position` mirrors the order the ids
+ # arrived in, leaving rows that already match untouched.
+ def sync_hosts
+ return unless defined?(@host_user_ids)
+
+ desired = @host_user_ids
+ remove_instance_variable(:@host_user_ids)
+
+ existing = EventHost.where(post_id: id).index_by(&:user_id)
+ stale_ids = existing.keys - desired
+ EventHost.where(post_id: id, user_id: stale_ids).delete_all if stale_ids.present?
+
+ timestamp = Time.zone.now
+ inserts = []
+ desired.each_with_index do |user_id, position|
+ host = existing[user_id]
+
+ if host.nil?
+ inserts << {
+ post_id: id,
+ user_id:,
+ position:,
+ created_at: timestamp,
+ updated_at: timestamp,
+ }
+ elsif host.position != position
+ host.update_columns(position:, updated_at: timestamp)
+ end
+ end
+ EventHost.insert_all!(inserts) if inserts.present?
+
+ association(:event_hosts).reset
+ end
+
def reset_invitees_topic_tracking
topic_id = post&.topic_id
return if topic_id.nil?
diff --git a/plugins/discourse-events/app/models/discourse_events/events/event_host.rb b/plugins/discourse-events/app/models/discourse_events/events/event_host.rb
new file mode 100644
index 00000000000..ec4078f1a7f
--- /dev/null
+++ b/plugins/discourse-events/app/models/discourse_events/events/event_host.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+module DiscourseEvents
+ module Events
+ class EventHost < ActiveRecord::Base
+ self.table_name = "discourse_post_event_hosts"
+
+ belongs_to :event, foreign_key: :post_id
+ belongs_to :user
+ end
+ end
+end
+
+# == Schema Information
+#
+# Table name: discourse_post_event_hosts
+#
+# id :bigint not null, primary key
+# position :integer default(0), not null
+# created_at :datetime not null
+# updated_at :datetime not null
+# post_id :bigint not null
+# user_id :integer not null
+#
+# Indexes
+#
+# index_discourse_post_event_hosts_on_post_id_and_user_id (post_id,user_id) UNIQUE
+# index_discourse_post_event_hosts_on_user_id (user_id)
+#
diff --git a/plugins/discourse-events/app/serializers/discourse_events/events/event_host_serializer.rb b/plugins/discourse-events/app/serializers/discourse_events/events/event_host_serializer.rb
new file mode 100644
index 00000000000..eff81cd5ea9
--- /dev/null
+++ b/plugins/discourse-events/app/serializers/discourse_events/events/event_host_serializer.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module DiscourseEvents
+ module Events
+ class EventHostSerializer < BasicUserSerializer
+ attributes :title
+ end
+ end
+end
diff --git a/plugins/discourse-events/app/serializers/discourse_events/events/event_serializer.rb b/plugins/discourse-events/app/serializers/discourse_events/events/event_serializer.rb
index 66a59f77ca0..92abbd4a301 100644
--- a/plugins/discourse-events/app/serializers/discourse_events/events/event_serializer.rb
+++ b/plugins/discourse-events/app/serializers/discourse_events/events/event_serializer.rb
@@ -6,6 +6,7 @@ module DiscourseEvents
attributes :can_act_on_discourse_post_event
attributes :can_update_attendance
attributes :creator
+ attributes :hosts
attributes :is_closed
attributes :is_expired
attributes :is_ongoing
@@ -152,6 +153,15 @@ module DiscourseEvents
BasicUserSerializer.new(object.post.user, embed: :objects, root: false)
end
+ def hosts
+ ActiveModel::ArraySerializer.new(
+ object.event_hosts.map(&:user),
+ each_serializer: EventHostSerializer,
+ scope:,
+ root: false,
+ )
+ end
+
def stats
EventStatsSerializer.new(object, root: false).as_json
end
diff --git a/plugins/discourse-events/app/services/discourse_events/events/event/action/attributes_from_raw.rb b/plugins/discourse-events/app/services/discourse_events/events/event/action/attributes_from_raw.rb
index dedd5566ad1..d2c346b2ddd 100644
--- a/plugins/discourse-events/app/services/discourse_events/events/event/action/attributes_from_raw.rb
+++ b/plugins/discourse-events/app/services/discourse_events/events/event/action/attributes_from_raw.rb
@@ -29,11 +29,26 @@ module DiscourseEvents
max_attendees: raw_event[:"max-attendees"]&.to_i,
all_day: all_day?,
custom_fields: custom_fields,
+ host_user_ids: host_user_ids,
}
end
private
+ # Unknown usernames are dropped rather than rejected here; the validator
+ # is what refuses the post. An absent attribute clears the hosts, so that
+ # removing it from the raw removes them too.
+ def host_user_ids
+ usernames = raw_event[:hosts].to_s.split(",").map { |name| name.strip.downcase }
+ usernames.reject!(&:blank?)
+ usernames.uniq!
+ return [] if usernames.empty?
+
+ by_username =
+ User.human_users.where(username_lower: usernames).pluck(:username_lower, :id).to_h
+ usernames.filter_map { |username| by_username[username] }
+ end
+
def all_day?
raw_event[:"all-day"] == "true"
end
diff --git a/plugins/discourse-events/config/locales/server.en.yml b/plugins/discourse-events/config/locales/server.en.yml
index 9adf160585f..85b9c8c6b25 100644
--- a/plugins/discourse-events/config/locales/server.en.yml
+++ b/plugins/discourse-events/config/locales/server.en.yml
@@ -154,6 +154,10 @@ en:
acting_user_not_allowed_to_create_event: "Current user is not allowed to create events."
acting_user_not_allowed_to_act_on_this_event: "Current user is not allowed to act on this event."
invalid_allowed_groups: "Invalid allowed groups."
+ invalid_hosts: "One or more hosts can't be added to this event."
+ too_many_hosts:
+ one: "An event is limited to %{count} host."
+ other: "An event is limited to %{count} hosts."
acting_user_not_allowed_to_invite_these_groups: "Current user is not allowed to invite these groups."
custom_field_is_invalid: "The custom field `%{field}` is not allowed."
name:
diff --git a/plugins/discourse-events/db/migrate/20260902150024_create_discourse_post_event_hosts.rb b/plugins/discourse-events/db/migrate/20260902150024_create_discourse_post_event_hosts.rb
new file mode 100644
index 00000000000..6a5e186fa43
--- /dev/null
+++ b/plugins/discourse-events/db/migrate/20260902150024_create_discourse_post_event_hosts.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+class CreateDiscoursePostEventHosts < ActiveRecord::Migration[8.0]
+ def up
+ create_table :discourse_post_event_hosts do |t|
+ t.bigint :post_id, null: false
+ t.integer :user_id, null: false
+ t.integer :position, null: false, default: 0
+ t.timestamps
+ end
+
+ add_index :discourse_post_event_hosts, %i[post_id user_id], unique: true
+ add_index :discourse_post_event_hosts, :user_id
+ end
+
+ def down
+ drop_table :discourse_post_event_hosts
+ end
+end
diff --git a/plugins/discourse-events/lib/discourse_events/events/onebox_data.rb b/plugins/discourse-events/lib/discourse_events/events/onebox_data.rb
index f53185fb982..ad9618272f6 100644
--- a/plugins/discourse-events/lib/discourse_events/events/onebox_data.rb
+++ b/plugins/discourse-events/lib/discourse_events/events/onebox_data.rb
@@ -51,7 +51,7 @@ module DiscourseEvents
events_by_topic =
DiscourseEvents::Events::Event
.where(id: first_post_ids)
- .includes(:image_upload, post: [:user, { topic: :category }])
+ .includes(:image_upload, { event_hosts: :user }, post: [:user, { topic: :category }])
.index_by { |event| event.post.topic_id }
return {} if events_by_topic.empty?
diff --git a/plugins/discourse-events/lib/discourse_events/events/parser.rb b/plugins/discourse-events/lib/discourse_events/events/parser.rb
index e084251940e..7f45e32ed74 100644
--- a/plugins/discourse-events/lib/discourse_events/events/parser.rb
+++ b/plugins/discourse-events/lib/discourse_events/events/parser.rb
@@ -23,6 +23,7 @@ module DiscourseEvents
:"max-attendees",
:"all-day",
:image,
+ :hosts,
]
LEGACY_ESCAPED_ATTRS = %w[data-location]
diff --git a/plugins/discourse-events/lib/discourse_events/events/validator.rb b/plugins/discourse-events/lib/discourse_events/events/validator.rb
index 5db7e1a2e10..0290ad9827e 100644
--- a/plugins/discourse-events/lib/discourse_events/events/validator.rb
+++ b/plugins/discourse-events/lib/discourse_events/events/validator.rb
@@ -37,6 +37,7 @@ module DiscourseEvents
extracted_event = extracted_events.first
return false unless can_invite_groups?(extracted_event)
+ return false unless valid_hosts?(extracted_event)
if @post.acting_user && @post.event
if [email protected]_user.guardian.can_act_on_discourse_post_event?(@post.event)
@@ -146,6 +147,57 @@ module DiscourseEvents
private
+ def valid_hosts?(event)
+ usernames = extract_hosts(event)
+ return true if usernames.empty?
+
+ if usernames.length > Event::MAX_HOSTS
+ @post.errors.add(
+ :base,
+ I18n.t(
+ "discourse_post_event.errors.models.event.too_many_hosts",
+ count: Event::MAX_HOSTS,
+ ),
+ )
+ return false
+ end
+
+ hosts = User.human_users.where(username_lower: usernames).to_a
+ if hosts.length != usernames.length
+ @post.errors.add(:base, I18n.t("discourse_post_event.errors.models.event.invalid_hosts"))
+ return false
+ end
+
+ # Only newly added hosts are held to the eligibility bar, so an event
+ # keeps hosts that were valid when they were named.
+ existing_ids = @post.event ? @post.event.host_user_ids : []
+ added = hosts.reject { |host| existing_ids.include?(host.id) }
+ return true if added.empty?
+
+ return true if added.all? { |host| eligible_host?(host) }
+
+ @post.errors.add(:base, I18n.t("discourse_post_event.errors.models.event.invalid_hosts"))
+ false
+ end
+
+ def eligible_host?(host)
+ return false if !host.active? || host.suspended?
+
+ category = @post.topic&.category
+ return true if category.blank?
+
+ host.guardian.can_see_category?(category)
+ end
+
+ def extract_hosts(event)
+ event[:hosts]
+ .to_s
+ .split(",")
+ .map { |username| username.strip.downcase }
+ .reject(&:blank?)
+ .uniq
+ end
+
def can_invite_groups?(event)
return true unless event[:"allowed-groups"]
diff --git a/plugins/discourse-events/plugin.rb b/plugins/discourse-events/plugin.rb
index 93cfe6708ba..86a3a251c9d 100644
--- a/plugins/discourse-events/plugin.rb
+++ b/plugins/discourse-events/plugin.rb
@@ -368,10 +368,33 @@ after_initialize do
TopicView.on_preload do |topic_view|
if SiteSetting.discourse_post_event_enabled
- topic_view.instance_variable_set(:@posts, topic_view.posts.includes(event: :image_upload))
+ topic_view.instance_variable_set(
+ :@posts,
+ topic_view.posts.includes(event: [:image_upload, { event_hosts: :user }]),
+ )
end
end
+ add_to_serializer(
+ :topic_view,
+ :discourse_post_event_first_post_event,
+ include_condition: -> do
+ first_post = object.topic.first_post
+ event = first_post&.event
+
+ Array(object.posts).none? { |post| post.post_number == 1 } && event.present? &&
+ event.deleted_at.nil? && scope.can_see?(first_post)
+ end,
+ ) do
+ first_post = object.topic.first_post
+ event =
+ DiscourseEvents::Events::Event.includes(:image_upload, { event_hosts: :user }).find_by(
+ id: first_post.id,
+ )
+
+ DiscourseEvents::Events::EventSerializer.new(event, scope: scope, root: false).as_json
+ end
+
add_to_serializer(
:post,
:event,
@@ -800,6 +823,7 @@ after_initialize do
on(:user_destroyed) do |user|
DiscourseEvents::Events::Invitee.where(user_id: user.id).destroy_all
+ DiscourseEvents::Events::EventHost.where(user_id: user.id).delete_all
end
on(:user_removed_from_group) do |user, group|
diff --git a/plugins/discourse-events/spec/integration/post_event_hosts_spec.rb b/plugins/discourse-events/spec/integration/post_event_hosts_spec.rb
new file mode 100644
index 00000000000..fc8c436cee8
--- /dev/null
+++ b/plugins/discourse-events/spec/integration/post_event_hosts_spec.rb
@@ -0,0 +1,61 @@
+# frozen_string_literal: true
+
+RSpec.describe "post-event hosts" do
+ before do
+ Jobs.run_immediately!
+ SiteSetting.discourse_events_enabled = true
+ SiteSetting.discourse_post_event_enabled = true
+ end
+
+ fab!(:admin)
+
+ it "persists ordered hosts from event attributes" do
+ first_host = Fabricate(:user)
+ second_host = Fabricate(:user)
+
+ post = PostCreator.create!(admin, title: "Community meetup", raw: <<~RAW)
+ [event start="#{1.day.from_now.iso8601}" hosts="#{second_host.username},#{first_host.username}"]
+ [/event]
+ RAW
+
+ event = post.reload.event
+ expect(event.hosts).to eq([second_host, first_host])
+
+ json = DiscourseEvents::Events::EventSerializer.new(event, scope: Guardian.new).as_json
+ expect(json.dig(:event, :hosts).as_json.map { |host| host[:username] }).to eq(
+ [second_host.username, first_host.username],
+ )
+ end
+
+ it "removes hosts when the event attribute is removed" do
+ host = Fabricate(:user)
+ post =
+ PostCreator.create!(
+ admin,
+ title: "Community meetup",
+ raw: "[event start=\"#{1.day.from_now.iso8601}\" hosts=\"#{host.username}\"]\n[/event]",
+ )
+
+ PostRevisor.new(post).revise!(
+ admin,
+ raw: "[event start=\"#{1.day.from_now.iso8601}\"]\n[/event]",
+ )
+
+ expect(post.reload.event.hosts).to be_empty
+ end
+
+ it "rejects a host who cannot see the event category" do
+ group = Fabricate(:group)
+ category = Fabricate(:private_category, group:)
+ host = Fabricate(:user)
+
+ expect {
+ PostCreator.create!(
+ admin,
+ category: category.id,
+ title: "Private community meetup",
+ raw: "[event start=\"#{1.day.from_now.iso8601}\" hosts=\"#{host.username}\"]\n[/event]",
+ )
+ }.to raise_error(I18n.t("discourse_post_event.errors.models.event.invalid_hosts"))
+ end
+end
diff --git a/plugins/discourse-events/spec/requests/api/schemas/json/events_index_detailed_response.json b/plugins/discourse-events/spec/requests/api/schemas/json/events_index_detailed_response.json
index 2e9783dc9a6..6da61db9d30 100644
--- a/plugins/discourse-events/spec/requests/api/schemas/json/events_index_detailed_response.json
+++ b/plugins/discourse-events/spec/requests/api/schemas/json/events_index_detailed_response.json
@@ -107,6 +107,41 @@
"avatar_template"
]
},
+ "hosts": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "additionalProperties": false,
+ "properties": {
+ "id": {
+ "type": "integer"
+ },
+ "username": {
+ "type": "string"
+ },
+ "name": {
+ "type": [
+ "string",
+ "null"
+ ]
+ },
+ "avatar_template": {
+ "type": "string"
+ },
+ "title": {
+ "type": [
+ "string",
+ "null"
+ ]
+ }
+ },
+ "required": [
+ "id",
+ "username",
+ "avatar_template"
+ ]
+ }
+ },
"custom_fields": {
"type": [
"object",
@@ -371,4 +406,4 @@
"required": [
"events"
]
-}
\ No newline at end of file
+}
diff --git a/plugins/discourse-events/spec/requests/api/schemas/json/events_index_response.json b/plugins/discourse-events/spec/requests/api/schemas/json/events_index_response.json
index 82c3680ceca..5e4fcf26511 100644
--- a/plugins/discourse-events/spec/requests/api/schemas/json/events_index_response.json
+++ b/plugins/discourse-events/spec/requests/api/schemas/json/events_index_response.json
@@ -170,4 +170,4 @@
"required": [
"events"
]
-}
\ No newline at end of file
+}
diff --git a/plugins/discourse-events/spec/serializers/topic_view_serializer_spec.rb b/plugins/discourse-events/spec/serializers/topic_view_serializer_spec.rb
index ba6a78e3fe7..e46ada37911 100644
--- a/plugins/discourse-events/spec/serializers/topic_view_serializer_spec.rb
+++ b/plugins/discourse-events/spec/serializers/topic_view_serializer_spec.rb
@@ -50,6 +50,48 @@ RSpec.describe TopicViewSerializer do
end
end
+ context "when the first post is omitted from the stream" do
+ let!(:event) do
+ Fabricate(
+ :event,
+ post: first_post,
+ original_starts_at: 1.hour.from_now,
+ description: "Community meetup",
+ )
+ end
+
+ let(:topic_view) do
+ TopicView
+ .new(topic)
+ .tap { |view| view.instance_variable_set(:@posts, [Fabricate(:post, topic:)]) }
+ end
+
+ let(:json) do
+ JSON.parse(described_class.new(topic_view, scope: Guardian.new, root: false).to_json)
+ end
+
+ it "includes an active event from a visible first post" do
+ expect(json.dig("discourse_post_event_first_post_event", "id")).to eq(event.id)
+ expect(json.dig("discourse_post_event_first_post_event", "description")).to eq(
+ "Community meetup",
+ )
+ end
+
+ it "does not include an event from a hidden first post" do
+ first_post.update_columns(hidden: true)
+
+ expect(json).not_to have_key("discourse_post_event_first_post_event")
+ expect(json.to_json).not_to include(event.description)
+ end
+
+ it "does not include a soft-deleted event" do
+ event.update_columns(deleted_at: Time.current)
+
+ expect(json).not_to have_key("discourse_post_event_first_post_event")
+ expect(json.to_json).not_to include(event.description)
+ end
+ end
+
context "with timezone and show_local_time true" do
before do
DiscourseEvents::Events::Event.create!(
diff --git a/plugins/discourse-events/spec/services/discourse_events/events/event/action/attributes_from_raw_spec.rb b/plugins/discourse-events/spec/services/discourse_events/events/event/action/attributes_from_raw_spec.rb
index 8a22ca925d7..0502964f8d0 100644
--- a/plugins/discourse-events/spec/services/discourse_events/events/event/action/attributes_from_raw_spec.rb
+++ b/plugins/discourse-events/spec/services/discourse_events/events/event/action/attributes_from_raw_spec.rb
@@ -125,6 +125,13 @@ RSpec.describe DiscourseEvents::Events::Event::Action::AttributesFromRaw do
expect(attributes[:raw_invitees]).to be_nil
end
+ it "resolves hosts to user ids" do
+ host = Fabricate(:user, username: "EventHost")
+ raw_event.merge!(hosts: "eventhost, EventHost")
+
+ expect(attributes).to include(host_user_ids: [host.id])
+ end
+
context "with custom fields" do
it "only keeps fields listed in the site setting that have a value" do
SiteSetting.discourse_post_event_allowed_custom_fields = "field_a|field_b"