From 0fae04c4e3961baf1f31ae664e5a7fa5de19158e Mon Sep 17 00:00:00 2001 From: lain Date: Tue, 8 Jan 2019 09:55:33 +0100 Subject: [PATCH] Add a setting for users to autofollow on sign up. --- config/config.exs | 3 ++- docs/config.md | 1 + lib/pleroma/user.ex | 19 ++++++++++++++++++- test/user_test.exs | 17 +++++++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/config/config.exs b/config/config.exs index e084d1a88..4f4e2368a 100644 --- a/config/config.exs +++ b/config/config.exs @@ -137,7 +137,8 @@ config :pleroma, :instance, "text/markdown" ], finmoji_enabled: true, - mrf_transparency: true + mrf_transparency: true, + autofollowed_nicknames: [] config :pleroma, :markup, # XXX - unfortunately, inline images must be enabled by default right now, because diff --git a/docs/config.md b/docs/config.md index e3b2fb35c..1a9706f5b 100644 --- a/docs/config.md +++ b/docs/config.md @@ -93,6 +93,7 @@ config :pleroma, Pleroma.Mailer, * `always_show_subject_input`: When set to false, auto-hide the subject field when it's empty. * `extended_nickname_format`: Set to `true` to use extended local nicknames format (allows underscores/dashes). This will break federation with older software for theses nicknames. +* `autofollowed_nicknames`: Set to nicknames of (local) users that every new user should automatically follow. ## :logger * `backends`: `:console` is used to send logs to stdout, `{ExSyslogger, :ex_syslogger}` to log to syslog diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 85d0f9fce..8edf1c730 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -229,10 +229,27 @@ defmodule Pleroma.User do end end + defp autofollow_users(user) do + candidates = Pleroma.Config.get([:instance, :autofollowed_nicknames]) + + autofollowed_users = + from(u in User, + where: u.local == true, + where: u.nickname in ^candidates + ) + |> Repo.all() + + autofollowed_users + |> Enum.reduce({:ok, user}, fn other_user, {:ok, user} -> + follow(user, other_user) + end) + end + @doc "Inserts provided changeset, performs post-registration actions (confirmation email sending etc.)" def register(%Ecto.Changeset{} = changeset) do with {:ok, user} <- Repo.insert(changeset), - {:ok, _} = try_send_confirmation_email(user) do + {:ok, _} <- try_send_confirmation_email(user), + {:ok, user} <- autofollow_users(user) do {:ok, user} end end diff --git a/test/user_test.exs b/test/user_test.exs index 74accb7c8..f8ef2b1dc 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -142,6 +142,23 @@ defmodule Pleroma.UserTest do email: "email@example.com" } + test "it autofollows accounts that are set for it" do + user = insert(:user) + remote_user = insert(:user, %{local: false}) + + Pleroma.Config.put([:instance, :autofollowed_nicknames], [ + user.nickname, + remote_user.nickname + ]) + + cng = User.register_changeset(%User{}, @full_user_data) + + {:ok, registered_user} = User.register(cng) + + assert User.following?(registered_user, user) + refute User.following?(registered_user, remote_user) + end + test "it requires an email, name, nickname and password, bio is optional" do @full_user_data |> Map.keys()