ChatMessageReferences: Move tests
This commit is contained in:
parent
f3ccd50a33
commit
2591745fc2
|
@ -6,9 +6,7 @@ defmodule Pleroma.Chat do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
|
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
import Ecto.Query
|
|
||||||
|
|
||||||
alias Pleroma.Object
|
|
||||||
alias Pleroma.Repo
|
alias Pleroma.Repo
|
||||||
alias Pleroma.User
|
alias Pleroma.User
|
||||||
|
|
||||||
|
@ -26,38 +24,6 @@ defmodule Pleroma.Chat do
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
def last_message_for_chat(chat) do
|
|
||||||
messages_for_chat_query(chat)
|
|
||||||
|> order_by(desc: :id)
|
|
||||||
|> limit(1)
|
|
||||||
|> Repo.one()
|
|
||||||
end
|
|
||||||
|
|
||||||
def messages_for_chat_query(chat) do
|
|
||||||
chat =
|
|
||||||
chat
|
|
||||||
|> Repo.preload(:user)
|
|
||||||
|
|
||||||
from(o in Object,
|
|
||||||
where: fragment("?->>'type' = ?", o.data, "ChatMessage"),
|
|
||||||
where:
|
|
||||||
fragment(
|
|
||||||
"""
|
|
||||||
(?->>'actor' = ? and ?->'to' = ?)
|
|
||||||
OR (?->>'actor' = ? and ?->'to' = ?)
|
|
||||||
""",
|
|
||||||
o.data,
|
|
||||||
^chat.user.ap_id,
|
|
||||||
o.data,
|
|
||||||
^[chat.recipient],
|
|
||||||
o.data,
|
|
||||||
^chat.recipient,
|
|
||||||
o.data,
|
|
||||||
^[chat.user.ap_id]
|
|
||||||
)
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
def creation_cng(struct, params) do
|
def creation_cng(struct, params) do
|
||||||
struct
|
struct
|
||||||
|> cast(params, [:user_id, :recipient, :unread])
|
|> cast(params, [:user_id, :recipient, :unread])
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.ChatMessageReferencTest do
|
||||||
|
use Pleroma.DataCase, async: true
|
||||||
|
|
||||||
|
alias Pleroma.Chat
|
||||||
|
alias Pleroma.ChatMessageReference
|
||||||
|
alias Pleroma.Web.CommonAPI
|
||||||
|
|
||||||
|
import Pleroma.Factory
|
||||||
|
|
||||||
|
describe "messages" do
|
||||||
|
test "it returns the last message in a chat" do
|
||||||
|
user = insert(:user)
|
||||||
|
recipient = insert(:user)
|
||||||
|
|
||||||
|
{:ok, _message_1} = CommonAPI.post_chat_message(user, recipient, "hey")
|
||||||
|
{:ok, _message_2} = CommonAPI.post_chat_message(recipient, user, "ho")
|
||||||
|
|
||||||
|
{:ok, chat} = Chat.get_or_create(user.id, recipient.ap_id)
|
||||||
|
|
||||||
|
message = ChatMessageReference.last_message_for_chat(chat)
|
||||||
|
|
||||||
|
assert message.object.data["content"] == "ho"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -10,22 +10,6 @@ defmodule Pleroma.ChatTest do
|
||||||
|
|
||||||
import Pleroma.Factory
|
import Pleroma.Factory
|
||||||
|
|
||||||
describe "messages" do
|
|
||||||
test "it returns the last message in a chat" do
|
|
||||||
user = insert(:user)
|
|
||||||
recipient = insert(:user)
|
|
||||||
|
|
||||||
{:ok, _message_1} = CommonAPI.post_chat_message(user, recipient, "hey")
|
|
||||||
{:ok, _message_2} = CommonAPI.post_chat_message(recipient, user, "ho")
|
|
||||||
|
|
||||||
{:ok, chat} = Chat.get_or_create(user.id, recipient.ap_id)
|
|
||||||
|
|
||||||
message = Chat.last_message_for_chat(chat)
|
|
||||||
|
|
||||||
assert message.data["content"] == "ho"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "creation and getting" do
|
describe "creation and getting" do
|
||||||
test "it only works if the recipient is a valid user (for now)" do
|
test "it only works if the recipient is a valid user (for now)" do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
|
|
Loading…
Reference in New Issue