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(article-comments): author has upvoted#320

Merged
mydearxym merged 4 commits intodevfromarticle-comments-author-has-upvoted
Apr 17, 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
2 changes: 1 addition & 1 deletioncover/excoveralls.json
View file
Open in desktop

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletionslib/groupher_server/accounts/delegates/fans.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -75,10 +75,10 @@ defmodule GroupherServer.Accounts.Delegate.Fans do
{:ok, _follow_user} <- ORM.find(User, follower_id) do
Multi.new()
|> Multi.run(:delete_follower, fn _, _ ->
ORM.findby_delete(UserFollower, %{user_id: follower_id, follower_id: user_id})
ORM.findby_delete!(UserFollower, %{user_id: follower_id, follower_id: user_id})
end)
|> Multi.run(:delete_following, fn _, _ ->
ORM.findby_delete(UserFollowing, %{user_id: user_id, following_id: follower_id})
ORM.findby_delete!(UserFollowing, %{user_id: user_id, following_id: follower_id})
end)
|> Multi.run(:minus_achievement, fn _, _ ->
Accounts.achieve(%User{id: follower_id}, :minus, :follow)
Expand Down
3 changes: 2 additions & 1 deletionlib/groupher_server/cms/article_comment.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -82,6 +82,7 @@ defmodule GroupherServer.CMS.ArticleComment do
article_comment
|> cast(attrs, @required_fields ++ @optional_fields)
|> cast_embed(:emotions, required: true, with: &Embeds.ArticleCommentEmotion.changeset/2)
|> cast_embed(:meta, required: true, with: &Embeds.ArticleCommentMeta.changeset/2)
|> validate_required(@required_fields)
|> generl_changeset
end
Expand All@@ -90,7 +91,7 @@ defmodule GroupherServer.CMS.ArticleComment do
def update_changeset(%ArticleComment{} = article_comment, attrs) do
article_comment
|> cast(attrs, @required_fields ++ @updatable_fields)
#|> cast_embed(:emotions, required:false, with: &Embeds.ArticleCommentEmotion.changeset/2)
|> cast_embed(:meta, required:true, with: &Embeds.ArticleCommentMeta.changeset/2)
|> generl_changeset
end

Expand Down
3 changes: 2 additions & 1 deletionlib/groupher_server/cms/author.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,10 +3,11 @@ defmodule GroupherServer.CMS.Author do
alias __MODULE__

use Ecto.Schema
use Accessible

import Ecto.Changeset

alias GroupherServer.{Accounts, CMS}

alias CMS.Post

@type t :: %Author{}
Expand Down
1 change: 1 addition & 0 deletionslib/groupher_server/cms/cms.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -124,6 +124,7 @@ defmodule GroupherServer.CMS do

defdelegate create_article_comment(thread, article_id, args, user), to: ArticleComment
defdelegate upvote_article_comment(comment_id, user), to: ArticleComment
defdelegate undo_upvote_article_comment(comment_id, user), to: ArticleComment
defdelegate delete_article_comment(comment_id, user), to: ArticleComment
defdelegate reply_article_comment(comment_id, args, user), to: ArticleComment

Expand Down
104 changes: 88 additions & 16 deletionslib/groupher_server/cms/delegates/article_comment.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,6 +8,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
import GroupherServer.CMS.Utils.Matcher2
import ShortMaps

alias Helper.Types, as: T
alias Helper.{ORM, QueryBuilder}
alias GroupherServer.{Accounts, CMS, Repo}

Expand All@@ -33,6 +34,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
@delete_hint ArticleComment.delete_hint()
@report_threshold_for_fold ArticleComment.report_threshold_for_fold()

@default_comment_meta Embeds.ArticleCommentMeta.default_meta()

@doc """
list paged article comments
"""
Expand DownExpand Up@@ -263,7 +266,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
with {:ok, info} <- match(thread),
# make sure the article exsit
# author is passed by middleware, it's exsit for sure
{:ok, article} <- ORM.find(info.model, article_id) do
{:ok, article} <- ORM.find(info.model, article_id, preload: [author: :user]) do
Multi.new()
|> Multi.run(:create_article_comment, fn _, _ ->
do_create_comment(content, info.foreign_key, article, user)
Expand DownExpand Up@@ -317,10 +320,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
end

@doc "upvote a comment"
# TODO: user has upvoted logic, move to GraphQL
def upvote_article_comment(comment_id, %User{id: user_id}) do
with {:ok, comment} <- ORM.find(ArticleComment, comment_id),
false <- comment.is_deleted do
# IO.inspect(comment, label: "the comment")
Multi.new()
|> Multi.run(:create_comment_upvote, fn _, _ ->
ORM.create(ArticleCommentUpvote, %{article_comment_id: comment.id, user_id: user_id})
Expand All@@ -330,24 +333,58 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
upvotes_count = Repo.aggregate(count_query, :count)
ORM.update(comment, %{upvotes_count: upvotes_count})
end)
|> Multi.run(:check_article_author_upvoted, fn _, _ ->
update_article_author_upvoted_info(comment, user_id)
end)
|> Repo.transaction()
|> upsert_comment_result()
end
end

@doc "upvote a comment"
def undo_upvote_article_comment(comment_id, %User{id: user_id}) do
with {:ok, comment} <- ORM.find(ArticleComment, comment_id),
false <- comment.is_deleted do
Multi.new()
|> Multi.run(:delete_comment_upvote, fn _, _ ->
ORM.findby_delete(ArticleCommentUpvote, %{
article_comment_id: comment.id,
user_id: user_id
})
end)
|> Multi.run(:desc_upvotes_count, fn _, _ ->
count_query = from(c in ArticleCommentUpvote, where: c.article_comment_id == ^comment_id)
upvotes_count = Repo.aggregate(count_query, :count)

ORM.update(comment, %{upvotes_count: Enum.max([upvotes_count - 1, 0])})
end)
|> Multi.run(:check_article_author_upvoted, fn _, %{desc_upvotes_count: updated_comment} ->
update_article_author_upvoted_info(updated_comment, user_id)
end)
|> Repo.transaction()
|> upsert_comment_result()
end
end

defp update_article_author_upvoted_info(%ArticleComment{} = comment, user_id) do
with {:ok, article} = get_full_comment(comment.id) do
is_article_author_upvoted = article.author.id == user_id

new_meta =
comment.meta
|> Map.from_struct()
|> Map.merge(%{is_article_author_upvoted: is_article_author_upvoted})

comment |> ORM.update(%{meta: new_meta})
end
end

# creat article comment for parent or reply
# set floor
# TODO: parse editor-json
# set default emotions
defp do_create_comment(
content,
foreign_key,
%{id: article_id, author_id: article_author_id},
%User{
id: user_id
}
) do
count_query = from(c in ArticleComment, where: field(c, ^foreign_key) == ^article_id)
defp do_create_comment(content, foreign_key, article, %User{id: user_id}) do
count_query = from(c in ArticleComment, where: field(c, ^foreign_key) == ^article.id)
floor = Repo.aggregate(count_query, :count) + 1

ArticleComment
Expand All@@ -358,10 +395,11 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
body_html: content,
emotions: @default_emotions,
floor: floor,
is_article_author: user_id == article_author_id
is_article_author: user_id == article.author.user.id,
meta: @default_comment_meta
},
foreign_key,
article_id
article.id
)
)
end
Expand DownExpand Up@@ -416,13 +454,13 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
defp add_participator_to_article(_, _), do: {:ok, :pass}

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) 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) do
with {:ok, article} <- ORM.find(Job, comment.job_id, preload: [author: :user]) do
{:job, article}
end
end
Expand DownExpand Up@@ -454,9 +492,43 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
%{paged_comments | entries: new_entries}
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: :post, preload: :job)

