Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into validate-user-info
[akkoma] / lib / pleroma / web / twitter_api / twitter_api.ex
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
7 import Ecto.Query
8
9 @httpoison Application.get_env(:pleroma, :httpoison)
10
11 def create_status(%User{} = user, %{"status" => _} = data) do
12 CommonAPI.post(user, data)
13 end
14
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
18 {:ok, activity}
19 end
20 end
21
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]),
29 follower,
30 followed
31 ) do
32 {:ok, follower, followed, activity}
33 else
34 err -> err
35 end
36 end
37
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}
43 else
44 err -> err
45 end
46 end
47
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}
53 else
54 err -> err
55 end
56 end
57
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}
63 else
64 err -> err
65 end
66 end
67
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
71 {:ok, activity}
72 end
73 end
74
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
78 {:ok, activity}
79 end
80 end
81
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
85 {:ok, activity}
86 end
87 end
88
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
92 {:ok, activity}
93 end
94 end
95
96 def upload(%Plug.Upload{} = file, format \\ "xml") do
97 {:ok, object} = ActivityPub.upload(file)
98
99 url = List.first(object.data["url"])
100 href = url["href"]
101 type = url["mediaType"]
102
103 case format do
104 "xml" ->
105 # Fake this as good as possible...
106 """
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>
115 </rsp>
116 """
117
118 "json" ->
119 %{
120 media_id: object.id,
121 media_id_string: "#{object.id}}",
122 media_url: href,
123 size: 0
124 }
125 |> Jason.encode!()
126 end
127 end
128
129 def register_user(params) do
130 tokenString = params["token"]
131
132 params = %{
133 nickname: params["nickname"],
134 name: params["fullname"],
135 bio: params["bio"],
136 email: params["email"],
137 password: params["password"],
138 password_confirmation: params["confirm"]
139 }
140
141 registrations_open = Pleroma.Config.get([:instance, :registrations_open])
142
143 # no need to query DB if registration is open
144 token =
145 unless registrations_open || is_nil(tokenString) do
146 Repo.get_by(UserInviteToken, %{token: tokenString})
147 end
148
149 cond do
150 registrations_open || (!is_nil(token) && !token.used) ->
151 changeset = User.register_changeset(%User{info: %{}}, params)
152
153 with {:ok, user} <- Repo.insert(changeset) do
154 !registrations_open && UserInviteToken.mark_as_used(token.token)
155 {:ok, user}
156 else
157 {:error, changeset} ->
158 errors =
159 Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> msg end)
160 |> Jason.encode!()
161
162 {:error, %{error: errors}}
163 end
164
165 !registrations_open && is_nil(token) ->
166 {:error, "Invalid token"}
167
168 !registrations_open && token.used ->
169 {:error, "Expired token"}
170 end
171 end
172
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)
176 else
177 Repo.get(User, id_or_nickname)
178 end
179 end
180
181 def get_user(user \\ nil, params) do
182 case params do
183 %{"user_id" => user_id} ->
184 case target = get_by_id_or_nickname(user_id) do
185 nil ->
186 {:error, "No user with such user_id"}
187
188 _ ->
189 {:ok, target}
190 end
191
192 %{"screen_name" => nickname} ->
193 case target = Repo.get_by(User, nickname: nickname) do
194 nil ->
195 {:error, "No user with such screen_name"}
196
197 _ ->
198 {:ok, target}
199 end
200
201 _ ->
202 if user do
203 {:ok, user}
204 else
205 {:error, "You need to specify screen_name or user_id"}
206 end
207 end
208 end
209
210 defp parse_int(string, default)
211
212 defp parse_int(string, default) when is_binary(string) do
213 with {n, _} <- Integer.parse(string) do
214 n
215 else
216 _e -> default
217 end
218 end
219
220 defp parse_int(_, default), do: default
221
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
226
227 q =
228 from(
229 a in Activity,
230 where: fragment("?->>'type' = 'Create'", a.data),
231 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
232 where:
233 fragment(
234 "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
235 a.data,
236 ^query
237 ),
238 limit: ^limit,
239 offset: ^offset,
240 # this one isn't indexed so psql won't take the wrong index.
241 order_by: [desc: :inserted_at]
242 )
243
244 _activities = Repo.all(q)
245 end
246
247 defp make_date do
248 DateTime.utc_now() |> DateTime.to_iso8601()
249 end
250
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
254 id
255 else
256 _e ->
257 changeset = Object.context_mapping(context)
258
259 case Repo.insert(changeset) do
260 {:ok, %{id: id}} ->
261 id
262
263 # This should be solved by an upsert, but it seems ecto
264 # has problems accessing the constraint inside the jsonb.
265 {:error, _} ->
266 Object.get_cached_by_ap_id(context).id
267 end
268 end
269 end
270
271 def conversation_id_to_context(id) do
272 with %Object{data: %{"id" => context}} <- Repo.get(Object, id) do
273 context
274 else
275 _e ->
276 {:error, "No such conversation"}
277 end
278 end
279
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})}
283 else
284 _e ->
285 {:error, "Couldn't find user"}
286 end
287 end
288 end