about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorThibaut Girka <thib@sitedethib.com>2019-07-28 16:28:05 +0200
committerThibaut Girka <thib@sitedethib.com>2019-07-28 16:28:05 +0200
commitbca3825c17dd2087f826d9269bb80537bd4ff395 (patch)
tree51043b99ec56d263f9d126091afb923292a9e301 /lib
parent91da921dbb51f55bc926c3997ae558d735292a67 (diff)
parentcfb2ed78231758a79af038a964ab7f7b7b35274e (diff)
Merge branch 'master' into glitch-soc/merge-upstream
Diffstat (limited to 'lib')
-rw-r--r--lib/cli.rb4
-rw-r--r--lib/json_ld/security.rb5
-rw-r--r--lib/mastodon/preview_cards_cli.rb94
-rw-r--r--lib/mastodon/version.rb12
4 files changed, 105 insertions, 10 deletions
diff --git a/lib/cli.rb b/lib/cli.rb
index be276583d..fbdf49fc3 100644
--- a/lib/cli.rb
+++ b/lib/cli.rb
@@ -9,6 +9,7 @@ require_relative 'mastodon/search_cli'
 require_relative 'mastodon/settings_cli'
 require_relative 'mastodon/statuses_cli'
 require_relative 'mastodon/domains_cli'
+require_relative 'mastodon/preview_cards_cli'
 require_relative 'mastodon/cache_cli'
 require_relative 'mastodon/version'
 
@@ -42,6 +43,9 @@ module Mastodon
     desc 'domains SUBCOMMAND ...ARGS', 'Manage account domains'
     subcommand 'domains', Mastodon::DomainsCLI
 
+    desc 'preview_cards SUBCOMMAND ...ARGS', 'Manage preview cards'
+    subcommand 'preview_cards', Mastodon::PreviewCardsCLI
+
     desc 'cache SUBCOMMAND ...ARGS', 'Manage cache'
     subcommand 'cache', Mastodon::CacheCLI
 
diff --git a/lib/json_ld/security.rb b/lib/json_ld/security.rb
index 1230206f0..a6fbce95f 100644
--- a/lib/json_ld/security.rb
+++ b/lib/json_ld/security.rb
@@ -1,9 +1,9 @@
 # -*- encoding: utf-8 -*-
 # frozen_string_literal: true
-# This file generated automatically from https://w3id.org/security/v1
+# This file generated automatically from http://w3id.org/security/v1
 require 'json/ld'
 class JSON::LD::Context
-  add_preloaded("https://w3id.org/security/v1") do
+  add_preloaded("http://w3id.org/security/v1") do
     new(processingMode: "json-ld-1.0", term_definitions: {
       "CryptographicKey" => TermDefinition.new("CryptographicKey", id: "https://w3id.org/security#Key", simple: true),
       "EcdsaKoblitzSignature2016" => TermDefinition.new("EcdsaKoblitzSignature2016", id: "https://w3id.org/security#EcdsaKoblitzSignature2016", simple: true),
@@ -47,4 +47,5 @@ class JSON::LD::Context
       "xsd" => TermDefinition.new("xsd", id: "http://www.w3.org/2001/XMLSchema#", simple: true, prefix: true)
     })
   end
+  alias_preloaded("https://w3id.org/security/v1", "http://w3id.org/security/v1")
 end
