From: dtluna Date: Sun, 16 Apr 2017 14:18:34 +0000 (+0300) Subject: Merge branch 'develop' of ssh.gitgud.io:lambadalambda/pleroma into bugfix/repeated... X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=ef5033d7a7f744607235e974c7beb96b014c2b61;p=akkoma Merge branch 'develop' of ssh.gitgud.io:lambadalambda/pleroma into bugfix/repeated-follow-unfollow --- ef5033d7a7f744607235e974c7beb96b014c2b61 diff --cc lib/pleroma/user.ex index 5f5bc1c38,fdcc1b7d5..418522172 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@@ -29,19 -32,35 +32,40 @@@ defmodule Pleroma.User d |> 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 + if following?(follower, followed) do + { :error, + "Could not follow user: #{followed.nickname} is already on your list." } + else + following = [ap_followers | follower.following] + |> Enum.uniq - follower - |> follow_changeset(%{following: following}) - |> Repo.update + follower + |> follow_changeset(%{following: following}) + |> Repo.update + end end def unfollow(%User{} = follower, %User{} = followed) do diff --cc test/web/twitter_api/twitter_api_test.exs index 405aa1221,1e5204db8..b61f16b47 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@@ -107,10 -113,10 +113,10 @@@ defmodule Pleroma.Web.TwitterAPI.Twitte end test "Follow another user" do - { :ok, user } = UserBuilder.insert - { :ok, followed } = UserBuilder.insert(%{nickname: "guy"}) + user = insert(:user) - following = insert(:user) ++ followed = insert(:user) - {:ok, user, following, activity } = TwitterAPI.follow(user, following.id) + { :ok, user, followed, activity } = TwitterAPI.follow(user, followed.id) user = Repo.get(User, user.id) follow = Repo.get(Activity, activity.id) @@@ -123,10 -126,10 +129,10 @@@ end test "Unfollow another user" do - { :ok, followed } = UserBuilder.insert(%{nickname: "guy"}) - { :ok, user } = UserBuilder.insert(%{following: [User.ap_followers(followed)]}) - following = insert(:user) - user = insert(:user, %{following: [User.ap_followers(following)]}) ++ followed = insert(:user) ++ user = insert(:user, %{following: [User.ap_followers(followed)]}) - {:ok, user, _following } = TwitterAPI.unfollow(user, following.id) + { :ok, user, _followed } = TwitterAPI.unfollow(user, followed.id) user = Repo.get(User, user.id)