Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

refactor: mutation debug with frontend 1#434

Merged
mydearxym merged 9 commits intodevfrommutation-debug-1
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes fromall 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
4 changes: 4 additions & 0 deletionsconfig/mock.exs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,10 @@ config :groupher_server, GroupherServerWeb.Endpoint,
check_origin: false,
watchers: []

config :groupher_server, Helper.Guardian,
issuer: "groupher_server",
secret_key: "hello"

config :logger, :console, format: "[$level] $message\n"

config :phoenix, :stacktrace_depth, 20
Expand Down
2 changes: 2 additions & 0 deletionslib/groupher_server/cms/cms.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -143,6 +143,8 @@ defmodule GroupherServer.CMS do
defdelegate undo_emotion_to_article(thread, article_id, args, user), to: ArticleEmotion

# Comment CURD

defdelegate update_user_in_comments_participants(user), to: CommentCurd
defdelegate paged_comments(thread, article_id, filters, mode), to: CommentCurd
defdelegate paged_comments(thread, article_id, filters, mode, user), to: CommentCurd

Expand Down
12 changes: 9 additions & 3 deletionslib/groupher_server/cms/delegates/abuse_report.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,6 +5,8 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
import Ecto.Query, warn: false
import Helper.Utils, only: [done: 1, strip_struct: 1, get_config: 2]

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

import GroupherServer.CMS.Helper.Matcher
import ShortMaps

Expand DownExpand Up@@ -187,6 +189,9 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
do: CMS.fold_comment(comment, user),
else: {:ok, comment}
end)
|> Multi.run(:sync_embed_replies, fn _, %{update_report_meta: comment} ->
sync_embed_replies(comment)
end)
|> Repo.transaction()
|> result()
end
Expand DownExpand Up@@ -258,10 +263,10 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
_ ->
report_cases = report.report_cases |> Enum.reject(&(&1.user.login == user.login))

changes = %{report_cases_count: length(report_cases)}

report
|> Ecto.Changeset.change(%{report_cases_count: length(report_cases)})
|> Ecto.Changeset.put_embed(:report_cases, report_cases)
|> Repo.update()
|> ORM.update_embed(:report_cases, report_cases, changes)
end
end
end
Expand DownExpand Up@@ -379,6 +384,7 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
|> Map.merge(%{thread: article_thread})
end

defp result({:ok, %{sync_embed_replies: result}}), do: result |> done()
defp result({:ok, %{update_report_meta: result}}), do: result |> done()
defp result({:ok, %{update_content_reported_flag: result}}), do: result |> done()

Expand Down
10 changes: 2 additions & 8 deletionslib/groupher_server/cms/delegates/article_collect.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -114,10 +114,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCollect do
def set_collect_folder(%ArticleCollect{} = collect, folder) do
collect_folders = (collect.collect_folders ++ [folder]) |> Enum.uniq()

collect
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_embed(:collect_folders, collect_folders)
|> Repo.update()
ORM.update_embed(collect, :collect_folders, collect_folders)
end

def undo_set_collect_folder(%ArticleCollect{} = collect, folder) do
Expand All@@ -129,10 +126,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCollect do
{:ok, :pass}

_ ->
collect
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_embed(:collect_folders, collect_folders)
|> Repo.update()
ORM.update_embed(collect, :collect_folders, collect_folders)
end
end

Expand Down
19 changes: 11 additions & 8 deletionslib/groupher_server/cms/delegates/article_upvote.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,6 +5,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleUpvote do
import GroupherServer.CMS.Helper.Matcher
import Ecto.Query, warn: false
import Helper.Utils, only: [done: 1]
import Helper.ErrorCode

