Add following TwAPI endpoint.
[akkoma] / lib / pleroma / user.ex
1 defmodule Pleroma.User do
2 use Ecto.Schema
3 import Ecto.Changeset
4 alias Pleroma.{Repo, User}
5
6 schema "users" do
7 field :bio, :string
8 field :email, :string
9 field :name, :string
10 field :nickname, :string
11 field :password_hash, :string
12 field :following, { :array, :string }, default: []
13 field :ap_id, :string
14
15 timestamps()
16 end
17
18 def ap_id(%User{nickname: nickname}) do
19 host =
20 Application.get_env(:pleroma, Pleroma.Web.Endpoint)
21 |> Keyword.fetch!(:url)
22 |> Keyword.fetch!(:host)
23
24 "https://#{host}/users/#{nickname}"
25 end
26
27 def ap_followers(%User{} = user) do
28 "#{ap_id(user)}/followers"
29 end
30
31 def follow_changeset(struct, params \\ %{}) do
32 struct
33 |> cast(params, [:following])
34 |> validate_required([:following])
35 end
36
37 def follow(%User{} = follower, %User{} = followed) do
38 ap_followers = User.ap_followers(followed)
39 following = [ap_followers | follower.following]
40 |> Enum.uniq
41
42 follower
43 |> follow_changeset(%{following: following})
44 |> Repo.update
45 end
46 end