X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fuser.ex;h=869a3eb1b5b77352b41b3920da7072ab488a2eeb;hb=6a184115a2431727b5039c1c726a8afc5eef228c;hp=23be6276ebe6abce6bffa9567d4c5a7d6225b600;hpb=c48c381e909240dcece9f961e4728fa712d089cc;p=akkoma diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 23be6276e..869a3eb1b 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -4,7 +4,9 @@ defmodule Pleroma.User do import Ecto.{Changeset, Query} alias Pleroma.{Repo, User, Object, Web} alias Comeonin.Pbkdf2 - alias Pleroma.Web.OStatus + 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,6 +105,10 @@ defmodule Pleroma.User 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 @@ -103,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 @@ -115,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