Class: FollowsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/follows_controller.rb

Instance Method Summary collapse

Instance Method Details

#create ⇒ Object



80
81
82
83
84
# File 'app/controllers/follows_controller.rb', line 80

def create
  authorize Federails::Following
  current_user.follow @target
  redirect_to @target
end

#destroy ⇒ Object



86
87
88
89
90
# File 'app/controllers/follows_controller.rb', line 86

def destroy
  authorize Federails::Following
  current_user.unfollow @target
  redirect_to @target
end

#follow_remote_actor ⇒ Object



66
67
68
69
70
71
# File 'app/controllers/follows_controller.rb', line 66

def follow_remote_actor
  authorize Federails::Following, :create?
  @actor = Federails::Actor.find_param(params[:id])
  current_user.follow(@actor)
  redirect_back_or_to helpers.landing_page_path, notice: t(".followed", actor: @actor.at_address)
end

#index ⇒ Object



6
7
8
9
10
# File 'app/controllers/follows_controller.rb', line 6

def index
  authorize Federails::Following
  @followings = current_user&.federails_actor&.follows
  @followers = current_user&.federails_actor&.followers
end

#new ⇒ Object

Incoming remote follow



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/follows_controller.rb', line 13

def new
  @query = params[:uri]&.strip
  # Get actor if this is a HTTP or acct URI
  if @query.present?
    begin
      actor_uri = if @query.starts_with?(%r{https?://})
        @query
      elsif @query.include?("@")
        Federails::Actor.(@query)&.federated_url # rubocop:disable Rails/DynamicFindBy
      else
        nil
      end
      @actor = Federails::Actor.find_or_create_by_federation_url actor_uri if actor_uri # rubocop:disable Rails/DynamicFindBy
      # Ignore local users
      @actor = nil if @actor&.local? && @actor.entity.is_a?(User)
      # If this is a local actor, go to the real thing
      # This will happen if anyone comes here from a remote follow
      redirect_to url_for(@actor.entity) if @actor&.local?
    rescue ActiveRecord::RecordNotFound
    end
    # If not local, we show a follow button and some details of the account
    @actors = @actor.nil? ?
      # If there's no actor still, run a search
      search(@query) :
      [@actor]
  else
    # Get recommended follows from FASPs if there's no query
    get_recommended_accounts
  end
end

#perform_remote_follow ⇒ Object

Outgoing remote follow - perform webfinger, then redirect



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/follows_controller.rb', line 52

def perform_remote_follow
  @remote_account = params[:remote_account]
  parts = Fediverse::Webfinger.(@remote_account)
  target = Fediverse::Webfinger.remote_follow_url(parts[:username], parts[:domain], actor_url: params[:uri])
  # Store remote username in a cookie for future convenience
  cookies[:fediverse_account] = @remote_account
  # And off we go
  redirect_to target, allow_other_host: true
rescue ActiveRecord::RecordNotFound, NoMethodError
  @name = params[:name]
  @uri = params[:uri]
  render :remote_follow
end

#remote_follow ⇒ Object

Outgoing remote follow - ask for target account



45
46
47
48
49
# File 'app/controllers/follows_controller.rb', line 45

def remote_follow
  @remote_account = cookies[:fediverse_account]
  @name = params[:name]
  @uri = params[:uri]
end

#unfollow_remote_actor ⇒ Object



73
74
75
76
77
78
# File 'app/controllers/follows_controller.rb', line 73

def unfollow_remote_actor
  authorize Federails::Following, :destroy?
  @actor = Federails::Actor.find_param(params[:id])
  current_user.unfollow(@actor)
  redirect_back_or_to helpers.landing_page_path, notice: t(".unfollowed", actor: @actor.at_address)
end