diff --git a/lib/mastodon/preview_cards_cli.rb b/lib/mastodon/preview_cards_cli.rb
new file mode 100644
index 000000000..465fe7d0b
--- /dev/null
+++ b/lib/mastodon/preview_cards_cli.rb
@@ -0,0 +1,94 @@
+# frozen_string_literal: true
+
+require 'tty-prompt'
+require_relative '../../config/boot'
+require_relative '../../config/environment'
+require_relative 'cli_helper'
+
+module Mastodon
+  class PreviewCardsCLI < Thor
+    include ActionView::Helpers::NumberHelper
+
+    def self.exit_on_failure?
+      true
+    end
+
+    option :days, type: :numeric, default: 180
+    option :background, type: :boolean, default: false
+    option :verbose, type: :boolean, default: false
+    option :dry_run, type: :boolean, default: false
+    option :link, type: :boolean, default: false
+    desc 'remove', 'Remove preview cards'
+    long_desc <<-DESC
+      Removes locally thumbnails for previews.
+
+      The --days option specifies how old preview cards have to be before
+      they are removed. It defaults to 180 days.
+
+      With the --background option, instead of deleting the files sequentially,
+      they will be queued into Sidekiq and the command will exit as soon as
+      possible. In Sidekiq they will be processed with higher concurrency, but
+      it may impact other operations of the Mastodon server, and it may overload
+      the underlying file storage.
+
+      With the --dry-run option, no work will be done.
+
+      With the --verbose option, when preview cards are processed sequentially in the
+      foreground, the IDs of the preview cards will be printed.
+
+      With the --link option, delete only link-type preview cards.
+    DESC
+    def remove
+      prompt    = TTY::Prompt.new
+      time_ago  = options[:days].days.ago
+      queued    = 0
+      processed = 0
+      size      = 0
+      dry_run   = options[:dry_run] ? '(DRY RUN)' : ''
+      link      = options[:link] ? 'link-type ' : ''
+      scope     = PreviewCard.where.not(image_file_name: nil)
+      scope     = scope.where.not(image_file_name: '')
+      scope     = scope.where(type: :link) if options[:link]
+      scope     = scope.where('updated_at < ?', time_ago)
+
+      if time_ago > 2.weeks.ago
+        prompt.say "\n"
+        prompt.say('The preview cards less than the past two weeks will not be re-acquired even when needed.')
+        prompt.say "\n"
+
+        unless prompt.yes?('Are you sure you want to delete the preview cards?', default: false)
+          prompt.say "\n"
+          prompt.warn 'Nothing execute. Bye!'
+          prompt.say "\n"
+          exit(1)
+        end
+      end
+
+      if options[:background]
+        scope.select(:id, :image_file_size).reorder(nil).find_in_batches do |preview_cards|
+          queued += preview_cards.size
+          size   += preview_cards.reduce(0) { |sum, p| sum + (p.image_file_size || 0) }
+          Maintenance::UncachePreviewWorker.push_bulk(preview_cards.map(&:id)) unless options[:dry_run]
+        end
+
+      else
+        scope.select(:id, :image_file_size).reorder(nil).find_in_batches do |preview_cards|
+          preview_cards.each do |p|
+            size += p.image_file_size || 0
+            Maintenance::UncachePreviewWorker.new.perform(p.id) unless options[:dry_run]
+            options[:verbose] ? say(p.id) : say('.', :green, false)
+            processed += 1
+          end
+        end
+      end
+
+      say
+
+      if options[:background]
+        say("Scheduled the deletion of #{queued} #{link}preview cards (approx. #{number_to_human_size(size)}) #{dry_run}", :green, true)
+      else
+        say("Removed #{processed} #{link}preview cards (approx. #{number_to_human_size(size)}) #{dry_run}", :green, true)
+      end
+    end
+  end
+end
diff --git a/lib/mastodon/version.rb b/lib/mastodon/version.rb
index cd216b92d..4645feb2f 100644
--- a/lib/mastodon/version.rb
+++ b/lib/mastodon/version.rb
@@ -16,22 +16,18 @@ module Mastodon
       2
     end
 
-    def pre
-      nil
-    end
-
     def flags
       ''
     end
 
-    def to_a
-      [major, minor, patch, pre].compact
-    end
-
     def suffix
       '+glitch'
     end
 
+    def to_a
+      [major, minor, patch].compact
+    end
+
     def to_s
       [to_a.join('.'), flags, suffix].join
     end