railslink/railslink

View on GitHub
app/jobs/slack_event/member_joined_channel_job.rb

Summary

Maintainability
A
45 mins
Test Coverage
#
# See https://api.slack.com/events/member_joined_channel
#
# Example payload:
#
# {"token"=>"xxxx",
# "team_id"=>"TCF0ZD3LL",
# "api_app_id"=>"ACELG3B4H",
# "event"=>{
#   "type"=>"member_joined_channel",
#   "user"=>"UCFC0B6E9",
#   "channel"=>"CDPS07TPV",
#   "channel_type"=>"C",
#   "team"=>"TCF0ZD3LL",
#   "event_ts"=>"1540669673.000100"},
# "type"=>"event_callback",
# "event_id"=>"EvDRB8G2P8",
# "event_time"=>1540669673,
# "authed_users"=>["UCFC0B6E9"]}
#
class SlackEvent::MemberJoinedChannelJob < ApplicationJob

  attr_reader :options
  private :options

  def perform(options = {})
    @options = options.with_indifferent_access
    event = @options[:event].with_indifferent_access

    # It would be highly odd for the channel not to exist,
    # since the only way this event is triggered is via channel-join
    # however, the website may not yet have the channel in it's database
    # and if it doesn't we won't be able to look it up so bail out now.
    channel = SlackChannel.find_by(uid: event[:channel])
    return if channel.nil?

    text = text_for(channel)
    return if text.blank?

    client = Slack::Web::Client.new
    client.chat_postEphemeral(
      channel: event[:channel],
      user: event[:user],
      text: text
    )

  # This too is highly unlikely, but if the use joins and leaves quickly
  # before this event can fire, it could happen.
  rescue Slack::Web::Api::Errors::SlackError => e
    raise unless %w[user_not_in_channel].include?(e.message)
  end

  private

  # Return the text message for the given channel. An empty response
  # is a valid response indicating there isn't a message for that channel.
  #
  # The text message should not be wrapped as that will break Slack's automatic
  # wrapping on presentation.
  #
  # Returns String (or nil)
  def text_for(channel)
    case channel.name
    when "job-offers"
      <<~END_OF_TEXT
        Welcome to our job posting channel. This channel is specifically for posting job offers.

        If you are seeking work, please use #job-seekers.

        For all other discussion of jobs, workplaces, career paths, etc. other than job opportunities, please use #job-chat.

        Your job offer should include the location, remote friendliness, company name, as well as instructions on how to apply.

        Be courteous and do not cross-post to other channels or post repeatedly for the same job offer. Do not pin your job post.

        Please use Slack threads to followup to keep the main channel's signal to noise ratio high.

        NOTE: To help keep this channel on topic for the community, we're asking that the job postings be related in some way to ruby or ruby on rails development (this include frontend work as well).  If we don't feel the job aligns with that heuristic we'll be taking down posts without warning.

        Thank you!
      END_OF_TEXT
    end
  end

end