Skip to content
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

Extract enhancer jobs to a speaker associated object #466

Merged
merged 5 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/avo/actions/speaker_github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ class Avo::Actions::SpeakerGitHub < Avo::BaseAction
self.name = "Try to enhance Speaker profile with GitHub"

def handle(query:, fields:, current_user:, resource:, **args)
query.each do |record|
Speaker::EnhanceProfileJob.perform_later(speaker: record, sleep: 2) # sleep 2 seconds to avoid rate limit
query.each do |speaker|
speaker.profile_enhancer.enhance_all_later!
end
end
end
3 changes: 1 addition & 2 deletions app/controllers/speakers/enhance_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ class Speakers::EnhanceController < ApplicationController
def update
@speaker = Speaker.find_by(slug: params[:slug])

Speaker::EnhanceProfileJob.perform_later(speaker: @speaker) if @speaker.github.present?
Speaker::FetchBskyMetadata.perform_later(speaker: @speaker) if @speaker.bsky.present?
@speaker.profile_enhancer.enhance_all_later!

flash[:notice] = "Speaker profile will be updated soon."

Expand Down
24 changes: 0 additions & 24 deletions app/jobs/speaker/fetch_bsky_metadata.rb

This file was deleted.

6 changes: 2 additions & 4 deletions app/models/speaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class Speaker < ApplicationRecord
belongs_to :canonical, class_name: "Speaker", optional: true
belongs_to :user, primary_key: :github_handle, foreign_key: :github, optional: true

has_object :profile_enhancer

# validations
validates :canonical, exclusion: {in: ->(speaker) { [speaker] }, message: "can't be itself"}

Expand Down Expand Up @@ -161,10 +163,6 @@ def fallback_avatar_url(size: 200)
"https://ui-avatars.com/api/?name=#{url_safe_initials}&size=#{size}&background=DC133C&color=fff"
end

def fetch_bsky_metadata!
Speaker::FetchBskyMetadata.new.perform(speaker: self)
end

def broadcast_header
broadcast_update target: dom_id(self, :header_content), partial: "speakers/header_content", locals: {speaker: self}
end
Expand Down
35 changes: 35 additions & 0 deletions app/models/speaker/profile_enhancer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Speaker::ProfileEnhancer < ActiveRecord::AssociatedObject
BSKY_HOST = "api.bsky.app".freeze

performs :enhance_all!, queue_as: :low do
retry_on StandardError, attempts: 3, wait: :polynomially_longer
limits_concurrency to: 1, key: "github_api"
end

def enhance_all!
enhance_with_github!
enhance_with_bsky!
end

def enhance_with_github!
# return unless speaker.github.present?

# TODO: implement
end

def enhance_with_bsky!
return unless speaker.bsky.present?

response = bsky.get_request("app.bsky.actor.getProfile", {
actor: speaker.bsky
})

speaker.update!(bsky_metadata: response)
end

private

def bsky
@bsky ||= Minisky.new(BSKY_HOST, nil)
end
end
8 changes: 8 additions & 0 deletions test/models/speaker/profile_enhancer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "test_helper"

class Speaker::ProfileEnhancerTest < ActiveSupport::TestCase
setup do
# @speaker = speakers(:TODO_fixture_name)
# @profile_enhancer = @speaker.profile_enhancer
end
end
Loading