Add following TwAPI endpoint.
[akkoma] / lib / pleroma / web / twitter_api / twitter_api.ex
1 defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
2 alias Pleroma.User
3 alias Pleroma.Web.ActivityPub.ActivityPub
4 alias Pleroma.Repo
5 alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
6
7 def create_status(user = %User{}, data = %{}) do
8 date = DateTime.utc_now() |> DateTime.to_iso8601
9 activity = %{
10 "type" => "Create",
11 "to" => [
12 User.ap_followers(user),
13 "https://www.w3.org/ns/activitystreams#Public"
14 ],
15 "actor" => User.ap_id(user),
16 "object" => %{
17 "type" => "Note",
18 "content" => data["status"],
19 "published" => date
20 },
21 "published" => date
22 }
23
24 ActivityPub.insert(activity)
25 end
26
27 def fetch_friend_statuses(user, opts \\ %{}) do
28 ActivityPub.fetch_activities(user.following, opts)
29 |> activities_to_statuses
30 end
31
32 def fetch_public_statuses(opts \\ %{}) do
33 ActivityPub.fetch_public_activities(opts)
34 |> activities_to_statuses
35 end
36
37 def follow(%User{} = follower, followed_id) do
38 with %User{} = followed <- Repo.get(User, followed_id),
39 { :ok, follower } <- User.follow(follower, followed)
40 do
41 { :ok, follower, followed }
42 end
43 end
44
45 defp activities_to_statuses(activities) do
46 Enum.map(activities, fn(activity) ->
47 actor = get_in(activity.data, ["actor"])
48 user = Repo.get_by!(User, ap_id: actor)
49 ActivityRepresenter.to_map(activity, %{user: user})
50 end)
51 end
52 end