summaryrefslogtreecommitdiff
path: root/test/web/twitter_api/representers/activity_representer_test.exs
blob: 3f85e028ba87ff62f8bb8be6b9d7e5ebaf9370f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
  use Pleroma.DataCase
  alias Pleroma.{User, Activity, Object}
  alias Pleroma.Web.TwitterAPI.Representers.{ActivityRepresenter, ObjectRepresenter}
  alias Pleroma.Web.ActivityPub.ActivityPub
  alias Pleroma.Builders.UserBuilder
  alias Pleroma.Web.TwitterAPI.UserView
  import Pleroma.Factory

  test "an announce activity" do
    user = insert(:user)
    note_activity = insert(:note_activity)
    activity_actor = Repo.get_by(User, ap_id: note_activity.data["actor"])
    object = Object.get_by_ap_id(note_activity.data["object"]["id"])

    {:ok, announce_activity, _object} = ActivityPub.announce(user, object)
    note_activity = Activity.get_by_ap_id(note_activity.data["id"])

    status =
      ActivityRepresenter.to_map(announce_activity, %{
        users: [user, activity_actor],
        announced_activity: note_activity,
        for: user
      })

    assert status["id"] == announce_activity.id
    assert status["user"] == UserView.render("show.json", %{user: user, for: user})

    retweeted_status =
      ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user})

    assert retweeted_status["repeated"] == true
    assert retweeted_status["id"] == note_activity.id
    assert status["statusnet_conversation_id"] == retweeted_status["statusnet_conversation_id"]

    assert status["retweeted_status"] == retweeted_status
    assert status["activity_type"] == "repeat"
  end

  test "a like activity" do
    user = insert(:user)
    note_activity = insert(:note_activity)
    object = Object.get_by_ap_id(note_activity.data["object"]["id"])

    {:ok, like_activity, _object} = ActivityPub.like(user, object)

    status =
      ActivityRepresenter.to_map(like_activity, %{user: user, liked_activity: note_activity})

    assert status["id"] == like_activity.id
    assert status["in_reply_to_status_id"] == note_activity.id

    note_activity = Activity.get_by_ap_id(note_activity.data["id"])
    activity_actor = Repo.get_by(User, ap_id: note_activity.data["actor"])
    liked_status = ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user})
    assert liked_status["favorited"] == true
    assert status["activity_type"] == "like"
  end

  test "an activity" do
    {:ok, user} = UserBuilder.insert()
    #   {:ok, mentioned_user } = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
    mentioned_user = insert(:user, %{nickname: "shp"})

    # {:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]})
    follower = insert(:user, %{following: [User.ap_followers(user)]})

    object = %Object{
      data: %{
        "type" => "Image",
        "url" => [
          %{
            "type" => "Link",
            "mediaType" => "image/jpg",
            "href" => "http://example.org/image.jpg"
          }
        ],
        "uuid" => 1
      }
    }

    content_html =
      "<script>alert('YAY')</script>Some :2hu: content mentioning <a href='#{mentioned_user.ap_id}'>@shp</shp>"

    content = HtmlSanitizeEx.strip_tags(content_html)
    date = DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC") |> DateTime.to_iso8601()

    {:ok, convo_object} = Object.context_mapping("2hu") |> Repo.insert()

    to = [
      User.ap_followers(user),
      "https://www.w3.org/ns/activitystreams#Public",
      mentioned_user.ap_id
    ]

    activity = %Activity{
      id: 1,
      data: %{
        "type" => "Create",
        "id" => "id",
        "to" => to,
        "actor" => User.ap_id(user),
        "object" => %{
          "published" => date,
          "type" => "Note",
          "content" => content_html,
          "summary" => "2hu",
          "inReplyToStatusId" => 213_123,
          "attachment" => [
            object
          ],
          "external_url" => "some url",
          "like_count" => 5,
          "announcement_count" => 3,
          "context" => "2hu",
          "tag" => ["content", "mentioning", "nsfw"],
          "emoji" => %{
            "2hu" => "corndog.png"
          }
        },
        "published" => date,
        "context" => "2hu"
      },
      local: false,
      recipients: to
    }

    expected_html =
      "<p>2hu</p>alert('YAY')Some <img height='32px' width='32px' alt='2hu' title='2hu' src='corndog.png' /> content mentioning <a href=\"#{
        mentioned_user.ap_id
      }\">@shp</a>"

    expected_status = %{
      "id" => activity.id,
      "user" => UserView.render("show.json", %{user: user, for: follower}),
      "is_local" => false,
      "statusnet_html" => expected_html,
      "text" => "2hu" <> content,
      "is_post_verb" => true,
      "created_at" => "Tue May 24 13:26:08 +0000 2016",
      "in_reply_to_status_id" => 213_123,
      "statusnet_conversation_id" => convo_object.id,
      "attachments" => [
        ObjectRepresenter.to_map(object)
      ],
      "attentions" => [
        UserView.render("show.json", %{user: mentioned_user, for: follower})
      ],
      "fave_num" => 5,
      "repeat_num" => 3,
      "favorited" => false,
      "repeated" => false,
      "external_url" => "some url",
      "tags" => ["nsfw", "content", "mentioning"],
      "activity_type" => "post",
      "possibly_sensitive" => true,
      "uri" => activity.data["object"]["id"],
      "visibility" => "direct",
      "summary" => "2hu"
    }

    assert ActivityRepresenter.to_map(activity, %{
             user: user,
             for: follower,
             mentioned: [mentioned_user]
           }) == expected_status
  end

  test "an undo for a follow" do
    follower = insert(:user)
    followed = insert(:user)

    {:ok, _follow} = ActivityPub.follow(follower, followed)
    {:ok, unfollow} = ActivityPub.unfollow(follower, followed)

    map = ActivityRepresenter.to_map(unfollow, %{user: follower})
    assert map["is_post_verb"] == false
    assert map["activity_type"] == "undo"
  end

  test "a delete activity" do
    object = insert(:note)
    user = User.get_by_ap_id(object.data["actor"])

    {:ok, delete} = ActivityPub.delete(object)

    map = ActivityRepresenter.to_map(delete, %{user: user})

    assert map["is_post_verb"] == false
    assert map["activity_type"] == "delete"
    assert map["uri"] == object.data["id"]
  end
end