diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/user.ex | 4 | ||||
-rw-r--r-- | lib/pleroma/web/activity_pub/activity_pub.ex | 48 | ||||
-rw-r--r-- | lib/pleroma/web/ostatus/activity_representer.ex | 30 | ||||
-rw-r--r-- | lib/pleroma/web/ostatus/ostatus.ex | 2 | ||||
-rw-r--r-- | lib/pleroma/web/twitter_api/representers/activity_representer.ex | 1 | ||||
-rw-r--r-- | lib/pleroma/web/twitter_api/twitter_api.ex | 22 |
6 files changed, 93 insertions, 14 deletions
diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 551c23445..4510be770 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -5,6 +5,7 @@ defmodule Pleroma.User do alias Pleroma.{Repo, User, Object, Web} alias Comeonin.Pbkdf2 alias Pleroma.Web.{OStatus, Websub} + alias Pleroma.Web.ActivityPub.ActivityPub schema "users" do field :bio, :string @@ -107,9 +108,10 @@ defmodule Pleroma.User do following = follower.following |> List.delete(ap_followers) - follower + { :ok, follower } = follower |> follow_changeset(%{following: following}) |> Repo.update + { :ok, follower, ActivityPub.fetch_latest_follow(follower, followed)} else {:error, "Not subscribed!"} end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 82aed7ce4..f3e94b101 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -205,12 +205,60 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do {:ok, activity, object} end + def follow(%User{ap_id: follower_id, local: actor_local}, %User{ap_id: followed_id}, local \\ true) do + data = %{ + "type" => "Follow", + "actor" => follower_id, + "to" => [followed_id], + "object" => followed_id, + "published" => make_date() + } + + with {:ok, activity} <- insert(data, local) do + if actor_local do + Pleroma.Web.Federator.enqueue(:publish, activity) + end + + {:ok, activity} + end + end + + def unfollow(follower, followed, local \\ true) do + with follow_activity when not is_nil(follow_activity) <- fetch_latest_follow(follower, followed) do + data = %{ + "type" => "Undo", + "actor" => follower.ap_id, + "to" => [followed.ap_id], + "object" => follow_activity.data["id"], + "published" => make_date() + } + + with {:ok, activity} <- insert(data, local) do + if follower.local do + Pleroma.Web.Federator.enqueue(:publish, activity) + end + + {:ok, activity} + end + end + end + def fetch_activities_for_context(context) do query = from activity in Activity, where: fragment("? @> ?", activity.data, ^%{ context: context }) Repo.all(query) end + def fetch_latest_follow(%User{ap_id: follower_id}, + %User{ap_id: followed_id}) do + query = from activity in Activity, + where: fragment("? @> ?", activity.data, ^%{type: "Follow", actor: follower_id, + object: followed_id}), + order_by: [desc: :inserted_at], + limit: 1 + Repo.one(query) + end + def upload(file) do data = Upload.store(file) Repo.insert(%Object{data: data}) diff --git a/lib/pleroma/web/ostatus/activity_representer.ex b/lib/pleroma/web/ostatus/activity_representer.ex index 66e9b0ec2..02d15ea94 100644 --- a/lib/pleroma/web/ostatus/activity_representer.ex +++ b/lib/pleroma/web/ostatus/activity_representer.ex @@ -146,6 +146,36 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do ] ++ mentions ++ author end + # Only undos of follow for now. Will need to get redone once there are more + def to_simple_form(%{data: %{"type" => "Undo"}} = activity, user, with_author) do + h = fn(str) -> [to_charlist(str)] end + + updated_at = activity.updated_at + |> NaiveDateTime.to_iso8601 + inserted_at = activity.inserted_at + |> NaiveDateTime.to_iso8601 + + author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: [] + follow_activity = Activity.get_by_ap_id(activity.data["object"]) + + mentions = (activity.data["to"] || []) |> get_mentions + [ + {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']}, + {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']}, + {:id, h.(activity.data["id"])}, + {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']}, + {:content, [type: 'html'], ['#{user.nickname} stopped following #{follow_activity.data["object"]}']}, + {:published, h.(inserted_at)}, + {:updated, h.(updated_at)}, + {:"activity:object", [ + {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']}, + {:id, h.(follow_activity.data["object"])}, + {:uri, h.(follow_activity.data["object"])}, + ]}, + {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}, + ] ++ mentions ++ author + end + def wrap_with_entry(simple_form) do [{ :entry, [ diff --git a/lib/pleroma/web/ostatus/ostatus.ex b/lib/pleroma/web/ostatus/ostatus.ex index a6d416b2c..842ad0f01 100644 --- a/lib/pleroma/web/ostatus/ostatus.ex +++ b/lib/pleroma/web/ostatus/ostatus.ex @@ -184,7 +184,7 @@ defmodule Pleroma.Web.OStatus do uri = string_from_xpath("//author/uri[1]", doc) with {:ok, user} <- find_or_make_user(uri) do avatar = make_avatar_object(doc) - if user.avatar != avatar do + if !user.local && user.avatar != avatar do change = Ecto.Changeset.change(user, %{avatar: avatar}) Repo.update(change) else diff --git a/lib/pleroma/web/twitter_api/representers/activity_representer.ex b/lib/pleroma/web/twitter_api/representers/activity_representer.ex index 3fef8eec8..affd43577 100644 --- a/lib/pleroma/web/twitter_api/representers/activity_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/activity_representer.ex @@ -4,6 +4,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do alias Pleroma.{Activity, User} alias Calendar.Strftime alias Pleroma.Web.TwitterAPI.TwitterAPI + alias Pleroma.Wi defp user_by_ap_id(user_list, ap_id) do Enum.find(user_list, fn (%{ap_id: user_id}) -> ap_id == user_id end) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 3921c0d74..793a55250 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -136,16 +136,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do def follow(%User{} = follower, params) do with {:ok, %User{} = followed} <- get_user(params), {:ok, follower} <- User.follow(follower, followed), - {:ok, activity} <- ActivityPub.insert(%{ - "type" => "Follow", - "actor" => follower.ap_id, - "to" => [followed.ap_id], - "object" => followed.ap_id, - "published" => make_date() - }) + {:ok, activity} <- ActivityPub.follow(follower, followed) do - # TODO move all this to ActivityPub - Pleroma.Web.Federator.enqueue(:publish, activity) {:ok, follower, followed, activity} else err -> err @@ -153,10 +145,16 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do end def unfollow(%User{} = follower, params) do - with {:ok, %User{} = unfollowed} <- get_user(params), - {:ok, follower} <- User.unfollow(follower, unfollowed) + with { :ok, %User{} = unfollowed } <- get_user(params), + { :ok, follower, follow_activity } <- User.unfollow(follower, unfollowed), + { :ok, _activity } <- ActivityPub.insert(%{ + "type" => "Undo", + "actor" => follower.ap_id, + "object" => follow_activity.data["id"], # get latest Follow for these users + "published" => make_date() + }) do - {:ok, follower, unfollowed} + { :ok, follower, unfollowed } else err -> err end |