use more correct way of propagating user through models

This commit is contained in:
byte[] 2019-11-18 19:33:27 -05:00
parent 889e2349d0
commit 3ee6c07609
8 changed files with 29 additions and 34 deletions

View file

@ -40,10 +40,10 @@ defmodule Philomena.Comments do
{:error, %Ecto.Changeset{}}
"""
def create_comment(image, user, attributes, params \\ %{}) do
def create_comment(image, attribution, params \\ %{}) do
comment =
struct(Comment, [image_id: image.id] ++ attributes)
|> Comment.creation_changeset(user, params)
Ecto.build_assoc(image, :comments)
|> Comment.creation_changeset(params, attribution)
image_query =
Image
@ -53,7 +53,7 @@ defmodule Philomena.Comments do
|> Multi.insert(:comment, comment)
|> Multi.update_all(:image, image_query, inc: [comments_count: 1])
|> Multi.run(:subscribe, fn _repo, _changes ->
Images.create_subscription(image, user)
Images.create_subscription(image, attribution[:user])
end)
|> Repo.transaction()
end

View file

@ -31,12 +31,13 @@ defmodule Philomena.Comments.Comment do
end
@doc false
def creation_changeset(comment, user, attrs) do
def creation_changeset(comment, attrs, attribution) do
comment
|> cast(attrs, [:body, :anonymous])
|> set_name_at_post_time(user)
|> validate_required([:body])
|> validate_length(:body, min: 1, max: 300_000, count: :bytes)
|> change(attribution)
|> put_name_at_post_time()
end
def changeset(comment, attrs) do
@ -47,8 +48,8 @@ defmodule Philomena.Comments.Comment do
|> validate_length(:edit_reason, max: 70, count: :bytes)
end
def set_name_at_post_time(changeset, nil), do: changeset
def set_name_at_post_time(changeset, %{name: name}) do
change(changeset, name_at_post_time: name)
end
defp put_name_at_post_time(%{changes: %{user: %{data: %{name: name}}}} = changeset),
do: change(changeset, name_at_post_time: name)
defp put_name_at_post_time(changeset),
do: changeset
end

View file

@ -15,10 +15,12 @@ defmodule Philomena.Images.Image do
alias Philomena.Users.User
alias Philomena.Images.Tagging
alias Philomena.Galleries
alias Philomena.Comments.Comment
schema "images" do
belongs_to :user, User
belongs_to :deleter, User, source: :deleted_by_id
has_many :comments, Comment
has_many :upvotes, ImageVote, where: [up: true]
has_many :downvotes, ImageVote, where: [up: false]
has_many :faves, ImageFave

View file

@ -41,7 +41,7 @@ defmodule Philomena.Posts do
{:error, %Ecto.Changeset{}}
"""
def create_post(topic, user, attributes, params \\ %{}) do
def create_post(topic, attributes, params \\ %{}) do
topic_query =
Topic
|> where(id: ^topic.id)
@ -61,7 +61,7 @@ defmodule Philomena.Posts do
|> repo.one()
Ecto.build_assoc(topic, :posts, [topic_position: (last_position || -1) + 1] ++ attributes)
|> Post.creation_changeset(params, user)
|> Post.creation_changeset(params, attributes)
|> repo.insert()
end)
|> Multi.run(:update_topic, fn repo, %{post: %{id: post_id}} ->
@ -77,7 +77,7 @@ defmodule Philomena.Posts do
{:ok, count}
end)
|> Multi.run(:subscribe, fn _repo, _changes ->
Topics.create_subscription(topic, user)
Topics.create_subscription(topic, attributes[:user])
end)
|> Repo.isolated_transaction(:serializable)
end

View file

@ -40,16 +40,17 @@ defmodule Philomena.Posts.Post do
end
@doc false
def creation_changeset(post, attrs, user) do
def creation_changeset(post, attrs, attribution) do
post
|> cast(attrs, [:body, :anonymous])
|> set_name_at_post_time(user)
|> validate_required([:body])
|> validate_length(:body, min: 1, max: 300_000, count: :bytes)
|> change(attribution)
|> put_name_at_post_time()
end
def set_name_at_post_time(changeset, nil), do: changeset
def set_name_at_post_time(changeset, %{name: name}) do
change(changeset, name_at_post_time: name)
end
defp put_name_at_post_time(%{changes: %{user: %{data: %{name: name}}}} = changeset),
do: change(changeset, name_at_post_time: name)
defp put_name_at_post_time(changeset),
do: changeset
end

View file

@ -63,9 +63,8 @@ defmodule PhilomenaWeb.Image.CommentController do
def create(conn, %{"comment" => comment_params}) do
attributes = conn.assigns.attributes
image = conn.assigns.image
user = conn.assigns.current_user
case Comments.create_comment(image, user, attributes, comment_params) do
case Comments.create_comment(image, attributes, comment_params) do
{:ok, %{comment: comment}} ->
Comments.notify_comment(comment)
Comments.reindex_comment(comment)

View file

@ -14,9 +14,8 @@ defmodule PhilomenaWeb.Topic.PostController do
attributes = conn.assigns.attributes
forum = conn.assigns.forum
topic = conn.assigns.topic
user = conn.assigns.current_user
case Posts.create_post(topic, user, attributes, post_params) do
case Posts.create_post(topic, attributes, post_params) do
{:ok, %{post: post}} ->
Posts.notify_post(post)
Posts.reindex_post(post)

View file

@ -19,13 +19,15 @@ defmodule PhilomenaWeb.UserAttributionPlug do
def call(conn, _opts) do
{:ok, remote_ip} = EctoNetwork.INET.cast(conn.remote_ip)
conn = Conn.fetch_cookies(conn)
user = Pow.Plug.current_user(conn)
attributes =
[
ip: remote_ip,
fingerprint: conn.cookies["_ses"],
referrer: conn.assigns.referrer,
user_agent: user_agent(conn),
user_id: user_id(conn)
user: user,
user_agent: user_agent(conn)
]
conn
@ -38,13 +40,4 @@ defmodule PhilomenaWeb.UserAttributionPlug do
_ -> nil
end
end
defp user_id(conn) do
user = Pow.Plug.current_user(conn)
case user do
%{id: id} -> id
_ -> nil
end
end
end