|> 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
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)
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)