Page MenuHomePhorge

No OneTemporary

Size
16 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex
index a30c8daed..af0e569a4 100644
--- a/lib/pleroma/user.ex
+++ b/lib/pleroma/user.ex
@@ -1,180 +1,189 @@
defmodule Pleroma.User do
use Ecto.Schema
import Ecto.{Changeset, Query}
alias Pleroma.{Repo, User, Object, Web}
alias Comeonin.Pbkdf2
alias Pleroma.Web.{OStatus, Websub}
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.Utils
schema "users" do
field :bio, :string
field :email, :string
field :name, :string
field :nickname, :string
field :password_hash, :string
field :password, :string, virtual: true
field :password_confirmation, :string, virtual: true
field :following, {:array, :string}, default: []
field :ap_id, :string
field :avatar, :map
field :local, :boolean, default: true
field :info, :map, default: %{}
+ field :follower_address, :string
timestamps()
end
def avatar_url(user) do
case user.avatar do
%{"url" => [%{"href" => href} | _]} -> href
_ -> "https://placehold.it/48x48"
end
end
def ap_id(%User{nickname: nickname}) do
"#{Web.base_url}/users/#{nickname}"
end
def ap_followers(%User{} = user) do
"#{ap_id(user)}/followers"
end
def follow_changeset(struct, params \\ %{}) do
struct
|> cast(params, [:following])
|> validate_required([:following])
end
def info_changeset(struct, params \\ %{}) do
struct
|> cast(params, [:info])
|> validate_required([:info])
end
def user_info(%User{} = user) do
note_count_query = from a in Object,
where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
select: count(a.id)
follower_count_query = from u in User,
- where: fragment("? @> ?", u.following, ^User.ap_followers(user)),
+ where: fragment("? @> ?", u.following, ^user.follower_address),
select: count(u.id)
%{
following_count: length(user.following),
note_count: Repo.one(note_count_query),
follower_count: Repo.one(follower_count_query)
}
end
@email_regex ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
def remote_user_creation(params) do
- %User{}
+ changes = %User{}
|> cast(params, [:bio, :name, :ap_id, :nickname, :info, :avatar])
|> validate_required([:name, :ap_id, :nickname])
|> unique_constraint(:nickname)
|> validate_format(:nickname, @email_regex)
|> validate_length(:bio, max: 5000)
|> validate_length(:name, max: 100)
|> put_change(:local, false)
+ if changes.valid? do
+ followers = User.ap_followers(%User{nickname: changes.changes[:nickname]})
+ changes
+ |> put_change(:follower_address, followers)
+ else
+ changes
+ end
end
def register_changeset(struct, params \\ %{}) do
changeset = struct
|> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
|> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
|> validate_confirmation(:password)
|> unique_constraint(:email)
|> unique_constraint(:nickname)
|> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
|> validate_format(:email, @email_regex)
|> validate_length(:bio, max: 1000)
|> validate_length(:name, max: 100)
if changeset.valid? do
hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
changeset
|> put_change(:password_hash, hashed)
|> put_change(:ap_id, ap_id)
|> put_change(:following, [followers])
+ |> put_change(:follower_address, followers)
else
changeset
end
end
def follow(%User{} = follower, %User{} = followed) do
- ap_followers = User.ap_followers(followed)
+ ap_followers = followed.follower_address
if following?(follower, followed) do
{:error,
"Could not follow user: #{followed.nickname} is already on your list."}
else
if !followed.local && follower.local do
Websub.subscribe(follower, followed)
end
following = [ap_followers | follower.following]
|> Enum.uniq
follower
|> follow_changeset(%{following: following})
|> Repo.update
end
end
def unfollow(%User{} = follower, %User{} = followed) do
- ap_followers = User.ap_followers(followed)
+ ap_followers = followed.follower_address
if following?(follower, followed) do
following = follower.following
|> List.delete(ap_followers)
{ :ok, follower } = follower
|> follow_changeset(%{following: following})
|> Repo.update
{ :ok, follower, Utils.fetch_latest_follow(follower, followed)}
else
{:error, "Not subscribed!"}
end
end
def following?(%User{} = follower, %User{} = followed) do
- Enum.member?(follower.following, User.ap_followers(followed))
+ Enum.member?(follower.following, followed.follower_address)
end
def get_by_ap_id(ap_id) do
Repo.get_by(User, ap_id: ap_id)
end
def get_cached_by_ap_id(ap_id) do
key = "ap_id:#{ap_id}"
Cachex.get!(:user_cache, key, fallback: fn(_) -> get_by_ap_id(ap_id) end)
end
def get_cached_by_nickname(nickname) do
key = "nickname:#{nickname}"
Cachex.get!(:user_cache, key, fallback: fn(_) -> get_or_fetch_by_nickname(nickname) end)
end
def get_by_nickname(nickname) do
Repo.get_by(User, nickname: nickname)
end
def get_cached_user_info(user) do
key = "user_info:#{user.id}"
Cachex.get!(:user_cache, key, fallback: fn(_) -> user_info(user) end)
end
def get_or_fetch_by_nickname(nickname) do
with %User{} = user <- get_by_nickname(nickname) do
user
else _e ->
with [nick, domain] <- String.split(nickname, "@"),
{:ok, user} <- OStatus.make_user(nickname) do
user
else _e -> nil
end
end
end
end
diff --git a/priv/repo/migrations/20170719152213_add_follower_address_to_user.exs b/priv/repo/migrations/20170719152213_add_follower_address_to_user.exs
new file mode 100644
index 000000000..bd3c93bd5
--- /dev/null
+++ b/priv/repo/migrations/20170719152213_add_follower_address_to_user.exs
@@ -0,0 +1,33 @@
+defmodule Pleroma.Repo.Migrations.AddFollowerAddressToUser do
+ use Ecto.Migration
+ import Ecto.Query
+ import Supervisor.Spec
+ alias Pleroma.{Repo, User}
+
+ def up do
+ alter table(:users) do
+ add :follower_address, :string, unique: true
+ end
+ flush()
+
+ children = [
+ # Start the endpoint when the application starts
+ supervisor(Pleroma.Web.Endpoint, [])
+ ]
+ opts = [strategy: :one_for_one, name: Pleroma.Supervisor]
+ Supervisor.start_link(children, opts)
+
+ Enum.each(Repo.all(User), fn (user) ->
+ if !user.follower_address do
+ cs = Ecto.Changeset.change(user, %{follower_address: User.ap_followers(user)})
+ Repo.update!(cs)
+ end
+ end)
+ end
+
+ def down do
+ alter table(:users) do
+ remove :follower_address
+ end
+ end
+end
diff --git a/test/support/factory.ex b/test/support/factory.ex
index bb3bdb02c..1356ebde9 100644
--- a/test/support/factory.ex
+++ b/test/support/factory.ex
@@ -1,107 +1,107 @@
defmodule Pleroma.Factory do
use ExMachina.Ecto, repo: Pleroma.Repo
def user_factory do
user = %Pleroma.User{
name: sequence(:name, &"Test テスト User #{&1}"),
email: sequence(:email, &"user#{&1}@example.com"),
nickname: sequence(:nickname, &"nick#{&1}"),
password_hash: Comeonin.Pbkdf2.hashpwsalt("test"),
bio: sequence(:bio, &"Tester Number #{&1}"),
}
- %{ user | ap_id: Pleroma.User.ap_id(user) }
+ %{ user | ap_id: Pleroma.User.ap_id(user), follower_address: Pleroma.User.ap_followers(user) }
end
def note_factory do
text = sequence(:text, &"This is note #{&1}")
user = insert(:user)
data = %{
"type" => "Note",
"content" => text,
"id" => Pleroma.Web.ActivityPub.Utils.generate_object_id,
"actor" => user.ap_id,
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
"published" => DateTime.utc_now() |> DateTime.to_iso8601,
"likes" => [],
"like_count" => 0,
"context" => "2hu",
"tag" => ["2hu"]
}
%Pleroma.Object{
data: data
}
end
def note_activity_factory do
note = insert(:note)
data = %{
"id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id,
"type" => "Create",
"actor" => note.data["actor"],
"to" => note.data["to"],
"object" => note.data,
"published" => DateTime.utc_now() |> DateTime.to_iso8601,
"context" => note.data["context"]
}
%Pleroma.Activity{
data: data
}
end
def like_activity_factory do
note_activity = insert(:note_activity)
user = insert(:user)
data = %{
"id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id,
"actor" => user.ap_id,
"type" => "Like",
"object" => note_activity.data["object"]["id"],
"published_at" => DateTime.utc_now() |> DateTime.to_iso8601
}
%Pleroma.Activity{
data: data
}
end
def follow_activity_factory do
follower = insert(:user)
followed = insert(:user)
data = %{
"id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id,
"actor" => follower.ap_id,
"type" => "Follow",
"object" => followed.ap_id,
"published_at" => DateTime.utc_now() |> DateTime.to_iso8601
}
%Pleroma.Activity{
data: data
}
end
def websub_subscription_factory do
%Pleroma.Web.Websub.WebsubServerSubscription{
topic: "http://example.org",
callback: "http://example/org/callback",
secret: "here's a secret",
valid_until: NaiveDateTime.add(NaiveDateTime.utc_now, 100),
state: "requested"
}
end
def websub_client_subscription_factory do
%Pleroma.Web.Websub.WebsubClientSubscription{
topic: "http://example.org",
secret: "here's a secret",
valid_until: nil,
state: "requested",
subscribers: []
}
end
end
diff --git a/test/user_test.exs b/test/user_test.exs
index c79564385..0f42e9b51 100644
--- a/test/user_test.exs
+++ b/test/user_test.exs
@@ -1,194 +1,201 @@
defmodule Pleroma.UserTest do
alias Pleroma.Builders.UserBuilder
alias Pleroma.{User, Repo}
alias Pleroma.Web.OStatus
alias Pleroma.Web.Websub.WebsubClientSubscription
use Pleroma.DataCase
import Pleroma.Factory
import Ecto.Query
test "ap_id returns the activity pub id for the user" do
host =
Application.get_env(:pleroma, Pleroma.Web.Endpoint)
|> Keyword.fetch!(:url)
|> Keyword.fetch!(:host)
user = UserBuilder.build
expected_ap_id = "#{Pleroma.Web.base_url}/users/#{user.nickname}"
assert expected_ap_id == User.ap_id(user)
end
test "ap_followers returns the followers collection for the user" do
user = UserBuilder.build
expected_followers_collection = "#{User.ap_id(user)}/followers"
assert expected_followers_collection == User.ap_followers(user)
end
test "follow takes a user and another user" do
user = insert(:user)
followed = insert(:user)
{:ok, user} = User.follow(user, followed)
user = Repo.get(User, user.id)
assert user.following == [User.ap_followers(followed)]
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
followed = insert(:user)
user = insert(:user, %{following: [User.ap_followers(followed)]})
{:ok, user, _activity } = User.unfollow(user, followed)
user = Repo.get(User, user.id)
assert user.following == []
end
test "test if a user is following another user" do
followed = insert(:user)
user = insert(:user, %{following: [User.ap_followers(followed)]})
assert User.following?(user, followed)
refute User.following?(followed, user)
end
describe "user registration" do
@full_user_data %{
bio: "A guy",
name: "my name",
nickname: "nick",
password: "test",
password_confirmation: "test",
email: "email@example.com"
}
test "it requires a bio, email, name, nickname and password" do
@full_user_data
|> Map.keys
|> Enum.each(fn (key) ->
params = Map.delete(@full_user_data, key)
changeset = User.register_changeset(%User{}, params)
assert changeset.valid? == false
end)
end
test "it sets the password_hash, ap_id and following fields" do
changeset = User.register_changeset(%User{}, @full_user_data)
assert changeset.valid?
assert is_binary(changeset.changes[:password_hash])
assert changeset.changes[:ap_id] == User.ap_id(%User{nickname: @full_user_data.nickname})
assert changeset.changes[:following] == [User.ap_followers(%User{nickname: @full_user_data.nickname})]
+ assert changeset.changes.follower_address == "#{changeset.changes.ap_id}/followers"
end
end
describe "fetching a user from nickname or trying to build one" do
test "gets an existing user" do
user = insert(:user)
fetched_user = User.get_or_fetch_by_nickname(user.nickname)
assert user == fetched_user
end
test "gets an existing user, case insensitive" do
user = insert(:user, nickname: "nick")
fetched_user = User.get_or_fetch_by_nickname("NICK")
assert user == fetched_user
end
test "fetches an external user via ostatus if no user exists" do
fetched_user = User.get_or_fetch_by_nickname("shp@social.heldscal.la")
assert fetched_user.nickname == "shp@social.heldscal.la"
end
test "returns nil if no user could be fetched" do
fetched_user = User.get_or_fetch_by_nickname("nonexistant@social.heldscal.la")
assert fetched_user == nil
end
test "returns nil for nonexistant local user" do
fetched_user = User.get_or_fetch_by_nickname("nonexistant")
assert fetched_user == nil
end
end
test "returns an ap_id for a user" do
user = insert(:user)
assert User.ap_id(user) == Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :feed_redirect, user.nickname)
end
test "returns an ap_followers link for a user" do
user = insert(:user)
assert User.ap_followers(user) == Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :feed_redirect, user.nickname) <> "/followers"
end
describe "remote user creation changeset" do
@valid_remote %{
bio: "hello",
name: "Someone",
nickname: "a@b.de",
ap_id: "http...",
info: %{some: "info"},
avatar: %{some: "avatar"}
}
test "it confirms validity" do
cs = User.remote_user_creation(@valid_remote)
assert cs.valid?
end
+ test "it sets the follower_adress" do
+ cs = User.remote_user_creation(@valid_remote)
+ # remote users get a fake local follower address
+ assert cs.changes.follower_address == User.ap_followers(%User{ nickname: @valid_remote[:nickname] })
+ end
+
test "it enforces the fqn format for nicknames" do
cs = User.remote_user_creation(%{@valid_remote | nickname: "bla"})
assert cs.changes.local == false
assert cs.changes.avatar
refute cs.valid?
end
test "it has required fields" do
[:name, :nickname, :ap_id]
|> Enum.each(fn (field) ->
cs = User.remote_user_creation(Map.delete(@valid_remote, field))
refute cs.valid?
end)
end
test "it restricts some sizes" do
[bio: 5000, name: 100]
|> Enum.each(fn ({field, size}) ->
string = String.pad_leading(".", size)
cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
assert cs.valid?
string = String.pad_leading(".", size + 1)
cs = User.remote_user_creation(Map.put(@valid_remote, field, string))
refute cs.valid?
end)
end
end
end

File Metadata

Mime Type
text/x-diff
Expires
Mon, Nov 25, 6:52 AM (1 d, 9 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
39704
Default Alt Text
(16 KB)

Event Timeline