add firehose API

This commit is contained in:
byte[] 2020-06-12 12:56:11 -04:00
parent 2a7dc2fe88
commit 8ef94e912b
9 changed files with 53 additions and 6 deletions

View file

@ -63,7 +63,8 @@ config :canary,
config :philomena, PhilomenaWeb.Endpoint,
url: [host: "localhost"],
secret_key_base: "xZYTon09JNRrj8snd7KL31wya4x71jmo5aaSSRmw1dGjWLRmEwWMTccwxgsGFGjM",
render_errors: [view: PhilomenaWeb.ErrorView, accepts: ~w(html json)]
render_errors: [view: PhilomenaWeb.ErrorView, accepts: ~w(html json)],
pubsub_server: Philomena.PubSub
config :phoenix, :template_engines,
slim: PhoenixSlime.Engine,

View file

@ -70,6 +70,7 @@ server {
proxy_temp_file_write_size 64k;
# Configuration for Phoenix WS
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}

View file

@ -0,0 +1,16 @@
defmodule PhilomenaWeb.FirehoseChannel do
use Phoenix.Channel
def join("firehose", _params, socket) do
{:ok, socket}
end
def join(_room, _params, _socket) do
{:error, %{reason: "not_found"}}
end
# Don't allow the connected client to send any messages to the socket
def handle_in(message, _params, socket) do
{:stop, :shutdown, socket}
end
end

View file

@ -1,8 +1,8 @@
defmodule PhilomenaWeb.UserSocket do
use Phoenix.Socket
## Channels
# channel "room:*", PhilomenaWeb.RoomChannel
# Channels
channel "firehose", PhilomenaWeb.FirehoseChannel
# Socket params are passed from the client and can
# be used to verify and authenticate a user. After

View file

@ -63,6 +63,12 @@ defmodule PhilomenaWeb.Image.CommentController do
case Comments.create_comment(image, attributes, comment_params) do
{:ok, %{comment: comment}} ->
PhilomenaWeb.Endpoint.broadcast!(
"firehose",
"comment:create",
PhilomenaWeb.Api.Json.CommentView.render("show.json", %{comment: comment})
)
Comments.notify_comment(comment)
Comments.reindex_comment(comment)
Images.reindex_image(conn.assigns.image)

View file

@ -12,7 +12,7 @@ defmodule PhilomenaWeb.Image.SourceController do
plug PhilomenaWeb.CaptchaPlug
plug PhilomenaWeb.UserAttributionPlug
plug PhilomenaWeb.CanaryMapPlug, update: :edit_metadata
plug :load_and_authorize_resource, model: Image, id_name: "image_id"
plug :load_and_authorize_resource, model: Image, id_name: "image_id", preload: [:tags, :user]
def update(conn, %{"image" => image_params}) do
attributes = conn.assigns.attributes
@ -21,6 +21,12 @@ defmodule PhilomenaWeb.Image.SourceController do
case Images.update_source(image, attributes, image_params) do
{:ok, %{image: image}} ->
PhilomenaWeb.Endpoint.broadcast!(
"firehose",
"image:update",
PhilomenaWeb.Api.Json.ImageView.render("show.json", %{image: image, interactions: []})
)
changeset = Images.change_image(image)
source_change_count =

View file

@ -14,7 +14,7 @@ defmodule PhilomenaWeb.Image.TagController do
plug PhilomenaWeb.CaptchaPlug
plug PhilomenaWeb.UserAttributionPlug
plug PhilomenaWeb.CanaryMapPlug, update: :edit_metadata
plug :load_and_authorize_resource, model: Image, id_name: "image_id"
plug :load_and_authorize_resource, model: Image, id_name: "image_id", preload: [:tags, :user]
def update(conn, %{"image" => image_params}) do
attributes = conn.assigns.attributes
@ -22,6 +22,12 @@ defmodule PhilomenaWeb.Image.TagController do
case Images.update_tags(image, attributes, image_params) do
{:ok, %{image: {image, added_tags, removed_tags}}} ->
PhilomenaWeb.Endpoint.broadcast!(
"firehose",
"image:update",
PhilomenaWeb.Api.Json.ImageView.render("show.json", %{image: image, interactions: []})
)
Comments.reindex_comments(image)
Images.reindex_image(image)
Tags.reindex_tags(added_tags ++ removed_tags)

View file

@ -108,6 +108,12 @@ defmodule PhilomenaWeb.ImageController do
case Images.create_image(attributes, image_params) do
{:ok, %{image: image}} ->
PhilomenaWeb.Endpoint.broadcast!(
"firehose",
"image:create",
PhilomenaWeb.Api.Json.ImageView.render("show.json", %{image: image, interactions: []})
)
conn
|> put_flash(:info, "Image created successfully.")
|> redirect(to: Routes.image_path(conn, :show, image))

View file

@ -45,6 +45,12 @@ defmodule PhilomenaWeb.Api.Json.ImageView do
end
def render("image.json", %{conn: conn, image: %{hidden_from_users: false} = image}) do
result = render_one(image, PhilomenaWeb.Api.Json.ImageView, "image.json", %{image: image})
Map.put(result, :spoilered, ImageView.filter_or_spoiler_hits?(conn, image))
end
def render("image.json", %{image: %{hidden_from_users: false} = image}) do
%{
id: image.id,
created_at: image.created_at,
@ -74,7 +80,6 @@ defmodule PhilomenaWeb.Api.Json.ImageView do
source_url: image.source_url,
view_url: ImageView.pretty_url(image, false, false),
representations: ImageView.thumb_urls(image, false),
spoilered: ImageView.filter_or_spoiler_hits?(conn, image),
thumbnails_generated: image.thumbnails_generated,
processed: image.processed,
deletion_reason: nil,