ostatus.ex 3.68 KB
Newer Older
lain's avatar
lain committed
1
defmodule Pleroma.Web.OStatus do
2
3
4
5
6
  import Ecto.Query
  require Logger

  alias Pleroma.{Repo, User, Web}
  alias Pleroma.Web.ActivityPub.ActivityPub
lain's avatar
lain committed
7
8
9
10
11

  def feed_path(user) do
    "#{user.ap_id}/feed.atom"
  end

lain's avatar
lain committed
12
13
  def pubsub_path(user) do
    "#{Web.base_url}/push/hub/#{user.nickname}"
lain's avatar
lain committed
14
15
  end

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  def salmon_path(user) do
    "#{user.ap_id}/salmon"
  end

  def handle_incoming(xml_string) do
    {doc, _rest} = :xmerl_scan.string(to_charlist(xml_string))

    {:xmlObj, :string, object_type } = :xmerl_xpath.string('string(/entry/activity:object-type[1])', doc)

    case object_type do
      'http://activitystrea.ms/schema/1.0/note' ->
        handle_note(doc)
      _ ->
        Logger.error("Couldn't parse incoming document")
    end
  end

  # TODO
  # wire up replies
  def handle_note(doc) do
    content_html = string_from_xpath("/entry/content[1]", doc)

    [author] = :xmerl_xpath.string('/entry/author[1]', doc)
    {:ok, actor} = find_or_make_user(author)

lain's avatar
lain committed
41
42
43
44
45
46
    context = string_from_xpath("/entry/ostatus:conversation[1]", doc) |> String.trim
    context = if String.length(context) > 0 do
      context
    else
      ActivityPub.generate_context_id
    end
47
48
49
50
51

    to = [
      "https://www.w3.org/ns/activitystreams#Public"
    ]

lain's avatar
lain committed
52
53
54
55
56
    mentions = :xmerl_xpath.string('/entry/link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]', doc)
    |> Enum.map(fn(person) -> string_from_xpath("@href", person) end)

    to = to ++ mentions

57
58
59
60
61
62
63
64
65
66
67
    date = string_from_xpath("/entry/published", doc)

    object = %{
      "type" => "Note",
      "to" => to,
      "content" => content_html,
      "published" => date,
      "context" => context,
      "actor" => actor.ap_id
    }

lain's avatar
lain committed
68
69
70
71
72
73
74
75
    inReplyTo = string_from_xpath("/entry/thr:in-reply-to[1]/@href", doc)

    object = if inReplyTo do
      Map.put(object, "inReplyTo", inReplyTo)
    else
      object
    end

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
    ActivityPub.create(to, actor, context, object, %{}, date)
  end

  def find_or_make_user(author_doc) do
    {:xmlObj, :string, uri } = :xmerl_xpath.string('string(/author[1]/uri)', author_doc)

    query = from user in User,
      where: user.local == false and fragment("? @> ?", user.info, ^%{ostatus_uri: to_string(uri)})

    user = Repo.one(query)

    if is_nil(user) do
      make_user(author_doc)
    else
      {:ok, user}
    end
  end

  defp string_from_xpath(xpath, doc) do
    {:xmlObj, :string, res} = :xmerl_xpath.string('string(#{xpath})', doc)

    res = res
    |> to_string
    |> String.trim

    if res == "", do: nil, else: res
  end

  def make_user(author_doc) do
    author = string_from_xpath("/author[1]/uri", author_doc)
    name = string_from_xpath("/author[1]/name", author_doc)
    preferredUsername = string_from_xpath("/author[1]/poco:preferredUsername", author_doc)
    displayName = string_from_xpath("/author[1]/poco:displayName", author_doc)
    avatar = make_avatar_object(author_doc)

    data = %{
      local: false,
      name: preferredUsername || name,
      nickname: displayName || name,
      ap_id: author,
      info: %{
        "ostatus_uri" => author,
        "host" => URI.parse(author).host,
        "system" => "ostatus"
      },
      avatar: avatar
    }

    Repo.insert(Ecto.Changeset.change(%User{}, data))
  end

  # TODO: Just takes the first one for now.
  defp make_avatar_object(author_doc) do
    href = string_from_xpath("/author[1]/link[@rel=\"avatar\"]/@href", author_doc)
    type = string_from_xpath("/author[1]/link[@rel=\"avatar\"]/@type", author_doc)

    if href do
      %{
        "type" => "Image",
        "url" =>
          [%{
              "type" => "Link",
              "mediaType" => type,
              "href" => href
           }]
      }
    else
      nil
    end
lain's avatar
lain committed
145
146
  end
end