aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/mix/tasks/pleroma/marker.ex36
-rw-r--r--lib/pleroma/marker.ex27
-rw-r--r--lib/pleroma/notification.ex64
-rw-r--r--lib/pleroma/web/mastodon_api/views/marker_view.ex5
4 files changed, 112 insertions, 20 deletions
diff --git a/lib/mix/tasks/pleroma/marker.ex b/lib/mix/tasks/pleroma/marker.ex
new file mode 100644
index 000000000..bebef0d6a
--- /dev/null
+++ b/lib/mix/tasks/pleroma/marker.ex
@@ -0,0 +1,36 @@
+defmodule Mix.Tasks.Pleroma.Marker do
+ use Mix.Task
+ import Mix.Pleroma
+ import Ecto.Query
+
+ alias Pleroma.Notification
+ alias Pleroma.Repo
+
+ def run(["update_markers"]) do
+ start_pleroma()
+
+ from(q in Notification,
+ select: %{
+ timeline: "notifications",
+ user_id: q.user_id,
+ unread_count: fragment("SUM( CASE WHEN seen = false THEN 1 ELSE 0 END )"),
+ last_read_id:
+ type(fragment("MAX( CASE WHEN seen = true THEN id ELSE null END )"), :string)
+ },
+ group_by: [q.user_id]
+ )
+ |> Repo.all()
+ |> Enum.each(fn attrs ->
+ Pleroma.Marker
+ |> struct(attrs)
+ |> Ecto.Changeset.change()
+ |> Pleroma.Repo.insert(
+ returning: true,
+ on_conflict: {:replace, [:last_read_id, :unread_count]},
+ conflict_target: [:user_id, :timeline]
+ )
+ end)
+
+ shell_info("Done")
+ end
+end
diff --git a/lib/pleroma/marker.ex b/lib/pleroma/marker.ex
index 7f87c86c3..a7ea542dd 100644
--- a/lib/pleroma/marker.ex
+++ b/lib/pleroma/marker.ex
@@ -11,6 +11,7 @@ defmodule Pleroma.Marker do
alias Ecto.Multi
alias Pleroma.Repo
alias Pleroma.User
+ alias __MODULE__
@timelines ["notifications"]
@@ -18,6 +19,7 @@ defmodule Pleroma.Marker do
field(:last_read_id, :string, default: "")
field(:timeline, :string, default: "")
field(:lock_version, :integer, default: 0)
+ field(:unread_count, :integer, default: 0)
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
timestamps()
@@ -38,13 +40,34 @@ defmodule Pleroma.Marker do
Multi.insert(multi, timeline, marker,
returning: true,
- on_conflict: {:replace, [:last_read_id]},
+ on_conflict: {:replace, [:last_read_id, :unread_count]},
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 ->
+ {:ok, Repo.one(Pleroma.Notification.notifications_info_query(user))}
+ end)
+ |> Multi.insert(
+ :marker,
+ fn %{counters: attrs} ->
+ Marker
+ |> struct(attrs)
+ |> Ecto.Changeset.change()
+ end,
+ returning: true,
+ on_conflict: {:replace, [:last_read_id, :unread_count]},
+ conflict_target: [:user_id, :timeline]
+ )
+ end
+
+ def multi_set_unread_count(multi, _, _), do: multi
+
defp get_marker(user, timeline) do
case Repo.find_resource(get_query(user, timeline)) do
{:ok, marker} -> %__MODULE__{marker | user: user}
@@ -55,7 +78,7 @@ defmodule Pleroma.Marker do
@doc false
defp changeset(marker, attrs) do
marker
- |> cast(attrs, [:last_read_id])
+ |> cast(attrs, [:last_read_id, :unread_count])
|> validate_required([:user_id, :timeline, :last_read_id])
|> validate_inclusion(:timeline, @timelines)
end
diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex
index b7ecf51e4..373f9b06a 100644
--- a/lib/pleroma/notification.ex
+++ b/lib/pleroma/notification.ex
@@ -5,7 +5,9 @@
defmodule Pleroma.Notification do
use Ecto.Schema
+ alias Ecto.Multi
alias Pleroma.Activity
+ alias Pleroma.Marker
alias Pleroma.Notification
alias Pleroma.Object
alias Pleroma.Pagination
@@ -34,6 +36,20 @@ defmodule Pleroma.Notification do
|> cast(attrs, [:seen])
end
+ @spec notifications_info_query(User.t()) :: Ecto.Queryable.t()
+ def notifications_info_query(user) do
+ from(q in Pleroma.Notification,
+ where: q.user_id == ^user.id,
+ select: %{
+ timeline: "notifications",
+ user_id: type(^user.id, :string),
+ unread_count: fragment("SUM( CASE WHEN seen = false THEN 1 ELSE 0 END )"),
+ last_read_id:
+ type(fragment("MAX( CASE WHEN seen = true THEN id ELSE null END )"), :string)
+ }
+ )
+ end
+
def for_user_query(user, opts \\ []) do
Notification
|> where(user_id: ^user.id)
@@ -151,25 +167,23 @@ defmodule Pleroma.Notification do
|> Repo.all()
end
- def set_read_up_to(%{id: user_id} = _user, id) do
+ def set_read_up_to(%{id: user_id} = user, id) do
query =
from(
n in Notification,
where: n.user_id == ^user_id,
where: n.id <= ^id,
where: n.seen == false,
- update: [
- set: [
- seen: true,
- updated_at: ^NaiveDateTime.utc_now()
- ]
- ],
# Ideally we would preload object and activities here
# but Ecto does not support preloads in update_all
select: n.id
)
- {_, notification_ids} = Repo.update_all(query, [])
+ {:ok, %{ids: {_, notification_ids}}} =
+ Multi.new()
+ |> Multi.update_all(:ids, query, set: [seen: true, updated_at: NaiveDateTime.utc_now()])
+ |> Marker.multi_set_unread_count(user, "notifications")
+ |> Repo.transaction()
Notification
|> where([n], n.id in ^notification_ids)
@@ -186,11 +200,18 @@ defmodule Pleroma.Notification do
|> Repo.all()
end
+ @spec read_one(User.t(), String.t()) ::
+ {:ok, Notification.t()} | {:error, Ecto.Changeset.t()} | nil
def read_one(%User{} = user, notification_id) do
with {:ok, %Notification{} = notification} <- get(user, notification_id) do
- notification
- |> changeset(%{seen: true})
- |> Repo.update()
+ Multi.new()
+ |> Multi.update(:update, changeset(notification, %{seen: true}))
+ |> Marker.multi_set_unread_count(user, "notifications")
+ |> Repo.transaction()
+ |> case do
+ {:ok, %{update: notification}} -> {:ok, notification}
+ {:error, :update, changeset, _} -> {:error, changeset}
+ end
end
end
@@ -243,8 +264,11 @@ defmodule Pleroma.Notification do
object = Object.normalize(activity)
unless object && object.data["type"] == "Answer" do
- users = get_notified_from_activity(activity)
- notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
+ notifications =
+ activity
+ |> get_notified_from_activity()
+ |> Enum.map(&create_notification(activity, &1))
+
{:ok, notifications}
else
{:ok, []}
@@ -253,8 +277,11 @@ defmodule Pleroma.Notification do
def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
when type in ["Like", "Announce", "Follow"] do
- users = get_notified_from_activity(activity)
- notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
+ notifications =
+ activity
+ |> get_notified_from_activity
+ |> Enum.map(&create_notification(activity, &1))
+
{:ok, notifications}
end
@@ -263,8 +290,11 @@ defmodule Pleroma.Notification do
# TODO move to sql, too.
def create_notification(%Activity{} = activity, %User{} = user) do
unless skip?(activity, user) do
- notification = %Notification{user_id: user.id, activity: activity}
- {:ok, notification} = Repo.insert(notification)
+ {:ok, %{notification: notification}} =
+ Multi.new()
+ |> Multi.insert(:notification, %Notification{user_id: user.id, activity: activity})
+ |> Marker.multi_set_unread_count(user, "notifications")
+ |> Repo.transaction()
["user", "user:notification"]
|> Streamer.stream(notification)
diff --git a/lib/pleroma/web/mastodon_api/views/marker_view.ex b/lib/pleroma/web/mastodon_api/views/marker_view.ex
index 38fbeed5f..81545cff0 100644
--- a/lib/pleroma/web/mastodon_api/views/marker_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/marker_view.ex
@@ -10,7 +10,10 @@ defmodule Pleroma.Web.MastodonAPI.MarkerView do
Map.put_new(acc, m.timeline, %{
last_read_id: m.last_read_id,
version: m.lock_version,
- updated_at: NaiveDateTime.to_iso8601(m.updated_at)
+ updated_at: NaiveDateTime.to_iso8601(m.updated_at),
+ pleroma: %{
+ unread_count: m.unread_count
+ }
})
end)
end