This repository was archived by the owner on Jul 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
Feature: add chat integration reference post #216
Merged
Merged
Changes from 12 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
af5e7e8
FEATURE: Add chat integration reference post
Grubba27 c5d7568
DEV: change how `excerpt` method works
Grubba27 3c237ec
feature: Add `send_chat_integration_message` scriptable
Grubba27 b316558
DEV: Add `get_channel_by_name` to every provider
Grubba27 846996d
DEV: Add `get_channel_name` to all providers
Grubba27 8c1513d
DEV: Add removal of old migration data
Grubba27 3d54fd1
DEV: solve review comments
Grubba27 6987ca3
DEV: update test locale strings
Grubba27 96b91bf
DEV: remove empty line to trigger lint
Grubba27 42e6830
DEV: lint applied
Grubba27 1df0946
DEV: Add tests for automation integration
Grubba27 f3eda71
DEV: add rails logger for when automatio error occurs
Grubba27 96f8da9
DEV: move migration to be SQL only
Grubba27 5144318
DEV: update migration with correct table names
Grubba27 b865868
DEV: Update migrate_tag_added_filter_to_all_providers to use smaller …
Grubba27 d5c0774
DEV: update comments in migration file
Grubba27 620a5a9
DEV: update indentation in client.en.yml
Grubba27 2e07334
DEV: update with review comments
Grubba27 e4d464e
Update spec/lib/discourse_chat_integration/chat_integration_reference…
Grubba27 86979fb
Update spec/lib/discourse_chat_integration/chat_integration_reference…
Grubba27 3d46798
Update spec/lib/discourse_chat_integration/chat_integration_reference…
Grubba27 94bdec9
Update spec/integration/automation_spec.rb
Grubba27 3ea5b6c
Update lib/discourse_chat_integration/chat_integration_reference_post.rb
Grubba27 4f681a8
DEV: update specs with review comments
Grubba27 fa2a8eb
DEV: update typos in tests
Grubba27 9a60a45
DEV: inlined functions for getting channel name for provider in migra…
Grubba27 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
93 changes: 93 additions & 0 deletions
93
db/migrate/20240903184807_migrate_tag_added_filter_to_all_providers.rb
This file contains hidden or 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,93 @@ | ||
# frozen_string_literal: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't have any rails class in a migration, it should be all sql, if we change these class, migrations are borked There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This ^. You'll have to inline whatever |
||
class MigrateTagAddedFilterToAllProviders < ActiveRecord::Migration[7.1] | ||
def up | ||
if defined?(DiscourseAutomation) | ||
begin | ||
# Trash old migration | ||
if DiscourseChatIntegration::Channel.with_provider("slack").exists? | ||
DiscourseAutomation::Automation | ||
.where(script: "send_slack_message", trigger: "topic_tags_changed") | ||
.each do |automation| | ||
# if is the same name as created and message is the same | ||
if automation.name == "When tags change in topic" && | ||
automation.fields.where(name: "message").first.metadata["value"] == | ||
"${ADDED_AND_REMOVED}" | ||
automation.destroy! | ||
end | ||
end | ||
end | ||
|
||
DiscourseChatIntegration::Rule | ||
.where("value::json->>'filter'=?", "tag_added") | ||
.each do |rule| | ||
channel_id = rule.channel_id | ||
channel = DiscourseChatIntegration::Channel.find(channel_id) | ||
# channel names are unique but built from the provider | ||
provider_name = channel.provider | ||
provider = DiscourseChatIntegration::Provider.get_by_name(provider_name) | ||
|
||
channel_name = provider.get_channel_name(channel) | ||
|
||
category_id = rule.category_id | ||
tags = rule.tags | ||
|
||
automation = | ||
DiscourseAutomation::Automation.new( | ||
script: "send_chat_integration_message", | ||
trigger: "topic_tags_changed", | ||
name: "When tags change in topic", | ||
enabled: true, | ||
last_updated_by_id: Discourse.system_user.id, | ||
) | ||
|
||
automation.save! | ||
|
||
# Triggers: | ||
# Watching categories | ||
|
||
metadata = (category_id ? { "value" => [category_id] } : {}) | ||
|
||
automation.upsert_field!( | ||
"watching_categories", | ||
"categories", | ||
metadata, | ||
target: "trigger", | ||
) | ||
|
||
# Watching tags | ||
|
||
metadata = (tags ? { "value" => tags } : {}) | ||
automation.upsert_field!("watching_tags", "tags", metadata, target: "trigger") | ||
|
||
# Script options: | ||
# Provider | ||
automation.upsert_field!( | ||
"provider", | ||
"choices", | ||
{ "value" => provider_name }, | ||
target: "script", | ||
) | ||
|
||
# Channel name | ||
automation.upsert_field!( | ||
"channel_name", | ||
"text", | ||
{ "value" => channel_name }, | ||
target: "script", | ||
) | ||
end | ||
rescue StandardError | ||
Rails.logger.warn("Failed to migrate tag_added rule to all providers automations") | ||
end | ||
end | ||
end | ||
|
||
def down | ||
if defined?(DiscourseAutomation) | ||
DiscourseAutomation::Automation | ||
.where(script: "send_chat_integration_message") | ||
.where(trigger: "topic_tags_changed") | ||
.where(name: "When tags change in topic") | ||
.destroy_all | ||
end | ||
end | ||
end |
75 changes: 75 additions & 0 deletions
75
lib/discourse_chat_integration/chat_integration_reference_post.rb
This file contains hidden or 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,75 @@ | ||
# frozen_string_literal: true | ||
|
||
module DiscourseChatIntegration | ||
class ChatIntegrationReferencePost | ||
def initialize(context) | ||
@context = context | ||
@user = context["user"] | ||
@topic = context["topic"] | ||
@kind = context["kind"] | ||
@raw = context["raw"] if context["raw"].present? | ||
@created_at = Time.zone.now | ||
Grubba27 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
end | ||
|
||
def id | ||
@topic.posts.empty? ? @topic.id : @topic.posts.first.id | ||
end | ||
|
||
def user | ||
@user | ||
end | ||
|
||
def topic | ||
@topic | ||
end | ||
|
||
def full_url | ||
@topic.posts.empty? ? @topic.full_url : @topic.posts.first.full_url | ||
end | ||
|
||
def excerpt(maxlength = nil, options = {}) | ||
cooked = PrettyText.cook(raw, { user_id: user.id }) | ||
maxlength ||= SiteSetting.post_excerpt_maxlength | ||
PrettyText.excerpt(cooked, maxlength, options) | ||
end | ||
|
||
def is_first_post? | ||
topic.try(:highest_post_number) == 0 | ||
end | ||
|
||
def created_at | ||
@created_at | ||
end | ||
|
||
def raw | ||
if @raw.nil? && @kind == DiscourseAutomation::Triggers::TOPIC_TAGS_CHANGED | ||
tag_list_to_raw = ->(tag_list) do | ||
tag_list.sort.map { |tag_name| "##{tag_name}" }.join(", ") | ||
end | ||
|
||
added_tags = @context["added_tags"] | ||
removed_tags = @context["removed_tags"] | ||
|
||
@raw = | ||
if added_tags.present? && removed_tags.present? | ||
I18n.t( | ||
"chat_integration.topic_tag_changed.added_and_removed", | ||
added: tag_list_to_raw.call(added_tags), | ||
removed: tag_list_to_raw.call(removed_tags), | ||
) | ||
elsif added_tags.present? | ||
I18n.t( | ||
"chat_integration.topic_tag_changed.added", | ||
added: tag_list_to_raw.call(added_tags), | ||
) | ||
elsif removed_tags.present? | ||
I18n.t( | ||
"chat_integration.topic_tag_changed.removed", | ||
removed: tag_list_to_raw.call(removed_tags), | ||
) | ||
end | ||
end | ||
@raw | ||
end | ||
end | ||
end |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is that valid indentation here? Maybe it's github tripping
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.