1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
6 alias Pleroma.UserInviteToken
11 alias Pleroma.UserEmail
13 alias Pleroma.Web.ActivityPub.ActivityPub
14 alias Pleroma.Web.TwitterAPI.UserView
15 alias Pleroma.Web.CommonAPI
19 def create_status(%User{} = user, %{"status" => _} = data) do
20 CommonAPI.post(user, data)
23 def delete(%User{} = user, id) do
24 with %Activity{data: %{"type" => _type}} <- Activity.get_by_id(id),
25 {:ok, activity} <- CommonAPI.delete(id, user) do
30 def follow(%User{} = follower, params) do
31 with {:ok, %User{} = followed} <- get_user(params),
32 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
33 {:ok, activity} <- ActivityPub.follow(follower, followed),
34 {:ok, follower, followed} <-
35 User.wait_and_refresh(
36 Pleroma.Config.get([:activitypub, :follow_handshake_timeout]),
40 {:ok, follower, followed, activity}
46 def unfollow(%User{} = follower, params) do
47 with {:ok, %User{} = unfollowed} <- get_user(params),
48 {:ok, follower, _follow_activity} <- User.unfollow(follower, unfollowed),
49 {:ok, _activity} <- ActivityPub.unfollow(follower, unfollowed) do
50 {:ok, follower, unfollowed}
56 def block(%User{} = blocker, params) do
57 with {:ok, %User{} = blocked} <- get_user(params),
58 {:ok, blocker} <- User.block(blocker, blocked),
59 {:ok, _activity} <- ActivityPub.block(blocker, blocked) do
60 {:ok, blocker, blocked}
66 def unblock(%User{} = blocker, params) do
67 with {:ok, %User{} = blocked} <- get_user(params),
68 {:ok, blocker} <- User.unblock(blocker, blocked),
69 {:ok, _activity} <- ActivityPub.unblock(blocker, blocked) do
70 {:ok, blocker, blocked}
76 def repeat(%User{} = user, ap_id_or_id) do
77 with {:ok, _announce, %{data: %{"id" => id}}} <- CommonAPI.repeat(ap_id_or_id, user),
78 %Activity{} = activity <- Activity.get_create_by_object_ap_id(id) do
83 def unrepeat(%User{} = user, ap_id_or_id) do
84 with {:ok, _unannounce, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
85 %Activity{} = activity <- Activity.get_create_by_object_ap_id(id) do
90 def pin(%User{} = user, ap_id_or_id) do
91 CommonAPI.pin(ap_id_or_id, user)
94 def unpin(%User{} = user, ap_id_or_id) do
95 CommonAPI.unpin(ap_id_or_id, user)
98 def fav(%User{} = user, ap_id_or_id) do
99 with {:ok, _fav, %{data: %{"id" => id}}} <- CommonAPI.favorite(ap_id_or_id, user),
100 %Activity{} = activity <- Activity.get_create_by_object_ap_id(id) do
105 def unfav(%User{} = user, ap_id_or_id) do
106 with {:ok, _unfav, _fav, %{data: %{"id" => id}}} <- CommonAPI.unfavorite(ap_id_or_id, user),
107 %Activity{} = activity <- Activity.get_create_by_object_ap_id(id) do
112 def upload(%Plug.Upload{} = file, %User{} = user, format \\ "xml") do
113 {:ok, object} = ActivityPub.upload(file, actor: User.ap_id(user))
115 url = List.first(object.data["url"])
117 type = url["mediaType"]
121 # Fake this as good as possible...
123 <?xml version="1.0" encoding="UTF-8"?>
124 <rsp stat="ok" xmlns:atom="http://www.w3.org/2005/Atom">
125 <mediaid>#{object.id}</mediaid>
126 <media_id>#{object.id}</media_id>
127 <media_id_string>#{object.id}</media_id_string>
128 <media_url>#{href}</media_url>
129 <mediaurl>#{href}</mediaurl>
130 <atom:link rel="enclosure" href="#{href}" type="#{type}"></atom:link>
137 media_id_string: "#{object.id}}",
145 def register_user(params) do
146 tokenString = params["token"]
149 nickname: params["nickname"],
150 name: params["fullname"],
151 bio: User.parse_bio(params["bio"]),
152 email: params["email"],
153 password: params["password"],
154 password_confirmation: params["confirm"],
155 captcha_solution: params["captcha_solution"],
156 captcha_token: params["captcha_token"],
157 captcha_answer_data: params["captcha_answer_data"]
160 captcha_enabled = Pleroma.Config.get([Pleroma.Captcha, :enabled])
161 # true if captcha is disabled or enabled and valid, false otherwise
163 if !captcha_enabled do
166 Pleroma.Captcha.validate(
167 params[:captcha_token],
168 params[:captcha_solution],
169 params[:captcha_answer_data]
174 if captcha_ok != :ok do
175 {:error, error} = captcha_ok
176 # I have no idea how this error handling works
177 {:error, %{error: Jason.encode!(%{captcha: [error]})}}
179 registrations_open = Pleroma.Config.get([:instance, :registrations_open])
181 # no need to query DB if registration is open
183 unless registrations_open || is_nil(tokenString) do
184 Repo.get_by(UserInviteToken, %{token: tokenString})
188 registrations_open || (!is_nil(token) && !token.used) ->
189 changeset = User.register_changeset(%User{}, params)
191 with {:ok, user} <- User.register(changeset) do
192 !registrations_open && UserInviteToken.mark_as_used(token.token)
196 {:error, changeset} ->
198 Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> msg end)
201 {:error, %{error: errors}}
204 !registrations_open && is_nil(token) ->
205 {:error, "Invalid token"}
207 !registrations_open && token.used ->
208 {:error, "Expired token"}
213 def password_reset(nickname_or_email) do
214 with true <- is_binary(nickname_or_email),
215 %User{local: true} = user <- User.get_by_nickname_or_email(nickname_or_email),
216 {:ok, token_record} <- Pleroma.PasswordResetToken.create_token(user) do
218 |> UserEmail.password_reset_email(token_record.token)
219 |> Mailer.deliver_async()
222 {:error, "bad user identifier"}
224 %User{local: false} ->
225 {:error, "remote user"}
228 {:error, "unknown user"}
232 def get_user(user \\ nil, params) do
234 %{"user_id" => user_id} ->
235 case User.get_cached_by_nickname_or_id(user_id) do
237 {:error, "No user with such user_id"}
239 %User{info: %{disabled: true}} ->
240 {:error, "User has been disabled"}
246 %{"screen_name" => nickname} ->
247 case User.get_by_nickname(nickname) do
249 {:error, "No user with such screen_name"}
251 %User{info: %{disabled: true}} ->
252 {:error, "User has been disabled"}
262 {:error, "You need to specify screen_name or user_id"}
267 defp parse_int(string, default)
269 defp parse_int(string, default) when is_binary(string) do
270 with {n, _} <- Integer.parse(string) do
277 defp parse_int(_, default), do: default
279 def search(_user, %{"q" => query} = params) do
280 limit = parse_int(params["rpp"], 20)
281 page = parse_int(params["page"], 1)
282 offset = (page - 1) * limit
287 where: fragment("?->>'type' = 'Create'", a.data),
288 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
291 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
297 # this one isn't indexed so psql won't take the wrong index.
298 order_by: [desc: :inserted_at]
301 _activities = Repo.all(q)
304 # DEPRECATED mostly, context objects are now created at insertion time.
305 def context_to_conversation_id(context) do
306 with %Object{id: id} <- Object.get_cached_by_ap_id(context) do
310 changeset = Object.context_mapping(context)
312 case Repo.insert(changeset) do
316 # This should be solved by an upsert, but it seems ecto
317 # has problems accessing the constraint inside the jsonb.
319 Object.get_cached_by_ap_id(context).id
324 def conversation_id_to_context(id) do
325 with %Object{data: %{"id" => context}} <- Repo.get(Object, id) do
329 {:error, "No such conversation"}
333 def get_external_profile(for_user, uri) do
334 with %User{} = user <- User.get_or_fetch(uri) do
335 {:ok, UserView.render("show.json", %{user: user, for: for_user})}
338 {:error, "Couldn't find user"}