1 defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
2 alias Pleroma.{UserInviteToken, User, Activity, Repo, Object}
3 alias Pleroma.Web.ActivityPub.ActivityPub
4 alias Pleroma.Web.TwitterAPI.UserView
5 alias Pleroma.Web.{OStatus, CommonAPI}
6 alias Pleroma.Web.MediaProxy
9 @httpoison Application.get_env(:pleroma, :httpoison)
11 def create_status(%User{} = user, %{"status" => _} = data) do
12 CommonAPI.post(user, data)
15 def delete(%User{} = user, id) do
16 with %Activity{data: %{"type" => type}} <- Repo.get(Activity, id),
17 {:ok, activity} <- CommonAPI.delete(id, user) do
22 def follow(%User{} = follower, params) do
23 with {:ok, %User{} = followed} <- get_user(params),
24 {:ok, follower} <- User.maybe_direct_follow(follower, followed),
25 {:ok, activity} <- ActivityPub.follow(follower, followed),
26 {:ok, follower, followed} <-
27 User.wait_and_refresh(
28 Pleroma.Config.get([:activitypub, :follow_handshake_timeout]),
32 {:ok, follower, followed, activity}
38 def unfollow(%User{} = follower, params) do
39 with {:ok, %User{} = unfollowed} <- get_user(params),
40 {:ok, follower, follow_activity} <- User.unfollow(follower, unfollowed),
41 {:ok, _activity} <- ActivityPub.unfollow(follower, unfollowed) do
42 {:ok, follower, unfollowed}
48 def block(%User{} = blocker, params) do
49 with {:ok, %User{} = blocked} <- get_user(params),
50 {:ok, blocker} <- User.block(blocker, blocked),
51 {:ok, _activity} <- ActivityPub.block(blocker, blocked) do
52 {:ok, blocker, blocked}
58 def unblock(%User{} = blocker, params) do
59 with {:ok, %User{} = blocked} <- get_user(params),
60 {:ok, blocker} <- User.unblock(blocker, blocked),
61 {:ok, _activity} <- ActivityPub.unblock(blocker, blocked) do
62 {:ok, blocker, blocked}
68 def repeat(%User{} = user, ap_id_or_id) do
69 with {:ok, _announce, %{data: %{"id" => id}}} <- CommonAPI.repeat(ap_id_or_id, user),
70 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
75 def unrepeat(%User{} = user, ap_id_or_id) do
76 with {:ok, _unannounce, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
77 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
82 def fav(%User{} = user, ap_id_or_id) do
83 with {:ok, _fav, %{data: %{"id" => id}}} <- CommonAPI.favorite(ap_id_or_id, user),
84 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
89 def unfav(%User{} = user, ap_id_or_id) do
90 with {:ok, _unfav, _fav, %{data: %{"id" => id}}} <- CommonAPI.unfavorite(ap_id_or_id, user),
91 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
96 def upload(%Plug.Upload{} = file, format \\ "xml") do
97 {:ok, object} = ActivityPub.upload(file)
99 url = List.first(object.data["url"])
101 type = url["mediaType"]
105 # Fake this as good as possible...
107 <?xml version="1.0" encoding="UTF-8"?>
108 <rsp stat="ok" xmlns:atom="http://www.w3.org/2005/Atom">
109 <mediaid>#{object.id}</mediaid>
110 <media_id>#{object.id}</media_id>
111 <media_id_string>#{object.id}</media_id_string>
112 <media_url>#{href}</media_url>
113 <mediaurl>#{href}</mediaurl>
114 <atom:link rel="enclosure" href="#{href}" type="#{type}"></atom:link>
121 media_id_string: "#{object.id}}",
129 def register_user(params) do
130 tokenString = params["token"]
133 nickname: params["nickname"],
134 name: params["fullname"],
136 email: params["email"],
137 password: params["password"],
138 password_confirmation: params["confirm"]
141 registrations_open = Pleroma.Config.get([:instance, :registrations_open])
143 # no need to query DB if registration is open
145 unless registrations_open || is_nil(tokenString) do
146 Repo.get_by(UserInviteToken, %{token: tokenString})
150 registrations_open || (!is_nil(token) && !token.used) ->
151 changeset = User.register_changeset(%User{info: %{}}, params)
153 with {:ok, user} <- Repo.insert(changeset) do
154 !registrations_open && UserInviteToken.mark_as_used(token.token)
157 {:error, changeset} ->
159 Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> msg end)
162 {:error, %{error: errors}}
165 !registrations_open && is_nil(token) ->
166 {:error, "Invalid token"}
168 !registrations_open && token.used ->
169 {:error, "Expired token"}
173 def get_by_id_or_nickname(id_or_nickname) do
174 if !is_integer(id_or_nickname) && :error == Integer.parse(id_or_nickname) do
175 Repo.get_by(User, nickname: id_or_nickname)
177 Repo.get(User, id_or_nickname)
181 def get_user(user \\ nil, params) do
183 %{"user_id" => user_id} ->
184 case target = get_by_id_or_nickname(user_id) do
186 {:error, "No user with such user_id"}
192 %{"screen_name" => nickname} ->
193 case target = Repo.get_by(User, nickname: nickname) do
195 {:error, "No user with such screen_name"}
205 {:error, "You need to specify screen_name or user_id"}
210 defp parse_int(string, default)
212 defp parse_int(string, default) when is_binary(string) do
213 with {n, _} <- Integer.parse(string) do
220 defp parse_int(_, default), do: default
222 def search(_user, %{"q" => query} = params) do
223 limit = parse_int(params["rpp"], 20)
224 page = parse_int(params["page"], 1)
225 offset = (page - 1) * limit
230 where: fragment("?->>'type' = 'Create'", a.data),
231 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
234 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
240 # this one isn't indexed so psql won't take the wrong index.
241 order_by: [desc: :inserted_at]
244 _activities = Repo.all(q)
248 DateTime.utc_now() |> DateTime.to_iso8601()
251 # DEPRECATED mostly, context objects are now created at insertion time.
252 def context_to_conversation_id(context) do
253 with %Object{id: id} <- Object.get_cached_by_ap_id(context) do
257 changeset = Object.context_mapping(context)
259 case Repo.insert(changeset) do
263 # This should be solved by an upsert, but it seems ecto
264 # has problems accessing the constraint inside the jsonb.
266 Object.get_cached_by_ap_id(context).id
271 def conversation_id_to_context(id) do
272 with %Object{data: %{"id" => context}} <- Repo.get(Object, id) do
276 {:error, "No such conversation"}
280 def get_external_profile(for_user, uri) do
281 with %User{} = user <- User.get_or_fetch(uri) do
282 {:ok, UserView.render("show.json", %{user: user, for: for_user})}
285 {:error, "Couldn't find user"}