ChatMessageReference -> Chat.MessageReference

This commit is contained in:
lain 2020-06-06 11:51:10 +02:00
commit ca0e6e702b
14 changed files with 66 additions and 68 deletions

View file

@ -2,7 +2,7 @@
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.ChatMessageReference do
defmodule Pleroma.Chat.MessageReference do
@moduledoc """
A reference that builds a relation between an AP chat message that a user can see and whether it has been seen
by them, or should be displayed to them. Used to build the chat view that is presented to the user.

View file

@ -7,7 +7,7 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
"""
alias Pleroma.Activity
alias Pleroma.Chat
alias Pleroma.ChatMessageReference
alias Pleroma.Chat.MessageReference
alias Pleroma.Notification
alias Pleroma.Object
alias Pleroma.Repo
@ -111,7 +111,7 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
Object.decrease_replies_count(in_reply_to)
end
ChatMessageReference.delete_for_object(deleted_object)
MessageReference.delete_for_object(deleted_object)
ActivityPub.stream_out(object)
ActivityPub.stream_out_participations(deleted_object, user)
@ -146,13 +146,13 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
|> Enum.each(fn [user, other_user] ->
if user.local do
{:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
{:ok, cm_ref} = ChatMessageReference.create(chat, object, user.ap_id != actor.ap_id)
{:ok, cm_ref} = MessageReference.create(chat, object, user.ap_id != actor.ap_id)
# We add a cache of the unread value here so that it
# doesn't change when being streamed out
chat =
chat
|> Map.put(:unread, ChatMessageReference.unread_count_for_chat(chat))
|> Map.put(:unread, MessageReference.unread_count_for_chat(chat))
Streamer.stream(
["user", "user:pleroma_chat"],

View file

@ -6,7 +6,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationView do
use Pleroma.Web, :view
alias Pleroma.Activity
alias Pleroma.ChatMessageReference
alias Pleroma.Chat.MessageReference
alias Pleroma.Notification
alias Pleroma.Object
alias Pleroma.User
@ -15,7 +15,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationView do
alias Pleroma.Web.MastodonAPI.AccountView
alias Pleroma.Web.MastodonAPI.NotificationView
alias Pleroma.Web.MastodonAPI.StatusView
alias Pleroma.Web.PleromaAPI.ChatMessageReferenceView
alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
@parent_types ~w{Like Announce EmojiReact}
@ -139,9 +139,9 @@ defmodule Pleroma.Web.MastodonAPI.NotificationView do
object = Object.normalize(activity)
author = User.get_cached_by_ap_id(object.data["actor"])
chat = Pleroma.Chat.get(reading_user.id, author.ap_id)
cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
cm_ref = MessageReference.for_chat_and_object(chat, object)
render_opts = Map.merge(opts, %{for: reading_user, chat_message_reference: cm_ref})
chat_message_render = ChatMessageReferenceView.render("show.json", render_opts)
chat_message_render = MessageReferenceView.render("show.json", render_opts)
Map.put(response, :chat_message, chat_message_render)
end

View file

@ -6,14 +6,14 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
alias Pleroma.Activity
alias Pleroma.Chat
alias Pleroma.ChatMessageReference
alias Pleroma.Chat.MessageReference
alias Pleroma.Object
alias Pleroma.Pagination
alias Pleroma.Plugs.OAuthScopesPlug
alias Pleroma.Repo
alias Pleroma.User
alias Pleroma.Web.CommonAPI
alias Pleroma.Web.PleromaAPI.ChatMessageReferenceView
alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
alias Pleroma.Web.PleromaAPI.ChatView
import Ecto.Query
@ -46,13 +46,13 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
message_id: message_id,
id: chat_id
}) do
with %ChatMessageReference{} = cm_ref <-
ChatMessageReference.get_by_id(message_id),
with %MessageReference{} = cm_ref <-
MessageReference.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, _} <- remove_or_delete(cm_ref, user) do
conn
|> put_view(ChatMessageReferenceView)
|> put_view(MessageReferenceView)
|> render("show.json", chat_message_reference: cm_ref)
else
_e ->
@ -71,7 +71,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
defp remove_or_delete(cm_ref, _) do
cm_ref
|> ChatMessageReference.delete()
|> MessageReference.delete()
end
def post_chat_message(
@ -87,9 +87,9 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
media_id: params[:media_id]
),
message <- Object.normalize(activity, false),
cm_ref <- ChatMessageReference.for_chat_and_object(chat, message) do
cm_ref <- MessageReference.for_chat_and_object(chat, message) do
conn
|> put_view(ChatMessageReferenceView)
|> put_view(MessageReferenceView)
|> render("show.json", for: user, chat_message_reference: cm_ref)
end
end
@ -98,20 +98,20 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
id: chat_id,
message_id: message_id
}) do
with %ChatMessageReference{} = cm_ref <-
ChatMessageReference.get_by_id(message_id),
with %MessageReference{} = cm_ref <-
MessageReference.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
{:ok, cm_ref} <- MessageReference.mark_as_read(cm_ref) do
conn
|> put_view(ChatMessageReferenceView)
|> put_view(MessageReferenceView)
|> 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
{_n, _} <- MessageReference.set_all_seen_for_chat(chat) do
conn
|> put_view(ChatView)
|> render("show.json", chat: chat)
@ -122,11 +122,11 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id) do
cm_refs =
chat
|> ChatMessageReference.for_chat_query()
|> MessageReference.for_chat_query()
|> Pagination.fetch_paginated(params |> stringify_keys())
conn
|> put_view(ChatMessageReferenceView)
|> put_view(MessageReferenceView)
|> render("index.json", for: user, chat_message_references: cm_refs)
else
_ ->

