A platform for community discussion. Free, open, simple.
FEAT: add images to post event events (#38643)
Bannon Tanner932946d5d450
diff --git a/plugins/discourse-calendar/app/controllers/discourse_post_event/events_controller.rb b/plugins/discourse-calendar/app/controllers/discourse_post_event/events_controller.rbindex c108e121a8b..95508763395 100644--- a/plugins/discourse-calendar/app/controllers/discourse_post_event/events_controller.rb+++ b/plugins/discourse-calendar/app/controllers/discourse_post_event/events_controller.rb@@ -16,6 +16,7 @@ module DiscoursePostEvent@events =DiscoursePostEvent::EventFinder.search(current_user, search_params).includes(:event_dates,+ :image_upload,post: {topic: %i[tags category],},@@ -98,7 +99,7 @@ module DiscoursePostEventenddef show- event = Event.find(params[:id])+ event = Event.includes(:image_upload).find(params[:id])guardian.ensure_can_see!(event.post)serializer = EventSerializer.new(event, scope: guardian)@@ -106,7 +107,7 @@ module DiscoursePostEventenddef destroy- event = Event.find(params[:id])+ event = Event.includes(:image_upload).find(params[:id])guardian.ensure_can_act_on_discourse_post_event!(event)event.publish_update!payload = WebHook.build_calendar_event_payload(event)diff --git a/plugins/discourse-calendar/app/models/discourse_post_event/event.rb b/plugins/discourse-calendar/app/models/discourse_post_event/event.rbindex 6419e8725ec..0a59bbc7899 100644--- a/plugins/discourse-calendar/app/models/discourse_post_event/event.rb+++ b/plugins/discourse-calendar/app/models/discourse_post_event/event.rb@@ -15,6 +15,8 @@ module DiscoursePostEventbelongs_to :chat_channel, class_name: "Chat::Channel"has_many :invitees, foreign_key: :post_id, dependent: :delete_allbelongs_to :post, foreign_key: :id+ belongs_to :image_upload, class_name: "Upload", optional: true+ has_many :upload_references, as: :target, dependent: :destroyscope :visible, -> { where(deleted_at: nil) }scope :open, -> { where(closed: false) }@@ -22,6 +24,11 @@ module DiscoursePostEventbefore_save :chat_channel_syncafter_commit :destroy_topic_custom_field, on: %i[destroy]after_commit :create_or_update_event_date, on: %i[create update]+ after_save do+ if saved_change_to_image_upload_id?+ UploadReference.ensure_exist!(upload_ids: [image_upload_id], target: self)+ end+ endvalidate :raw_invitees_are_groupsvalidates :original_starts_at, presence: true@@ -324,6 +331,54 @@ module DiscoursePostEvent)end+ def self.resolve_image_upload(image_param, post)+ return if image_param.blank?++ upload =+ if image_param.start_with?("upload://")+ sha1 = Upload.sha1_from_short_url(image_param)+ Upload.find_by(sha1: sha1) if sha1+ else+ Upload.get_from_url(image_param)+ end++ return if upload.nil?++ if !upload.secure? || upload.user_id == post.user_id ||+ UserUpload.exists?(upload_id: upload.id, user_id: post.user_id)+ upload+ end+ end++ def sync_image_to_post_and_topic(generate_thumbnails: false)+ return unless image_upload_id++ post.update_column(:image_upload_id, image_upload_id)+ if post.is_first_post?+ post.topic.update_column(:image_upload_id, image_upload_id)+ if generate_thumbnails+ extra_sizes =+ ThemeModifierHelper.new(+ theme_ids: Theme.user_selectable.pluck(:id),+ ).topic_thumbnail_sizes+ post.topic.generate_thumbnails!(extra_sizes: extra_sizes)+ end+ end+ end++ def self.handle_post_event_webhooks(post, event_before)+ had_event_before = event_before.present?++ if post.event && had_event_before+ WebHook.enqueue_calendar_event_hooks(:calendar_event_updated, post.event)+ elsif post.event && !had_event_before+ WebHook.enqueue_calendar_event_hooks(:calendar_event_created, post.event)+ elsif !post.event && had_event_before+ payload = WebHook.build_calendar_event_payload(event_before)+ WebHook.enqueue_calendar_event_hooks(:calendar_event_destroyed, event_before, payload)+ end+ end+def self.update_from_raw(post)events = DiscoursePostEvent::EventParser.extract_events(post)@@ -369,6 +424,7 @@ module DiscoursePostEventchat_enabled: event_params[:"chat-enabled"]&.downcase == "true",max_attendees: event_params[:"max-attendees"]&.to_i,all_day: parsed_all_day,+ image_upload_id: resolve_image_upload(event_params[:image], post)&.id,}params[:custom_fields] = {}@@ -560,25 +616,30 @@ end# Table name: discourse_post_event_events## id :bigint not null, primary key-# status :integer default(0), not null-# original_starts_at :datetime not null-# original_ends_at :datetime+# chat_enabled :boolean default(FALSE), not null+# closed :boolean default(FALSE), not null+# custom_fields :jsonb not null# deleted_at :datetime-# raw_invitees :string is an Array-# name :string-# url :string(1000)# description :string(1000)# location :string(1000)-# custom_fields :jsonb not null-# reminders :string-# recurrence :string-# timezone :string+# max_attendees :integer# minimal :boolean-# closed :boolean default(FALSE), not null-# chat_enabled :boolean default(FALSE), not null-# chat_channel_id :bigint+# name :string+# original_ends_at :datetime+# original_starts_at :datetime not null+# raw_invitees :string is an Array+# recurrence :string# recurrence_until :datetime+# reminders :string# show_local_time :boolean default(FALSE), not null-# max_attendees :integer# all_day :boolean default(FALSE), not null+# status :integer default(0), not null+# timezone :string+# url :string(1000)+# chat_channel_id :bigint+# image_upload_id :bigint+#+# Indexes+#+# index_discourse_post_event_events_on_image_upload_id (image_upload_id)#diff --git a/plugins/discourse-calendar/app/serializers/discourse_post_event/event_serializer.rb b/plugins/discourse-calendar/app/serializers/discourse_post_event/event_serializer.rbindex ab0b5d334c5..4f1f80d95cc 100644--- a/plugins/discourse-calendar/app/serializers/discourse_post_event/event_serializer.rb+++ b/plugins/discourse-calendar/app/serializers/discourse_post_event/event_serializer.rb@@ -33,6 +33,8 @@ module DiscoursePostEventattributes :max_attendeesattributes :at_capacity+ has_one :image_upload, embed: :object, serializer: UploadSerializer+def channel::Chat::ChannelSerializer.new(object.chat_channel, root: false, scope:)enddiff --git a/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/image.gjs b/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/image.gjsnew file mode 100644index 00000000000..f691f746df7--- /dev/null+++ b/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/image.gjs@@ -0,0 +1,9 @@+<template>+ {{#if @imageUpload}}+ <section class="event__section event-image">+ <a class="lightbox" href={{@imageUpload.url}}>+ <img src={{@imageUpload.url}} alt={{@alt}} />+ </a>+ </section>+ {{/if}}+</template>diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/index.gjs b/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/index.gjsindex fa686be941d..87320938c5a 100644--- a/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/index.gjs+++ b/plugins/discourse-calendar/assets/javascripts/discourse/components/discourse-post-event/index.gjs@@ -17,6 +17,7 @@ import Creator from "./creator";import Dates from "./dates";import Description from "./description";import EventStatus from "./event-status";+import Image from "./image";import Invitees from "./invitees";import Location from "./location";import MoreMenu from "./more-menu";@@ -140,6 +141,10 @@ export default class DiscoursePostEvent extends Component {<div class="discourse-post-event"><div class="discourse-post-event-widget">{{#if event}}+ <Image+ @imageUpload={{event.imageUpload}}+ @alt={{this.eventName}}+ /><header class="event-header" {{this.setupMessageBus}}><div class="event-date"><div class="month">@@ -224,6 +229,9 @@ export default class DiscoursePostEvent extends Component {Invitees=(component Invitees event=event)Status=(component Status event=event)ChatChannel=(component ChatChannel event=event)+ Image=(component+ Image imageUpload=event.imageUpload alt=this.eventName+ )}}><Datesdiff --git a/plugins/discourse-calendar/assets/javascripts/discourse/components/event-node-view.gjs b/plugins/discourse-calendar/assets/javascripts/discourse/components/event-node-view.gjsindex 8d74a95261a..237da6040d1 100644--- a/plugins/discourse-calendar/assets/javascripts/discourse/components/event-node-view.gjs+++ b/plugins/discourse-calendar/assets/javascripts/discourse/components/event-node-view.gjs@@ -441,6 +441,7 @@ export default class EventNodeView extends Component {ends_at: this.eventData.end? moment(this.eventData.end).tz(timezone): null,+ image_upload: this.eventData.image ? { url: this.eventData.image } : null,};}diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/components/modal/post-event-builder.gjs b/plugins/discourse-calendar/assets/javascripts/discourse/components/modal/post-event-builder.gjsindex 0a8e43696c3..eed178778a1 100644--- a/plugins/discourse-calendar/assets/javascripts/discourse/components/modal/post-event-builder.gjs+++ b/plugins/discourse-calendar/assets/javascripts/discourse/components/modal/post-event-builder.gjs@@ -13,6 +13,7 @@ import DateTimeInputRange from "discourse/components/date-time-input-range";import GroupSelector from "discourse/components/group-selector";import PluginOutlet from "discourse/components/plugin-outlet";import RadioButton from "discourse/components/radio-button";+import UppyImageUploader from "discourse/components/uppy-image-uploader";import lazyHash from "discourse/helpers/lazy-hash";import { extractError } from "discourse/lib/ajax-error";import Group from "discourse/models/group";@@ -301,6 +302,16 @@ export default class PostEventBuilder extends Component {this.event.showLocalTime = e.target.checked;}+ @action+ setImage(upload) {+ this.event.imageUpload = upload;+ }++ @action+ removeImage() {+ this.event.imageUpload = null;+ }+@actionsetMinimal(e) {this.event.minimal = e.target.checked;@@ -619,6 +630,19 @@ export default class PostEventBuilder extends Component {/></EventField>+ <EventField+ @label="discourse_post_event.builder_modal.image.label"+ class="image"+ >+ <UppyImageUploader+ @id="post-event-image-uploader"+ @imageUrl={{this.event.imageUrl}}+ @onUploadDone={{this.setImage}}+ @onUploadDeleted={{this.removeImage}}+ @type="event_image"+ />+ </EventField>+<EventFieldclass="reminders"@label="discourse_post_event.builder_modal.reminders.label"diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/lib/raw-event-helper.js b/plugins/discourse-calendar/assets/javascripts/discourse/lib/raw-event-helper.jsindex 48e6f17a549..58b11e5b1b9 100644--- a/plugins/discourse-calendar/assets/javascripts/discourse/lib/raw-event-helper.js+++ b/plugins/discourse-calendar/assets/javascripts/discourse/lib/raw-event-helper.js@@ -94,6 +94,12 @@ export function buildParams(startsAt, endsAt, event, siteSettings) {.join(",");}+ if (event.imageUpload?.short_url) {+ params.image = event.imageUpload.short_url;+ } else if (event.imageUpload?.url) {+ params.image = event.imageUpload.url;+ }+siteSettings.discourse_post_event_allowed_custom_fields.split("|").filter(Boolean)diff --git a/plugins/discourse-calendar/assets/javascripts/discourse/models/discourse-post-event-event.js b/plugins/discourse-calendar/assets/javascripts/discourse/models/discourse-post-event-event.jsindex 126b053619a..742cc35a84a 100644--- a/plugins/discourse-calendar/assets/javascripts/discourse/models/discourse-post-event-event.js+++ b/plugins/discourse-calendar/assets/javascripts/discourse/models/discourse-post-event-event.js@@ -53,6 +53,7 @@ export default class DiscoursePostEventEvent {@tracked recurrence;@tracked customFields;@tracked channel;+ @tracked imageUpload;@tracked _watchingInvitee;@tracked _sampleInvitees;@@ -98,6 +99,7 @@ export default class DiscoursePostEventEvent {if (args.channel && ChatChannel) {this.channel = ChatChannel.create(args.channel);}+ this.imageUpload = args.image_upload;}get watchingInvitee() {@@ -152,6 +154,10 @@ export default class DiscoursePostEventEvent {return this.status === "private";}+ get imageUrl() {+ return this.imageUpload?.url;+ }+updateFromEvent(event) {this.name = event.name;this.startsAt = event.startsAt;@@ -181,6 +187,7 @@ export default class DiscoursePostEventEvent {this.stats = event.stats;this.sampleInvitees = event.sampleInvitees || [];this.reminders = event.reminders;+ this.imageUpload = event.imageUpload;}@binddiff --git a/plugins/discourse-calendar/assets/javascripts/discourse/pre-initializers/rich-editor-extension.js b/plugins/discourse-calendar/assets/javascripts/discourse/pre-initializers/rich-editor-extension.jsindex 2197f1ae84a..9082a01eb4a 100644--- a/plugins/discourse-calendar/assets/javascripts/discourse/pre-initializers/rich-editor-extension.js+++ b/plugins/discourse-calendar/assets/javascripts/discourse/pre-initializers/rich-editor-extension.js@@ -21,6 +21,7 @@ export const EVENT_ATTRIBUTES = {recurrenceUntil: { default: null },chatEnabled: { default: null },allDay: { default: null },+ image: { default: null },};/** @type {RichEditorExtension} */diff --git a/plugins/discourse-calendar/assets/stylesheets/common/discourse-post-event.scss b/plugins/discourse-calendar/assets/stylesheets/common/discourse-post-event.scssindex 701b153d29f..16fce9ea5f4 100644--- a/plugins/discourse-calendar/assets/stylesheets/common/discourse-post-event.scss+++ b/plugins/discourse-calendar/assets/stylesheets/common/discourse-post-event.scss@@ -12,12 +12,10 @@ $show-interested: inherit;.event__section {padding: 0.5em 0.75rem;- &:first-child {- border-top: 1px solid var(--primary-low);- }-p {margin: 0;+ overflow-wrap: break-word;+ word-break: break-word;}> .d-icon {@@ -29,6 +27,23 @@ $show-interested: inherit;}}+ .event-image {+ position: relative;+ overflow: hidden;+ width: 100%;+ padding: 1px;+ aspect-ratio: 16 / 9;+ box-sizing: border-box;++ img {+ width: 100%;+ height: 100%;+ object-fit: cover;+ border-top-left-radius: var(--d-border-radius);+ border-top-right-radius: var(--d-border-radius);+ }+ }+.discourse-post-event-widget {display: flex;background: var(--secondary);@@ -36,6 +51,7 @@ $show-interested: inherit;flex: 1 0 auto;max-width: 100%;box-sizing: border-box;+ overflow: hidden;.cooked & {box-shadow: 0 0 0 3px var(--primary-100);diff --git a/plugins/discourse-calendar/config/locales/client.en.yml b/plugins/discourse-calendar/config/locales/client.en.ymlindex 29d2894d1d9..34ddc520609 100644--- a/plugins/discourse-calendar/config/locales/client.en.yml+++ b/plugins/discourse-calendar/config/locales/client.en.yml@@ -516,6 +516,8 @@ en:status:label: "Status"delete_confirmation_message: "Delete this event?"+ image:+ label: "Event image"invite_user_or_group:title: "Notify user(s) or group(s)"invite: "Send"diff --git a/plugins/discourse-calendar/db/migrate/20260316154328_add_image_upload_id_to_events.rb b/plugins/discourse-calendar/db/migrate/20260316154328_add_image_upload_id_to_events.rbnew file mode 100644index 00000000000..ed94b7275d0--- /dev/null+++ b/plugins/discourse-calendar/db/migrate/20260316154328_add_image_upload_id_to_events.rb@@ -0,0 +1,11 @@+# frozen_string_literal: true+class AddImageUploadIdToEvents < ActiveRecord::Migration[8.0]+ def up+ add_column :discourse_post_event_events, :image_upload_id, :bigint+ add_index :discourse_post_event_events, :image_upload_id+ end++ def down+ remove_column :discourse_post_event_events, :image_upload_id+ end+enddiff --git a/plugins/discourse-calendar/lib/discourse_post_event/event_parser.rb b/plugins/discourse-calendar/lib/discourse_post_event/event_parser.rbindex 620a841d759..f64fcc1f833 100644--- a/plugins/discourse-calendar/lib/discourse_post_event/event_parser.rb+++ b/plugins/discourse-calendar/lib/discourse_post_event/event_parser.rb@@ -20,6 +20,7 @@ module DiscoursePostEvent:"chat-enabled",:"max-attendees",:"all-day",+ :image,]def self.extract_events(post)@@ -48,7 +49,9 @@ module DiscoursePostEventif value && valid_options.include?(name)event ||= {}- event[name.sub("data-", "").to_sym] = if %w[data-name data-url].include?(name)+ event[name.sub("data-", "").to_sym] = if %w[data-name data-url data-image].include?(+ name,+ )valueelseCGI.escapeHTML(value)diff --git a/plugins/discourse-calendar/lib/discourse_post_event/web_hook_extension.rb b/plugins/discourse-calendar/lib/discourse_post_event/web_hook_extension.rbindex 2d17a8744c2..77711737a9d 100644--- a/plugins/discourse-calendar/lib/discourse_post_event/web_hook_extension.rb+++ b/plugins/discourse-calendar/lib/discourse_post_event/web_hook_extension.rb@@ -41,6 +41,16 @@ module DiscoursePostEventend)+ event_image =+ if calendar_event.image_upload.present?+ {+ id: calendar_event.image_upload.id,+ url: UrlHelper.absolute(calendar_event.image_upload.url),+ width: calendar_event.image_upload.width,+ height: calendar_event.image_upload.height,+ }+ end+{event: {id: calendar_event.id,@@ -60,6 +70,7 @@ module DiscoursePostEventmax_attendees: calendar_event.max_attendees,chat_enabled: calendar_event.chat_enabled,allowed_groups: calendar_event.raw_invitees,+ image: event_image,},post: {id: post&.id,diff --git a/plugins/discourse-calendar/plugin.rb b/plugins/discourse-calendar/plugin.rbindex ab9218b9591..17ce6c6aab1 100644--- a/plugins/discourse-calendar/plugin.rb+++ b/plugins/discourse-calendar/plugin.rb@@ -229,7 +229,7 @@ after_initialize doTopicView.on_preload do |topic_view|if SiteSetting.discourse_post_event_enabled- topic_view.instance_variable_set(:@posts, topic_view.posts.includes(:event))+ topic_view.instance_variable_set(:@posts, topic_view.posts.includes(event: :image_upload))endend@@ -251,19 +251,17 @@ after_initialize doon(:post_edited) do |post|event_before = post.event- had_event_before = event_before.present?+ had_image_before = event_before&.image_upload_id.present?DiscoursePostEvent::Event.update_from_raw(post)post.reloadif SiteSetting.discourse_post_event_enabled- if post.event && had_event_before- WebHook.enqueue_calendar_event_hooks(:calendar_event_updated, post.event)- elsif post.event && !had_event_before- WebHook.enqueue_calendar_event_hooks(:calendar_event_created, post.event)- elsif !post.event && had_event_before- payload = WebHook.build_calendar_event_payload(event_before)- WebHook.enqueue_calendar_event_hooks(:calendar_event_destroyed, event_before, payload)+ if post.event&.image_upload_id+ post.event.sync_image_to_post_and_topic+ elsif had_image_before+ post.trigger_post_processend+ DiscoursePostEvent::Event.handle_post_event_webhooks(post, event_before)endend@@ -426,6 +424,11 @@ after_initialize doDiscourseCalendar::Calendar.update(post)DiscourseCalendar::GroupTimezones.update(post)CalendarEvent.update(post)++ if SiteSetting.discourse_post_event_enabled+ event = DiscoursePostEvent::Event.find_by(id: post.id)+ event&.sync_image_to_post_and_topic(generate_thumbnails: true) if event&.image_upload_id+ endendon(:post_recovered) do |post, _, _|@@ -623,7 +626,19 @@ after_initialize dolocation = event_node["data-location"]url = event_node["data-url"]+ event = DiscoursePostEvent::Event.includes(:image_upload).find_by(id: post.id)+ image_url = UrlHelper.absolute(event.image_upload.url) if event&.image_upload_id+rows = +""++ rows << <<~HTML if image_url.present?+ <tr>+ <td style="padding: 0;">+ <img src="#{CGI.escape_html(image_url)}" style="width: 100%; max-height: 400px; object-fit: cover; display: block;" />+ </td>+ </tr>+ HTML+rows << <<~HTML<tr><td style="padding: 12px;">diff --git a/plugins/discourse-calendar/spec/lib/discourse_post_event/event_parser_spec.rb b/plugins/discourse-calendar/spec/lib/discourse_post_event/event_parser_spec.rbindex e5c5641b7d7..0d18de787b9 100644--- a/plugins/discourse-calendar/spec/lib/discourse_post_event/event_parser_spec.rb+++ b/plugins/discourse-calendar/spec/lib/discourse_post_event/event_parser_spec.rb@@ -114,12 +114,13 @@ describe DiscoursePostEvent::EventParser doit "doesn't escape urls" dopost_event = build_post user, <<~TXT- [event start="2020" url="https://example.com/?q=foo&all=true"]+ [event start="2020" url="https://example.com/?q=foo&all=true" image="upload://6c4fsAgNM6Npo7raNCPqVm2whzz.jpeg"][/event]TXTevents = parser.extract_events(post_event)expect(events[0][:url]).to eq("https://example.com/?q=foo&all=true")+ expect(events[0][:image]).to eq("upload://6c4fsAgNM6Npo7raNCPqVm2whzz.jpeg")endcontext "with custom fields" dodiff --git a/plugins/discourse-calendar/spec/lib/discourse_post_event/pretty_text_spec.rb b/plugins/discourse-calendar/spec/lib/discourse_post_event/pretty_text_spec.rbindex b139fcc0463..42f93cb3dfb 100644--- a/plugins/discourse-calendar/spec/lib/discourse_post_event/pretty_text_spec.rb+++ b/plugins/discourse-calendar/spec/lib/discourse_post_event/pretty_text_spec.rb@@ -94,6 +94,19 @@ describe PrettyText doexpect(result).to include("https://example.com/meeting")endend++ context "when the event has an image" do+ fab!(:upload)+ let(:post_1) { create_post_with_event(user_1, "image=\"#{upload.short_url}\"") }++ it "displays the image" do+ cooked = PrettyText.cook(post_1.raw)+ result = PrettyText.format_for_email(cooked, post_1)++ expect(result).to include("<img")+ expect(result).to include(upload.url)+ end+ endendendenddiff --git a/plugins/discourse-calendar/spec/models/discourse_post_event/event_spec.rb b/plugins/discourse-calendar/spec/models/discourse_post_event/event_spec.rbindex 49adebc65c6..eaf5bedfb69 100644--- a/plugins/discourse-calendar/spec/models/discourse_post_event/event_spec.rb+++ b/plugins/discourse-calendar/spec/models/discourse_post_event/event_spec.rb@@ -772,6 +772,171 @@ describe DiscoursePostEvent::Event doendendend++ describe ".update_from_raw" do+ fab!(:user) { Fabricate(:user, admin: true) }+ fab!(:topic) { Fabricate(:topic, user: user) }+ fab!(:post) { Fabricate(:post, topic: topic, user: user) }+ fab!(:upload)++ context "with image" do+ before do+ post.update!(+ raw: "[event start=\"2020-04-24 14:15\" image=\"#{upload.short_url}\"]\n[/event]",+ )+ post.rebake!+ DiscoursePostEvent::Event.update_from_raw(post)+ post.reload+ end++ it "resolves image to image_upload_id and creates UploadReference" do+ expect(post.event.image_upload_id).to eq(upload.id)+ expect(UploadReference.exists?(upload_id: upload.id, target: post.event)).to eq(true)+ end++ it "clears image_upload_id when image is removed" do+ post.update!(raw: "[event start=\"2020-04-24 14:15\"]\n[/event]")+ post.rebake!+ DiscoursePostEvent::Event.update_from_raw(post)+ post.reload++ expect(post.event.image_upload_id).to be_nil+ expect(UploadReference.exists?(upload_id: upload.id, target: post.event)).to eq(false)+ end+ end++ describe "post.image_upload_id sync on post_process_cooked" do+ fab!(:topic) { Fabricate(:topic, user: user) }++ it "sets post.image_upload_id from the event image after post processing" do+ post =+ Fabricate(+ :post,+ topic: topic,+ user: user,+ raw: "[event start=\"2020-04-24 14:15\" image=\"#{upload.short_url}\"]\n[/event]",+ )+ post.rebake!+ DiscoursePostEvent::Event.update_from_raw(post)+ post.reload+ CookedPostProcessor.new(post).post_process++ post.reload+ expect(post.image_upload_id).to eq(upload.id)+ end++ it "sets topic.image_upload_id when the post is the first post" do+ post = topic.first_post || Fabricate(:post, topic: topic, user: user, post_number: 1)+ post.update!(+ raw: "[event start=\"2020-04-24 14:15\" image=\"#{upload.short_url}\"]\n[/event]",+ )+ post.rebake!+ DiscoursePostEvent::Event.update_from_raw(post)+ post.reload+ CookedPostProcessor.new(post).post_process++ post.reload+ topic.reload+ expect(topic.image_upload_id).to eq(upload.id)+ end++ it "does not override post.image_upload_id when event has no image" do+ other_upload = Fabricate(:upload)+ post =+ Fabricate(+ :post,+ topic: topic,+ user: user,+ raw: "[event start=\"2020-04-24 14:15\"]\n[/event]\n",+ )+ post.rebake!+ DiscoursePostEvent::Event.update_from_raw(post)+ post.reload+ CookedPostProcessor.new(post).post_process++ post.reload+ expect(post.image_upload_id).not_to eq(nil)+ expect(post.event.image_upload_id).to be_nil+ end+ end++ context "with an image from a different secure post" do+ fab!(:private_upload_owner, :user)+ fab!(:private_upload_recipient, :user)+ fab!(:private_upload_post) do+ Fabricate(+ :private_message_post,+ user: private_upload_owner,+ recipient: private_upload_recipient,+ )+ end+ fab!(:private_upload) do+ Fabricate(+ :secure_upload,+ user: private_upload_owner,+ access_control_post: private_upload_post,+ )+ end++ before do+ setup_s3+ SiteSetting.secure_uploads = true++ post.update!(+ raw: "[event start=\"2020-04-24 14:15\" image=\"#{private_upload.url}\"]\n[/event]",+ )+ post.rebake!+ end++ it "does not associate the upload" do+ DiscoursePostEvent::Event.update_from_raw(post)+ post.reload++ expect(post.event.image_upload_id).to be_nil+ expect(UploadReference.exists?(upload_id: private_upload.id, target: post.event)).to eq(+ false,+ )+ end+ end+ end++ describe "post/topic image_upload_id sync on post_edited" do+ fab!(:user) { Fabricate(:user, admin: true) }+ fab!(:topic) { Fabricate(:topic, user: user) }+ fab!(:post) { Fabricate(:post, topic: topic, user: user, post_number: 1) }+ fab!(:upload)++ it "syncs event image to post and topic when image is added" do+ PostRevisor.new(post).revise!(+ user,+ raw: "[event start=\"2020-04-24 14:15\" image=\"#{upload.short_url}\"]\n[/event]",+ )+ post.reload+ topic.reload++ expect(post.image_upload_id).to eq(upload.id)+ expect(topic.image_upload_id).to eq(upload.id)+ end++ it "does not clear post image_upload_id when event has no image but post has inline images" do+ other_upload = Fabricate(:upload)+ PostRevisor.new(post).revise!(+ user,+ raw: "[event start=\"2020-04-24 14:15\"]\n[/event]\n",+ )+ CookedPostProcessor.new(post).post_process+ post.reload++ PostRevisor.new(post).revise!(+ user,+ raw:+ "[event start=\"2020-04-24 14:15\"]\n[/event]\n\nupdated text",+ )+ post.reload++ expect(post.image_upload_id).to eq(other_upload.id)+ end+ endenddescribe DiscoursePostEvent::Event, "#capacity" dodiff --git a/plugins/discourse-calendar/spec/requests/api/schemas/json/events_index_detailed_response.json b/plugins/discourse-calendar/spec/requests/api/schemas/json/events_index_detailed_response.jsonindex c1498c639ee..cd58ccba015 100644--- a/plugins/discourse-calendar/spec/requests/api/schemas/json/events_index_detailed_response.json+++ b/plugins/discourse-calendar/spec/requests/api/schemas/json/events_index_detailed_response.json@@ -235,6 +235,12 @@"at_capacity": {"type": "boolean"},+ "image_upload": {+ "type": [+ "object",+ "null"+ ]+ },"post": {"type": "object","additionalProperties": false,diff --git a/plugins/discourse-calendar/spec/serializers/discourse_post_event/event_serializer_spec.rb b/plugins/discourse-calendar/spec/serializers/discourse_post_event/event_serializer_spec.rbindex 98767a5417e..37e9fb30b4e 100644--- a/plugins/discourse-calendar/spec/serializers/discourse_post_event/event_serializer_spec.rb+++ b/plugins/discourse-calendar/spec/serializers/discourse_post_event/event_serializer_spec.rb@@ -54,6 +54,26 @@ describe DiscoursePostEvent::EventSerializer doexpect(json[:event][:category_id]).to eq(category.id)end+ it "includes image_upload key as nil when not set" do+ json = DiscoursePostEvent::EventSerializer.new(event, scope: Guardian.new).as_json+ expect(json[:event]).to have_key(:image_upload)+ expect(json[:event][:image_upload]).to be_nil+ end++ context "when event has an image" do+ fab!(:upload)+ fab!(:event_with_image) do+ Fabricate(:event, post: Fabricate(:post, topic: topic), image_upload: upload)+ end++ it "includes image_upload in serialized output" do+ json =+ DiscoursePostEvent::EventSerializer.new(event_with_image, scope: Guardian.new).as_json+ expect(json[:event][:image_upload][:id]).to eq(upload.id)+ expect(json[:event][:image_upload][:url]).to include(upload.url)+ end+ end+context "when event has duration" dofab!(:post_with_duration) { Fabricate(:post, topic: topic) }fab!(:event_with_duration) dodiff --git a/plugins/discourse-calendar/test/javascripts/lib/raw-event-helper-test.js b/plugins/discourse-calendar/test/javascripts/lib/raw-event-helper-test.jsindex 84100db2673..f80a65cf60f 100644--- a/plugins/discourse-calendar/test/javascripts/lib/raw-event-helper-test.js+++ b/plugins/discourse-calendar/test/javascripts/lib/raw-event-helper-test.js@@ -1,5 +1,6 @@import { module, test } from "qunit";import {+ buildParams,removeEvent,replaceRaw,} from "discourse/plugins/discourse-calendar/discourse/lib/raw-event-helper";@@ -75,4 +76,42 @@ module("Unit | Lib | raw-event-helper", function () {"omits whitespace-only name parameter");});++ test("buildParams image handling", function (assert) {+ const startsAt = "2024-06-15T10:00:00Z";+ const siteSettings = { discourse_post_event_allowed_custom_fields: "" };++ assert.strictEqual(+ buildParams(+ startsAt,+ null,+ {+ imageUpload: {+ short_url: "upload://abc123.png",+ url: "/uploads/default/original/1X/abc123.png",+ },+ },+ siteSettings+ ).image,+ "upload://abc123.png",+ "prefers short_url when available"+ );++ assert.strictEqual(+ buildParams(+ startsAt,+ null,+ { imageUpload: { url: "/uploads/default/original/1X/abc123.png" } },+ siteSettings+ ).image,+ "/uploads/default/original/1X/abc123.png",+ "falls back to url when short_url is not set"+ );++ assert.strictEqual(+ buildParams(startsAt, null, {}, siteSettings).image,+ undefined,+ "omits image when imageUpload is not set"+ );+ });});