Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/groupher_server/cms/article_collect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule GroupherServer.CMS.ArticleCollect do
import GroupherServer.CMS.Helper.Macros
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]

alias GroupherServer.{Accounts, CMS}
alias GroupherServer.Accounts
alias Accounts.{User, CollectFolder}

@article_threads get_config(:article, :article_threads)
Expand Down
2 changes: 1 addition & 1 deletion lib/groupher_server/cms/article_upvote.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule GroupherServer.CMS.ArticleUpvote do
import GroupherServer.CMS.Helper.Macros
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]

alias GroupherServer.{Accounts, CMS}
alias GroupherServer.Accounts
alias Accounts.User

@article_threads get_config(:article, :article_threads)
Expand Down
2 changes: 1 addition & 1 deletion lib/groupher_server/cms/article_user_emotion.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule GroupherServer.CMS.ArticleUserEmotion do
import GroupherServer.CMS.Helper.Macros
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]

alias GroupherServer.{Accounts, CMS}
alias GroupherServer.Accounts

@supported_emotions get_config(:article, :supported_emotions)
@article_threads get_config(:article, :article_threads)
Expand Down
56 changes: 16 additions & 40 deletions lib/groupher_server/cms/delegates/article_comment_action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
alias GroupherServer.{Accounts, CMS, Repo}

alias Accounts.User

alias CMS.{
ArticleComment,
ArticlePinnedComment,
ArticleCommentUpvote,
ArticleCommentReply,
Community,
# TODO: remove spec type
Post,
Job
}
alias CMS.{ArticleComment, ArticlePinnedComment, ArticleCommentUpvote, ArticleCommentReply}

alias Ecto.Multi

Expand Down Expand Up @@ -250,45 +240,25 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
{:ok, parent_comment}
end

defp get_article(%ArticleComment{post_id: post_id} = comment) when not is_nil(post_id) do
with {:ok, article} <- ORM.find(Post, comment.post_id, preload: [author: :user]) do
{:post, article}
end
end

defp get_article(%ArticleComment{job_id: job_id} = comment) when not is_nil(job_id) do
with {:ok, article} <- ORM.find(Job, comment.job_id, preload: [author: :user]) do
{:job, article}
end
end

defp get_article(%ArticleComment{repo_id: repo_id} = comment) when not is_nil(repo_id) do
with {:ok, article} <- ORM.find(CMS.Repo, comment.repo_id, preload: [author: :user]) do
{:repo, article}
defp get_article(%ArticleComment{} = comment) do
with article_thread <- find_comment_article_thread(comment),
{:ok, info} <- match(article_thread),
article_id <- Map.get(comment, info.foreign_key),
{:ok, article} <- ORM.find(info.model, article_id, preload: [author: :user]) do
{article_thread, article}
end
end

@spec get_full_comment(String.t()) :: {:ok, T.article_info()} | {:error, nil}
defp get_full_comment(comment_id) do
query = from(c in ArticleComment, where: c.id == ^comment_id, preload: ^@article_threads)

with {:ok, comment} <- Repo.one(query) |> done() do
extract_article_info(comment)
with {:ok, comment} <- Repo.one(query) |> done(),
article_thread <- find_comment_article_thread(comment) do
do_extract_article_info(article_thread, Map.get(comment, article_thread))
end
end

defp extract_article_info(%ArticleComment{post: %Post{} = post}) when not is_nil(post) do
do_extract_article_info(:post, post)
end

defp extract_article_info(%ArticleComment{job: %Job{} = job}) when not is_nil(job) do
do_extract_article_info(:job, job)
end

defp extract_article_info(%ArticleComment{repo: %CMS.Repo{} = repo}) when not is_nil(repo) do
do_extract_article_info(:repo, repo)
end

@spec do_extract_article_info(T.article_thread(), T.article_common()) :: {:ok, T.article_info()}
defp do_extract_article_info(thread, article) do
with {:ok, article_with_author} <- Repo.preload(article, author: :user) |> done(),
Expand All @@ -306,6 +276,12 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
end
end

defp find_comment_article_thread(%ArticleComment{} = comment) do
@article_threads
|> Enum.filter(&Map.get(comment, :"#{&1}_id"))
|> List.first()
end

# used in replies mode, for those reply to other user in replies box (for frontend)
# 用于回复模式,指代这条回复是回复“回复列表其他人的” (方便前端展示)
defp update_reply_to_others_state(parent_comment, replying_comment, replyed_comment) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do
end

describe "[basic article comment replies]" do
@tag :wip2
test "exsit comment can be reply", ~m(post user user2)a do
parent_content = "parent comment"
reply_content = "reply comment"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedPosts do
assert results["entries"] |> Enum.any?(&(&1["id"] == to_string(post.id)))
end

@tag :wip2
test "filter sort should have default :desc_inserted", ~m(guest_conn)a do
variables = %{filter: %{}}
results = guest_conn |> query_result(@query, variables, "pagedPosts")
Expand Down