Merge branch 'descriptions-fixes' into 'develop'
[akkoma] / lib / pleroma / marker.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Marker do
6 use Ecto.Schema
7
8 import Ecto.Changeset
9 import Ecto.Query
10
11 alias Ecto.Multi
12 alias Pleroma.Repo
13 alias Pleroma.User
14
15 @timelines ["notifications"]
16
17 schema "markers" do
18 field(:last_read_id, :string, default: "")
19 field(:timeline, :string, default: "")
20 field(:lock_version, :integer, default: 0)
21
22 belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
23 timestamps()
24 end
25
26 def get_markers(user, timelines \\ []) do
27 Repo.all(get_query(user, timelines))
28 end
29
30 def upsert(%User{} = user, attrs) do
31 attrs
32 |> Map.take(@timelines)
33 |> Enum.reduce(Multi.new(), fn {timeline, timeline_attrs}, multi ->
34 marker =
35 user
36 |> get_marker(timeline)
37 |> changeset(timeline_attrs)
38
39 Multi.insert(multi, timeline, marker,
40 returning: true,
41 on_conflict: {:replace, [:last_read_id]},
42 conflict_target: [:user_id, :timeline]
43 )
44 end)
45 |> Repo.transaction()
46 end
47
48 defp get_marker(user, timeline) do
49 case Repo.find_resource(get_query(user, timeline)) do
50 {:ok, marker} -> %__MODULE__{marker | user: user}
51 _ -> %__MODULE__{timeline: timeline, user_id: user.id}
52 end
53 end
54
55 @doc false
56 defp changeset(marker, attrs) do
57 marker
58 |> cast(attrs, [:last_read_id])
59 |> validate_required([:user_id, :timeline, :last_read_id])
60 |> validate_inclusion(:timeline, @timelines)
61 end
62
63 defp by_timeline(query, timeline) do
64 from(m in query, where: m.timeline in ^List.wrap(timeline))
65 end
66
67 defp by_user_id(query, id), do: from(m in query, where: m.user_id == ^id)
68
69 defp get_query(user, timelines) do
70 __MODULE__
71 |> by_user_id(user.id)
72 |> by_timeline(timelines)
73 end
74 end