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: debug new comments#430

Merged
mydearxym merged 6 commits intodevfromdebug-new-comments
Sep 12, 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
34 changes: 33 additions & 1 deletionlib/groupher_server/cms/delegates/Seeds/seeds.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,14 +21,15 @@ defmodule GroupherServer.CMS.Delegate.Seeds do
alias Helper.ORM
alias GroupherServer.CMS

alias CMS.Model.{Community, Category, Post}
alias CMS.Model.{Community, Category, Post, Comment}
alias CMS.Delegate.Seeds
alias Seeds.Domain

@article_threads get_config(:article, :threads)
# categories
@community_types [:pl, :framework, :editor, :database, :devops, :city]

@comment_emotions get_config(:article, :comment_emotions)
# seed community

@doc """
Expand DownExpand Up@@ -88,6 +89,7 @@ defmodule GroupherServer.CMS.Delegate.Seeds do
attrs = mock_attrs(thread, %{community_id: community.id})
{:ok, article} = CMS.create_article(community, thread, attrs, user)
seed_tags(tags, thread, article.id)
seed_comments(thread, article.id, user)
seed_upvotes(thread, article.id)
end)
end
Expand DownExpand Up@@ -117,6 +119,36 @@ defmodule GroupherServer.CMS.Delegate.Seeds do
tags.entries |> Enum.map(& &1.id) |> Enum.shuffle() |> Enum.take(1)
end

defp seed_comments(thread, article_id, user) do
0..Enum.random(1..5)
|> Enum.each(fn _ ->
text = Faker.Lorem.sentence(%Range{first: 30, last: 80})
{:ok, comment} = CMS.create_comment(thread, article_id, mock_comment(text), user)
seed_comment_emotions(comment)
seed_comment_replies(comment)
end)
end

defp seed_comment_replies(%Comment{} = comment) do
with {:ok, users} <- db_insert_multi(:user, Enum.random(1..5)) do
users
|> Enum.each(fn user ->
text = Faker.Lorem.sentence(%Range{first: 30, last: 80})
{:ok, _} = CMS.reply_comment(comment.id, mock_comment(text), user)
end)
end
end

defp seed_comment_emotions(%Comment{} = comment) do
with {:ok, users} <- db_insert_multi(:user, Enum.random(1..5)) do
users
|> Enum.each(fn user ->
emotion = @comment_emotions |> Enum.random()
{:ok, _} = CMS.emotion_to_comment(comment.id, emotion, user)
end)
end
end

# clean up

def clean_up(:all) do
Expand Down
58 changes: 39 additions & 19 deletionslib/groupher_server/cms/delegates/comment_curd.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
CURD and operations for article comments
"""
import Ecto.Query, warn: false
import Helper.Utils, only: [done: 1, ensure: 2, get_config: 2]
import Helper.Utils, only: [done: 1, ensure: 2,strip_struct: 1,get_config: 2]
import Helper.ErrorCode

import GroupherServer.CMS.Delegate.Helper,
Expand DownExpand Up@@ -110,24 +110,40 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
@spec paged_comments_participants(T.article_thread(), Integer.t(), T.paged_filter()) ::
{:ok, T.paged_users()}
def paged_comments_participants(thread, article_id, filters) do
%{page: page, size: size} = filters
with {:ok, thread_query} <- match(thread, :query, article_id),
{:ok, info} <- match(thread),
{:ok, article} <- ORM.find(info.model, article_id),
{:ok, paged_data} <- do_paged_comments_participants(thread_query, filters) do
# check participants_count if history data do not match
case article.comments_participants_count !== paged_data.total_count do
true ->
article |> ORM.update(%{comments_participants_count: paged_data.total_count})

false ->
{:ok, :pass}
end

with {:ok, thread_query} <- match(thread, :query, article_id) do
Comment
|> where(^thread_query)
|> QueryBuilder.filter_pack(Map.merge(filters, %{sort: :desc_inserted}))
|> join(:inner, [c], a in assoc(c, :author))
|> distinct([c, a], a.id)
# group_by
|> group_by([c, a], a.id)
|> group_by([c, a], c.inserted_at)
|> group_by([c, a], c.id)
|> select([c, a], a)
|> ORM.paginator(~m(page size)a)
|> done()
paged_data |> done
end
end

defp do_paged_comments_participants(query, filters) do
%{page: page, size: size} = filters

Comment
|> where(^query)
|> QueryBuilder.filter_pack(Map.merge(filters, %{sort: :desc_inserted}))
|> join(:inner, [c], a in assoc(c, :author))
|> distinct([c, a], a.id)
# group_by
|> group_by([c, a], a.id)
|> group_by([c, a], c.inserted_at)
|> group_by([c, a], c.id)
|> select([c, a], a)
|> ORM.paginator(~m(page size)a)
|> done()
end