import GroupherServer.CMS.Delegate.Helper,
only: [
Expand DownExpand Up@@ -33,22 +34,24 @@ defmodule GroupherServer.CMS.Delegate.ArticleUpvote do
with {:ok, info} <- match(thread),
{:ok, article} <- ORM.find(info.model, article_id, preload: [author: :user]) do
Multi.new()
|> Multi.run(:inc_article_upvotes_count, fn _, _ ->
|> Multi.run(:update_upvotes_count, fn _, _ ->
update_article_reactions_count(info, article, :upvotes_count, :inc)
end)
|> Multi.run(:update_article_reaction_user_list, fn _,_ ->
|> Multi.run(:update_reaction_user_list, fn _,%{update_upvotes_count: article} ->
update_article_reaction_user_list(:upvot, article, from_user, :add)
end)
|> Multi.run(:add_achievement, fn _, _ ->
achiever_id = article.author.user_id
Accounts.achieve(%User{id: achiever_id}, :inc, :upvote)
end)
|> Multi.run(:create_upvote, fn _,_ ->
|> Multi.run(:create_upvote, fn _,%{update_reaction_user_list: article} ->
thread = thread |> to_string |> String.upcase()
args = Map.put(%{user_id: user_id, thread: thread}, info.foreign_key, article.id)

with {:ok, _} <- ORM.create(ArticleUpvote, args) do
ORM.find(info.model, article.id)
article |> done
else
_ -> {:error, [message: "viewer already upvoted", code: ecode(:already_upvoted)]}
end
end)
|> Multi.run(:after_hooks, fn _, _ ->
Expand All@@ -64,17 +67,17 @@ defmodule GroupherServer.CMS.Delegate.ArticleUpvote do
with {:ok, info} <- match(thread),
{:ok, article} <- ORM.find(info.model, article_id) do
Multi.new()
|> Multi.run(:inc_article_upvotes_count, fn _, _ ->
|> Multi.run(:update_upvotes_count, fn _, _ ->
update_article_reactions_count(info, article, :upvotes_count, :dec)
end)
|> Multi.run(:update_article_reaction_user_list, fn _,_ ->
|> Multi.run(:update_reaction_user_list, fn _,%{update_upvotes_count: article} ->
update_article_reaction_user_list(:upvot, article, from_user, :remove)
end)
|> Multi.run(:undo_upvote, fn _,_ ->
|> Multi.run(:undo_upvote, fn _,%{update_reaction_user_list: article} ->
args = Map.put(%{user_id: user_id}, info.foreign_key, article.id)

ORM.findby_delete(ArticleUpvote, args)
ORM.find(info.model,article.id)
article |> done
end)
|> Multi.run(:after_hooks, fn _, _ ->
Later.run({Hooks.Notify, :handle, [:undo, :upvote, article, from_user]})
Expand Down
27 changes: 18 additions & 9 deletionslib/groupher_server/cms/delegates/comment_action.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,7 +4,9 @@ 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 GroupherServer.CMS.Delegate.Helper,
only: [article_of: 1, thread_of: 1, sync_embed_replies: 1]

import Helper.ErrorCode

Expand DownExpand Up@@ -169,7 +171,7 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
|> Multi.run(:check_article_author_upvoted, fn _, %{inc_upvotes_count: comment} ->
update_article_author_upvoted_info(comment, user_id)
end)
|> Multi.run(:upvote_comment_done, fn _, %{check_article_author_upvoted: comment} ->
|> Multi.run(:viewer_states, fn _, %{check_article_author_upvoted: comment} ->
viewer_has_upvoted = Enum.member?(comment.meta.upvoted_user_ids, user_id)
viewer_has_reported = Enum.member?(comment.meta.reported_user_ids, user_id)

Expand All@@ -178,6 +180,9 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
|> Map.merge(%{viewer_has_reported: viewer_has_reported})
|> done
end)
|> Multi.run(:sync_embed_replies, fn _, %{viewer_states: comment} ->
sync_embed_replies(comment)
end)
|> Multi.run(:after_hooks, fn _, _ ->
Later.run({Hooks.Notify, :handle, [:upvote, comment, from_user]})
end)
Expand All@@ -204,12 +209,12 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
{:ok, upvotes_count} =
from(c in CommentUpvote, where: c.comment_id == ^comment_id) |> ORM.count()

ORM.update(comment, %{upvotes_count: Enum.max([upvotes_count - 1, 0])})
ORM.update(comment, %{upvotes_count: Enum.max([upvotes_count, 0])})
end)
|> Multi.run(:check_article_author_upvoted, fn _, %{desc_upvotes_count: updated_comment} ->
update_article_author_upvoted_info(updated_comment, user_id)
end)
|> Multi.run(:upvote_comment_done, fn _, %{check_article_author_upvoted: comment} ->
|> Multi.run(:viewer_states, fn _, %{check_article_author_upvoted: comment} ->
viewer_has_upvoted = Enum.member?(comment.meta.upvoted_user_ids, user_id)
viewer_has_reported = Enum.member?(comment.meta.reported_user_ids, user_id)

Expand All@@ -218,6 +223,9 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
|> Map.merge(%{viewer_has_reported: viewer_has_reported})
|> done
end)
|> Multi.run(:sync_embed_replies, fn _, %{viewer_states: comment} ->
sync_embed_replies(comment)
end)
|> Multi.run(:after_hooks, fn _, _ ->
Later.run({Hooks.Notify, :handle, [:undo, :upvote, comment, from_user]})
end)
Expand DownExpand Up@@ -296,10 +304,7 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
|> List.insert_at(length(replies), replyed_comment)
|> Enum.slice(0, @max_parent_replies_count)

parent_comment
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_embed(:replies, new_replies)
|> Repo.update()
ORM.update_embed(parent_comment, :replies, new_replies)
end

# 如果已经有 @max_parent_replies_count 以上的回复了,直接忽略即可
Expand DownExpand Up@@ -385,7 +390,7 @@ 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, %{sync_embed_replies: 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}
Expand All@@ -394,6 +399,10 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
raise_error(:create_comment, result)
end

defp result({:error, :create_comment_upvote, result, _steps}) do
raise_error(:comment_already_upvote, result)
end

defp result({:error, :add_participator, result, _steps}) do
{:error, result}
end
Expand Down
59 changes: 51 additions & 8 deletionslib/groupher_server/cms/delegates/comment_curd.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,7 +7,7 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
import Helper.ErrorCode

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

import GroupherServer.CMS.Helper.Matcher
import ShortMaps
Expand DownExpand Up@@ -144,6 +144,15 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
|> done()
end

def update_user_in_comments_participants(%User{login: login}) do
from(a in CMS.Model.Post,
cross_join: cp in fragment("jsonb_array_elements(?)", a.comments_participants),
where: fragment("?->>'login' = ?", cp, ^login)
)
|> Repo.all()
|> IO.inspect(label: "TODO")
end

@doc """
creates a comment for article like psot, job ...
"""
Expand DownExpand Up@@ -200,15 +209,33 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
with {:ok, post} <- ORM.find(Post, comment.post_id),
{:ok, parsed} <- Converter.Article.parse_body(body),
{:ok, digest} <- Converter.Article.parse_digest(parsed.body_map) do
%{body: body, body_html: body_html} = parsed
post |> ORM.update(%{solution_digest: digest})
comment |> ORM.update(%{body: body, body_html: body_html})
Multi.new()
|> Multi.run(:update_parent_post, fn _, _ ->
ORM.update(post, %{solution_digest: digest})
end)
|> Multi.run(:update_comment, fn _, _ ->
%{body: body, body_html: body_html} = parsed
comment |> ORM.update(%{body: body, body_html: body_html})
end)
|> Multi.run(:sync_embed_replies, fn _, %{update_comment: comment} ->
sync_embed_replies(comment)
end)
|> Repo.transaction()
|> result()
end
end

def update_comment(%Comment{} = comment, body) do
with {:ok, %{body: body, body_html: body_html}} <- Converter.Article.parse_body(body) do
comment |> ORM.update(%{body: body, body_html: body_html})
Multi.new()
|> Multi.run(:update_comment, fn _, _ ->
ORM.update(comment, %{body: body, body_html: body_html})
end)
|> Multi.run(:sync_embed_replies, fn _, %{update_comment: comment} ->
sync_embed_replies(comment)
end)
|> Repo.transaction()
|> result()
end
end

Expand DownExpand Up@@ -245,6 +272,9 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
|> Multi.run(:update_post_state, fn _, _ ->
ORM.update(post, %{is_solved: is_solution, solution_digest: comment.body_html})
end)
|> Multi.run(:sync_embed_replies, fn _, %{mark_solution: comment} ->
sync_embed_replies(comment)
end)
|> Repo.transaction()
|> result()
else
Expand DownExpand Up@@ -378,7 +408,7 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
|> QueryBuilder.filter_pack(Map.merge(filters, %{sort: sort}))
|> ORM.paginator(~m(page size)a)
|> add_pinned_comments_ifneed(thread, article_id, filters)
|> mark_viewer_emotion_states(user, :comment)
|> mark_viewer_emotion_states(user)
|> mark_viewer_has_upvoted(user)
|> done()
end
Expand All@@ -394,7 +424,7 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
|> where(^where_query)
|> QueryBuilder.filter_pack(filters)
|> ORM.paginator(~m(page size)a)
|> mark_viewer_emotion_states(user, :comment)
|> mark_viewer_emotion_states(user)
|> mark_viewer_has_upvoted(user)
|> done()
end
Expand DownExpand Up@@ -457,7 +487,19 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
entries =
Enum.map(
entries,
&Map.merge(&1, %{viewer_has_upvoted: Enum.member?(&1.meta.upvoted_user_ids, user.id)})
fn comment ->
replies =
Enum.map(comment.replies, fn reply_comment ->
Map.merge(reply_comment, %{
viewer_has_upvoted: Enum.member?(reply_comment.meta.upvoted_user_ids, user.id)
})
end)

Map.merge(comment, %{
viewer_has_upvoted: Enum.member?(comment.meta.upvoted_user_ids, user.id),
replies: replies
})
end
)

Map.merge(paged_comments, %{entries: entries})
Expand DownExpand Up@@ -492,6 +534,7 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
defp result({:ok, %{set_question_flag_ifneed: result}}), do: {:ok, result}
defp result({:ok, %{delete_comment: result}}), do: {:ok, result}
defp result({:ok, %{mark_solution: result}}), do: {:ok, result}
defp result({:ok, %{sync_embed_replies: result}}), do: {:ok, result}

defp result({:error, :create_comment, result, _steps}) do
raise_error(:create_comment, result)
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp