forked from AkkomaGang/akkoma
Merge branch 'feature/chat' into 'develop'
Feature/chat See merge request pleroma/pleroma!36
This commit is contained in:
commit
08e7e249b1
21 changed files with 70 additions and 7 deletions
|
@ -20,6 +20,7 @@ def start(_type, _args) do
|
||||||
limit: 2500
|
limit: 2500
|
||||||
]]),
|
]]),
|
||||||
worker(Pleroma.Web.Federator, []),
|
worker(Pleroma.Web.Federator, []),
|
||||||
|
worker(Pleroma.Web.ChatChannel.ChatChannelState, []),
|
||||||
]
|
]
|
||||||
++ if Mix.env == :test, do: [], else: [worker(Pleroma.Web.Streamer, [])]
|
++ if Mix.env == :test, do: [], else: [worker(Pleroma.Web.Streamer, [])]
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
defmodule Pleroma.Web.UserSocket do
|
defmodule Pleroma.Web.UserSocket do
|
||||||
use Phoenix.Socket
|
use Phoenix.Socket
|
||||||
|
alias Pleroma.User
|
||||||
|
alias Comeonin.Pbkdf2
|
||||||
|
|
||||||
## Channels
|
## Channels
|
||||||
# channel "room:*", Pleroma.Web.RoomChannel
|
# channel "room:*", Pleroma.Web.RoomChannel
|
||||||
|
channel "chat:*", Pleroma.Web.ChatChannel
|
||||||
|
|
||||||
## Transports
|
## Transports
|
||||||
transport :websocket, Phoenix.Transports.WebSocket
|
transport :websocket, Phoenix.Transports.WebSocket
|
||||||
|
@ -19,8 +22,13 @@ defmodule Pleroma.Web.UserSocket do
|
||||||
#
|
#
|
||||||
# See `Phoenix.Token` documentation for examples in
|
# See `Phoenix.Token` documentation for examples in
|
||||||
# performing token verification on connect.
|
# performing token verification on connect.
|
||||||
def connect(_params, socket) do
|
def connect(%{"token" => token}, socket) do
|
||||||
{:ok, socket}
|
with {:ok, user_id} <- Phoenix.Token.verify(socket, "user socket", token, max_age: 84600),
|
||||||
|
%User{} = user <- Pleroma.Repo.get(User, user_id) do
|
||||||
|
{:ok, assign(socket, :user_name, user.nickname)}
|
||||||
|
else
|
||||||
|
_e -> :error
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Socket id's are topics that allow you to identify all sockets for a given user:
|
# Socket id's are topics that allow you to identify all sockets for a given user:
|
||||||
|
|
46
lib/pleroma/web/chat_channel.ex
Normal file
46
lib/pleroma/web/chat_channel.ex
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
defmodule Pleroma.Web.ChatChannel do
|
||||||
|
use Phoenix.Channel
|
||||||
|
alias Pleroma.Web.ChatChannel.ChatChannelState
|
||||||
|
alias Pleroma.User
|
||||||
|
|
||||||
|
def join("chat:public", _message, socket) do
|
||||||
|
send(self(), :after_join)
|
||||||
|
{:ok, socket}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_info(:after_join, socket) do
|
||||||
|
push socket, "messages", %{messages: ChatChannelState.messages()}
|
||||||
|
{:noreply, socket}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_in("new_msg", %{"text" => text}, %{assigns: %{user_name: user_name}} = socket) do
|
||||||
|
author = User.get_cached_by_nickname(user_name)
|
||||||
|
author = Pleroma.Web.MastodonAPI.AccountView.render("account.json", user: author)
|
||||||
|
message = ChatChannelState.add_message(%{text: text, author: author})
|
||||||
|
|
||||||
|
broadcast! socket, "new_msg", message
|
||||||
|
{:noreply, socket}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defmodule Pleroma.Web.ChatChannel.ChatChannelState do
|
||||||
|
use Agent
|
||||||
|
@max_messages 20
|
||||||
|
|
||||||
|
def start_link do
|
||||||
|
Agent.start_link(fn -> %{max_id: 1, messages: []} end, name: __MODULE__)
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_message(message) do
|
||||||
|
Agent.get_and_update(__MODULE__, fn state ->
|
||||||
|
id = state[:max_id] + 1
|
||||||
|
message = Map.put(message, "id", id)
|
||||||
|
messages = [message | state[:messages]] |> Enum.take(@max_messages)
|
||||||
|
{message, %{max_id: id, messages: messages}}
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def messages() do
|
||||||
|
Agent.get(__MODULE__, fn state -> state[:messages] |> Enum.reverse end)
|
||||||
|
end
|
||||||
|
end
|
|
@ -10,7 +10,8 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
|
def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
|
||||||
render(conn, UserView, "show.json", %{user: user})
|
token = Phoenix.Token.sign(conn, "user socket", user.id)
|
||||||
|
render(conn, UserView, "show.json", %{user: user, token: token})
|
||||||
end
|
end
|
||||||
|
|
||||||
def status_update(%{assigns: %{user: user}} = conn, %{"status" => _} = status_data) do
|
def status_update(%{assigns: %{user: user}} = conn, %{"status" => _} = status_data) do
|
||||||
|
|
|
@ -25,7 +25,7 @@ def render("user.json", %{user: user = %User{}} = assigns) do
|
||||||
|
|
||||||
user_info = User.get_cached_user_info(user)
|
user_info = User.get_cached_user_info(user)
|
||||||
|
|
||||||
%{
|
data = %{
|
||||||
"created_at" => user.inserted_at |> Utils.format_naive_asctime,
|
"created_at" => user.inserted_at |> Utils.format_naive_asctime,
|
||||||
"description" => HtmlSanitizeEx.strip_tags(user.bio),
|
"description" => HtmlSanitizeEx.strip_tags(user.bio),
|
||||||
"favourites_count" => 0,
|
"favourites_count" => 0,
|
||||||
|
@ -47,6 +47,12 @@ def render("user.json", %{user: user = %User{}} = assigns) do
|
||||||
"cover_photo" => image_url(user.info["banner"]),
|
"cover_photo" => image_url(user.info["banner"]),
|
||||||
"background_image" => image_url(user.info["background"])
|
"background_image" => image_url(user.info["background"])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if assigns[:token] do
|
||||||
|
Map.put(data, "token", assigns[:token])
|
||||||
|
else
|
||||||
|
data
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def render("short.json", %{user: %User{
|
def render("short.json", %{user: %User{
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>Pleroma</title><link rel=stylesheet href=/static/font/css/fontello.css><link rel=stylesheet href=/static/font/css/animation.css><link href=/static/css/app.1c9a07d790a4262baf47d33a2a3e71c5.css rel=stylesheet></head><body style="display: none"><div id=app></div><script type=text/javascript src=/static/js/manifest.8e9b51ea14e08fc9a22a.js></script><script type=text/javascript src=/static/js/vendor.6ffc1793217fe26edf39.js></script><script type=text/javascript src=/static/js/app.a4d345815ef9d13e9524.js></script></body></html>
|
<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>Pleroma</title><link rel=stylesheet href=/static/font/css/fontello.css><link rel=stylesheet href=/static/font/css/animation.css><link href=/static/css/app.67f64792f89a96e59442c437c7ded0b3.css rel=stylesheet></head><body style="display: none"><div id=app></div><script type=text/javascript src=/static/js/manifest.ee87253244897e08bdce.js></script><script type=text/javascript src=/static/js/vendor.50cd70f77f559bfe1f27.js></script><script type=text/javascript src=/static/js/app.fefccf252cac9e1310ea.js></script></body></html>
|
Binary file not shown.
File diff suppressed because one or more lines are too long
BIN
priv/static/static/css/app.67f64792f89a96e59442c437c7ded0b3.css
Normal file
BIN
priv/static/static/css/app.67f64792f89a96e59442c437c7ded0b3.css
Normal file
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
BIN
priv/static/static/js/app.fefccf252cac9e1310ea.js
Normal file
BIN
priv/static/static/js/app.fefccf252cac9e1310ea.js
Normal file
Binary file not shown.
BIN
priv/static/static/js/app.fefccf252cac9e1310ea.js.map
Normal file
BIN
priv/static/static/js/app.fefccf252cac9e1310ea.js.map
Normal file
Binary file not shown.
Binary file not shown.
BIN
priv/static/static/js/manifest.ee87253244897e08bdce.js
Normal file
BIN
priv/static/static/js/manifest.ee87253244897e08bdce.js
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
priv/static/static/js/vendor.50cd70f77f559bfe1f27.js.map
Normal file
BIN
priv/static/static/js/vendor.50cd70f77f559bfe1f27.js.map
Normal file
Binary file not shown.
Binary file not shown.
|
@ -22,7 +22,8 @@ test "with credentials", %{conn: conn, user: user} do
|
||||||
|> with_credentials(user.nickname, "test")
|
|> with_credentials(user.nickname, "test")
|
||||||
|> post("/api/account/verify_credentials.json")
|
|> post("/api/account/verify_credentials.json")
|
||||||
|
|
||||||
assert json_response(conn, 200) == UserView.render("show.json", %{user: user})
|
assert response = json_response(conn, 200)
|
||||||
|
assert response == UserView.render("show.json", %{user: user, token: response["token"]})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue