Skip to content

Commit

Permalink
Merge pull request #725 from pil0u/main
Browse files Browse the repository at this point in the history
Release 2024.12.4
  • Loading branch information
pil0u authored Dec 5, 2024
2 parents 73a6ba7 + f0375b8 commit cb1af0f
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 34 deletions.
16 changes: 8 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ GEM
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
logger (1.6.1)
loofah (2.22.0)
loofah (2.23.1)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
mail (2.8.1)
Expand Down Expand Up @@ -224,13 +224,13 @@ GEM
net-smtp (0.5.0)
net-protocol
nio4r (2.7.3)
nokogiri (1.16.7-aarch64-linux)
nokogiri (1.16.8-aarch64-linux)
racc (~> 1.4)
nokogiri (1.16.7-arm64-darwin)
nokogiri (1.16.8-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.7-x86_64-darwin)
nokogiri (1.16.8-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.16.7-x86_64-linux)
nokogiri (1.16.8-x86_64-linux)
racc (~> 1.4)
oauth2 (2.0.9)
faraday (>= 0.17.3, < 3.0)
Expand Down Expand Up @@ -298,9 +298,9 @@ GEM
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-html-sanitizer (1.6.0)
rails-html-sanitizer (1.6.1)
loofah (~> 2.21)
nokogiri (~> 1.14)
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
railties (7.2.1.1)
actionpack (= 7.2.1.1)
activesupport (= 7.2.1.1)
Expand Down Expand Up @@ -494,4 +494,4 @@ RUBY VERSION
ruby 3.3.5p100

BUNDLED WITH
2.5.22
2.5.23
21 changes: 13 additions & 8 deletions app/controllers/snippets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def create
)

if @snippet.save
post_slack_message
update_slack_thread
redirect_to snippet_path(day: params[:day], challenge: params[:challenge]), notice: "Your solution was published"
else
redirect_to snippet_path(day: params[:day], challenge: params[:challenge]), alert: @snippet.errors.full_messages
Expand All @@ -63,8 +63,9 @@ def discuss
@snippet = Snippet.find(params[:id])
return redirect_to @snippet.slack_url if @snippet.slack_url.present?

solution_markdown = "<#{helpers.snippet_url(day: @snippet.day, challenge: @snippet.challenge, anchor: @snippet.id)}|solution>"
text = "`SOLUTION` Hey <@#{@snippet.user.slack_id}>, some people want to discuss your :#{@snippet.language}-hd: #{solution_markdown} on puzzle #{@snippet.day} part #{@snippet.challenge}"
message = client.chat_postMessage(channel: ENV.fetch("SLACK_CHANNEL", "#aoc-dev"), text:)
message = client.chat_postMessage(channel: ENV.fetch("SLACK_CHANNEL", "C064BH3TLGJ"), text:)
slack_thread = client.chat_getPermalink(channel: message["channel"], message_ts: message["message"]["ts"])
@snippet.update(slack_url: slack_thread[:permalink])

Expand All @@ -77,21 +78,25 @@ def client
@client ||= Slack::Web::Client.new
end

def post_slack_message
def update_slack_thread
puzzle = Puzzle.by_date(Aoc.begin_time.change(day: params[:day]))
return if puzzle.thread_ts.nil?

username = "<#{helpers.profile_url(current_user.uid)}|#{current_user.username}>"
text = "#{username} submitted a new #{solution_markdown} for part #{params[:challenge]} in :#{@snippet.language}-hd:"
client.chat_postMessage(channel: ENV.fetch("SLACK_CHANNEL", "#aoc-dev"), text:, thread_ts: puzzle.thread_ts)
snippets = Snippet.includes(:user).where(day: @snippet.day)
part_one, part_two = snippets.partition { |snippet| snippet.challenge == 1 }.map do |solutions|
solutions.map { |snippet| solution_markdown(snippet, "#{snippet.user.username} :#{snippet.language}-hd:") }
end

text = "#{puzzle.title}\n\nSolutions for part 1: #{part_one.join(', ')}\nSolutions for part 2: #{part_two.join(', ')}"
client.chat_update(channel: ENV.fetch("SLACK_CHANNEL", "C064BH3TLGJ"), text:, ts: puzzle.thread_ts)
end

def set_snippet
@snippet = current_user.snippets.find(params[:id])
end

def solution_markdown
"<#{helpers.snippet_url(day: @snippet.day, challenge: @snippet.challenge, anchor: @snippet.id)}|solution>"
def solution_markdown(snippet = @snippet, text = "solution")
"<#{helpers.snippet_url(day: snippet.day, challenge: snippet.challenge, anchor: snippet.id)}|#{text}>"
end

def snippet_params
Expand Down
2 changes: 1 addition & 1 deletion app/domains/completions/fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def fetch_completions(id)
JSON.parse(response.body)["members"]
rescue JSON::ParserError => e
client = Slack::Web::Client.new
channel = "#aoc-dev"
channel = "C064BH3TLGJ"

if Rails.env.development? || (Rails.env.production? && ENV.fetch("THIS_IS_STAGING", nil))
text = "(not prod) Failed to parse JSON from AoC API"
Expand Down
19 changes: 9 additions & 10 deletions app/jobs/generate_slack_thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,26 @@ class GenerateSlackThread < ApplicationJob
queue_as :default

retry_on SlackError do |_, error|
client.chat_postMessage(channel: "#aoc-dev", text: error)
client.chat_postMessage(channel: "C064BH3TLGJ", text: error)
end

def perform(date)
@puzzle = Puzzle.find_or_create_by(date:)
return if @puzzle.slack_url.present?

if @puzzle.persisted?
@puzzle.title = scraped_title || "`SPOILER` <#{@puzzle.url}|Day #{date.day}>"
@puzzle.thread_ts = message["message"]["ts"]
@puzzle.slack_url = permalink
@puzzle.save!
@puzzle.update!(title: scraped_title || "`SPOILER` <#{@puzzle.url}|Day #{date.day}>")
@puzzle.update!(thread_ts: message["message"]["ts"])
@puzzle.update!(slack_url: permalink)
else
client.chat_postMessage(channel: "#aoc-dev", text: @puzzle.errors.full_messages.join(", "))
client.chat_postMessage(channel: "C064BH3TLGJ", text: @puzzle.errors.full_messages.join(", "))
end
end

private

def channel
ENV.fetch("SLACK_CHANNEL", "#aoc-dev")
ENV.fetch("SLACK_CHANNEL", "C064BH3TLGJ")
end

def client
Expand All @@ -46,10 +45,10 @@ def message

def permalink
@permalink ||= begin
response = client.chat_getPermalink(channel:, message_ts: message["message"]["ts"])[:permalink]
response = client.chat_getPermalink(channel:, message_ts: message["message"]["ts"])
raise SlackError.new, "Failed to get permalink for day ##{@puzzle.date.day}" unless response["ok"]

response
response[:permalink]
end
end

Expand All @@ -64,7 +63,7 @@ def scraped_title
rescue OpenURI::HTTPError
day = @puzzle.date.day
client.chat_postMessage(
channel: "#aoc-dev",
channel: "C064BH3TLGJ",
text: "Title not found for day ##{day}, run `bundle exec rake 'update_puzzle_thread[#{day},#{channel}]'`"
)

Expand Down
4 changes: 2 additions & 2 deletions app/jobs/update_puzzles_difficulty_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def perform
end

client = Slack::Web::Client.new
channel = "#aoc-dev"
channel = "C064BH3TLGJ"

puzzles.each do |puzzle|
day = puzzle.date.day
Expand Down Expand Up @@ -47,7 +47,7 @@ def perform
- part 2 is *`#{part_2[:difficulty]}`* #{part_2[:colour]}
TEXT

client.chat_postMessage(channel: ENV.fetch("SLACK_CHANNEL", "#aoc-dev"), text:)
client.chat_postMessage(channel: ENV.fetch("SLACK_CHANNEL", "C064BH3TLGJ"), text:)
end

Rails.logger.info "✔ Puzzle difficulties updated"
Expand Down
6 changes: 3 additions & 3 deletions app/presenters/stats_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def set_influencer_achievement
user_referrals_count = @user&.referees&.count

state = :locked
state = :unlocked if referrals_count >= 100
state = :unlocked_plus if referrals_count >= 100 && user_referrals_count.to_i > 0
title = "Influencer\n\nWe have reached 100 referrals 🤝 Actually #{referrals_count} and counting!"
state = :unlocked if referrals_count >= 50
state = :unlocked_plus if referrals_count >= 50 && user_referrals_count.to_i > 0
title = "Influencer\n\nWe have reached 50 referrals 🤝 Actually #{referrals_count} and counting!"
title += " - and you have personally invited #{user_referrals_count} of them, thank you for spreading the love <3" if state == :unlocked_plus

{ nature: "influencer", state:, title: }
Expand Down
2 changes: 1 addition & 1 deletion app/views/days/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<span>·</span>
<%= link_to "puzzle", Aoc.url(@day), target: :_blank, rel: :noopener, class: "link-explicit link-external" %>

<% if @puzzle.slack_url.present? %>
<% if @puzzle&.slack_url.present? %>
<span>·</span>
<%= link_to "slack thread", @puzzle.slack_url, target: :_blank, rel: :noopener, class: "link-explicit link-slack" %>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/update_puzzle_thread.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
desc "update_puzzle_thread"
task :update_puzzle_thread, %i[day channel] => :environment do |_, args|
next p "day param missing" if args[:day].nil?
next p "channel missing or incorrect" unless args[:channel].in? %w[aoc aoc-dev]
next p "channel missing or incorrect" unless args[:channel].in? %w[C02PN711H09 C064BH3TLGJ]

puzzle = Puzzle.by_date(Aoc.begin_time.change(day: args[:day]))
next p "Puzzle not found" if puzzle.nil?
Expand Down

0 comments on commit cb1af0f

Please sign in to comment.