X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fuser.ex;h=ed85447fe3007d816700cce16367f3b7a9acbc93;hb=ce1eef9c989f0387168b48f6f61a4c1f84b3f5b5;hp=6d9fe623d5781505ce5a8d9746f30a2a71f4da7a;hpb=9a8850eb9ec9eba9350fcb6aadf70fd966623c94;p=akkoma diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 6d9fe623d..ed85447fe 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1,6 +1,7 @@ defmodule Pleroma.User do use Ecto.Schema - alias Pleroma.User + import Ecto.Changeset + alias Pleroma.{Repo, User} schema "users" do field :bio, :string @@ -8,6 +9,8 @@ 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 @@ -15,15 +18,74 @@ defmodule Pleroma.User do end def ap_id(%User{nickname: nickname}) do - host = - Application.get_env(:pleroma, Pleroma.Web.Endpoint) - |> Keyword.fetch!(:url) - |> Keyword.fetch!(:host) - - "https://#{host}/users/#{nickname}" + "#{Pleroma.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 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) + following = [ap_followers | follower.following] + |> Enum.uniq + + follower + |> follow_changeset(%{following: following}) + |> Repo.update + end + + def unfollow(%User{} = follower, %User{} = followed) do + ap_followers = User.ap_followers(followed) + following = follower.following + |> List.delete(ap_followers) + + follower + |> follow_changeset(%{following: following}) + |> Repo.update + end + + 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