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
7 changes: 3 additions & 4 deletions lib/groupher_server/cms/delegates/article_curd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
module_to_atom: 1,
get_config: 2,
ensure: 2,
module_to_upcase: 1,
thread_of_article: 1
module_to_upcase: 1
]

import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 2]
import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 2, thread_of: 1]
import Helper.ErrorCode
import ShortMaps

Expand Down Expand Up @@ -353,7 +352,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
"""
def delete_article(article, reason \\ @remove_article_hint) do
article = Repo.preload(article, [:communities, [author: :user]])
{:ok, thread} = thread_of_article(article)
{:ok, thread} = thread_of(article)

Multi.new()
|> Multi.run(:delete_article, fn _, _ ->
Expand Down
19 changes: 12 additions & 7 deletions lib/groupher_server/cms/delegates/cited_artiment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ defmodule GroupherServer.CMS.Delegate.CitedArtiment do
import Ecto.Query, warn: false

import Helper.Utils,
only: [done: 1, get_config: 2, thread_of_article: 1, atom_values_to_upcase: 1, to_upcase: 1]
only: [
done: 1,
get_config: 2,
atom_values_to_upcase: 1,
to_upcase: 1
]

import GroupherServer.CMS.Delegate.Helper, only: [thread_of: 1, article_of: 1]

import GroupherServer.CMS.Helper.Matcher
import ShortMaps
Expand Down Expand Up @@ -45,7 +52,7 @@ defmodule GroupherServer.CMS.Delegate.CitedArtiment do
end

def batch_delete_by(article) do
with {:ok, thread} <- thread_of_article(article),
with {:ok, thread} <- thread_of(article),
{:ok, info} <- match(thread) do
thread = to_upcase(thread)

Expand Down Expand Up @@ -104,9 +111,9 @@ defmodule GroupherServer.CMS.Delegate.CitedArtiment do
defp shape(%CitedArtiment{comment_id: comment_id} = cited) when not is_nil(comment_id) do
%{block_linker: block_linker, comment: comment, inserted_at: inserted_at} = cited

comment_thread = comment.thread |> String.downcase() |> String.to_atom()
article = comment |> Map.get(comment_thread)
article_thread = thread_to_atom(article.meta.thread)
{:ok, article} = article_of(comment)
{:ok, article_thread} = thread_of(article)

user = comment.author |> Map.take([:login, :nickname, :avatar])

article
Expand Down Expand Up @@ -144,6 +151,4 @@ defmodule GroupherServer.CMS.Delegate.CitedArtiment do
defp citing_thread(cited) do
@article_threads |> Enum.find(fn thread -> not is_nil(Map.get(cited, :"#{thread}_id")) end)
end

defp thread_to_atom(thread), do: thread |> String.downcase() |> String.to_atom()
end
53 changes: 36 additions & 17 deletions lib/groupher_server/cms/delegates/comment_action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
"""
import Ecto.Query, warn: false
import Helper.Utils, only: [done: 1, strip_struct: 1, get_config: 2, ensure: 2]
import GroupherServer.CMS.Delegate.Helper, only: [article_of: 1, thread_of: 1]

import Helper.ErrorCode

import GroupherServer.CMS.Delegate.CommentCurd,
Expand Down Expand Up @@ -40,26 +42,25 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
{:ok, info} <- match(full_comment.thread) do
Multi.new()
|> Multi.run(:checked_pined_comments_count, fn _, _ ->
{:ok, pined_comments_count} =
pined_comments_query =
from(p in PinnedComment,
where: field(p, ^info.foreign_key) == ^full_comment.article.id
)
|> ORM.count()

case pined_comments_count >= @pinned_comment_limit do
true -> {:error, "max #{@pinned_comment_limit} pinned comment for each article"}
false -> {:ok, :pass}
with {:ok, pined_comments_count} <- ORM.count(pined_comments_query) do
case pined_comments_count >= @pinned_comment_limit do
true -> {:error, "max #{@pinned_comment_limit} pinned comment for each article"}
false -> {:ok, :pass}
end
end
end)
|> Multi.run(:update_comment_flag, fn _, _ ->
ORM.update(comment, %{is_pinned: true})
end)
|> Multi.run(:add_pined_comment, fn _, _ ->
PinnedComment
|> ORM.create(
%{comment_id: comment.id}
|> Map.put(info.foreign_key, full_comment.article.id)
)
attrs = %{comment_id: comment.id} |> Map.put(info.foreign_key, full_comment.article.id)

PinnedComment |> ORM.create(attrs)
end)
|> Repo.transaction()
|> result()
Expand All @@ -81,21 +82,19 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
end

@doc "fold a comment"
def fold_comment(%Comment{} = comment, %User{} = _user) do
comment |> ORM.update(%{is_folded: true})
end
def fold_comment(%Comment{} = comment, %User{} = _user), do: do_fold_comment(comment, true)

@doc "fold a comment"
@doc "fold a comment by id"
def fold_comment(comment_id, %User{} = _user) do
with {:ok, comment} <- ORM.find(Comment, comment_id) do
comment |> ORM.update(%{is_folded: true})
do_fold_comment(comment, true)
end
end

@doc "unfold a comment"
def unfold_comment(comment_id, %User{} = _user) do
with {:ok, comment} <- ORM.find(Comment, comment_id) do
comment |> ORM.update(%{is_folded: false})
do_fold_comment(comment, false)
end
end

Expand Down Expand Up @@ -249,6 +248,26 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
end
end

# do (un)fold and update folded count in article meta
defp do_fold_comment(%Comment{} = comment, is_folded) when is_boolean(is_folded) do
Multi.new()
|> Multi.run(:fold_comment, fn _, _ ->
comment |> ORM.update(%{is_folded: is_folded})
end)
|> Multi.run(:update_article_fold_count, fn _, _ ->
{:ok, article} = article_of(comment)
{:ok, article_thread} = thread_of(article)

{:ok, %{total_count: total_count}} =
CMS.paged_folded_comments(article_thread, article.id, %{page: 1, size: 1})

meta = article.meta |> Map.put(:folded_comment_count, total_count)
article |> ORM.update_meta(meta)
end)
|> Repo.transaction()
|> result()
end

defp update_article_author_upvoted_info(%Comment{} = comment, user_id) do
with {:ok, article} = get_full_comment(comment.id) do
is_article_author_upvoted = article.author.id == user_id
Expand Down Expand Up @@ -367,9 +386,9 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
defp result({:ok, %{create_comment: result}}), do: {:ok, result}
defp result({:ok, %{add_reply_to: result}}), do: {:ok, result}
defp result({:ok, %{upvote_comment_done: result}}), do: {:ok, result}
defp result({:ok, %{fold_comment_report_too_many: result}}), do: {:ok, result}
defp result({:ok, %{update_comment_flag: result}}), do: {:ok, result}
defp result({:ok, %{delete_comment: result}}), do: {:ok, result}
defp result({:ok, %{fold_comment: result}}), do: {:ok, result}

defp result({:error, :create_comment, result, _steps}) do
raise_error(:create_comment, result)
Expand Down
26 changes: 14 additions & 12 deletions lib/groupher_server/cms/delegates/comment_curd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
import Helper.Utils, only: [done: 1, ensure: 2]
import Helper.ErrorCode

import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 3]
import GroupherServer.CMS.Delegate.Helper,
only: [mark_viewer_emotion_states: 3, article_of: 1, thread_of: 1]

import GroupherServer.CMS.Helper.Matcher
import ShortMaps

alias Helper.Types, as: T
alias Helper.{Later, ORM, QueryBuilder, Converter}
alias GroupherServer.{Accounts, CMS, Repo}
alias CMS.Model.Post
alias CMS.Delegate.Hooks

alias Accounts.Model.User
alias CMS.Model.{Comment, PinnedComment, Embeds}
alias CMS.Model.{Post, Comment, PinnedComment, Embeds}

alias CMS.Delegate.Hooks
alias Helper.{Later, ORM, QueryBuilder, Converter}

alias Ecto.Multi

@max_participator_count Comment.max_participator_count()
Expand Down Expand Up @@ -259,10 +262,7 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
end

# add participator to article-like(Post, Job ...) and update count
def add_participant_to_article(
%{comments_participants: participants} = article,
%User{} = user
) do
def add_participant_to_article(%{comments_participants: participants} = article, %User{} = user) do
total_participants = participants |> List.insert_at(0, user) |> Enum.uniq_by(& &1.id)

latest_participants = total_participants |> Enum.slice(0, @max_participator_count)
Expand All @@ -280,10 +280,12 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
# update comment's parent article's comments total count
@spec update_comments_count(Comment.t(), :inc | :dec) :: Comment.t()
def update_comments_count(%Comment{} = comment, opt) do
with {:ok, article_info} <- match(:comment_article, comment),
{:ok, article} <- ORM.find(article_info.model, article_info.id) do
with {:ok, article} <- article_of(comment),
{:ok, article_thread} <- thread_of(article) do
foreign_key = :"#{article_thread}_id"

{:ok, cur_count} =
from(c in Comment, where: field(c, ^article_info.foreign_key) == ^article_info.id)
from(c in Comment, where: field(c, ^foreign_key) == ^article.id)
|> ORM.count()

# dec 是 comment 还没有删除的时候的操作,和 inc 不同
Expand Down
8 changes: 4 additions & 4 deletions lib/groupher_server/cms/delegates/document.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule GroupherServer.CMS.Delegate.Document do
CURD operation on post/job ...
"""
import Ecto.Query, warn: false
import Helper.Utils, only: [done: 1, thread_of_article: 2, get_config: 2]
import Helper.Utils, only: [done: 1, thread_of: 2, get_config: 2]

import Helper.ErrorCode
import ShortMaps
Expand All @@ -28,7 +28,7 @@ defmodule GroupherServer.CMS.Delegate.Document do

# for create artilce step in Multi.new
def create(article, %{body: body} = attrs) do
with {:ok, article_thread} <- thread_of_article(article, :upcase),
with {:ok, article_thread} <- thread_of(article, :upcase),
{:ok, parsed} <- Converter.Article.parse_body(body) do
attrs = Map.take(parsed, [:body, :body_html])

Expand Down Expand Up @@ -59,7 +59,7 @@ defmodule GroupherServer.CMS.Delegate.Document do
update both article and thread document
"""
def update(article, %{body: body} = attrs) when not is_nil(body) do
with {:ok, article_thread} <- thread_of_article(article, :upcase),
with {:ok, article_thread} <- thread_of(article, :upcase),
{:ok, article_doc} <- find_article_document(article_thread, article),
{:ok, thread_doc} <- find_thread_document(article_thread, article),
{:ok, parsed} <- Converter.Article.parse_body(body) do
Expand All @@ -82,7 +82,7 @@ defmodule GroupherServer.CMS.Delegate.Document do

