conflict_target: [:user_id, :recipient]
)
end
+
+ def mark_as_read(chat) do
+ chat
+ |> change(%{unread: 0})
+ |> Repo.update()
+ end
end
apply(__MODULE__, operation, [])
end
+ def mark_as_read_operation do
+ %Operation{
+ tags: ["chat"],
+ summary: "Mark all messages in the chat as read",
+ operationId: "ChatController.mark_as_read",
+ parameters: [Operation.parameter(:id, :path, :string, "The ID of the Chat")],
+ responses: %{
+ 200 =>
+ Operation.response(
+ "The updated chat",
+ "application/json",
+ Chat
+ )
+ },
+ security: [
+ %{
+ "oAuth" => ["write"]
+ }
+ ]
+ }
+ end
+
def create_operation do
%Operation{
tags: ["chat"],
plug(
OAuthScopesPlug,
- %{scopes: ["write:statuses"]} when action in [:post_chat_message, :create]
+ %{scopes: ["write:statuses"]} when action in [:post_chat_message, :create, :mark_as_read]
)
plug(
end
end
+ def mark_as_read(%{assigns: %{user: %{id: user_id}}} = conn, %{id: id}) do
+ with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id),
+ {:ok, chat} <- Chat.mark_as_read(chat) do
+ conn
+ |> put_view(ChatView)
+ |> render("show.json", chat: chat)
+ end
+ end
+
def messages(%{assigns: %{user: %{id: user_id} = user}} = conn, %{id: id} = params) do
with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id) do
messages =
get("/chats", ChatController, :index)
get("/chats/:id/messages", ChatController, :messages)
post("/chats/:id/messages", ChatController, :post_chat_message)
+ post("/chats/:id/read", ChatController, :mark_as_read)
end
scope [] do
import Pleroma.Factory
+ describe "POST /api/v1/pleroma/chats/:id/read" do
+ setup do: oauth_access(["write:statuses"])
+
+ test "it marks all messages in a chat as read", %{conn: conn, user: user} do
+ other_user = insert(:user)
+
+ {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
+
+ assert chat.unread == 1
+
+ result =
+ conn
+ |> post("/api/v1/pleroma/chats/#{chat.id}/read")
+ |> json_response_and_validate_schema(200)
+
+ assert result["unread"] == 0
+
+ {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
+
+ assert chat.unread == 0
+ end
+ end
+
describe "POST /api/v1/pleroma/chats/:id/messages" do
setup do: oauth_access(["write:statuses"])