Add follow notifications.
[akkoma] / lib / pleroma / notification.ex
1 defmodule Pleroma.Notification do
2 use Ecto.Schema
3 alias Pleroma.{User, Activity, Notification, Repo}
4 import Ecto.Query
5
6 schema "notifications" do
7 field :seen, :boolean, default: false
8 belongs_to :user, Pleroma.User
9 belongs_to :activity, Pleroma.Activity
10
11 timestamps()
12 end
13
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],
18 preload: [:activity],
19 limit: 20
20 Repo.all(query)
21 end
22
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)
25
26 notifications = Enum.map(users, fn (user) -> create_notification(activity, user) end)
27 {:ok, notifications}
28 end
29 def create_notifications(_), do: {:ok, []}
30
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)
35 notification
36 end
37 end
38