akkoma/lib/pleroma/marker.ex

128 lines
3.4 KiB
Elixir
Raw Normal View History

2019-10-17 12:26:59 +00:00
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Marker do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query
alias Ecto.Multi
2019-11-12 12:59:34 +00:00
alias Pleroma.Notification
2019-10-17 12:26:59 +00:00
alias Pleroma.Repo
alias Pleroma.User
alias __MODULE__
2019-10-17 12:26:59 +00:00
@timelines ["notifications"]
@type t :: %__MODULE__{}
2019-10-17 12:26:59 +00:00
schema "markers" do
field(:last_read_id, :string, default: "")
field(:timeline, :string, default: "")
field(:lock_version, :integer, default: 0)
2019-10-22 10:42:59 +00:00
field(:unread_count, :integer, default: 0)
2019-10-17 12:26:59 +00:00
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
timestamps()
end
@doc """
Gets markers by user and timeline.
opts:
`recount_unread` - run force recount unread notifications for `true` value
"""
@spec get_markers(User.t(), list(String), map()) :: list(t())
def get_markers(user, timelines \\ [], opts \\ %{}) do
user
|> get_query(timelines)
|> recount_unread_notifications(opts[:recount_unread])
|> Repo.all()
2019-10-17 12:26:59 +00:00
end
def upsert(%User{} = user, attrs) do
attrs
|> Map.take(@timelines)
|> Enum.reduce(Multi.new(), fn {timeline, timeline_attrs}, multi ->
marker =
user
|> get_marker(timeline)
|> changeset(timeline_attrs)
Multi.insert(multi, timeline, marker,
returning: true,
2019-10-22 10:42:59 +00:00
on_conflict: {:replace, [:last_read_id, :unread_count]},
2019-10-17 12:26:59 +00:00
conflict_target: [:user_id, :timeline]
)
end)
|> Repo.transaction()
end
@spec multi_set_unread_count(Multi.t(), User.t(), String.t()) :: Multi.t()
def multi_set_unread_count(multi, %User{} = user, "notifications") do
multi
|> Multi.run(:counters, fn _repo, _changes ->
2019-11-12 12:59:34 +00:00
{:ok,
%{
unread_count: Repo.aggregate(Notification.unread_count_query(user), :count, :id),
last_read_id: Repo.one(Notification.last_read_query(user))
}}
end)
|> Multi.insert(
:marker,
fn %{counters: attrs} ->
2019-11-11 14:06:41 +00:00
%Marker{timeline: "notifications", user_id: user.id}
|> struct(attrs)
|> Ecto.Changeset.change()
end,
returning: true,
on_conflict: {:replace, [:last_read_id, :unread_count]},
conflict_target: [:user_id, :timeline]
)
end
2019-10-23 19:48:04 +00:00
def multi_set_unread_count(multi, _, _), do: multi
2019-10-17 12:26:59 +00:00
defp get_marker(user, timeline) do
case Repo.find_resource(get_query(user, timeline)) do
{:ok, marker} -> %__MODULE__{marker | user: user}
_ -> %__MODULE__{timeline: timeline, user_id: user.id}
end
end
@doc false
defp changeset(marker, attrs) do
marker
2019-10-22 10:42:59 +00:00
|> cast(attrs, [:last_read_id, :unread_count])
2019-10-17 12:26:59 +00:00
|> validate_required([:user_id, :timeline, :last_read_id])
|> validate_inclusion(:timeline, @timelines)
end
defp by_timeline(query, timeline) do
from(m in query, where: m.timeline in ^List.wrap(timeline))
end
defp by_user_id(query, id), do: from(m in query, where: m.user_id == ^id)
defp get_query(user, timelines) do
__MODULE__
|> by_user_id(user.id)
|> by_timeline(timelines)
end
defp recount_unread_notifications(query, true) do
from(
q in query,
left_join: n in "notifications",
on: n.user_id == q.user_id and n.seen == false,
group_by: [:id],
select_merge: %{
unread_count: fragment("count(?)", n.id)
}
)
end
defp recount_unread_notifications(query, _), do: query
2019-10-17 12:26:59 +00:00
end