X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fuser.ex;h=869a3eb1b5b77352b41b3920da7072ab488a2eeb;hb=39bacba2803260a21dc1c50ba90695baa25193b3;hp=551c23445067b6e73f2dd2ef3e406ca14df5a7aa;hpb=bda389d7d942b7e90ffb519bc680a424a8400e2f;p=akkoma diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 551c23445..869a3eb1b 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -5,6 +5,8 @@ defmodule Pleroma.User do 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 @@ -60,6 +62,18 @@ defmodule Pleroma.User do } 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{} + |> 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: 1000) + |> validate_length(:name, max: 100) + |> put_change(:local, false) + end + def register_changeset(struct, params \\ %{}) do changeset = struct |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation]) @@ -68,6 +82,9 @@ defmodule Pleroma.User do |> 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]) @@ -88,7 +105,7 @@ defmodule Pleroma.User do {:error, "Could not follow user: #{followed.nickname} is already on your list."} else - if !followed.local do + if !followed.local && follower.local do Websub.subscribe(follower, followed) end @@ -107,9 +124,10 @@ defmodule Pleroma.User do following = follower.following |> List.delete(ap_followers) - follower + { :ok, follower } = follower |> follow_changeset(%{following: following}) |> Repo.update + { :ok, follower, Utils.fetch_latest_follow(follower, followed)} else {:error, "Not subscribed!"} end @@ -119,9 +137,13 @@ defmodule Pleroma.User do Enum.member?(follower.following, User.ap_followers(followed)) 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(_) -> Repo.get_by(User, ap_id: ap_id) end) + Cachex.get!(:user_cache, key, fallback: fn(_) -> get_by_ap_id(ap_id) end) end def get_cached_by_nickname(nickname) do