twitter_api.ex 2.63 KB
Newer Older
lain's avatar
lain committed
1
defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
lain's avatar
lain committed
2
  alias Pleroma.{User, Activity, Repo}
lain's avatar
lain committed
3
4
5
6
  alias Pleroma.Web.ActivityPub.ActivityPub
  alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter

  def create_status(user = %User{}, data = %{}) do
lain's avatar
lain committed
7
    date = DateTime.utc_now() |> DateTime.to_iso8601
lain's avatar
lain committed
8
9

    context = ActivityPub.generate_context_id
lain's avatar
lain committed
10
    activity = %{
lain's avatar
lain committed
11
12
      "type" => "Create",
      "to" => [
lain's avatar
lain committed
13
14
15
        User.ap_followers(user),
        "https://www.w3.org/ns/activitystreams#Public"
      ],
lain's avatar
lain committed
16
17
18
      "actor" => User.ap_id(user),
      "object" => %{
        "type" => "Note",
lain's avatar
lain committed
19
        "content" => data["status"],
lain's avatar
lain committed
20
21
        "published" => date,
        "context" => context
lain's avatar
lain committed
22
      },
lain's avatar
lain committed
23
24
      "published" => date,
      "context" => context
lain's avatar
lain committed
25
26
    }

lain's avatar
lain committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    # Wire up reply info.
    activity = with inReplyToId when not is_nil(inReplyToId) <- data["in_reply_to_status_id"],
                    inReplyTo <- Repo.get(Activity, inReplyToId),
                    context <- inReplyTo.data["context"]
               do
               activity
               |> put_in(["context"], context)
               |> put_in(["object", "context"], context)
               |> put_in(["object", "inReplyTo"], inReplyTo.data["object"]["id"])
               |> put_in(["object", "inReplyToStatusId"], inReplyToId)
               else _e ->
                 activity
               end

lain's avatar
lain committed
41
42
43
    ActivityPub.insert(activity)
  end

lain's avatar
lain committed
44
45
  def fetch_friend_statuses(user, opts \\ %{}) do
    ActivityPub.fetch_activities(user.following, opts)
46
    |> activities_to_statuses(%{for: user})
lain's avatar
lain committed
47
48
  end

49
  def fetch_public_statuses(user, opts \\ %{}) do
lain's avatar
lain committed
50
    ActivityPub.fetch_public_activities(opts)
51
    |> activities_to_statuses(%{for: user})
lain's avatar
lain committed
52
  end
lain's avatar
lain committed
53

lain's avatar
lain committed
54
55
56
57
58
59
60
61
  def follow(%User{} = follower, followed_id) do
    with %User{} = followed <- Repo.get(User, followed_id),
         { :ok, follower } <- User.follow(follower, followed)
    do
      { :ok, follower, followed }
    end
  end

lain's avatar
lain committed
62
63
64
65
66
67
68
69
  def unfollow(%User{} = follower, followed_id) do
    with %User{} = followed <- Repo.get(User, followed_id),
         { :ok, follower } <- User.unfollow(follower, followed)
    do
      { :ok, follower, followed }
    end
  end

70
  defp activities_to_statuses(activities, opts) do
lain's avatar
lain committed
71
    Enum.map(activities, fn(activity) ->
lain's avatar
lain committed
72
      activity_to_status(activity, opts)
lain's avatar
lain committed
73
74
    end)
  end
lain's avatar
lain committed
75
76
77
78
79
80
81
82
83
84
85
86

  defp activity_to_status(activity, opts) do
    actor = get_in(activity.data, ["actor"])
    user = Repo.get_by!(User, ap_id: actor)
    ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user}))
  end

  def fetch_status(user, id) do
    with %Activity{} = activity <- Repo.get(Activity, id) do
      activity_to_status(activity, %{for: user})
    end
  end
lain's avatar
lain committed
87
end