Merge branch 'develop' of ssh.gitgud.io:lambadalambda/pleroma into bugfix/repeated-follow-unfollow

This commit is contained in:
dtluna 2017-04-16 17:18:34 +03:00
commit ef5033d7a7
47 changed files with 1721 additions and 92 deletions

View file

@ -9,8 +9,11 @@ defmodule Pleroma.User do
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
timestamps()
end
@ -29,6 +32,27 @@ defmodule Pleroma.User do
|> validate_required([:following])
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)
if changeset.valid? do
hashed = Comeonin.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])
else
changeset
end
end
def follow(%User{} = follower, %User{} = followed) do
ap_followers = User.ap_followers(followed)
if following?(follower, followed) do
@ -61,4 +85,17 @@ defmodule Pleroma.User do
def following?(%User{} = follower, %User{} = followed) do
Enum.member?(follower.following, User.ap_followers(followed))
end
def get_cached_by_ap_id(ap_id) do
ConCache.get_or_store(:users, "ap_id:#{ap_id}", fn() ->
# Return false so the cache will store it.
Repo.get_by(User, ap_id: ap_id) || false
end)
end
def get_cached_by_nickname(nickname) do
ConCache.get_or_store(:users, "nickname:#{nickname}", fn() ->
Repo.get_by(User, nickname: nickname) || false
end)
end
end