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
6 def create_status(user = %User{}, data = %{}) do
7 date = DateTime.utc_now() |> DateTime.to_iso8601
9 context = ActivityPub.generate_context_id
13 User.ap_followers(user),
14 "https://www.w3.org/ns/activitystreams#Public"
16 "actor" => User.ap_id(user),
19 "content" => data["status"],
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"]
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)
41 ActivityPub.insert(activity)
44 def fetch_friend_statuses(user, opts \\ %{}) do
45 ActivityPub.fetch_activities(user.following, opts)
46 |> activities_to_statuses(%{for: user})
49 def fetch_public_statuses(user, opts \\ %{}) do
50 ActivityPub.fetch_public_activities(opts)
51 |> activities_to_statuses(%{for: user})
54 def follow(%User{} = follower, followed_id) do
55 with %User{} = followed <- Repo.get(User, followed_id),
56 { :ok, follower } <- User.follow(follower, followed)
58 { :ok, follower, followed }
62 def unfollow(%User{} = follower, followed_id) do
63 with %User{} = followed <- Repo.get(User, followed_id),
64 { :ok, follower } <- User.unfollow(follower, followed)
66 { :ok, follower, followed }
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}))