aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/notification.ex
diff options
context:
space:
mode:
authorRoger Braun <roger@rogerbraun.net>2017-09-11 16:15:28 +0200
committerRoger Braun <roger@rogerbraun.net>2017-09-11 18:53:19 +0200
commit61adf676d56db274cb4688a137787e8806e77be9 (patch)
treee611c2be85343be893156abe127731101bf4f85f /lib/pleroma/notification.ex
parent7616b202ea6ab9cd2db107eea59aba1393f4f996 (diff)
downloadpleroma-61adf676d56db274cb4688a137787e8806e77be9.tar.gz
Add basic mastodon notification support.
Diffstat (limited to 'lib/pleroma/notification.ex')
-rw-r--r--lib/pleroma/notification.ex38
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex
new file mode 100644
index 000000000..f8835fce6
--- /dev/null
+++ b/lib/pleroma/notification.ex
@@ -0,0 +1,38 @@
+defmodule Pleroma.Notification do
+ use Ecto.Schema
+ alias Pleroma.{User, Activity, Notification, Repo}
+ import Ecto.Query
+
+ schema "notifications" do
+ field :seen, :boolean, default: false
+ belongs_to :user, Pleroma.User
+ belongs_to :activity, Pleroma.Activity
+
+ timestamps()
+ end
+
+ def for_user(user, opts \\ %{}) do
+ query = from n in Notification,
+ where: n.user_id == ^user.id,
+ order_by: [desc: n.id],
+ preload: [:activity],
+ limit: 20
+ Repo.all(query)
+ end
+
+ def create_notifications(%Activity{id: id, data: %{"to" => to, "type" => type}} = activity) when type in ["Create"] do
+ users = User.get_notified_from_activity(activity)
+
+ notifications = Enum.map(users, fn (user) -> create_notification(activity, user) end)
+ {:ok, notifications}
+ end
+ def create_notifications(_), do: {:ok, []}
+
+ # TODO move to sql, too.
+ def create_notification(%Activity{} = activity, %User{} = user) do
+ notification = %Notification{user_id: user.id, activity_id: activity.id}
+ {:ok, notification} = Repo.insert(notification)
+ notification
+ end
+end
+