Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit be00788

Browse files
committed
fix(commentsParticipators): temp solution for issue #16
1 parent aa09ed0 commit be00788

File tree

9 files changed

+65
-62
lines changed

9 files changed

+65
-62
lines changed

lib/groupher_server/cms/utils/loader.ex

+27-11
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,37 @@ defmodule GroupherServer.CMS.Utils.Loader do
179179
# should use WINDOW function
180180
# see https://github.com/coderplanets/coderplanets_server/issues/16
181181
def query({"posts_comments", PostComment}, %{filter: filter, unique: true}) do
182-
# PostComment
183-
# |> QueryBuilder.filter_pack(filter)
184-
# |> join(:inner, [c], a in assoc(c, :author))
185-
# |> distinct([c, a], a.id)
186-
# |> select([c, a], %{
187-
# nickname: a.nickname,
188-
# id: a.id,
189-
# what_ever: row_number() |> over(partition_by: a.id)
190-
# })
191-
192182
PostComment
193-
|> QueryBuilder.filter_pack(filter)
194183
|> join(:inner, [c], a in assoc(c, :author))
195184
|> distinct([c, a], a.id)
196185
|> select([c, a], a)
186+
187+
# |> select([c, a], %{
188+
# rankid: rank() |> over(partition_by: c.inserted_at),
189+
# id: a.id,
190+
# nickname: a.nickname
191+
# })
192+
# |> windows([rankid: [partition_by: c.inserted_at]])
193+
# |> where([c, a], a.no < 3)
194+
# |> select([c, a], rank() |> over(partition_by: c.inserted_at))
195+
# |> select([c, a], %{
196+
# nickname: a.nickname,
197+
198+
# working raw sql
199+
# select * from(
200+
# select rank() over(partition by cid order by pinserted_at desc) as r, * from(
201+
# select c.id as cid,
202+
# c.body as cbody,
203+
# p.inserted_at as pinserted_at,
204+
# u.* from "cms_posts" as c join "posts_comments" as p on c.id= p.post_id join "users" as u on p.author_id= u.id) as view
205+
# ) as v where r<= 3;
206+
207+
# backup ->
208+
# PostComment
209+
# |> QueryBuilder.filter_pack(filter)
210+
# |> join(:inner, [c], a in assoc(c, :author))
211+
# |> distinct([c, a], a.id)
212+
# |> select([c, a], a)
197213
end
198214

199215
def query({"posts_comments", PostComment}, %{count: _, unique: true}) do

lib/groupher_server_web/middleware/cut_participators.ex

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ defmodule GroupherServerWeb.Middleware.CutParticipators do
1111
"""
1212

1313
@behaviour Absinthe.Middleware
14-
@count 5
14+
@default_length 5
1515

1616
def call(%{errors: errors} = resolution, _) when length(errors) > 0, do: resolution
1717

18+
def call(%{value: value, arguments: %{filter: %{first: first}} = args} = resolution, _) do
19+
%{resolution | value: value |> Enum.reverse() |> Enum.slice(0, first)}
20+
end
21+
1822
def call(%{value: value} = resolution, _) do
19-
%{resolution | value: value |> Enum.slice(0, @count)}
23+
%{resolution | value: value |> Enum.reverse() |> Enum.slice(0, @default_length)}
2024
end
2125

2226
def call(resolution, _), do: resolution

lib/groupher_server_web/middleware/pagesize_proof.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ defmodule GroupherServerWeb.Middleware.PageSizeProof do
2424
when not is_nil(customization) do
2525
# NOTE: c11n display_density should also linted by page limit,
2626
# otherwise dataloader will crash for empty extra items
27-
size = customization.display_density |> String.to_integer() |> min(@max_page_size - 2)
27+
size = customization.display_density |> String.to_integer() |> min(@max_page_size)
2828
# size = 28
2929

3030
case Map.has_key?(arguments, :filter) do

lib/groupher_server_web/schema/utils/helper.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,10 @@ defmodule GroupherServerWeb.Schema.Utils.Helper do
273273
arg(:filter, :members_filter)
274274
arg(:unique, :unique_type, default_value: true)
275275

276-
middleware(M.ForceLoader)
276+
# middleware(M.ForceLoader)
277277
middleware(M.PageSizeProof)
278278
resolve(dataloader(CMS, :comments))
279-
# middleware(M.CutParticipators)
279+
middleware(M.CutParticipators)
280280
end
281281

282282
field(:paged_comments_participators, :paged_users) do

test/groupher_server_web/mutation/accounts/customization_test.exs

+1-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ defmodule GroupherServer.Test.Mutation.Account.Customization do
8787

8888
variables = %{filter: %{page: 1}}
8989
results = user_conn |> query_result(@paged_post_query, variables, "pagedPosts")
90-
# FIXME: this 2 is magic number, otherwise it will crash the dataloader
91-
assert results["pageSize"] == @max_page_size - 2
90+
assert results["pageSize"] == @max_page_size
9291
end
9392

9493
test "set single customization should merge not overwright other settings", ~m(user_conn)a do

test/groupher_server_web/query/cms/job_comment_test.exs

+2-3
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,9 @@ defmodule GroupherServer.Test.Query.JobComment do
8787
variables = %{thread: "JOB", filter: %{community: community.raw}}
8888
results = guest_conn |> query_result(@query, variables, "pagedJobs")
8989

90-
assert results["entries"] |> List.first() |> Map.get("commentsParticipators") |> length ==
91-
10
90+
assert results["entries"] |> List.first() |> Map.get("commentsParticipators") |> length == 5
9291

93-
assert results["entries"] |> List.last() |> Map.get("commentsParticipators") |> length == 10
92+
assert results["entries"] |> List.last() |> Map.get("commentsParticipators") |> length == 5
9493
end
9594

9695
test "can get paged commetns participators of a job", ~m(user guest_conn)a do

test/groupher_server_web/query/cms/post_comment_test.exs

+22-34
Original file line numberDiff line numberDiff line change
@@ -143,52 +143,40 @@ defmodule GroupherServer.Test.Query.PostComment do
143143
}
144144
}
145145
"""
146-
@tag :pain_in_ass
147-
test "top N per Group test", ~m(user guest_conn)a do
146+
test "top N per Group test v2", ~m(user guest_conn)a do
148147
body = "this is a test comment"
149148

150149
{:ok, community} = db_insert(:community)
151150
{:ok, post} = CMS.create_content(community, :post, mock_attrs(:post), user)
152151
{:ok, post2} = CMS.create_content(community, :post, mock_attrs(:post), user)
153-
# {:ok, users_list} = db_insert_multi(:user, 10)
154-
155-
CMS.create_comment(:post, post.id, %{community: community.raw, body: body}, user)
156-
CMS.create_comment(:post, post2.id, %{community: community.raw, body: body}, user)
157-
158-
variables = %{filter: %{community: community.raw}}
159-
results = guest_conn |> query_result(@query, variables, "pagedPosts")
152+
{:ok, users_list} = db_insert_multi(:user, 10)
153+
{:ok, users_list2} = db_insert_multi(:user, 10)
160154

161-
# for test window function
162-
_commentsParticipators = results["entries"]
163-
# IO.inspect(commentsParticipators, label: "commentsParticipators->")
164-
end
155+
post_last_comment_user_id = users_list |> List.last() |> Map.get(:id)
156+
post2_last_comment_user_id = users_list2 |> List.last() |> Map.get(:id)
165157

166-
# ~m(user guest_conn)a do
167-
test "top N per Group test v2" do
168-
# body = "this is a test comment"
158+
Enum.each(
159+
users_list,
160+
&CMS.create_comment(:post, post.id, %{community: community.raw, body: body}, &1)
161+
)
169162

170-
# {:ok, community} = db_insert(:community)
171-
# {:ok, post} = CMS.create_content(community, :post, mock_attrs(:post), user)
172-
# {:ok, post2} = CMS.create_content(community, :post, mock_attrs(:post), user)
173-
# {:ok, users_list} = db_insert_multi(:user, 10)
174-
# {:ok, users_list2} = db_insert_multi(:user, 10)
163+
Enum.each(
164+
users_list2,
165+
&CMS.create_comment(:post, post2.id, %{community: community.raw, body: body}, &1)
166+
)
175167

176-
# Enum.each(
177-
# users_list,
178-
# &CMS.create_comment(:post, post.id, %{body: body}, &1)
179-
# )
168+
variables = %{filter: %{community: community.raw}}
169+
results = guest_conn |> query_result(@query, variables, "pagedPosts")
180170

181-
# Enum.each(
182-
# users_list2,
183-
# &CMS.create_comment(:post, post2.id, body, &1)
184-
# )
171+
first_result = results["entries"] |> List.first() |> Map.get("commentsParticipators")
172+
last_result = results["entries"] |> List.last() |> Map.get("commentsParticipators")
185173

186-
# variables = %{filter: %{community: community.raw}}
187-
# results = guest_conn |> query_result(@query, variables, "pagedPosts")
188-
# commentsParticipators =
189-
# results["entries"] |> List.first() |> Map.get("commentsParticipators")
174+
assert 5 == first_result |> length
175+
assert 5 == last_result |> length
190176

191-
# IO.inspect(commentsParticipators, label: "commentsParticipators->")
177+
# 默认显示最新评论的登陆用户
178+
assert to_string(post_last_comment_user_id) == first_result |> List.first() |> Map.get("id")
179+
assert to_string(post2_last_comment_user_id) == last_result |> List.first() |> Map.get("id")
192180
end
193181
end
194182

test/groupher_server_web/query/cms/repo_comment_test.exs

+2-3
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,9 @@ defmodule GroupherServer.Test.Query.RepoComment do
8888
variables = %{thread: "REPO", filter: %{community: community.raw}}
8989
results = guest_conn |> query_result(@query, variables, "pagedRepos")
9090

91-
assert results["entries"] |> List.first() |> Map.get("commentsParticipators") |> length ==
92-
10
91+
assert results["entries"] |> List.first() |> Map.get("commentsParticipators") |> length == 5
9392

94-
assert results["entries"] |> List.last() |> Map.get("commentsParticipators") |> length == 10
93+
assert results["entries"] |> List.last() |> Map.get("commentsParticipators") |> length == 5
9594
end
9695

9796
test "can get paged commetns participators of a repo", ~m(guest_conn user)a do

test/groupher_server_web/query/cms/video_comment_test.exs

+2-4
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,8 @@ defmodule GroupherServer.Test.Query.VideoComment do
9292
variables = %{thread: "VIDEO", filter: %{community: community.raw}}
9393
results = guest_conn |> query_result(@query, variables, "pagedVideos")
9494

95-
assert results["entries"] |> List.first() |> Map.get("commentsParticipators") |> length ==
96-
10
97-
98-
assert results["entries"] |> List.last() |> Map.get("commentsParticipators") |> length == 10
95+
assert results["entries"] |> List.first() |> Map.get("commentsParticipators") |> length == 5
96+
assert results["entries"] |> List.last() |> Map.get("commentsParticipators") |> length == 5
9997
end
10098

10199
test "can get paged commetns participators of a video", ~m(user guest_conn)a do

0 commit comments

Comments
 (0)