logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

user_socket.ex (1586B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.UserSocket do
  5. use Phoenix.Socket
  6. alias Pleroma.User
  7. ## Channels
  8. # channel "room:*", Pleroma.Web.RoomChannel
  9. channel("chat:*", Pleroma.Web.ShoutChannel)
  10. # Socket params are passed from the client and can
  11. # be used to verify and authenticate a user. After
  12. # verification, you can put default assigns into
  13. # the socket that will be set for all channels, ie
  14. #
  15. # {:ok, assign(socket, :user_id, verified_user_id)}
  16. #
  17. # To deny connection, return `:error`.
  18. #
  19. # See `Phoenix.Token` documentation for examples in
  20. # performing token verification on connect.
  21. def connect(%{"token" => token}, socket) do
  22. with true <- Pleroma.Config.get([:shout, :enabled]),
  23. {:ok, user_id} <- Phoenix.Token.verify(socket, "user socket", token, max_age: 84_600),
  24. %User{} = user <- Pleroma.User.get_cached_by_id(user_id) do
  25. {:ok, assign(socket, :user_name, user.nickname)}
  26. else
  27. _e -> :error
  28. end
  29. end
  30. # Socket id's are topics that allow you to identify all sockets for a given user:
  31. #
  32. # def id(socket), do: "user_socket:#{socket.assigns.user_id}"
  33. #
  34. # Would allow you to broadcast a "disconnect" event and terminate
  35. # all active sockets and channels for a given user:
  36. #
  37. # Pleroma.Web.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{})
  38. #
  39. # Returning `nil` makes this socket anonymous.
  40. def id(_socket), do: nil
  41. end