add `user delete_activities` mix task
[akkoma] / lib / mix / tasks / pleroma / user.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.User do
6 use Mix.Task
7 import Ecto.Changeset
8 alias Mix.Tasks.Pleroma.Common
9 alias Pleroma.Repo
10 alias Pleroma.User
11
12 @shortdoc "Manages Pleroma users"
13 @moduledoc """
14 Manages Pleroma users.
15
16 ## Create a new user.
17
18 mix pleroma.user new NICKNAME EMAIL [OPTION...]
19
20 Options:
21 - `--name NAME` - the user's name (i.e., "Lain Iwakura")
22 - `--bio BIO` - the user's bio
23 - `--password PASSWORD` - the user's password
24 - `--moderator`/`--no-moderator` - whether the user is a moderator
25 - `--admin`/`--no-admin` - whether the user is an admin
26 - `-y`, `--assume-yes`/`--no-assume-yes` - whether to assume yes to all questions
27
28 ## Generate an invite link.
29
30 mix pleroma.user invite
31
32 ## Delete the user's account.
33
34 mix pleroma.user rm NICKNAME
35
36 ## Delete the user's activities.
37
38 mix pleroma.user delete_activities NICKNAME
39
40 ## Deactivate or activate the user's account.
41
42 mix pleroma.user toggle_activated NICKNAME
43
44 ## Unsubscribe local users from user's account and deactivate it
45
46 mix pleroma.user unsubscribe NICKNAME
47
48 ## Create a password reset link.
49
50 mix pleroma.user reset_password NICKNAME
51
52 ## Set the value of the given user's settings.
53
54 mix pleroma.user set NICKNAME [OPTION...]
55
56 Options:
57 - `--locked`/`--no-locked` - whether the user's account is locked
58 - `--moderator`/`--no-moderator` - whether the user is a moderator
59 - `--admin`/`--no-admin` - whether the user is an admin
60
61 ## Add tags to a user.
62
63 mix pleroma.user tag NICKNAME TAGS
64
65 ## Delete tags from a user.
66
67 mix pleroma.user untag NICKNAME TAGS
68 """
69 def run(["new", nickname, email | rest]) do
70 {options, [], []} =
71 OptionParser.parse(
72 rest,
73 strict: [
74 name: :string,
75 bio: :string,
76 password: :string,
77 moderator: :boolean,
78 admin: :boolean,
79 assume_yes: :boolean
80 ],
81 aliases: [
82 y: :assume_yes
83 ]
84 )
85
86 name = Keyword.get(options, :name, nickname)
87 bio = Keyword.get(options, :bio, "")
88
89 {password, generated_password?} =
90 case Keyword.get(options, :password) do
91 nil ->
92 {:crypto.strong_rand_bytes(16) |> Base.encode64(), true}
93
94 password ->
95 {password, false}
96 end
97
98 moderator? = Keyword.get(options, :moderator, false)
99 admin? = Keyword.get(options, :admin, false)
100 assume_yes? = Keyword.get(options, :assume_yes, false)
101
102 Mix.shell().info("""
103 A user will be created with the following information:
104 - nickname: #{nickname}
105 - email: #{email}
106 - password: #{
107 if(generated_password?, do: "[generated; a reset link will be created]", else: password)
108 }
109 - name: #{name}
110 - bio: #{bio}
111 - moderator: #{if(moderator?, do: "true", else: "false")}
112 - admin: #{if(admin?, do: "true", else: "false")}
113 """)
114
115 proceed? = assume_yes? or Mix.shell().yes?("Continue?")
116
117 unless not proceed? do
118 Common.start_pleroma()
119
120 params = %{
121 nickname: nickname,
122 email: email,
123 password: password,
124 password_confirmation: password,
125 name: name,
126 bio: bio
127 }
128
129 changeset = User.register_changeset(%User{}, params, confirmed: true)
130 {:ok, _user} = User.register(changeset)
131
132 Mix.shell().info("User #{nickname} created")
133
134 if moderator? do
135 run(["set", nickname, "--moderator"])
136 end
137
138 if admin? do
139 run(["set", nickname, "--admin"])
140 end
141
142 if generated_password? do
143 run(["reset_password", nickname])
144 end
145 else
146 Mix.shell().info("User will not be created.")
147 end
148 end
149
150 def run(["rm", nickname]) do
151 Common.start_pleroma()
152
153 with %User{local: true} = user <- User.get_by_nickname(nickname) do
154 User.delete(user)
155 Mix.shell().info("User #{nickname} deleted.")
156 else
157 _ ->
158 Mix.shell().error("No local user #{nickname}")
159 end
160 end
161
162 def run(["toggle_activated", nickname]) do
163 Common.start_pleroma()
164
165 with %User{} = user <- User.get_by_nickname(nickname) do
166 {:ok, user} = User.deactivate(user, !user.info.deactivated)
167
168 Mix.shell().info(
169 "Activation status of #{nickname}: #{if(user.info.deactivated, do: "de", else: "")}activated"
170 )
171 else
172 _ ->
173 Mix.shell().error("No user #{nickname}")
174 end
175 end
176
177 def run(["reset_password", nickname]) do
178 Common.start_pleroma()
179
180 with %User{local: true} = user <- User.get_by_nickname(nickname),
181 {:ok, token} <- Pleroma.PasswordResetToken.create_token(user) do
182 Mix.shell().info("Generated password reset token for #{user.nickname}")
183
184 IO.puts(
185 "URL: #{
186 Pleroma.Web.Router.Helpers.util_url(
187 Pleroma.Web.Endpoint,
188 :show_password_reset,
189 token.token
190 )
191 }"
192 )
193 else
194 _ ->
195 Mix.shell().error("No local user #{nickname}")
196 end
197 end
198
199 def run(["unsubscribe", nickname]) do
200 Common.start_pleroma()
201
202 with %User{} = user <- User.get_by_nickname(nickname) do
203 Mix.shell().info("Deactivating #{user.nickname}")
204 User.deactivate(user)
205
206 {:ok, friends} = User.get_friends(user)
207
208 Enum.each(friends, fn friend ->
209 user = Repo.get(User, user.id)
210
211 Mix.shell().info("Unsubscribing #{friend.nickname} from #{user.nickname}")
212 User.unfollow(user, friend)
213 end)
214
215 :timer.sleep(500)
216
217 user = Repo.get(User, user.id)
218
219 if Enum.empty?(user.following) do
220 Mix.shell().info("Successfully unsubscribed all followers from #{user.nickname}")
221 end
222 else
223 _ ->
224 Mix.shell().error("No user #{nickname}")
225 end
226 end
227
228 def run(["set", nickname | rest]) do
229 Common.start_pleroma()
230
231 {options, [], []} =
232 OptionParser.parse(
233 rest,
234 strict: [
235 moderator: :boolean,
236 admin: :boolean,
237 locked: :boolean
238 ]
239 )
240
241 with %User{local: true} = user <- User.get_by_nickname(nickname) do
242 user =
243 case Keyword.get(options, :moderator) do
244 nil -> user
245 value -> set_moderator(user, value)
246 end
247
248 user =
249 case Keyword.get(options, :locked) do
250 nil -> user
251 value -> set_locked(user, value)
252 end
253
254 _user =
255 case Keyword.get(options, :admin) do
256 nil -> user
257 value -> set_admin(user, value)
258 end
259 else
260 _ ->
261 Mix.shell().error("No local user #{nickname}")
262 end
263 end
264
265 def run(["tag", nickname | tags]) do
266 Common.start_pleroma()
267
268 with %User{} = user <- User.get_by_nickname(nickname) do
269 user = user |> User.tag(tags)
270
271 Mix.shell().info("Tags of #{user.nickname}: #{inspect(tags)}")
272 else
273 _ ->
274 Mix.shell().error("Could not change user tags for #{nickname}")
275 end
276 end
277
278 def run(["untag", nickname | tags]) do
279 Common.start_pleroma()
280
281 with %User{} = user <- User.get_by_nickname(nickname) do
282 user = user |> User.untag(tags)
283
284 Mix.shell().info("Tags of #{user.nickname}: #{inspect(tags)}")
285 else
286 _ ->
287 Mix.shell().error("Could not change user tags for #{nickname}")
288 end
289 end
290
291 def run(["invite"]) do
292 Common.start_pleroma()
293
294 with {:ok, token} <- Pleroma.UserInviteToken.create_token() do
295 Mix.shell().info("Generated user invite token")
296
297 url =
298 Pleroma.Web.Router.Helpers.redirect_url(
299 Pleroma.Web.Endpoint,
300 :registration_page,
301 token.token
302 )
303
304 IO.puts(url)
305 else
306 _ ->
307 Mix.shell().error("Could not create invite token.")
308 end
309 end
310
311 def run(["delete_activities", nickname]) do
312 Common.start_pleroma()
313
314 with %User{local: true} = user <- User.get_by_nickname(nickname) do
315 User.delete_user_activities(user)
316 Mix.shell().info("User #{nickname} statuses deleted.")
317 else
318 _ ->
319 Mix.shell().error("No local user #{nickname}")
320 end
321 end
322
323 defp set_moderator(user, value) do
324 info_cng = User.Info.admin_api_update(user.info, %{is_moderator: value})
325
326 user_cng =
327 Ecto.Changeset.change(user)
328 |> put_embed(:info, info_cng)
329
330 {:ok, user} = User.update_and_set_cache(user_cng)
331
332 Mix.shell().info("Moderator status of #{user.nickname}: #{user.info.is_moderator}")
333 user
334 end
335
336 defp set_admin(user, value) do
337 info_cng = User.Info.admin_api_update(user.info, %{is_admin: value})
338
339 user_cng =
340 Ecto.Changeset.change(user)
341 |> put_embed(:info, info_cng)
342
343 {:ok, user} = User.update_and_set_cache(user_cng)
344
345 Mix.shell().info("Admin status of #{user.nickname}: #{user.info.is_admin}")
346 user
347 end
348
349 defp set_locked(user, value) do
350 info_cng = User.Info.user_upgrade(user.info, %{locked: value})
351
352 user_cng =
353 Ecto.Changeset.change(user)
354 |> put_embed(:info, info_cng)
355
356 {:ok, user} = User.update_and_set_cache(user_cng)
357
358 Mix.shell().info("Locked status of #{user.nickname}: #{user.info.locked}")
359 user
360 end
361 end