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)
37 |> put_in(["statusnet_conversation_id"], inReplyTo.data["statusnet_conversation_id"])
38 |> put_in(["object", "statusnet_conversation_id"], inReplyTo.data["statusnet_conversation_id"])
43 with {:ok, activity} <- ActivityPub.insert(activity) do
44 add_conversation_id(activity)
48 def fetch_friend_statuses(user, opts \\ %{}) do
49 ActivityPub.fetch_activities(user.following, opts)
50 |> activities_to_statuses(%{for: user})
53 def fetch_public_statuses(user, opts \\ %{}) do
54 ActivityPub.fetch_public_activities(opts)
55 |> activities_to_statuses(%{for: user})
58 def follow(%User{} = follower, followed_id) do
59 with %User{} = followed <- Repo.get(User, followed_id),
60 { :ok, follower } <- User.follow(follower, followed)
62 { :ok, follower, followed }
66 def unfollow(%User{} = follower, followed_id) do
67 with %User{} = followed <- Repo.get(User, followed_id),
68 { :ok, follower } <- User.unfollow(follower, followed)
70 { :ok, follower, followed }
74 defp add_conversation_id(activity) do
75 if is_integer(activity.data["statusnet_conversation_id"]) do
79 |> put_in(["object", "statusnet_conversation_id"], activity.id)
80 |> put_in(["statusnet_conversation_id"], activity.id)
82 changeset = Ecto.Changeset.change(activity, data: data)
83 Repo.update(changeset)
87 defp activities_to_statuses(activities, opts) do
88 Enum.map(activities, fn(activity) ->
89 activity_to_status(activity, opts)
93 defp activity_to_status(activity, opts) do
94 actor = get_in(activity.data, ["actor"])
95 user = Repo.get_by!(User, ap_id: actor)
96 ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user}))
99 def fetch_status(user, id) do
100 with %Activity{} = activity <- Repo.get(Activity, id) do
101 activity_to_status(activity, %{for: user})