with {:ok, comment} <- Repo.one(query) |> done() do
extract_article_info(comment)
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

@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(),
article_author <- get_in(article_with_author, [:author, :user]) do
#
article_info = %{title: article.title}

author_info = %{
id: article_author.id,
login: article_author.login,
nickname: article_author.nickname
}

{:ok, %{thread: thread, article: article_info, author: author_info}}
end
end

defp upsert_comment_result({:ok, %{create_article_comment: result}}), do: {:ok, result}
defp upsert_comment_result({:ok, %{add_reply_to: result}}), do: {:ok, result}
defp upsert_comment_result({:ok, %{inc_upvotes_count: result}}), do: {:ok, result}
defp upsert_comment_result({:ok, %{check_article_author_upvoted: result}}), do: {:ok, result}
defp upsert_comment_result({:ok, %{update_report_flag: result}}), do: {:ok, result}
defp upsert_comment_result({:ok, %{update_comment_emotion: result}}), do: {:ok, result}

Expand Down
2 changes: 1 addition & 1 deletionlib/groupher_server/cms/delegates/comment_reaction.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -37,7 +37,7 @@ defmodule GroupherServer.CMS.Delegate.CommentReaction do
with {:ok, action} <- match_action(thread, feeling) do
clause = Map.merge(%{user_id: user_id}, merge_thread_comment_id(thread, comment_id))
# clause = %{post_comment_id: comment_id, user_id: user_id}
ORM.findby_delete(action.reactor, clause)
ORM.findby_delete!(action.reactor, clause)
ORM.find(action.target, comment_id)
end
end
Expand Down
12 changes: 6 additions & 6 deletionslib/groupher_server/cms/delegates/community_operation.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,7 +35,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
"""
def unset_category(%Community{id: community_id}, %Category{id: category_id}) do
with {:ok, community_category} <-
CommunityCategory |> ORM.findby_delete(~m(community_id category_id)a) do
CommunityCategory |> ORM.findby_delete!(~m(community_id category_id)a) do
Community |> ORM.find(community_category.community_id)
end
end
Expand All@@ -54,7 +54,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
"""
def unset_thread(%Community{id: community_id}, %Thread{id: thread_id}) do
with {:ok, community_thread} <-
CommunityThread |> ORM.findby_delete(~m(community_id thread_id)a) do
CommunityThread |> ORM.findby_delete!(~m(community_id thread_id)a) do
Community |> ORM.find(community_thread.community_id)
end
end
Expand All@@ -80,7 +80,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
unset a community editor
"""
def unset_editor(%Community{id: community_id}, %User{id: user_id}) do
with {:ok, _} <- ORM.findby_delete(CommunityEditor, ~m(user_id community_id)a),
with {:ok, _} <- ORM.findby_delete!(CommunityEditor, ~m(user_id community_id)a),
{:ok, _} <- PassportCURD.delete_passport(%User{id: user_id}) do
User |> ORM.find(user_id)
end
Expand DownExpand Up@@ -132,7 +132,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
with {:ok, community} <- ORM.find(Community, community_id),
true <- community.raw !== "home",
{:ok, record} <-
ORM.findby_delete(CommunitySubscriber, community_id: community.id, user_id: user_id) do
ORM.findby_delete!(CommunitySubscriber, community_id: community.id, user_id: user_id) do
Community |> ORM.find(record.community_id)
else
false ->
Expand All@@ -151,7 +151,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
with {:ok, community} <- ORM.find(Community, community_id),
true <- community.raw !== "home",
{:ok, record} <-
CommunitySubscriber |> ORM.findby_delete(community_id: community.id, user_id: user_id) do
CommunitySubscriber |> ORM.findby_delete!(community_id: community.id, user_id: user_id) do
update_community_geo(community_id, user_id, remote_ip, :dec)
Community |> ORM.find(record.community_id)
else
Expand All@@ -171,7 +171,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
with {:ok, community} <- ORM.find(Community, community_id),
true <- community.raw !== "home",
{:ok, record} <-
CommunitySubscriber |> ORM.findby_delete(community_id: community.id, user_id: user_id) do
CommunitySubscriber |> ORM.findby_delete!(community_id: community.id, user_id: user_id) do
update_community_geo_map(community.id, city, :dec)
Community |> ORM.find(record.community_id)
else
Expand Down
2 changes: 1 addition & 1 deletionlib/groupher_server/cms/delegates/passport_curd.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -65,7 +65,7 @@ defmodule GroupherServer.CMS.Delegate.PassportCURD do
end

def delete_passport(%Accounts.User{id: user_id}) do
ORM.findby_delete(UserPasport, ~m(user_id)a)
ORM.findby_delete!(UserPasport, ~m(user_id)a)
end

defp reject_invalid_rules(rules) when is_map(rules) do
Expand Down
12 changes: 8 additions & 4 deletionslib/groupher_server/cms/embeds/article_comment_meta.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,14 +3,14 @@ defmodule GroupherServer.CMS.Embeds.ArticleCommentMeta do
general article comment meta info
"""
use Ecto.Schema
import Ecto.Changeset

alias CMS.Embeds
@optional_fields ~w(is_article_author_upvoted is_solution report_count)a

@default_meta %{
is_article_author_upvoted: false,
is_solution: false,
report_count: 0,
report_users: []
report_count: 0
}

@doc "for test usage"
Expand All@@ -21,6 +21,10 @@ defmodule GroupherServer.CMS.Embeds.ArticleCommentMeta do
field(:is_solution, :boolean, default: false)

field(:report_count, :integer, default: 0)
embeds_many(:report_users, Embeds.User, on_replace: :delete)
end

def changeset(struct, params) do
struct
|> cast(params, @optional_fields)
end
end
2 changes: 2 additions & 0 deletionslib/groupher_server/cms/job.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,8 @@ defmodule GroupherServer.CMS.Job do
alias __MODULE__

use Ecto.Schema
use Accessible

import Ecto.Changeset

alias GroupherServer.CMS
Expand Down
2 changes: 2 additions & 0 deletionslib/groupher_server/cms/post.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,8 @@ defmodule GroupherServer.CMS.Post do
alias __MODULE__

use Ecto.Schema
use Accessible

import Ecto.Changeset

alias GroupherServer.{CMS, Accounts}
Expand Down
6 changes: 3 additions & 3 deletionslib/groupher_server_web/resolvers/cms_resolver.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -41,7 +41,7 @@ defmodule GroupherServerWeb.Resolvers.CMS do

def update_community(_root, args, _info), do: Community |> ORM.find_update(args)

def delete_community(_root, %{id: id}, _info), do: Community |> ORM.find_delete(id)
def delete_community(_root, %{id: id}, _info), do: Community |> ORM.find_delete!(id)

# #######################
# community thread (post, job), login user should be logged
Expand DownExpand Up@@ -178,7 +178,7 @@ defmodule GroupherServerWeb.Resolvers.CMS do
CMS.create_category(%{title: title, raw: raw}, user)
end

def delete_category(_root, %{id: id}, _info), do: Category |> ORM.find_delete(id)
def delete_category(_root, %{id: id}, _info), do: Category |> ORM.find_delete!(id)

def update_category(_root, ~m(id title)a, %{context: %{cur_user: _}}) do
CMS.update_category(~m(%Category id title)a)
Expand DownExpand Up@@ -243,7 +243,7 @@ defmodule GroupherServerWeb.Resolvers.CMS do
CMS.create_tag(%Community{id: community_id}, thread, args, user)
end

def delete_tag(_root, %{id: id}, _info), do: Tag |> ORM.find_delete(id)
def delete_tag(_root, %{id: id}, _info), do: Tag |> ORM.find_delete!(id)

def update_tag(_root, args, _info), do: CMS.update_tag(args)

Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp