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

Commit 67cf040

Browse files
committed
refactor(pinned-article): wip
1 parent a85b191 commit 67cf040

File tree

4 files changed

+27
-80
lines changed

4 files changed

+27
-80
lines changed

lib/groupher_server/cms/delegates/article_curd.ex

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
33
CURD operation on post/job ...
44
"""
55
import Ecto.Query, warn: false
6-
import GroupherServer.CMS.Utils.Matcher
6+
7+
import GroupherServer.CMS.Utils.Matcher2
8+
9+
import GroupherServer.CMS.Utils.Matcher, only: [match_action: 2, dynamic_where: 2]
710
import Helper.Utils, only: [done: 1, pick_by: 2, integerfy: 1]
811
import Helper.ErrorCode
912
import ShortMaps
1013

1114
alias GroupherServer.{Accounts, CMS, Delivery, Email, Repo, Statistics}
1215

1316
alias Accounts.User
14-
alias CMS.{Author, Community, Embeds, Delegate, Tag}
17+
alias CMS.{Author, Community, PinnedArticle, Embeds, Delegate, Tag}
1518

1619
alias Delegate.ArticleOperation
1720
alias Helper.{Later, ORM, QueryBuilder}
@@ -271,28 +274,22 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
271274
end
272275

273276
defp read_state_query(queryable, %{read: false} = _filter, _user) do
274-
# queryable
275-
# |> join(:left, [content, f, c], viewers in assoc(content, :viewers))
276-
# |> where([content, f, c, viewers], viewers.user_id != ^user.id)
277-
# |> where([content, f, c, viewers], content.id != viewers.post_id)
278-
# |> IO.inspect(label: "query")
279277
queryable
280278
end
281279

282280
defp read_state_query(queryable, _, _), do: queryable
283281

284-
# only first page need pin contents
285-
# TODO: use seperate pined table, which is much more smaller
286-
defp add_pin_contents_ifneed(contents, CMS.Post, %{community: community} = filter) do
282+
defp add_pin_contents_ifneed(contents, querable, %{community: _community} = filter) do
287283
with {:ok, _} <- should_add_pin?(filter),
284+
{:ok, info} <- match(querable),
288285
{:ok, normal_contents} <- contents,
289286
true <- Map.has_key?(filter, :community),
290287
true <- 1 == Map.get(normal_contents, :page_number) do
291288
{:ok, pined_content} =
292-
CMS.PinedPost
289+
PinnedArticle
293290
|> join(:inner, [p], c in assoc(p, :community))
294-
|> join(:inner, [p], content in assoc(p, :post))
295-
|> where([p, c, content], c.raw == ^community)
291+
|> join(:inner, [p], content in assoc(p, ^info.thread))
292+
|> where([p, c, content], c.raw == ^filter.community)
296293
|> select([p, c, content], content)
297294
# 10 pined contents per community/thread, at most
298295
|> ORM.paginater(%{page: 1, size: 10})
@@ -305,38 +302,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
305302
end
306303
end
307304

308-
defp add_pin_contents_ifneed(contents, CMS.Job, %{community: _community} = filter) do
309-
merge_pin_contents(contents, :job, CMS.PinedJob, filter)
310-
end
311-
312-
defp add_pin_contents_ifneed(contents, CMS.Repo, %{community: _community} = filter) do
313-
merge_pin_contents(contents, :repo, CMS.PinedRepo, filter)
314-
end
315-
316305
defp add_pin_contents_ifneed(contents, _querable, _filter), do: contents
317306

318-
defp merge_pin_contents(contents, thread, pin_schema, %{community: _community} = filter) do
319-
with {:ok, _} <- should_add_pin?(filter),
320-
{:ok, normal_contents} <- contents,
321-
true <- Map.has_key?(filter, :community),
322-
true <- 1 == Map.get(normal_contents, :page_number) do
323-
{:ok, pined_content} =
324-
pin_schema
325-
|> join(:inner, [p], c in assoc(p, :community))
326-
|> join(:inner, [p], content in assoc(p, ^thread))
327-
|> where([p, c, content], c.raw == ^filter.community)
328-
|> select([p, c, content], content)
329-
# 10 pined contents per community/thread, at most
330-
|> ORM.paginater(%{page: 1, size: 10})
331-
|> done()
332-
333-
concat_contents(pined_content, normal_contents)
334-
else
335-
_error ->
336-
contents
337-
end
338-
end
339-
340307
# if filter contains like: tags, sort.., then don't add pin content
341308
defp should_add_pin?(%{page: 1, tag: :all, sort: :desc_inserted, read: :all} = filter) do
342309
filter

lib/groupher_server/cms/utils/matcher2.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,19 @@ defmodule GroupherServer.CMS.Utils.Matcher2 do
2525
{:error, "not supported"}
2626
end
2727

28+
# used for paged pin articles
29+
def match(Post) do
30+
{:ok, %{thread: :post}}
31+
end
32+
2833
def match(:post) do
2934
{:ok, %{model: Post, foreign_key: :post_id}}
3035
end
3136

37+
def match(Job) do
38+
{:ok, %{thread: :job}}
39+
end
40+
3241
def match(:job) do
3342
{:ok, %{model: Job, foreign_key: :job_id}}
3443
end

test/groupher_server/cms/article_pin_test.exs

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ defmodule GroupherServer.Test.CMS.ArticlePin do
88

99
alias CMS.{
1010
Community,
11-
PinnedArticle,
12-
Post,
13-
Job
11+
PinnedArticle
12+
# Post,
13+
# Job
1414
}
1515

1616
@max_pinned_article_count_per_thread Community.max_pinned_article_count_per_thread()
@@ -35,7 +35,7 @@ defmodule GroupherServer.Test.CMS.ArticlePin do
3535
assert pind_article.post_id == post.id
3636
end
3737

38-
@tag :wip2
38+
@tag :wip
3939
test "one community & thread can only pin certern count of post", ~m(community post user)a do
4040
{:ok, post2} = CMS.create_content(community, :post, mock_attrs(:post), user)
4141
{:ok, post3} = CMS.create_content(community, :post, mock_attrs(:post), user)
@@ -54,7 +54,7 @@ defmodule GroupherServer.Test.CMS.ArticlePin do
5454
assert {:error, _} = CMS.pin_article(:post, 8848, community.id)
5555
end
5656

57-
@tag :wip2
57+
@tag :wip
5858
test "can undo pin to a post", ~m(community post)a do
5959
{:ok, _} = CMS.pin_article(:post, post.id, community.id)
6060

@@ -65,35 +65,5 @@ defmodule GroupherServer.Test.CMS.ArticlePin do
6565
end
6666
end
6767

68-
# describe "[cms job pin]" do
69-
# test "can pin a job", ~m(community job)a do
70-
# {:ok, pined_job} = CMS.pin_content(job, community)
71-
72-
# assert pined_job.id == job.id
73-
# end
74-
75-
# test "can undo pin to a job", ~m(community job)a do
76-
# {:ok, pined_job} = CMS.pin_content(job, community)
77-
# assert pined_job.id == job.id
78-
79-
# assert {:ok, unpined} = CMS.undo_pin_content(job, community)
80-
# assert unpined.id == job.id
81-
# end
82-
# end
83-
84-
# describe "[cms repo pin]" do
85-
# test "can pin a repo", ~m(community repo)a do
86-
# {:ok, pined_repo} = CMS.pin_content(repo, community)
87-
88-
# assert pined_repo.id == repo.id
89-
# end
90-
91-
# test "can undo pin to a repo", ~m(community repo)a do
92-
# {:ok, pined_repo} = CMS.pin_content(repo, community)
93-
# assert pined_repo.id == repo.id
94-
95-
# assert {:ok, unpined} = CMS.undo_pin_content(repo, community)
96-
# assert unpined.id == repo.id
97-
# end
98-
# end
68+
# TODO: Job, Repo
9969
end

test/groupher_server_web/query/cms/posts_flags_test.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ defmodule GroupherServer.Test.Query.PostsFlags do
5151
}
5252
}
5353
"""
54+
@tag :wip2
5455
test "if have pined posts, the pined posts should at the top of entries",
5556
~m(guest_conn community post_m)a do
5657
variables = %{filter: %{community: community.raw}}
@@ -62,7 +63,7 @@ defmodule GroupherServer.Test.Query.PostsFlags do
6263
assert results["pageSize"] == @page_size
6364
assert results["totalCount"] == @total_count
6465

65-
{:ok, _pined_post} = CMS.pin_content(post_m, community)
66+
{:ok, _pined_post} = CMS.pin_article(:post, post_m.id, community.id)
6667

6768
results = guest_conn |> query_result(@query, variables, "pagedPosts")
6869
entries_first = results["entries"] |> List.first()

0 commit comments

Comments
 (0)