1 defmodule Pleroma.Notification do
3 alias Pleroma.{User, Activity, Notification, Repo}
6 schema "notifications" do
7 field :seen, :boolean, default: false
8 belongs_to :user, Pleroma.User
9 belongs_to :activity, Pleroma.Activity
14 def for_user(user, opts \\ %{}) do
15 query = from n in Notification,
16 where: n.user_id == ^user.id,
17 order_by: [desc: n.id],
23 def create_notifications(%Activity{id: id, data: %{"to" => to, "type" => type}} = activity) when type in ["Create", "Like", "Announce", "Follow"] do
24 users = User.get_notified_from_activity(activity)
26 notifications = Enum.map(users, fn (user) -> create_notification(activity, user) end)
29 def create_notifications(_), do: {:ok, []}
31 # TODO move to sql, too.
32 def create_notification(%Activity{} = activity, %User{} = user) do
33 notification = %Notification{user_id: user.id, activity_id: activity.id}
34 {:ok, notification} = Repo.insert(notification)