|> Repo.aggregate(:count)
end
+ def mark_as_read(cm_ref) do
+ cm_ref
+ |> changeset(%{seen: true})
+ |> Repo.update()
+ end
+
def set_all_seen_for_chat(chat) do
chat
|> for_chat_query()
}
end
+ def mark_message_as_read_operation do
+ %Operation{
+ tags: ["chat"],
+ summary: "Mark one message in the chat as read",
+ operationId: "ChatController.mark_message_as_read",
+ parameters: [
+ Operation.parameter(:id, :path, :string, "The ID of the Chat"),
+ Operation.parameter(:message_id, :path, :string, "The ID of the message")
+ ],
+ responses: %{
+ 200 =>
+ Operation.response(
+ "The read ChatMessage",
+ "application/json",
+ ChatMessage
+ )
+ },
+ security: [
+ %{
+ "oAuth" => ["write"]
+ }
+ ]
+ }
+ end
+
def show_operation do
%Operation{
tags: ["chat"],
"content" => "Check this out :firefox:",
"id" => "13",
"chat_id" => "1",
- "actor_id" => "someflakeid"
+ "actor_id" => "someflakeid",
+ "unread" => false
},
%{
"actor_id" => "someflakeid",
"id" => "12",
"chat_id" => "1",
"emojis" => [],
- "created_at" => "2020-04-21T15:06:45.000Z"
+ "created_at" => "2020-04-21T15:06:45.000Z",
+ "unread" => false
}
]
}
plug(
OAuthScopesPlug,
%{scopes: ["write:statuses"]}
- when action in [:post_chat_message, :create, :mark_as_read, :delete_message]
+ when action in [
+ :post_chat_message,
+ :create,
+ :mark_as_read,
+ :mark_message_as_read,
+ :delete_message
+ ]
)
plug(
end
end
+ def mark_message_as_read(%{assigns: %{user: %{id: user_id} = user}} = conn, %{
+ id: chat_id,
+ message_id: message_id
+ }) do
+ with %ChatMessageReference{} = cm_ref <-
+ ChatMessageReference.get_by_id(message_id),
+ ^chat_id <- cm_ref.chat_id |> to_string(),
+ %Chat{user_id: ^user_id} <- Chat.get_by_id(chat_id),
+ {:ok, cm_ref} <- ChatMessageReference.mark_as_read(cm_ref) do
+ conn
+ |> put_view(ChatMessageReferenceView)
+ |> render("show.json", for: user, chat_message_reference: cm_ref)
+ 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),
{_n, _} <- ChatMessageReference.set_all_seen_for_chat(chat) do
post("/chats/:id/messages", ChatController, :post_chat_message)
delete("/chats/:id/messages/:message_id", ChatController, :delete_message)
post("/chats/:id/read", ChatController, :mark_as_read)
+ post("/chats/:id/messages/:message_id/read", ChatController, :mark_message_as_read)
get("/conversations/:id/statuses", ConversationController, :statuses)
get("/conversations/:id", ConversationController, :show)
import Pleroma.Factory
+ describe "POST /api/v1/pleroma/chats/:id/messages/:message_id/read" do
+ setup do: oauth_access(["write:statuses"])
+
+ test "it marks one message as read", %{conn: conn, user: user} do
+ other_user = insert(:user)
+
+ {:ok, create} = CommonAPI.post_chat_message(other_user, user, "sup")
+ {:ok, _create} = CommonAPI.post_chat_message(other_user, user, "sup part 2")
+ {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
+ object = Object.normalize(create, false)
+ cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
+
+ assert cm_ref.seen == false
+
+ result =
+ conn
+ |> post("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}/read")
+ |> json_response_and_validate_schema(200)
+
+ assert result["unread"] == false
+
+ cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
+
+ assert cm_ref.seen == true
+ end
+ end
+
describe "POST /api/v1/pleroma/chats/:id/read" do
setup do: oauth_access(["write:statuses"])
other_user = insert(:user)
{:ok, create} = CommonAPI.post_chat_message(other_user, user, "sup")
+ {:ok, _create} = CommonAPI.post_chat_message(other_user, user, "sup part 2")
{:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
object = Object.normalize(create, false)
cm_ref = ChatMessageReference.for_chat_and_object(chat, object)