diff options
author | eal <eal@waifu.club> | 2017-11-10 15:24:39 +0200 |
---|---|---|
committer | eal <eal@waifu.club> | 2017-11-10 15:24:52 +0200 |
commit | e6a78c6ed0925c27ea4d194c0e52ab07542c444e (patch) | |
tree | ea3dfb5fed6ee60e1c34d47d4b8cd432bbeff9b6 /lib | |
parent | ccde03285debe8def5d89d49b9afdcc48a76d7a6 (diff) | |
download | pleroma-e6a78c6ed0925c27ea4d194c0e52ab07542c444e.tar.gz |
MastoAPI: Add notification get, clear and dismiss.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/notification.ex | 31 | ||||
-rw-r--r-- | lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 65 | ||||
-rw-r--r-- | lib/pleroma/web/router.ex | 3 |
3 files changed, 82 insertions, 17 deletions
diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex index 00a382f31..039cc7312 100644 --- a/lib/pleroma/notification.ex +++ b/lib/pleroma/notification.ex @@ -36,6 +36,37 @@ defmodule Pleroma.Notification do Repo.all(query) end + def get(%{id: user_id} = _user, id) do + query = from n in Notification, + where: n.id == ^id, + preload: [:activity] + + notification = Repo.one(query) + case notification do + %{user_id: ^user_id} -> + {:ok, notification} + _ -> + {:error, "Cannot get notification"} + end + end + + def clear(user) do + query = from n in Notification, + where: n.user_id == ^user.id + + Repo.delete_all(query) + end + + def dismiss(%{id: user_id} = _user, id) do + notification = Repo.get(Notification, id) + case notification do + %{user_id: ^user_id} -> + Repo.delete(notification) + _ -> + {:error, "Cannot dismiss notification"} + end + end + def create_notifications(%Activity{id: id, data: %{"to" => to, "type" => type}} = activity) when type in ["Create", "Like", "Announce", "Follow"] do users = User.get_notified_from_activity(activity) diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index feaf9a900..d95b18315 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -193,23 +193,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do def notifications(%{assigns: %{user: user}} = conn, params) do notifications = Notification.for_user(user, params) - result = Enum.map(notifications, fn (%{id: id, activity: activity, inserted_at: created_at}) -> - actor = User.get_cached_by_ap_id(activity.data["actor"]) - created_at = NaiveDateTime.to_iso8601(created_at) - |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false) - case activity.data["type"] do - "Create" -> - %{id: id, type: "mention", created_at: created_at, account: AccountView.render("account.json", %{user: actor}), status: StatusView.render("status.json", %{activity: activity, for: user})} - "Like" -> - liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"]) - %{id: id, type: "favourite", created_at: created_at, account: AccountView.render("account.json", %{user: actor}), status: StatusView.render("status.json", %{activity: liked_activity, for: user})} - "Announce" -> - announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"]) - %{id: id, type: "reblog", created_at: created_at, account: AccountView.render("account.json", %{user: actor}), status: StatusView.render("status.json", %{activity: announced_activity, for: user})} - "Follow" -> - %{id: id, type: "follow", created_at: created_at, account: AccountView.render("account.json", %{user: actor})} - _ -> nil - end + result = Enum.map(notifications, fn x -> + render_notification(user, x) end) |> Enum.filter(&(&1)) @@ -218,6 +203,33 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |> json(result) end + def get_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do + with {:ok, notification} <- Notification.get(user, id) do + json(conn, render_notification(user, notification)) + else + {:error, reason} -> + conn + |> put_resp_content_type("application/json") + |> send_resp(403, Poison.encode!(%{"error" => reason})) + end + end + + def clear_notifications(%{assigns: %{user: user}} = conn, _params) do + Notification.clear(user) + json(conn, %{}) + end + + def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do + with {:ok, _notif} <- Notification.dismiss(user, id) do + json(conn, %{}) + else + {:error, reason} -> + conn + |> put_resp_content_type("application/json") + |> send_resp(403, Poison.encode!(%{"error" => reason})) + end + end + def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do id = List.wrap(id) q = from u in User, @@ -408,4 +420,23 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do Logger.debug("Unimplemented, returning an empty array") json(conn, []) end + + defp render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do + actor = User.get_cached_by_ap_id(activity.data["actor"]) + created_at = NaiveDateTime.to_iso8601(created_at) + |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false) + case activity.data["type"] do + "Create" -> + %{id: id, type: "mention", created_at: created_at, account: AccountView.render("account.json", %{user: actor}), status: StatusView.render("status.json", %{activity: activity, for: user})} + "Like" -> + liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"]) + %{id: id, type: "favourite", created_at: created_at, account: AccountView.render("account.json", %{user: actor}), status: StatusView.render("status.json", %{activity: liked_activity, for: user})} + "Announce" -> + announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"]) + %{id: id, type: "reblog", created_at: created_at, account: AccountView.render("account.json", %{user: actor}), status: StatusView.render("status.json", %{activity: announced_activity, for: user})} + "Follow" -> + %{id: id, type: "follow", created_at: created_at, account: AccountView.render("account.json", %{user: actor})} + _ -> nil + end + end end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 0a0aea966..efd37ede2 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -82,7 +82,10 @@ defmodule Pleroma.Web.Router do post "/statuses/:id/favourite", MastodonAPIController, :fav_status post "/statuses/:id/unfavourite", MastodonAPIController, :unfav_status + post "/notifications/clear", MastodonAPIController, :clear_notifications + post "/notifications/dismiss", MastodonAPIController, :dismiss_notification get "/notifications", MastodonAPIController, :notifications + get "/notifications/:id", MastodonAPIController, :get_notification post "/media", MastodonAPIController, :upload end |