-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: add chat integration reference post #216
Changes from 12 commits
af5e7e8
c5d7568
3c237ec
b316558
846996d
8c1513d
3d54fd1
6987ca3
96b91bf
42e6830
1df0946
f3eda71
96f8da9
5144318
b865868
d5c0774
620a5a9
2e07334
e4d464e
86979fb
3d46798
94bdec9
3ea5b6c
4f681a8
fa2a8eb
9a60a45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
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. I think even this should be avoided, we should harcode everything in migrations |
||
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 |
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.
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 |
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.