@doc """
creates a comment for article like psot, job ...
"""
Expand DownExpand Up@@ -273,15 +289,19 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do

# add participator to article-like(Post, Job ...) and update count
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)
cur_participants = participants |> List.insert_at(0, user) |> Enum.uniq_by(& &1.id)

meta = article.meta |> strip_struct
cur_participants_ids = (meta.comments_participant_user_ids ++ [user.id]) |> Enum.uniq()
meta = Map.merge(meta, %{comments_participant_user_ids: cur_participants_ids})

latest_participants = total_participants |> Enum.slice(0, @max_participator_count)
total_participants_count = length(total_participants)
latest_participants = cur_participants |> Enum.slice(0, @max_participator_count)

article
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_change(:comments_participants_count,total_participants_count)
|> Ecto.Changeset.put_change(:comments_participants_count,cur_participants_ids |> length)
|> Ecto.Changeset.put_embed(:comments_participants, latest_participants)
|> Ecto.Changeset.put_embed(:meta, meta)
|> Repo.update()
end

Expand Down
5 changes: 4 additions & 1 deletionlib/groupher_server/cms/models/embeds/article_meta.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@ defmodule GroupherServer.CMS.Model.Embeds.ArticleMeta do

alias GroupherServer.CMS.Model.Embeds

@optional_fields ~w(thread is_edited is_comment_locked upvoted_user_ids collected_user_ids viewed_user_ids reported_user_ids reported_count is_sinked can_undo_sink last_active_at)a
@optional_fields ~w(thread is_edited is_comment_locked upvoted_user_ids collected_user_ids viewed_user_idscomments_participant_user_idsreported_user_ids reported_count is_sinked can_undo_sink last_active_at)a

@doc "for test usage"
def default_meta() do
Expand All@@ -21,6 +21,7 @@ defmodule GroupherServer.CMS.Model.Embeds.ArticleMeta do
collected_user_ids: [],
viewed_user_ids: [],
reported_user_ids: [],
comments_participant_user_ids: [],
reported_count: 0,
is_sinked: false,
can_undo_sink: true,
Expand All@@ -44,6 +45,8 @@ defmodule GroupherServer.CMS.Model.Embeds.ArticleMeta do
field(:reported_user_ids, {:array, :integer}, default: [])
field(:reported_count, :integer, default: 0)

field(:comments_participant_user_ids, {:array, :integer}, default: [])

field(:is_sinked, :boolean, default: false)
field(:can_undo_sink, :boolean, default: false)
# if undo_sink, can recover last active_at from here
Expand Down
26 changes: 26 additions & 0 deletionslib/groupher_server_web/schema/Helper/fields.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -151,6 +151,32 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
end
end

defmacro comment_general_fields do
quote do
field(:id, :id)
field(:body, :string)
field(:body_html, :string)
field(:author, :user, resolve: dataloader(CMS, :author))
field(:is_pinned, :boolean)
field(:floor, :integer)
field(:upvotes_count, :integer)
field(:is_article_author, :boolean)
field(:emotions, :comment_emotions)
field(:meta, :comment_meta)
field(:replies_count, :integer)
field(:thread, :string)
field(:viewer_has_upvoted, :boolean)
field(:thread, :string)
field(:replies_count, :integer)

field(:is_deleted, :boolean)
field(:is_archived, :boolean)
field(:archived_at, :datetime)

timestamp_fields()
end
end

# see: https://github.com/absinthe-graphql/absinthe/issues/363
defmacro pagination_args do
quote do
Expand Down
37 changes: 3 additions & 34 deletionslib/groupher_server_web/schema/cms/cms_types.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -302,49 +302,18 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
end

object :comment_reply do
field(:id, :id)
field(:body, :string)
field(:body_html, :string)
field(:author, :user, resolve: dataloader(CMS, :author))
field(:floor, :integer)
field(:upvotes_count, :integer)
field(:is_article_author, :boolean)
field(:emotions, :comment_emotions)
field(:meta, :comment_meta)
field(:replies_count, :integer)
field(:reply_to, :comment_reply)
field(:viewer_has_upvoted, :boolean)
field(:thread, :string)

timestamp_fields()
comment_general_fields()
end

object :comment do
field(:id, :id)
field(:body_html, :string)
field(:author, :user, resolve: dataloader(CMS, :author))
field(:is_pinned, :boolean)
field(:floor, :integer)
field(:upvotes_count, :integer)
field(:emotions, :comment_emotions)
field(:is_article_author, :boolean)
field(:meta, :comment_meta)
comment_general_fields()
field(:reply_to, :comment_reply)

field(:replies, list_of(:comment_reply))
field(:replies_count, :integer)
field(:thread, :string)
field(:article, :common_article)

field(:is_deleted, :boolean)
field(:viewer_has_upvoted, :boolean)

field(:is_for_question, :boolean)
field(:is_solution, :boolean)

field(:is_archived, :boolean)
field(:archived_at, :datetime)

timestamp_fields()
end

####### reports
Expand Down
26 changes: 26 additions & 0 deletionstest/groupher_server/seeds/articles_seed_test.exs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,6 +29,12 @@ defmodule GroupherServer.Test.Seeds.Articles do
posts.entries |> Enum.map(& &1.original_community_id) |> Enum.uniq()

assert original_community_ids === [community.id]

{:ok, paged_comments} =
CMS.paged_comments(:post, ramdom_post.id, %{page: 1, size: 10}, :timeline)

# IO.inspect(paged_comments, label: "paged_comments -> ")
assert paged_comments.total_count !== 0
end

test "can seed jobs" do
Expand All@@ -45,6 +51,11 @@ defmodule GroupherServer.Test.Seeds.Articles do
original_community_ids = jobs.entries |> Enum.map(& &1.original_community_id) |> Enum.uniq()

assert original_community_ids === [community.id]

{:ok, paged_comments} =
CMS.paged_comments(:job, ramdom_job.id, %{page: 1, size: 20}, :timeline)

assert paged_comments.total_count !== 0
end

test "can seed radars" do
Expand All@@ -62,6 +73,11 @@ defmodule GroupherServer.Test.Seeds.Articles do
radars.entries |> Enum.map(& &1.original_community_id) |> Enum.uniq()

assert original_community_ids === [community.id]

{:ok, paged_comments} =
CMS.paged_comments(:radar, ramdom_radar.id, %{page: 1, size: 20}, :timeline)

assert paged_comments.total_count !== 0
end

test "can seed blogs" do
Expand All@@ -78,6 +94,11 @@ defmodule GroupherServer.Test.Seeds.Articles do
blogs.entries |> Enum.map(& &1.original_community_id) |> Enum.uniq()

assert original_community_ids === [community.id]

{:ok, paged_comments} =
CMS.paged_comments(:blog, ramdom_blog.id, %{page: 1, size: 20}, :timeline)

assert paged_comments.total_count !== 0
end

test "can seed works" do
Expand All@@ -95,6 +116,11 @@ defmodule GroupherServer.Test.Seeds.Articles do
works.entries |> Enum.map(& &1.original_community_id) |> Enum.uniq()

assert original_community_ids === [community.id]

{:ok, paged_comments} =
CMS.paged_comments(:works, ramdom_works.id, %{page: 1, size: 20}, :timeline)

assert paged_comments.total_count !== 0
end
end
end
4 changes: 3 additions & 1 deletiontest/groupher_server/seeds/community_seed_test.exs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -42,7 +42,9 @@ defmodule GroupherServer.Test.Seeds.CommunitySeed do
assert found.threads |> length == 3

threads = found.threads |> Enum.map(& &1.thread.title)
assert threads == ["帖子", "看板", "分布"]
assert "帖子" in threads
assert "看板" in threads
assert "分布" in threads
end

# Makers, 广告墙, 求助,外包合作
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -605,16 +605,6 @@ defmodule GroupherServer.Test.Query.Comments.BlogComment do
meta {
isArticleAuthorUpvoted
}
replyTo {
id
bodyHtml
floor
isArticleAuthor
author {
id
login
}
}
repliesCount
viewerHasUpvoted
}
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -614,16 +614,6 @@ defmodule GroupherServer.Test.Query.Comments.DrinkComment do
meta {
isArticleAuthorUpvoted
}
replyTo {
id
bodyHtml
floor
isArticleAuthor
author {
id
login
}
}
repliesCount
viewerHasUpvoted
}
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,7 +30,7 @@ defmodule GroupherServer.Test.Query.Comments.GuideComment do
}
}
"""

@tag :wip
test "guest user can get comment participants after comment created",
~m(guest_conn guide user user2)a do
total_count = 5
Expand DownExpand Up@@ -614,16 +614,6 @@ defmodule GroupherServer.Test.Query.Comments.GuideComment do
meta {
isArticleAuthorUpvoted
}
replyTo {
id
bodyHtml
floor
isArticleAuthor
author {
id
login
}
}
repliesCount
viewerHasUpvoted
}
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp