Skip to content

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

17.9 KB
Raw Blame
# frozen_string_literal: true

module DiscourseWorkflows
  module Nodes
    module Post
      class V1 < NodeType
        OPERATIONS = %w[create edit get list delete recover].freeze
        STATUS_OPTIONS = %w[
          any
          open
          closed
          archived
          listed
          unlisted
          deleted
          public
          noreplies
          single_user
        ].freeze
        POST_TYPE_OPTIONS = %w[regular all first reply moderator_action small_action whisper].freeze
        ORDER_OPTIONS = %w[latest oldest latest_topic oldest_topic likes].freeze
        DEFAULT_LIMIT = 30
        DEFAULTS = {
          "operation" => "create",
          "whisper" => false,
          "bypass_permission_checks" => false,
          "include_raw" => true,
          "include_cooked" => false,
          "body_character_limit" => 0,
          "post_type" => "regular",
          "status" => "any",
          "order" => "latest",
          "limit" => DEFAULT_LIMIT,
          "offset" => 0,
        }.freeze
        MAX_LIMIT = 800
        REPLY_TO_POST_JOIN = <<~SQL
          LEFT JOIN posts reply_posts
            ON reply_posts.topic_id = posts.topic_id
            AND reply_posts.post_number = posts.reply_to_post_number
            AND reply_posts.deleted_at IS NULL
        SQL

        def self.list_string_property(control: nil, hidden: false)
          property = {
            type: :string,
            required: false,
            display_options: {
              show: {
                operation: ["list"],
              },
            },
          }

          ui = {}
          ui[:control] = control if control
          ui[:hidden] = true if hidden
          property[:ui] = ui if ui.present?

          property
        end

        description(
          name: "action:post",
          version: "1.0",
          defaults: {
            icon: "reply",
            color: "teal",
          },
          group: "discourse_actions",
          capabilities: {
            run_scope: "per_item",
          },
          output_contracts: [{ schema: Schema::POST_SCHEMA }],
          properties: {
            operation: {
              type: :options,
              required: true,
              options: OPERATIONS,
              default: "create",
            },
            topic_id: {
              type: :string,
              required: true,
              display_options: {
                show: {
                  operation: ["create"],
                },
              },
            },
            raw: {
              type: :string,
              required: true,
              ui: {
                control: :textarea,
              },
              display_options: {
                show: {
                  operation: %w[create edit],
                },
              },
            },
            reply_to_post_number: {
              type: :integer,
              required: false,
              display_options: {
                show: {
                  operation: ["create"],
                },
              },
            },
            whisper: {
              type: :boolean,
              required: false,
              default: false,
              ui: {
                control: :boolean,
                expression: true,
              },
              display_options: {
                show: {
                  operation: ["create"],
                },
              },
            },
            author_username: {
              type: :string,
              required: false,
              default: "system",
              ui: {
                control: :actor,
              },
              display_options: {
                show: {
                  operation: ["create"],
                },
              },
            },
            bypass_permission_checks: {
              type: :boolean,
              required: false,
              default: false,
              ui: {
                control: :boolean,
              },
              display_options: {
                show: {
                  operation: ["create"],
                },
              },
            },
            post_id: {
              type: :string,
              required: true,
              display_options: {
                show: {
                  operation: %w[edit get delete recover],
                },
              },
            },
            editor_username: {
              type: :string,
              required: false,
              default: "system",
              ui: {
                control: :actor,
              },
              display_options: {
                show: {
                  operation: ["edit"],
                },
              },
            },
            query: {
              type: :string,
              required: false,
              ui: {
                control: :filter_query,
                filter: :posts,
              },
              display_options: {
                show: {
                  operation: ["list"],
                },
              },
            },
            include_raw: {
              type: :boolean,
              required: false,
              default: true,
              ui: {
                control: :boolean,
              },
              display_options: {
                show: {
                  operation: %w[get list],
                },
              },
            },
            include_cooked: {
              type: :boolean,
              required: false,
              default: false,
              ui: {
                control: :boolean,
              },
              display_options: {
                show: {
                  operation: %w[get list],
                },
              },
            },
            body_character_limit: {
              type: :integer,
              required: false,
              default: 0,
              display_options: {
                show: {
                  operation: %w[get list],
                },
              },
            },
            created_after: list_string_property(hidden: true),
            created_before: list_string_property(hidden: true),
            topic_created_after: list_string_property(hidden: true),
            topic_created_before: list_string_property(hidden: true),
            categories: list_string_property(control: :category, hidden: true),
            exclude_categories: list_string_property(hidden: true),
            exact_category_match: {
              type: :boolean,
              required: false,
              default: false,
              ui: {
                control: :boolean,
                hidden: true,
              },
              display_options: {
                show: {
                  operation: ["list"],
                },
              },
            },
            tags: list_string_property(control: :tags, hidden: true),
            exclude_tags: list_string_property(control: :tags, hidden: true),
            topics: list_string_property(hidden: true),
            usernames: list_string_property(hidden: true),
            groups: list_string_property(hidden: true),
            post_type: {
              type: :options,
              required: false,
              options: POST_TYPE_OPTIONS,
              default: "regular",
              ui: {
                hidden: true,
              },
              display_options: {
                show: {
                  operation: ["list"],
                },
              },
            },
            status: {
              type: :options,
              required: false,
              options: STATUS_OPTIONS,
              default: "any",
              ui: {
                hidden: true,
              },
              display_options: {
                show: {
                  operation: ["list"],
                },
              },
            },
            keywords: list_string_property(hidden: true),
            topic_keywords: list_string_property(hidden: true),
            order: {
              type: :options,
              required: false,
              options: ORDER_OPTIONS,
              default: "latest",
              ui: {
                hidden: true,
              },
              display_options: {
                show: {
                  operation: ["list"],
                },
              },
            },
            limit: {
              type: :integer,
              required: false,
              default: DEFAULT_LIMIT,
              min: 1,
              max: MAX_LIMIT,
              display_options: {
                show: {
                  operation: ["list"],
                },
              },
            },
            offset: {
              type: :integer,
              required: false,
              default: 0,
              display_options: {
                show: {
                  operation: ["list"],
                },
              },
            },
            advanced_filter: {
              type: :string,
              required: false,
              ui: {
                control: :textarea,
                hidden: true,
              },
              display_options: {
                show: {
                  operation: ["list"],
                },
              },
            },
            **actor_property(display_options: { show: { operation: %w[get list delete recover] } }),
          },
        )

        def execute(exec_ctx)
          items =
            exec_ctx.input_items.flat_map.with_index do |_item, item_index|
              config = config_for(exec_ctx, item_index)
              Array.wrap(execute_with_config(exec_ctx, config, item_index))
            end

          [items]
        end

        private

        def config_for(exec_ctx, item_index)
          Hash.new do |config, key|
            config[key] = exec_ctx.get_node_parameter(key, item_index, default: DEFAULTS[key])
          end
        end

        def execute_with_config(exec_ctx, config, item_index)
          case config["operation"]
          when "create"
            wrap(create_post(exec_ctx, config, item_index))
          when "edit"
            wrap(edit_post(exec_ctx, config, item_index))
          when "get"
            wrap(get_post(exec_ctx, config, item_index))
          when "list"
            list_posts(exec_ctx, config, item_index).map { |data| wrap(data) }
          when "delete"
            wrap(delete_post(exec_ctx, config, item_index))
          when "recover"
            wrap(recover_post(exec_ctx, config, item_index))
          else
            raise_node_error!(
              I18n.t(
                "discourse_workflows.errors.unknown_operation",
                operation: config["operation"],
              ),
            )
          end
        end

        def create_post(exec_ctx, config, item_index)
          actor = exec_ctx.actor_from_parameter("author_username", item_index)
          post =
            exec_ctx.create_post(
              user: actor,
              raw: config["raw"],
              topic_id: config["topic_id"],
              reply_to_post_number: config["reply_to_post_number"],
              whisper: config["whisper"],
              bypass_permission_checks: config["bypass_permission_checks"],
            )

          {
            post:
              serialize_post(
                post,
                guardian: actor.guardian,
                include_raw: true,
                include_cooked: true,
              ),
          }
        end

        def edit_post(exec_ctx, config, item_index)
          editor = exec_ctx.actor_from_parameter("editor_username", item_index)
          post = exec_ctx.edit_post(user: editor, post_id: config["post_id"], raw: config["raw"])

          {
            post:
              serialize_post(
                post,
                guardian: editor.guardian,
                include_raw: true,
                include_cooked: true,
              ),
          }
        end

        def delete_post(exec_ctx, config, item_index)
          actor = exec_ctx.actor_from_parameter("actor_username", item_index)
          post = exec_ctx.destroy_post(user: actor, post_id: config["post_id"])

          { post: serialize_post(post, guardian: actor.guardian) }
        end

        def recover_post(exec_ctx, config, item_index)
          actor = exec_ctx.actor_from_parameter("actor_username", item_index)
          post = exec_ctx.recover_post(user: actor, post_id: config["post_id"])

          { post: serialize_post(post, guardian: actor.guardian) }
        end

        def get_post(exec_ctx, config, item_index)
          post = ::Post.find(config["post_id"])
          actor = exec_ctx.actor_from_parameter("actor_username", item_index)
          raise Discourse::InvalidAccess if !actor.guardian.can_see?(post)

          { post: serialized_post(post, guardian: actor.guardian, config: config) }
        end

        def list_posts(exec_ctx, config, item_index)
          guardian = exec_ctx.actor_from_parameter("actor_username", item_index).guardian
          limit = bounded_integer(config["limit"], default: DEFAULT_LIMIT, min: 1, max: MAX_LIMIT)
          offset = bounded_integer(config["offset"], default: 0, min: 0)
          query = query_from_config(config)
          filter = PostsFilter.new(query, guardian:, limit: limit, offset: offset)

          if filter.invalid_filters.present?
            raise_node_error!(
              I18n.t(
                "discourse_workflows.errors.post.invalid_filter",
                fragments: filter.invalid_filters.join(" "),
              ),
            )
          end

          posts =
            filter
              .search
              .joins(REPLY_TO_POST_JOIN)
              .select("posts.*, reply_posts.id AS reply_to_post_id")
              .preload(:user, :reply_to_user, :uploads, topic: %i[category tags])
          posts.map { |post| { post: serialized_post(post, guardian:, config:) } }
        end

        def serialized_post(post, guardian:, config:)
          data =
            serialize_post(
              post,
              guardian: guardian,
              include_raw: config["include_raw"],
              include_cooked: config["include_cooked"],
            )

          truncate_body_fields(data, config["body_character_limit"])
        end

        def truncate_body_fields(data, limit)
          limit = bounded_integer(limit, default: 0, min: 0)
          return data if limit <= 0

          %i[raw cooked].each do |field|
            value = data[field]
            next if !value.is_a?(String) || value.length <= limit

            data[field] = truncate_middle(value, limit)
            data[:"#{field}_truncated"] = true
            data[:"#{field}_original_length"] = value.length
          end

          data
        end

        def truncate_middle(value, limit)
          return "" if limit <= 0

          head_length = (limit / 2.0).ceil
          tail_length = limit - head_length
          characters = value.each_char.to_a
          characters.first(head_length).join + characters.last(tail_length).join
        end

        def query_from_config(config)
          query = config["query"].to_s.strip
          return query if query.present?

          parts = []
          add_query_part(parts, "after", config["created_after"])
          add_query_part(parts, "before", config["created_before"])
          add_query_part(parts, "topic_after", config["topic_created_after"])
          add_query_part(parts, "topic_before", config["topic_created_before"])
          add_category_part(
            parts,
            "category",
            config["categories"],
            exact: config["exact_category_match"],
          )
          add_category_part(
            parts,
            "exclude_category",
            config["exclude_categories"],
            exact: config["exact_category_match"],
          )
          add_query_part(parts, "tag", config["tags"])
          add_query_part(parts, "exclude_tag", config["exclude_tags"])
          add_query_part(parts, "topics", config["topics"])
          add_query_part(parts, "usernames", config["usernames"])
          add_query_part(parts, "groups", config["groups"])
          add_query_part(parts, "keywords", config["keywords"])
          add_query_part(parts, "topic_keywords", config["topic_keywords"])
          if POST_TYPE_OPTIONS.include?(config["post_type"]) && config["post_type"] != "regular"
            add_query_part(parts, "post_type", config["post_type"])
          end
          if STATUS_OPTIONS.include?(config["status"]) && config["status"] != "any"
            add_query_part(parts, "status", config["status"])
          end
          add_query_part(parts, "order", config["order"]) if ORDER_OPTIONS.include?(config["order"])

          advanced_filter = config["advanced_filter"].to_s.strip
          parts << advanced_filter if advanced_filter.present?
          parts.join(" ")
        end

        def add_query_part(parts, key, value)
          normalized = list_value(value)
          parts << "#{key}:#{normalized}" if normalized.present?
        end

        def add_category_part(parts, key, value, exact:)
          normalized = list_value(value)
          return if normalized.blank?

          if exact
            normalized =
              normalized
                .split(",")
                .map { |part| part.start_with?("=") ? part : "=#{part}" }
                .join(",")
          end
          parts << "#{key}:#{normalized}"
        end

        def list_value(value)
          Array
            .wrap(value)
            .flat_map { |item| item.to_s.split(",") }
            .map(&:strip)
            .reject(&:blank?)
            .map { |item| quote_query_value(item) }
            .join(",")
        end

        def quote_query_value(value)
          return value if value.match?(/\A["'].*["']\z/)
          return value if !value.match?(/\s/)

          %("#{value.delete('"')}")
        end

        def bounded_integer(value, default:, min:, max: nil)
          integer = Integer(value.presence || default)
          integer = [integer, min].max
          integer = [integer, max].min if max
          integer
        rescue ArgumentError, TypeError
          default
        end
      end
    end
  end
end