# 只更新 title 的情况
def update(article, %{title: title} = attrs) do
with {:ok, article_thread} <- thread_of_article(article, :upcase),
with {:ok, article_thread} <- thread_of(article, :upcase),
{:ok, article_doc} <- find_article_document(article_thread, article) do
article_doc |> ORM.update(%{title: attrs.title})
end
Expand Down
26 changes: 21 additions & 5 deletions lib/groupher_server/cms/delegates/helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ defmodule GroupherServer.CMS.Delegate.Helper do
@moduledoc """
helpers for GroupherServer.CMS.Delegate
"""
import Helper.Utils, only: [get_config: 2, done: 1, strip_struct: 1]
import Ecto.Query, warn: false
import GroupherServer.CMS.Helper.Matcher
import ShortMaps
import Helper.Utils, only: [get_config: 2, done: 1, strip_struct: 1]

alias Helper.{ORM, QueryBuilder}
alias GroupherServer.{Accounts, Repo, CMS}
Expand Down Expand Up @@ -42,13 +42,29 @@ defmodule GroupherServer.CMS.Delegate.Helper do
end

@doc "get parent article of a comment"
def parent_article_of(%Comment{} = comment) do
article_thread = comment.thread |> String.downcase() |> String.to_atom()
def article_of(%Comment{} = comment) do
with {:ok, article_thread} <- thread_of(comment) do
comment |> Repo.preload(article_thread) |> Map.get(article_thread) |> done
end
end

def article_of(_), do: {:error, "only support comment"}

comment |> Repo.preload(article_thread) |> Map.get(article_thread) |> done
# get thread of comment
def thread_of(%Comment{thread: thread}) do
thread |> String.downcase() |> String.to_atom() |> done
end

def parent_article_of(_), do: {:error, "only support comment"}
# get thread of article
def thread_of(%{meta: %{thread: thread}}) do
thread |> String.downcase() |> String.to_atom() |> done
end

def thread_of(_), do: {:error, "invalid article"}

def thread_of(%{meta: %{thread: thread}}, :upcase) do
thread |> to_string |> String.upcase() |> done
end

#######
# emotion related
Expand Down
9 changes: 5 additions & 4 deletions lib/groupher_server/cms/delegates/hooks/cite.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Cite do
"""

import Ecto.Query, warn: false
import Helper.Utils, only: [get_config: 2, thread_of_article: 1]
import Helper.Utils, only: [get_config: 2]

import GroupherServer.CMS.Helper.Matcher
import GroupherServer.CMS.Delegate.Helper, only: [preload_author: 1]
import GroupherServer.CMS.Delegate.Helper, only: [preload_author: 1, thread_of: 1]
import GroupherServer.CMS.Delegate.Hooks.Helper, only: [merge_same_block_linker: 2]

import Helper.ErrorCode
Expand Down Expand Up @@ -221,7 +222,7 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Cite do
# cite article in article
# 文章之间相互引用
defp shape(article, %{type: :article, artiment: cited}, block_id) do
{:ok, thread} = thread_of_article(article)
{:ok, thread} = thread_of(article)
{:ok, info} = match(thread)

%{
Expand All @@ -241,7 +242,7 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Cite do
# cite comment in article
# 文章中引用评论
defp shape(article, %{type: :comment, artiment: cited}, block_id) do
{:ok, thread} = thread_of_article(article)
{:ok, thread} = thread_of(article)
{:ok, info} = match(thread)

%{
Expand Down
6 changes: 3 additions & 3 deletions lib/groupher_server/cms/delegates/hooks/mention.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Mention do
parse and fmt(see shape function) mentions to Delivery module
"""
import Ecto.Query, warn: false
import Helper.Utils, only: [get_config: 2, thread_of_article: 1]
import Helper.Utils, only: [get_config: 2]

import GroupherServer.CMS.Delegate.Helper, only: [preload_author: 1, author_of: 1]
import GroupherServer.CMS.Delegate.Helper, only: [preload_author: 1, author_of: 1, thread_of: 1]
import GroupherServer.CMS.Delegate.Hooks.Helper, only: [merge_same_block_linker: 2]

alias GroupherServer.{Accounts, CMS, Delivery, Repo}
Expand Down Expand Up @@ -91,7 +91,7 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Mention do
end

defp shape(article, to_user_id, block_id) do
{:ok, thread} = thread_of_article(article)
{:ok, thread} = thread_of(article)

%{
thread: thread,
Expand Down
Loading