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

Social media profiles to conferences #600

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion app/avo/resources/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Avo::Resources::Event < Avo::BaseResource
if id.is_a?(Array)
query.where(slug: id)
else
query.find_by(slug: id)
query.find_by(slug: id) || query.find_by(id:)
end
}
self.external_link = -> {
Expand All @@ -27,6 +27,7 @@ def fields
field :talks, as: :has_many
field :speakers, as: :has_many, through: :talks
field :topics, as: :has_many
field :social_profiles, as: :has_many, use_resource: "Avo::Resources::SocialProfile"
end

def actions
Expand Down
8 changes: 8 additions & 0 deletions app/avo/resources/social_profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Avo::Resources::SocialProfile < Avo::BaseResource
def fields
field :id, as: :id
field :provider, enum: ::SocialProfile.providers, as: :select, required: true
field :value, as: :text
field :sociable, as: :belongs_to, polymorphic_as: :sociable, types: [::Speaker, ::Event], foreign_key: :slug
end
end
10 changes: 3 additions & 7 deletions app/avo/resources/speaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Avo::Resources::Speaker < Avo::BaseResource
if id.is_a?(Array)
query.where(slug: id)
else
query.find_by(slug: id)
query.find_by(slug: id) || query.find_by(id:)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why this why needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Speaker lookup is done based on :slug, but when creating association the lookup is done based on id. I'm new to Avo, so couldn't find a better solution. Feel free to suggest a better approach.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok thanks. I understand now. Will look if we can have an alternative. this is a minor point

end
}
self.search = {
Expand All @@ -17,19 +17,15 @@ class Avo::Resources::Speaker < Avo::BaseResource
def fields
field :id, as: :id, link_to_record: true
field :name, as: :text, link_to_record: true, sortable: true
field :twitter, as: :text
field :github, as: :text
field :speakerdeck, as: :text
field :mastodon, as: :text, hide_on: :index
field :linkedin, as: :text, hide_on: :index
field :bsky, as: :text, hide_on: :index
field :bio, as: :textarea, hide_on: :index
field :website, as: :text, hide_on: :index
field :slug, as: :text, hide_on: :index
field :talks_count, as: :number, sortable: true
field :canonical, as: :belongs_to, hide_on: :index
# field :suggestions, as: :has_many
field :speaker_talks, as: :has_many, resource: Avo::Resources::SpeakerTalk, attach_scope: -> { query.order(title: :asc) }
# field :speaker_talks, as: :has_many
field :social_profiles, as: :has_many, use_resource: "Avo::Resources::SocialProfile"
field :talks, as: :has_many, use_resource: "Avo::Resources::Talk", attach_scope: -> { query.order(title: :asc) }, searchable: true
end

Expand Down
4 changes: 4 additions & 0 deletions app/controllers/avo/social_profiles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This controller has been generated to enable Rails' resource routes.
# More information on https://docs.avohq.io/3.0/controllers.html
class Avo::SocialProfilesController < Avo::ResourcesController
end
74 changes: 74 additions & 0 deletions app/controllers/social_profiles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class SocialProfilesController < ApplicationController
skip_before_action :authenticate_user!, only: [:edit, :update]
before_action :check_ownership, only: [:new, :create]
before_action :check_provider, only: [:new]
before_action :set_social_profile, only: [:edit, :update]

def new
@social_profile = @sociable.social_profiles.new(provider: params[:provider])
end

def create
@social_profile = @sociable.social_profiles.new(social_profile_params)

begin
ActiveRecord::Base.transaction do
@social_profile.save!
@suggestion = @social_profile.create_suggestion_from(
params: @social_profile.attributes.slice("provider", "value"),
new_record: true
)
end
rescue ActiveRecord::RecordInvalid
end

respond_to do |format|
if @suggestion&.persisted?
flash[:notice] = "Saved"
format.turbo_stream
else
format.turbo_stream { render :create, status: :unprocessable_entity }
end
end
end

def edit
end

def update
begin
@suggestion = @social_profile.create_suggestion_from(params: social_profile_params, user: Current.user)
rescue ActiveRecord::RecordInvalid
end

respond_to do |format|
if @suggestion&.persisted?
flash[:notice] = "Saved"
format.turbo_stream
else
format.turbo_stream { render :update, status: :unprocessable_entity }
end
end
end

private

def social_profile_params
params.require(:social_profile).permit(
:provider,
:value
)
end

def set_social_profile
@social_profile ||= SocialProfile.find(params[:id])
end

def check_provider
raise StandardError, "Invalid Social Provider" unless SocialProfile::PROVIDERS.include?(params[:provider])
end

def check_ownership
head :forbidden unless @sociable.managed_by?(Current.user)
end
end
9 changes: 9 additions & 0 deletions app/controllers/speakers/social_profiles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Speakers::SocialProfilesController < ::SocialProfilesController
prepend_before_action :set_sociable

private

def set_sociable
@sociable ||= Speaker.find_by!(slug: params[:speaker_slug])
end
end
6 changes: 0 additions & 6 deletions app/controllers/speakers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,7 @@ def speaker_params
params.require(:speaker).permit(
:name,
:github,
:twitter,
:bsky,
:linkedin,
:mastodon,
:bio,
:website,
:speakerdeck,
:pronouns_type,
:pronouns,
:slug
Expand Down
21 changes: 21 additions & 0 deletions app/models/concerns/sociable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Sociable
extend ActiveSupport::Concern

included do
has_many :social_profiles, -> { order(:created_at) }, as: :sociable, dependent: :destroy
end

SocialProfile::PROVIDERS.each do |method|
define_method(method) do
social_profiles.send(method).first&.value
end

define_method(:"build_#{method}") do |value|
social_profiles.build(provider: method, value:)
end

define_method(:"create_#{method}") do |value|
social_profiles.create(provider: method, value:)
end
end
end
7 changes: 5 additions & 2 deletions app/models/concerns/suggestable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ module Suggestable
has_many :suggestions, as: :suggestable, dependent: :destroy
end

def create_suggestion_from(params:, user: Current.user)
suggestions.create(content: select_differences_for(params), suggested_by_id: user&.id).tap do |suggestion|
# NOTE: validate before saving
def create_suggestion_from(params:, user: Current.user, new_record: false)
params = select_differences_for(params) unless new_record

suggestions.create(content: params, suggested_by_id: user&.id).tap do |suggestion|
suggestion.approved!(approver: user) if managed_by?(user)
end
end
Expand Down
3 changes: 2 additions & 1 deletion app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
class Event < ApplicationRecord
include Suggestable
include Sluggable
include Sociable
slug_from :name

# associations
Expand Down Expand Up @@ -289,6 +290,6 @@ def year
end

def website
self[:website].presence || organisation.website
social_profiles.detect { |sp| sp.website? }&.value || organisation.website
end
end
1 change: 1 addition & 0 deletions app/models/organisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
class Organisation < ApplicationRecord
include Sluggable
include Suggestable
include Sociable

include ActionView::Helpers::TextHelper

Expand Down
66 changes: 66 additions & 0 deletions app/models/social_profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# == Schema Information
#
# Table name: social_profiles
#
# id :integer not null, primary key
# provider :string not null
# sociable_type :string not null, indexed => [sociable_id]
# value :string not null
# created_at :datetime not null
# updated_at :datetime not null
# sociable_id :integer not null, indexed => [sociable_type]
#
# Indexes
#
# index_social_profiles_on_sociable (sociable_type,sociable_id)
#
class SocialProfile < ApplicationRecord
include Suggestable
PROVIDERS = %w[twitter linkedin bsky mastodon speakerdeck website]

belongs_to :sociable, polymorphic: true

enum :provider, PROVIDERS.index_by(&:itself), validate: {presence: true}

after_initialize do
self.value = self.class.normalize_value_for(provider.to_sym, value) if provider.present?
end

validates :provider, presence: true
validates :value, presence: true, uniqueness: {scope: :provider}

scope :excluding_provider, ->(provider) { where.not(provider:) }

# normalizes
normalizes :twitter, with: ->(value) { value.gsub(%r{https?://(?:www\.)?(?:x\.com|twitter\.com)/}, "").gsub(/@/, "") }
normalizes :linkedin, with: ->(value) { value.gsub(%r{https?://(?:www\.)?(?:linkedin\.com/in)/}, "") }
normalizes :bsky, with: ->(value) { value.gsub(%r{https?://(?:www\.)?(?:x\.com|bsky\.app/profile)/}, "").gsub(/@/, "") }
normalizes :speakerdeck, with: ->(value) { value.gsub(/^(?:https?:\/\/)?(?:www\.)?speakerdeck\.com\//, "").gsub(/^@/, "") }
normalizes :mastodon, with: ->(value) {
return value if value&.match?(URI::DEFAULT_PARSER.make_regexp)
return "" unless value.count("@") == 2

_, handle, instance = value.split("@")

"https://#{instance}/@#{handle}"
}

def url
case provider.to_sym
when :twitter, :speakerdeck
"https://#{provider}.com/#{value}"
when :linkedin
"https://linkedin.com/in/#{value}"
when :bsky
"https://bsky.app/profile/#{value}"
else
value
end
end

private

def managed_by?(visiting_user)
sociable.managed_by?(visiting_user)
end
end
16 changes: 1 addition & 15 deletions app/models/speaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Speaker < ApplicationRecord
include ActionView::RecordIdentifier
include Sluggable
include Suggestable
include Sociable
include Speaker::Searchable
slug_from :name

Expand Down Expand Up @@ -77,21 +78,6 @@ class Speaker < ApplicationRecord

# normalizes
normalizes :github, with: ->(value) { value.gsub(/^(?:https?:\/\/)?(?:www\.)?github\.com\//, "").gsub(/^@/, "") }
normalizes :twitter, with: ->(value) { value.gsub(%r{https?://(?:www\.)?(?:x\.com|twitter\.com)/}, "").gsub(/@/, "") }
normalizes :bsky, with: ->(value) {
value.gsub(%r{https?://(?:www\.)?(?:x\.com|bsky\.app/profile)/}, "").gsub(/@/, "")
}
normalizes :linkedin, with: ->(value) { value.gsub(%r{https?://(?:www\.)?(?:linkedin\.com/in)/}, "") }
normalizes :bsky, with: ->(value) { value.gsub(%r{https?://(?:www\.)?(?:[^\/]+\.com)/}, "").gsub(/@/, "") }

normalizes :mastodon, with: ->(value) {
return value if value&.match?(URI::DEFAULT_PARSER.make_regexp)
return "" unless value.count("@") == 2

_, handle, instance = value.split("@")

"https://#{instance}/@#{handle}"
}

def self.reset_talks_counts
find_each do |speaker|
Expand Down
11 changes: 6 additions & 5 deletions app/models/speaker/profiles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ def enhance_all_later
socials = github_client.social_accounts(speaker.github)
links = socials.pluck(:provider, :url).to_h

speaker.build_twitter(links["twitter"]) if links["twitter"].present?
speaker.build_mastodon(links["mastodon"]) if links["mastodon"].present?
speaker.build_bsky(links["bluesky"]) if links["bluesky"].present?
speaker.build_linkedin(links["linkedin"]) if links["linkedin"].present?
speaker.build_website(profile.blog) if profile.blog.present?

speaker.update!(
twitter: speaker.twitter.presence || links["twitter"] || "",
mastodon: speaker.mastodon.presence || links["mastodon"] || "",
bsky: speaker.bsky.presence || links["bluesky"] || "",
linkedin: speaker.linkedin.presence || links["linkedin"] || "",
bio: speaker.bio.presence || profile.bio || "",
website: speaker.website.presence || profile.blog || "",
github_metadata: {
profile: JSON.parse(profile.body),
socials: JSON.parse(socials.body)
Expand Down
12 changes: 7 additions & 5 deletions app/views/events/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@
</div>

<div class="container py-8">
<div class="block lg:flex gap-8 align-center justify-between">
<div class="flex flex-col lg:flex-row gap-8 items-center lg:justify-right text-center lg:text-left mb-6 lg:mb-0">
<div class="justify-between block lg:flex gap-8 align-center">
<div class="flex flex-col items-center mb-6 text-center lg:flex-row gap-8 lg:justify-right lg:text-left lg:mb-0">
<%= image_tag image_path(event.avatar_image_path),
class: "rounded-full border border-[#D9DFE3] size-24 md:size-36",
alt: "#{event.name} Avatar",
style: "view-transition-name: avatar",
loading: :lazy %>

<div class="flex-col flex justify-center">
<h1 class="mb-2 text-black font-bold" style="view-transition-name: title"><%= event.name %></h1>
<div class="flex flex-col justify-center">
<h1 class="mb-2 font-bold text-black" style="view-transition-name: title"><%= event.name %></h1>
<h3 class="text-[#636B74]"><%= event.location %> • <%= event.formatted_dates %></h3>
<%= external_link_to event.website.gsub(%r{^https?://}, "").gsub(%r{/$}, ""), event.website %>
<% if event.website.present? %>
<%= external_link_to event.website.gsub(%r{^https?://}, "").gsub(%r{/$}, ""), event.website %>
<% end %>
</div>
</div>

Expand Down
14 changes: 14 additions & 0 deletions app/views/shared/_new_profile.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<%= turbo_frame_tag social_profile do %>
<div class="flex items-center gap-x-2">
<span class="font-semibold">Add</span>
<% SocialProfile::PROVIDERS.each do |provider| %>
<%= ui_button url: new_polymorphic_path([sociable, social_profile], provider:),
id: "add-#{provider}",
kind: :circle,
size: :sm,
class: "hover:bg-#{provider} hover:fill-white border-base-200" do %>
<%= fab(provider, size: :md) %>
<% end %>
<% end %>
</div>
<% end %>
13 changes: 13 additions & 0 deletions app/views/social_profiles/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<%= turbo_frame_tag social_profile do %>
<%= form_with(model: social_profile, url: url, class: "flex flex-col") do |form| %>
<%= form.label :value, "#{form.object.provider.humanize} (URL/Handle/Username)" %>
<div class="flex flex-col gap-x-1">
<span class="text-red-500"><%= social_profile.errors["value"] ? social_profile.errors["value"].first : "" %></span>
<div class="flex items-center gap-x-2">
<%= form.hidden_field :provider, class: "input input-bordered w-full" %>
<%= form.text_field :value, class: "input input-bordered w-full" %>
<%= ui_button "Save", type: :submit %>
</div>
</div>
<% end %>
<% end %>
Loading