Subscribe to remote users on following.

This commit is contained in:
Roger Braun 2017-05-06 14:09:39 +02:00
parent 9cafb67fc1
commit bda389d7d9
3 changed files with 27 additions and 5 deletions

View File

@ -4,7 +4,7 @@ defmodule Pleroma.User do
import Ecto.{Changeset, Query} import Ecto.{Changeset, Query}
alias Pleroma.{Repo, User, Object, Web} alias Pleroma.{Repo, User, Object, Web}
alias Comeonin.Pbkdf2 alias Comeonin.Pbkdf2
alias Pleroma.Web.OStatus alias Pleroma.Web.{OStatus, Websub}
schema "users" do schema "users" do
field :bio, :string field :bio, :string
@ -88,6 +88,10 @@ def follow(%User{} = follower, %User{} = followed) do
{:error, {:error,
"Could not follow user: #{followed.nickname} is already on your list."} "Could not follow user: #{followed.nickname} is already on your list."}
else else
if !followed.local do
Websub.subscribe(follower, followed)
end
following = [ap_followers | follower.following] following = [ap_followers | follower.following]
|> Enum.uniq |> Enum.uniq

View File

@ -126,7 +126,7 @@ def subscribe(subscriber, subscribed, requester \\ &request_subscription/1) do
topic = subscribed.info["topic"] topic = subscribed.info["topic"]
# FIXME: Race condition, use transactions # FIXME: Race condition, use transactions
{:ok, subscription} = with subscription when not is_nil(subscription) <- Repo.get_by(WebsubClientSubscription, topic: topic) do {:ok, subscription} = with subscription when not is_nil(subscription) <- Repo.get_by(WebsubClientSubscription, topic: topic) do
subscribers = [subscriber.ap_id, subscription.subscribers] |> Enum.uniq subscribers = [subscriber.ap_id | subscription.subscribers] |> Enum.uniq
change = Ecto.Changeset.change(subscription, %{subscribers: subscribers}) change = Ecto.Changeset.change(subscription, %{subscribers: subscribers})
Repo.update(change) Repo.update(change)
else _e -> else _e ->

View File

@ -1,9 +1,12 @@
defmodule Pleroma.UserTest do defmodule Pleroma.UserTest do
alias Pleroma.Builders.UserBuilder alias Pleroma.Builders.UserBuilder
alias Pleroma.User alias Pleroma.{User, Repo}
alias Pleroma.Web.OStatus
alias Pleroma.Web.Websub.WebsubClientSubscription
use Pleroma.DataCase use Pleroma.DataCase
import Pleroma.Factory import Pleroma.Factory
import Ecto.Query
test "ap_id returns the activity pub id for the user" do test "ap_id returns the activity pub id for the user" do
host = host =
@ -30,13 +33,29 @@ test "follow takes a user and another user" do
user = insert(:user) user = insert(:user)
followed = insert(:user) followed = insert(:user)
{:ok, user } = User.follow(user, followed) {:ok, user} = User.follow(user, followed)
user = Repo.get(User, user.id) user = Repo.get(User, user.id)
assert user.following == [User.ap_followers(followed)] assert user.following == [User.ap_followers(followed)]
end end
test "following a remote user will ensure a websub subscription is present" do
user = insert(:user)
{:ok, followed} = OStatus.make_user("shp@social.heldscal.la")
assert followed.local == false
{:ok, user} = User.follow(user, followed)
assert user.following == [User.ap_followers(followed)]
query = from w in WebsubClientSubscription,
where: w.topic == ^followed.info["topic"]
websub = Repo.one(query)
assert websub
end
test "unfollow takes a user and another user" do test "unfollow takes a user and another user" do
followed = insert(:user) followed = insert(:user)
user = insert(:user, %{following: [User.ap_followers(followed)]}) user = insert(:user, %{following: [User.ap_followers(followed)]})
@ -95,7 +114,6 @@ test "gets an existing user" do
assert user == fetched_user assert user == fetched_user
end end
# TODO: Make the test local.
test "fetches an external user via ostatus if no user exists" do test "fetches an external user via ostatus if no user exists" do
fetched_user = User.get_or_fetch_by_nickname("shp@social.heldscal.la") fetched_user = User.get_or_fetch_by_nickname("shp@social.heldscal.la")
assert fetched_user.nickname == "shp@social.heldscal.la" assert fetched_user.nickname == "shp@social.heldscal.la"