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

Commit d4a95a7

Browse files
authored
Paged cited contents workflow (#402)
* refactor(cite-workflow): wip * refactor(cite-workflow): wip * refactor(cite-workflow): wip * refactor(cite-workflow): gq endpoint
1 parent 617cd3d commit d4a95a7

File tree

17 files changed

+569
-26
lines changed

17 files changed

+569
-26
lines changed

lib/groupher_server/accounts/delegates/upvoted_articles.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ defmodule GroupherServer.Accounts.Delegate.UpvotedArticles do
1717
get paged upvoted articles
1818
"""
1919
def paged_upvoted_articles(user_id, %{thread: thread} = filter) do
20-
thread_upcase = thread |> to_string |> String.upcase()
21-
where_query = dynamic([a], a.user_id == ^user_id and a.thread == ^thread_upcase)
20+
thread = thread |> to_string |> String.upcase()
21+
where_query = dynamic([a], a.user_id == ^user_id and a.thread == ^thread)
2222

2323
load_upvoted_articles(where_query, filter)
2424
end

lib/groupher_server/cms/cms.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ defmodule GroupherServer.CMS do
1212
ArticleCURD,
1313
ArticleCommunity,
1414
ArticleEmotion,
15+
CitedContent,
1516
CommentCurd,
1617
ArticleCollect,
1718
ArticleUpvote,
@@ -96,6 +97,8 @@ defmodule GroupherServer.CMS do
9697
defdelegate sink_article(thread, id), to: ArticleCURD
9798
defdelegate undo_sink_article(thread, id), to: ArticleCURD
9899

100+
defdelegate paged_citing_contents(type, id, filter), to: CitedContent
101+
99102
defdelegate upvote_article(thread, article_id, user), to: ArticleUpvote
100103
defdelegate undo_upvote_article(thread, article_id, user), to: ArticleUpvote
101104

lib/groupher_server/cms/delegates/article_collect.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleCollect do
4646
update_article_reaction_user_list(:collect, article, user_id, :add)
4747
end)
4848
|> Multi.run(:create_collect, fn _, _ ->
49-
thread_upcase = thread |> to_string |> String.upcase()
50-
args = Map.put(%{user_id: user_id, thread: thread_upcase}, info.foreign_key, article.id)
49+
thread = thread |> to_string |> String.upcase()
50+
args = Map.put(%{user_id: user_id, thread: thread}, info.foreign_key, article.id)
5151

5252
ORM.create(ArticleCollect, args)
5353
end)
@@ -131,8 +131,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleCollect do
131131

132132
defp collection_findby_args(thread, article_id, user_id) do
133133
with {:ok, info} <- match(thread) do
134-
thread_upcase = thread |> to_string |> String.upcase()
135-
%{thread: thread_upcase, user_id: user_id} |> Map.put(info.foreign_key, article_id)
134+
thread = thread |> to_string |> String.upcase()
135+
%{thread: thread, user_id: user_id} |> Map.put(info.foreign_key, article_id)
136136
end
137137
end
138138

lib/groupher_server/cms/delegates/article_community.ex

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
4141
defp pack_pin_args(thread, article_id, community_id) do
4242
with {:ok, info} <- match(thread),
4343
{:ok, community} <- ORM.find(Community, community_id) do
44-
thread_upcase = thread |> to_string |> String.upcase()
44+
thread = thread |> to_string |> String.upcase()
4545

4646
Map.put(
47-
%{community_id: community.id, thread: thread_upcase},
47+
%{community_id: community.id, thread: thread},
4848
info.foreign_key,
4949
article_id
5050
)
@@ -145,12 +145,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
145145

146146
# check if the thread has aready enough pinned articles
147147
defp check_pinned_article_count(community_id, thread) do
148-
thread_upcase = thread |> to_string |> String.upcase()
148+
thread = thread |> to_string |> String.upcase()
149149

150150
query =
151-
from(p in PinnedArticle,
152-
where: p.community_id == ^community_id and p.thread == ^thread_upcase
153-
)
151+
from(p in PinnedArticle, where: p.community_id == ^community_id and p.thread == ^thread)
154152

155153
pinned_articles = query |> Repo.all()
156154

lib/groupher_server/cms/delegates/article_upvote.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleUpvote do
4343
Accounts.achieve(%User{id: achiever_id}, :inc, :upvote)
4444
end)
4545
|> Multi.run(:create_upvote, fn _, _ ->
46-
thread_upcase = thread |> to_string |> String.upcase()
47-
args = Map.put(%{user_id: user_id, thread: thread_upcase}, info.foreign_key, article.id)
46+
thread = thread |> to_string |> String.upcase()
47+
args = Map.put(%{user_id: user_id, thread: thread}, info.foreign_key, article.id)
4848

4949
with {:ok, _} <- ORM.create(ArticleUpvote, args) do
5050
ORM.find(info.model, article.id)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
defmodule GroupherServer.CMS.Delegate.CitedContent do
2+
@moduledoc """
3+
CURD operation on post/job ...
4+
"""
5+
import Ecto.Query, warn: false
6+
7+
import Helper.Utils, only: [done: 1, get_config: 2]
8+
import ShortMaps
9+
10+
alias Helper.Types, as: T
11+
alias GroupherServer.{CMS, Repo}
12+
alias Helper.{ORM, QueryBuilder}
13+
14+
alias CMS.Model.CitedContent
15+
16+
@article_threads get_config(:article, :threads)
17+
18+
@article_preloads @article_threads |> Enum.map(&Keyword.new([{&1, [author: :user]}]))
19+
20+
@comment_article_preloads @article_threads |> Enum.map(&Keyword.new([{:comment, &1}]))
21+
@cited_preloads @article_preloads ++ [[comment: :author] ++ @comment_article_preloads]
22+
23+
@doc "get paged citing contents"
24+
def paged_citing_contents(cited_by_type, cited_by_id, %{page: page, size: size} = filter) do
25+
cited_by_type = cited_by_type |> to_string |> String.upcase()
26+
27+
CitedContent
28+
|> where([c], c.cited_by_id == ^cited_by_id and c.cited_by_type == ^cited_by_type)
29+
|> QueryBuilder.filter_pack(Map.merge(filter, %{sort: :asc_inserted}))
30+
|> ORM.paginater(~m(page size)a)
31+
|> extract_contents
32+
|> done
33+
end
34+
35+
def extract_contents(%{entries: entries} = paged_contents) do
36+
entries = entries |> Repo.preload(@cited_preloads) |> Enum.map(&shape_article(&1))
37+
38+
Map.put(paged_contents, :entries, entries)
39+
end
40+
41+
defp thread_to_atom(thread), do: thread |> String.downcase() |> String.to_atom()
42+
43+
# shape comment cite
44+
@spec shape_article(CitedContent.t()) :: T.cite_info()
45+
defp shape_article(%CitedContent{comment_id: comment_id} = cited) when not is_nil(comment_id) do
46+
%{
47+
block_linker: block_linker,
48+
cited_by_type: cited_by_type,
49+
comment: comment,
50+
inserted_at: inserted_at
51+
} = cited
52+
53+
comment_thread = comment.thread |> String.downcase() |> String.to_atom()
54+
article = comment |> Map.get(comment_thread)
55+
user = comment.author |> Map.take([:login, :nickname, :avatar])
56+
57+
article
58+
|> Map.take([:id, :title])
59+
|> Map.merge(%{
60+
inserted_at: inserted_at,
61+
user: user,
62+
thread: thread_to_atom(cited_by_type),
63+
comment_id: comment.id,
64+
block_linker: block_linker
65+
})
66+
end
67+
68+
# shape general article cite
69+
defp shape_article(%CitedContent{} = cited) do
70+
%{block_linker: block_linker, cited_by_type: cited_by_type, inserted_at: inserted_at} = cited
71+
72+
thread = thread_to_atom(cited_by_type)
73+
article = Map.get(cited, thread)
74+
75+
user = get_in(article, [:author, :user]) |> Map.take([:login, :nickname, :avatar])
76+
77+
article
78+
|> Map.take([:id, :title])
79+
|> Map.merge(%{
80+
user: user,
81+
thread: thread,
82+
block_linker: block_linker,
83+
inserted_at: inserted_at
84+
})
85+
end
86+
end

lib/groupher_server_web/resolvers/cms_resolver.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ defmodule GroupherServerWeb.Resolvers.CMS do
102102
CMS.undo_report_article(thread, id, user)
103103
end
104104

105+
def paged_citing_contents(_root, ~m(content id filter)a, _info) do
106+
CMS.paged_citing_contents(content, id, filter)
107+
end
108+
105109
# #######################
106110
# thread reaction ..
107111
# #######################

lib/groupher_server_web/schema/cms/cms_metrics.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ defmodule GroupherServerWeb.Schema.CMS.Metrics do
4141
value(:share)
4242
end
4343

44+
enum :content do
45+
article_values()
46+
value(:comment)
47+
end
48+
4449
enum :when_enum do
4550
value(:today)
4651
value(:this_week)

lib/groupher_server_web/schema/cms/cms_queries.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ defmodule GroupherServerWeb.Schema.CMS.Queries do
124124
resolve(&R.CMS.paged_reports/3)
125125
end
126126

127+
@desc "paged citings list"
128+
field :paged_citing_contents, :paged_citings do
129+
arg(:id, non_null(:id))
130+
arg(:content, :content, default_value: :post)
131+
arg(:filter, :paged_filter)
132+
133+
resolve(&R.CMS.paged_citing_contents/3)
134+
end
135+
127136
@desc "search communities by title"
128137
field :search_communities, :paged_communities do
129138
arg(:title, non_null(:string))

lib/groupher_server_web/schema/cms/cms_types.ex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,29 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
317317
timestamp_fields()
318318
end
319319

320+
object :citing do
321+
field(:id, :id)
322+
field(:thread, :string)
323+
field(:title, :string)
324+
field(:block_linker, list_of(:string))
325+
field(:comment_id, :id)
326+
field(:user, :common_user)
327+
328+
timestamp_fields()
329+
end
330+
320331
paged_article_objects()
321332

322333
object :paged_reports do
323334
field(:entries, list_of(:abuse_report))
324335
pagination_fields()
325336
end
326337

338+
object :paged_citings do
339+
field(:entries, list_of(:citing))
340+
pagination_fields()
341+
end
342+
327343
object :paged_categories do
328344
field(:entries, list_of(:category))
329345
pagination_fields()

0 commit comments

Comments
 (0)