Merge branch 'develop' into 'develop'

Add Follow activity insertion

See merge request !1
This commit is contained in:
lambadalambda 2017-04-10 08:44:19 -04:00
commit b5e94bf930
5 changed files with 33 additions and 7 deletions

View File

@ -6,7 +6,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
def insert(map) when is_map(map) do def insert(map) when is_map(map) do
map = Map.put_new_lazy(map, "id", &generate_activity_id/0) map = Map.put_new_lazy(map, "id", &generate_activity_id/0)
map = if map["object"] do map = if is_map(map["object"]) do
object = Map.put_new_lazy(map["object"], "id", &generate_object_id/0) object = Map.put_new_lazy(map["object"], "id", &generate_object_id/0)
Repo.insert!(%Object{data: object}) Repo.insert!(%Object{data: object})
Map.put(map, "object", object) Map.put(map, "object", object)

View File

@ -3,6 +3,20 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ObjectRepresenter} alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ObjectRepresenter}
alias Pleroma.Activity alias Pleroma.Activity
def to_map(%Activity{data: %{"type" => "Follow"}} = activity, %{user: user} = opts) do
%{
"id" => activity.id,
"user" => UserRepresenter.to_map(user, opts),
"attentions" => [],
"statusnet_html" => "", # TODO: add summary
"text" => "",
"is_local" => true,
"is_post_verb" => false,
"created_at" => get_in(activity.data, ["published"]),
"in_reply_to_status_id" => nil,
}
end
def to_map(%Activity{} = activity, %{user: user} = opts) do def to_map(%Activity{} = activity, %{user: user} = opts) do
content = get_in(activity.data, ["object", "content"]) content = get_in(activity.data, ["object", "content"])
published = get_in(activity.data, ["object", "published"]) published = get_in(activity.data, ["object", "published"])

View File

@ -6,8 +6,6 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
import Ecto.Query import Ecto.Query
def create_status(user = %User{}, data = %{}) do def create_status(user = %User{}, data = %{}) do
date = DateTime.utc_now() |> DateTime.to_iso8601
attachments = Enum.map(data["media_ids"] || [], fn (media_id) -> attachments = Enum.map(data["media_ids"] || [], fn (media_id) ->
Repo.get(Object, media_id).data Repo.get(Object, media_id).data
end) end)
@ -27,6 +25,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
content_html = add_user_links(content, mentions) content_html = add_user_links(content, mentions)
date = make_date()
activity = %{ activity = %{
"type" => "Create", "type" => "Create",
"to" => to, "to" => to,
@ -103,9 +103,15 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
def follow(%User{} = follower, followed_id) do def follow(%User{} = follower, followed_id) do
with %User{} = followed <- Repo.get(User, followed_id), with %User{} = followed <- Repo.get(User, followed_id),
{ :ok, follower } <- User.follow(follower, followed) { :ok, follower } <- User.follow(follower, followed),
{ :ok, activity } <- ActivityPub.insert(%{
"type" => "Follow",
"actor" => follower.ap_id,
"object" => followed.ap_id,
"published" => make_date()
})
do do
{ :ok, follower, followed } { :ok, follower, followed, activity }
end end
end end
@ -178,4 +184,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
mentioned_users = Repo.all(from user in User, where: user.ap_id in ^activity.data["to"]) mentioned_users = Repo.all(from user in User, where: user.ap_id in ^activity.data["to"])
ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user, mentioned: mentioned_users})) ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user, mentioned: mentioned_users}))
end end
defp make_date do
DateTime.utc_now() |> DateTime.to_iso8601
end
end end

View File

@ -44,7 +44,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
end end
def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
{ :ok, _user, follower } = TwitterAPI.follow(user, followed_id) { :ok, _user, follower, _activity } = TwitterAPI.follow(user, followed_id)
response = follower |> UserRepresenter.to_json(%{for: user}) response = follower |> UserRepresenter.to_json(%{for: user})

View File

@ -107,11 +107,13 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
{ :ok, user } = UserBuilder.insert { :ok, user } = UserBuilder.insert
{ :ok, following } = UserBuilder.insert(%{nickname: "guy"}) { :ok, following } = UserBuilder.insert(%{nickname: "guy"})
{:ok, user, following } = TwitterAPI.follow(user, following.id) {:ok, user, following, activity } = TwitterAPI.follow(user, following.id)
user = Repo.get(User, user.id) user = Repo.get(User, user.id)
follow = Repo.get(Activity, activity.id)
assert user.following == [User.ap_followers(following)] assert user.following == [User.ap_followers(following)]
assert follow == activity
end end
test "Unfollow another user" do test "Unfollow another user" do