about summary refs log tree commit diff
path: root/lib/mastodon/canonical_email_blocks_cli.rb
blob: 64b72e60310294ab9f8302a79917f48a904deb04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true

require 'concurrent'
require_relative '../../config/boot'
require_relative '../../config/environment'
require_relative 'cli_helper'

module Mastodon
  class CanonicalEmailBlocksCLI < Thor
    include CLIHelper

    def self.exit_on_failure?
      true
    end

    desc 'find EMAIL', 'Find a given e-mail address in the canonical e-mail blocks'
    long_desc <<-LONG_DESC
      When suspending a local user, a hash of a "canonical" version of their e-mail
      address is stored to prevent them from signing up again.

      This command can be used to find whether a known email address is blocked,
      and if so, which account it was attached to.
    LONG_DESC
    def find(email)
      accts = CanonicalEmailBlock.find_blocks(email).map(&:reference_account).map(&:acct).to_a
      if accts.empty?
        say("#{email} is not blocked", :yellow)
      else
        accts.each do |acct|
          say(acct, :white)
        end
      end
    end

    desc 'remove EMAIL', 'Remove a canonical e-mail block'
    long_desc <<-LONG_DESC
      When suspending a local user, a hash of a "canonical" version of their e-mail
      address is stored to prevent them from signing up again.

      This command allows removing a canonical email block.
    LONG_DESC
    def remove(email)
      blocks = CanonicalEmailBlock.find_blocks(email)
      if blocks.empty?
        say("#{email} is not blocked", :yellow)
      else
        blocks.destroy_all
        say("Removed canonical email block for #{email}", :green)
      end
    end

    private

    def color(processed, failed)
      if !processed.zero? && failed.zero?
        :green
      elsif failed.zero?
        :yellow
      else
        :red
      end
    end
  end
end