1 defmodule Pleroma.User do
4 alias Pleroma.{Repo, User}
10 field :nickname, :string
11 field :password_hash, :string
12 field :following, { :array, :string }, default: []
18 def ap_id(%User{nickname: nickname}) do
20 Application.get_env(:pleroma, Pleroma.Web.Endpoint)
21 |> Keyword.fetch!(:url)
22 |> Keyword.fetch!(:host)
24 "https://#{host}/users/#{nickname}"
27 def ap_followers(%User{} = user) do
28 "#{ap_id(user)}/followers"
31 def follow_changeset(struct, params \\ %{}) do
33 |> cast(params, [:following])
34 |> validate_required([:following])
37 def follow(%User{} = follower, %User{} = followed) do
38 ap_followers = User.ap_followers(followed)
39 following = [ap_followers | follower.following]
43 |> follow_changeset(%{following: following})
47 def unfollow(%User{} = follower, %User{} = followed) do
48 ap_followers = User.ap_followers(followed)
49 following = follower.following
50 |> List.delete(ap_followers)
53 |> follow_changeset(%{following: following})
57 def following?(%User{} = follower, %User{} = followed) do
58 Enum.member?(follower.following, User.ap_followers(followed))