-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cff577e
commit bcfcbef
Showing
16 changed files
with
588 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
db/migrate/20240823051532_add_sync_type_to_discourse_events_source.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
class AddSyncTypeToDiscourseEventsSource < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :discourse_events_sources, :sync_type, :integer | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# frozen_string_literal: true | ||
module EventsGuardianExtension | ||
def can_edit_post?(post) | ||
return false if post.event_connection.present? | ||
return false if post.event_connections.present? | ||
super | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
module DiscourseEventsTopicExtension | ||
def reload(options = nil) | ||
@event = nil | ||
super(options) | ||
end | ||
|
||
def event | ||
@event ||= DiscourseEvents::Event.find_by(id: self.custom_fields['event_id']) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# frozen_string_literal: true | ||
|
||
module DiscourseEvents | ||
class PublishManager | ||
attr_reader :post | ||
attr_accessor :publisher, :publication_type, :connections, :event_data, :published_events | ||
|
||
def initialize(post, publication_type) | ||
@post = post | ||
@publication_type = publication_type | ||
@connections = [] | ||
@published_events = {} | ||
end | ||
|
||
def ready? | ||
publisher.present? && connections.present? | ||
end | ||
|
||
def perform | ||
@publisher = get_publisher | ||
|
||
setup_connections | ||
|
||
return false unless ready? | ||
return false unless publish_events | ||
|
||
send("#{publication_type}_event") | ||
end | ||
|
||
def self.perform(post, publication_type) | ||
new(post, publication_type).perform | ||
end | ||
|
||
protected | ||
|
||
def create_event | ||
event = nil | ||
|
||
ActiveRecord::Base.transaction do | ||
event = Event.create!(event_data.create_params) | ||
|
||
connections.each do |connection| | ||
if published_events[connection.id].present? | ||
params = get_event_connection_params(event, connection) | ||
EventConnection.create!(params) | ||
end | ||
end | ||
|
||
publisher.after_publish(post, event) | ||
end | ||
|
||
event.present? ? event : false | ||
end | ||
|
||
def update_event | ||
ActiveRecord::Base.transaction { post.topic.event.update!(event_data.update_params) } | ||
end | ||
|
||
def destroy_event | ||
ActiveRecord::Base.transaction { post.topic.event.destroy! } | ||
end | ||
|
||
def setup_connections | ||
if publication_type == "create" | ||
## We inherent the category connections on create | ||
post.topic&.category&.discourse_events_connections&.each do |connection| | ||
next unless connection.publish? | ||
@connections << connection | ||
end | ||
end | ||
|
||
if %w[update destroy].include?(publication_type) | ||
## We only update or destroy the established connections. | ||
post.event_connections.each do |event_connection| | ||
connection = event_connection.connection | ||
next unless connection.publish? | ||
@connections << connection | ||
end | ||
end | ||
end | ||
|
||
def publish_events | ||
@event_data = publisher.get_event_data(post) | ||
return false unless event_data&.valid? | ||
publish_event_to_connection_sources | ||
@published_events.present? | ||
end | ||
|
||
def publish_event_to_connection_sources | ||
connections.each do |connection| | ||
publisher.setup_provider(connection.source.provider) | ||
event = publisher.send("#{publication_type}_event", event_data) | ||
@published_events[connection.id] ||= [] | ||
@published_events[connection.id] << event if event.present? | ||
end | ||
end | ||
|
||
def get_publisher | ||
client = detect_client | ||
return false unless Connection.client_names.include?(client) | ||
|
||
publisher = "DiscourseEvents::Publisher::#{client.camelize}".constantize.new | ||
return false unless publisher.ready? | ||
|
||
publisher | ||
end | ||
|
||
def detect_client | ||
if post.topic.has_event? | ||
"events" | ||
elsif post.respond_to?(:event) && post.event.present? | ||
"discourse_events" | ||
else | ||
nil | ||
end | ||
end | ||
|
||
def get_event_connection_params(event, connection) | ||
params = { | ||
event_id: event.id, | ||
connection_id: connection.id, | ||
topic_id: post.topic.id, | ||
post_id: post.id, | ||
client: connection.client, | ||
} | ||
params[:series_id] = event.series_id if event.series_id | ||
params | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
module DiscourseEvents | ||
class Publisher | ||
attr_reader :logger | ||
attr_accessor :provider | ||
|
||
def initialize | ||
@logger = Logger.new(:publisher) | ||
OmniEvent.config.logger = @logger | ||
end | ||
|
||
def setup_provider(_provider) | ||
OmniEvent::Builder.new { provider _provider.provider_type, _provider.options } | ||
|
||
@provider = _provider | ||
end | ||
|
||
def create_event(event_data) | ||
OmniEvent.create_event(provider.provider_type, event: event_data.create_event_hash) | ||
end | ||
|
||
def update_event(event_data) | ||
OmniEvent.update_event(provider.provider_type, event: event_data.update_event_hash) | ||
end | ||
|
||
def destroy_event(event_data) | ||
OmniEvent.destroy_event(provider.provider_type, event: event_data.destroy_event_hash) | ||
end | ||
|
||
def get_event_data(post) | ||
raise NotImplementedError | ||
end | ||
|
||
def after_publish(post, event) | ||
raise NotImplementedError | ||
end | ||
|
||
protected | ||
|
||
def log(type, message) | ||
logger.send(type.to_s, message) | ||
end | ||
|
||
def create_event_hash(event_data) | ||
OmniEvent::EventHash.new( | ||
provider: provider.provider_type, | ||
data: event_data.data, | ||
metadata: event_data.metadata, | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module DiscourseEvents | ||
class Publisher::DiscourseEvents < Publisher | ||
def ready? | ||
defined?(DiscoursePostEvent) == "constant" && DiscoursePostEvent.class == Module && | ||
::SiteSetting.calendar_enabled && ::SiteSetting.discourse_post_event_enabled | ||
end | ||
|
||
def get_event_data(post) | ||
return nil unless post&.event&.starts_at.present? | ||
event = post.event | ||
|
||
Publisher::EventData.new( | ||
start_time: event.starts_at, | ||
end_time: event.ends_at, | ||
name: event.name, | ||
url: event.url, | ||
) | ||
end | ||
end | ||
end |
Oops, something went wrong.