about summary refs log tree commit diff
path: root/app/services/bootstrap_timeline_service.rb
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2017-09-10 09:58:38 +0200
committerGitHub <noreply@github.com>2017-09-10 09:58:38 +0200
commit7d7844a47fdfb8862847f025dc4c1b1bc5fdafe5 (patch)
treef15741c04d74eb2999a8c36ea758cc20da17830f /app/services/bootstrap_timeline_service.rb
parentf2cbfb2eb375cde302461a91e5fb0a3af8f6eeb5 (diff)
Default follows for new users (#4871)
When a new user confirms their e-mail, bootstrap their home timeline
by automatically following a set of accounts. By default, all local
admin accounts (that are unlocked). Can be customized by new admin
setting (comma-separated usernames, local and unlocked only)
Diffstat (limited to 'app/services/bootstrap_timeline_service.rb')
-rw-r--r--app/services/bootstrap_timeline_service.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/app/services/bootstrap_timeline_service.rb b/app/services/bootstrap_timeline_service.rb
new file mode 100644
index 000000000..c01e25824
--- /dev/null
+++ b/app/services/bootstrap_timeline_service.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+class BootstrapTimelineService < BaseService
+  def call(source_account)
+    bootstrap_timeline_accounts.each do |target_account|
+      FollowService.new.call(source_account, target_account)
+    end
+  end
+
+  private
+
+  def bootstrap_timeline_accounts
+    return @bootstrap_timeline_accounts if defined?(@bootstrap_timeline_accounts)
+
+    @bootstrap_timeline_accounts = bootstrap_timeline_accounts_usernames.empty? ? admin_accounts : local_unlocked_accounts(bootstrap_timeline_accounts_usernames)
+  end
+
+  def bootstrap_timeline_accounts_usernames
+    @bootstrap_timeline_accounts_usernames ||= (Setting.bootstrap_timeline_accounts || '').split(',').map { |str| str.strip.gsub(/\A@/, '') }.reject(&:blank?)
+  end
+
+  def admin_accounts
+    User.admins
+        .includes(:account)
+        .where(accounts: { locked: false })
+        .map(&:account)
+  end
+
+  def local_unlocked_accounts(usernames)
+    Account.local
+           .where(username: usernames)
+           .where(locked: false)
+  end
+end