View file

@ -2,7 +2,7 @@
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.PleromaAPI.ChatMessageReferenceView do
defmodule Pleroma.Web.PleromaAPI.Chat.MessageReferenceView do
use Pleroma.Web, :view
alias Pleroma.User

View file

@ -6,24 +6,24 @@ defmodule Pleroma.Web.PleromaAPI.ChatView do
use Pleroma.Web, :view
alias Pleroma.Chat
alias Pleroma.ChatMessageReference
alias Pleroma.Chat.MessageReference
alias Pleroma.User
alias Pleroma.Web.CommonAPI.Utils
alias Pleroma.Web.MastodonAPI.AccountView
alias Pleroma.Web.PleromaAPI.ChatMessageReferenceView
alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
def render("show.json", %{chat: %Chat{} = chat} = opts) do
recipient = User.get_cached_by_ap_id(chat.recipient)
last_message = opts[:last_message] || ChatMessageReference.last_message_for_chat(chat)
last_message = opts[:last_message] || MessageReference.last_message_for_chat(chat)
%{
id: chat.id |> to_string(),
account: AccountView.render("show.json", Map.put(opts, :user, recipient)),
unread: Map.get(chat, :unread) || ChatMessageReference.unread_count_for_chat(chat),
unread: Map.get(chat, :unread) || MessageReference.unread_count_for_chat(chat),
last_message:
last_message &&
ChatMessageReferenceView.render("show.json", chat_message_reference: last_message),
MessageReferenceView.render("show.json", chat_message_reference: last_message),
updated_at: Utils.to_masto_date(chat.updated_at)
}
end

View file

@ -6,7 +6,7 @@ defmodule Pleroma.Web.Streamer do
require Logger
alias Pleroma.Activity
alias Pleroma.ChatMessageReference
alias Pleroma.Chat.MessageReference
alias Pleroma.Config
alias Pleroma.Conversation.Participation
alias Pleroma.Notification
@ -187,7 +187,7 @@ defmodule Pleroma.Web.Streamer do
end)
end
defp do_stream(topic, {user, %ChatMessageReference{} = cm_ref})
defp do_stream(topic, {user, %MessageReference{} = cm_ref})
when topic in ["user", "user:pleroma_chat"] do
topic = "#{topic}:#{user.id}"