Add context and inReplyTo.
[akkoma] / lib / pleroma / web / twitter_api / twitter_api.ex
1 defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
2 alias Pleroma.{User, Activity, Repo}
3 alias Pleroma.Web.ActivityPub.ActivityPub
4 alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
5
6 def create_status(user = %User{}, data = %{}) do
7 date = DateTime.utc_now() |> DateTime.to_iso8601
8
9 context = ActivityPub.generate_context_id
10 activity = %{
11 "type" => "Create",
12 "to" => [
13 User.ap_followers(user),
14 "https://www.w3.org/ns/activitystreams#Public"
15 ],
16 "actor" => User.ap_id(user),
17 "object" => %{
18 "type" => "Note",
19 "content" => data["status"],
20 "published" => date,
21 "context" => context
22 },
23 "published" => date,
24 "context" => context
25 }
26
27 # Wire up reply info.
28 activity = with inReplyToId when not is_nil(inReplyToId) <- data["in_reply_to_status_id"],
29 inReplyTo <- Repo.get(Activity, inReplyToId),
30 context <- inReplyTo.data["context"]
31 do
32 activity
33 |> put_in(["context"], context)
34 |> put_in(["object", "context"], context)
35 |> put_in(["object", "inReplyTo"], inReplyTo.data["object"]["id"])
36 |> put_in(["object", "inReplyToStatusId"], inReplyToId)
37 else _e ->
38 activity
39 end
40
41 ActivityPub.insert(activity)
42 end
43
44 def fetch_friend_statuses(user, opts \\ %{}) do
45 ActivityPub.fetch_activities(user.following, opts)
46 |> activities_to_statuses(%{for: user})
47 end
48
49 def fetch_public_statuses(user, opts \\ %{}) do
50 ActivityPub.fetch_public_activities(opts)
51 |> activities_to_statuses(%{for: user})
52 end
53
54 def follow(%User{} = follower, followed_id) do
55 with %User{} = followed <- Repo.get(User, followed_id),
56 { :ok, follower } <- User.follow(follower, followed)
57 do
58 { :ok, follower, followed }
59 end
60 end
61
62 def unfollow(%User{} = follower, followed_id) do
63 with %User{} = followed <- Repo.get(User, followed_id),
64 { :ok, follower } <- User.unfollow(follower, followed)
65 do
66 { :ok, follower, followed }
67 end
68 end
69
70 defp activities_to_statuses(activities, opts) do
71 Enum.map(activities, fn(activity) ->
72 actor = get_in(activity.data, ["actor"])
73 user = Repo.get_by!(User, ap_id: actor)
74 ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user}))
75 end)
76 end
77 end