Thursday, June 04, 2026 3:09:44 AM
> user_notification_route.rb
# frozen_string_literal: true

module ESM
  class UserNotificationRoute < ApplicationRecord
    # =============================================================================
    # INITIALIZE
    # =============================================================================

    # =============================================================================
    # DATA STRUCTURE
    # =============================================================================

    attr_writer :channel

    # =============================================================================
    # ASSOCIATIONS
    # =============================================================================

    # =============================================================================
    # VALIDATIONS
    # =============================================================================

    # =============================================================================
    # CALLBACKS
    # =============================================================================

    # =============================================================================
    # SCOPES
    # =============================================================================

    # =============================================================================
    # CLASS METHODS
    # =============================================================================

    def self.by_user_community_channel_and_server
      routes = includes(:user, :source_server, :destination_community)
        .load
        .group_by(&:user)
        .sort_by.dig(0).method(:discord_username).case_insensitive
        .map do |user, routes|
          routes = routes.group_by do |route|
            [route.destination_community_id, route.channel_id, route.source_server_id]
          end

          routes = routes.filter_map do |_, routes|
            route = routes.first

            channel = route.channel
            server = route.source_server
            community = route.destination_community

            routes = routes.sort_by(&:notification_type)

            {channel:, server:, community:, routes:}
          end

          [user, routes]
        end

      routes.to_h
    end

    # =============================================================================
    # INSTANCE METHODS
    # =============================================================================

    def channel?
      !!channel
    end

    def channel
      @channel ||= ESM.bot.channel(
        channel_id,
        user_id:,
        community_id: destination_community_id
      )&.to_istruct
    end

    def channel_name
      channel&.name
    end
  end
end
All opinions represented herein are my own
- © 2024 - 2026 itsthedevman
- build 4294fb2