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.

Commit5a97331

Browse files
authored
refactor(user-published): improve (#374)
* refactor(user): extract published queries* refactor(user): wip* refactor(user): wip* refactor(user): use new matcher in publish* refactor(user): seperate published tests* refactor(user): seperate published wip* refactor(user): seperate published wip* refactor(user): published article states in meta* refactor(user): done* refactor(user): add paged prefix on api
1 parent83e1387 commit5a97331

File tree

26 files changed

+780
-434
lines changed

26 files changed

+780
-434
lines changed

‎lib/groupher_server/accounts/accounts.ex‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ defmodule GroupherServer.Accounts do
4646
# defdelegate paged_editable_communities(filter), to: Achievements
4747

4848
# publish
49-
defdelegatepublished_contents(user,thread,filter),to:Publish
49+
defdelegatepaged_published_articles(user,thread,filter),to:Publish
5050
defdelegatepublished_comments(user,thread,filter),to:Publish
51+
defdelegatepaged_published_article_comments(user,thread,filter),to:Publish
52+
defdelegatepaged_published_article_comments(user,thread),to:Publish
53+
defdelegateupdate_published_states(user,thread),to:Publish
5154

5255
# fans
5356
defdelegatefollow(user,follower),to:Fans

‎lib/groupher_server/accounts/delegates/publish.ex‎

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,89 @@ defmodule GroupherServer.Accounts.Delegate.Publish do
33
user followers / following related
44
"""
55
importEcto.Query,warn:false
6-
importHelper.Utils,only:[done:1]
6+
importHelper.Utils,only:[done:1,ensure:2]
77
# import Helper.ErrorCode
88
importShortMaps
99

10+
importGroupherServer.CMS.Helper.Matcher
1011
importGroupherServer.CMS.Helper.MatcherOld
1112

13+
aliasGroupherServer.Accounts.{Embeds,User}
14+
aliasGroupherServer.CMS.ArticleComment
1215
aliasHelper.{ORM,QueryBuilder}
13-
# alias GroupherServer.{Accounts, Repo}
1416

15-
aliasGroupherServer.Accounts.User
16-
# alias GroupherServer.CMS
17+
@default_metaEmbeds.UserMeta.default_meta()
1718

1819
@doc"""
1920
get paged published contets of a user
2021
"""
21-
defpublished_contents(%User{id:user_id},thread,%{page:page,size:size}=filter)do
22-
with{:ok,user}<-ORM.find(User,user_id),
23-
{:ok,content}<-match_action(thread,:self)do
24-
content.target
25-
|>join(:inner,[content],authorinassoc(content,:author))
26-
|>where([content,author],author.user_id==^user.id)
27-
|>select([content,author],content)
22+
defpaged_published_articles(%User{id:user_id},thread,filter)do
23+
with{:ok,info}<-match(thread),
24+
{:ok,user}<-ORM.find(User,user_id)do
25+
do_paged_published_articles(info.model,user,filter)
26+
end
27+
end
28+
29+
@doc"""
30+
update published articles count in user meta
31+
"""
32+
defupdate_published_states(user_id,thread)do
33+
filter=%{page:1,size:1}
34+
35+
with{:ok,info}<-match(thread),
36+
{:ok,user}<-ORM.find(User,user_id),
37+
{:ok,paged_published_articles}<-do_paged_published_articles(info.model,user,filter)do
38+
articles_count=paged_published_articles.total_count
39+
40+
meta=
41+
ensure(user.meta,@default_meta)|>Map.put(:"published_#{thread}s_count",articles_count)
42+
43+
ORM.update_meta(user,meta)
44+
end
45+
end
46+
47+
defpdo_paged_published_articles(queryable,%User{}=user,%{page:page,size:size}=filter)do
48+
queryable
49+
|>join(:inner,[article],authorinassoc(article,:author))
50+
|>where([article,author],author.user_id==^user.id)
51+
|>select([article,author],article)
52+
|>QueryBuilder.filter_pack(filter)
53+
|>ORM.paginater(~m(page size)a)
54+
|>done()
55+
end
56+
57+
defpaged_published_article_comments(%User{id:user_id},%{page:page,size:size}=filter)do
58+
with{:ok,user}<-ORM.find(User,user_id)do
59+
ArticleComment
60+
|>join(:inner,[comment],authorinassoc(comment,:author))
61+
|>where([comment,author],author.id==^user.id)
62+
|>QueryBuilder.filter_pack(filter)
63+
|>ORM.paginater(~m(page size)a)
64+
|>ORM.extract_and_assign_article()
65+
|>done()
66+
end
67+
end
68+
69+
defpaged_published_article_comments(
70+
%User{id:user_id},
71+
thread,
72+
%{page:page,size:size}=filter
73+
)do
74+
with{:ok,user}<-ORM.find(User,user_id)do
75+
thread=thread|>to_string|>String.upcase()
76+
thread_atom=thread|>String.downcase()|>String.to_atom()
77+
78+
# article_preload = Keyword.new([{thread_atom, :author}])
79+
# query = from(comment in ArticleComment, preload: ^article_preload)
80+
query=from(commentinArticleComment,preload:^thread_atom)
81+
82+
query
83+
|>join(:inner,[comment],authorinassoc(comment,:author))
84+
|>where([comment,author],author.id==^user.id)
85+
|>where([comment,author],comment.thread==^thread)
2886
|>QueryBuilder.filter_pack(filter)
2987
|>ORM.paginater(~m(page size)a)
88+
|>ORM.extract_and_assign_article()
3089
|>done()
3190
end
3291
end
Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,51 @@
1+
defmoduleGroupherServer.Accounts.Embeds.UserMeta.Macrodo
2+
@moduledocfalse
3+
4+
importHelper.Utils,only:[get_config:2]
5+
6+
@article_threadsget_config(:article,:article_threads)
7+
8+
defmacropublished_article_count_fields()do
9+
@article_threads
10+
|>Enum.map(fnthread->
11+
quotedo
12+
field(unquote(:"published_#{thread}s_count"),:integer,default:0)
13+
end
14+
end)
15+
end
16+
end
17+
118
defmoduleGroupherServer.Accounts.Embeds.UserMetado
219
@moduledoc"""
3-
general article meta info forarticle-like content, like post, job, works ...
20+
general article meta info forarticles
421
"""
522
useEcto.Schema
623
useAccessible
24+
725
importEcto.Changeset
26+
importGroupherServer.Accounts.Embeds.UserMeta.Macro
27+
importHelper.Utils,only:[get_config:2]
828

9-
@optional_fields~w(reported_count follower_user_ids following_user_ids)a
29+
@article_threadsget_config(:article,:article_threads)
1030

11-
@default_meta%{
31+
@general_options%{
1232
reported_count:0,
1333
reported_user_ids:[],
1434
follower_user_ids:[],
1535
following_user_ids:[]
1636
}
1737

18-
@doc"for test usage"
19-
defdefault_meta(),do:@default_meta
38+
@optional_fieldsMap.keys(@general_options)++
39+
Enum.map(@article_threads,&:"published_#{&1}s_count")
40+
41+
defdefault_meta()do
42+
published_article_counts=
43+
@article_threads
44+
|>Enum.reduce([],&(&2++["published_#{&1}s_count":0]))
45+
|>Enum.into(%{})
46+
47+
@general_options|>Map.merge(published_article_counts)
48+
end
2049

2150
embedded_schemado
2251
field(:reported_count,:integer,default:0)
@@ -25,10 +54,11 @@ defmodule GroupherServer.Accounts.Embeds.UserMeta do
2554
# TODO: 怎样处理历史数据 ?
2655
field(:follower_user_ids,{:array,:integer},default:[])
2756
field(:following_user_ids,{:array,:integer},default:[])
57+
58+
published_article_count_fields()
2859
end
2960

3061
defchangeset(struct,params)do
31-
struct
32-
|>cast(params,@optional_fields)
62+
struct|>cast(params,@optional_fields)
3363
end
3464
end

‎lib/groupher_server/cms/article_comment.ex‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule GroupherServer.CMS.ArticleComment do
1717
@article_threadsget_config(:article,:article_threads)
1818

1919
@required_fields~w(body_html author_id)a
20-
@optional_fields~w(reply_to_id replies_count is_folded is_deleted floor is_article_author)a
20+
@optional_fields~w(reply_to_id replies_count is_folded is_deleted floor is_article_author thread)a
2121
@updatable_fields~w(is_folded is_deleted floor upvotes_count is_pinned)a
2222

2323
@article_fields@article_threads|>Enum.map(&:"#{&1}_id")
@@ -41,8 +41,6 @@ defmodule GroupherServer.CMS.ArticleComment do
4141

4242
@doc"操作某 emotion 的最近用户"
4343
defmax_latest_emotion_users_count(),do:@max_latest_emotion_users_count
44-
45-
defsupported_emotions(),do:@supported_emotions
4644
defdelete_hint(),do:@delete_hint
4745

4846
defreport_threshold_for_fold,do:@report_threshold_for_fold
@@ -52,6 +50,7 @@ defmodule GroupherServer.CMS.ArticleComment do
5250
schema"articles_comments"do
5351
belongs_to(:author,Accounts.User,foreign_key::author_id)
5452

53+
field(:thread,:string)
5554
field(:body_html,:string)
5655
# 是否被折叠
5756
field(:is_folded,:boolean,default:false)

‎lib/groupher_server/cms/article_user_emotion.ex‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
defmoduleGroupherServer.CMS.ArticleUserEmotion.Macrosdo
22
importHelper.Utils,only:[get_config:2]
33

4-
aliasGroupherServer.CMS
5-
64
@supported_emotionsget_config(:article,:supported_emotions)
75

86
defmacroemotion_fields()do

‎lib/groupher_server/cms/delegates/article_comment.ex‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
175175
# TODO: parse editor-json
176176
# set default emotions
177177
defdo_create_comment(content,foreign_key,article,%User{id:user_id})do
178+
thread=foreign_key|>to_string|>String.split("_id")|>List.first()|>String.upcase()
179+
178180
count_query=from(cinArticleComment,where:field(c,^foreign_key)==^article.id)
179181
floor=Repo.aggregate(count_query,:count)+1
180182

@@ -187,6 +189,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
187189
emotions:@default_emotions,
188190
floor:floor,
189191
is_article_author:user_id==article.author.user.id,
192+
thread:thread,
190193
meta:@default_comment_meta
191194
},
192195
foreign_key,

‎lib/groupher_server/cms/delegates/article_comment_action.ex‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
108108
|>Multi.run(:create_reply_comment,fn_,_->
109109
do_create_comment(content,info.foreign_key,article,user)
110110
end)
111-
|>Multi.run(:update_article_comments_count,fn_,
112-
%{create_reply_comment:replyed_comment}->
111+
|>Multi.run(:update_comments_count,fn_,%{create_reply_comment:replyed_comment}->
113112
update_article_comments_count(replyed_comment,:inc)
114113
end)
115-
|>Multi.run(:create_article_comment_reply,fn_,
116-
%{create_reply_comment:replyed_comment}->
114+
|>Multi.run(:create_comment_reply,fn_,%{create_reply_comment:replyed_comment}->
117115
ArticleCommentReply
118116
|>ORM.create(%{article_comment_id:replyed_comment.id,reply_to_id:replying_comment.id})
119117
end)

‎lib/groupher_server/cms/delegates/article_curd.ex‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,12 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
121121
end
122122

123123
@doc"""
124-
Creates a article(post/job ...), and set community.
124+
Creates a article
125125
126126
## Examples
127127
128-
iex>create_post(%{field: value})
128+
iex>create_article(community, :post, %{title: ...}, user)
129129
{:ok, %Post{}}
130-
131-
iex> create_post(%{field: bad_value})
132-
{:error, %Ecto.Changeset{}}
133-
134130
"""
135131
defcreate_article(%Community{id:cid},thread,attrs,%User{id:uid})do
136132
with{:ok,author}<-ensure_author_exists(%User{id:uid}),
@@ -149,6 +145,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
149145
|>Multi.run(:update_community_article_count,fn_,_->
150146
CommunityCURD.update_community_count_field(community,thread)
151147
end)
148+
|>Multi.run(:update_user_published_meta,fn_,_->
149+
Accounts.update_published_states(uid,thread)
150+
end)
152151
|>Multi.run(:mention_users,fn_,%{create_article:article}->
153152
Delivery.mention_from_content(community.raw,thread,article,attrs,%User{id:uid})
154153
{:ok,:pass}
@@ -448,7 +447,5 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
448447
defpresult({:ok,%{update_article:result}}),do:{:ok,result}
449448
defpresult({:ok,%{set_viewer_has_states:result}}),do:result|>done()
450449

451-
defpresult({:error,_,result,_steps})do
452-
{:error,result}
453-
end
450+
defpresult({:error,_,result,_steps}),do:{:error,result}
454451
end

‎lib/groupher_server_web/resolvers/accounts_resolver.ex‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,32 @@ defmodule GroupherServerWeb.Resolvers.Accounts do
179179
end
180180

181181
# published contents
182-
defpublished_contents(_root,~m(user_id filter thread)a,_info)do
183-
Accounts.published_contents(%User{id:user_id},thread,filter)
182+
defpaged_published_articles(_root,~m(login filter thread)a,_info)do
183+
with{:ok,user_id}<-Accounts.get_userid_and_cache(login)do
184+
Accounts.paged_published_articles(%User{id:user_id},thread,filter)
185+
else
186+
_->raise_error(:not_exsit,"#{login} not found")
187+
end
188+
end
189+
190+
defpaged_published_articles(_root,~m(filter thread)a,%{context:%{cur_user:cur_user}})do
191+
Accounts.paged_published_articles(cur_user,thread,filter)
192+
end
193+
194+
defpaged_published_article_comments(_root,~m(login filter thread)a,_info)do
195+
with{:ok,user_id}<-Accounts.get_userid_and_cache(login)do
196+
Accounts.paged_published_article_comments(%User{id:user_id},thread,filter)
197+
else
198+
_->raise_error(:not_exsit,"#{login} not found")
199+
end
184200
end
185201

186-
defpublished_contents(_root,~m(filter thread)a,%{context:%{cur_user:cur_user}})do
187-
Accounts.published_contents(cur_user,thread,filter)
202+
defpaged_published_article_comments(_root,~m(login filter)a,_info)do
203+
with{:ok,user_id}<-Accounts.get_userid_and_cache(login)do
204+
Accounts.paged_published_article_comments(%User{id:user_id},filter)
205+
else
206+
_->raise_error(:not_exsit,"#{login} not found")
207+
end
188208
end
189209

190210
# published comments

‎lib/groupher_server_web/schema/Helper/queries.ex‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ defmodule GroupherServerWeb.Schema.Helper.Queries do
99

1010
@article_threadsget_config(:article,:article_threads)
1111

12+
# user published articles
13+
defmacropublished_article_queries()do
14+
@article_threads
15+
|>Enum.map(fnthread->
16+
quotedo
17+
@descunquote("paged published#{thread}s")
18+
fieldunquote(:"paged_published_#{thread}s"),unquote(:"paged_#{thread}s")do
19+
arg(:login,non_null(:string))
20+
arg(:filter,non_null(:paged_filter))
21+
arg(:thread,:thread,default_value:unquote(thread))
22+
23+
middleware(M.PageSizeProof)
24+
resolve(&R.Accounts.paged_published_articles/3)
25+
end
26+
end
27+
end)
28+
end
29+
1230
defmacroarticle_search_queries()do
1331
@article_threads
1432
|>Enum.map(fnthread->

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp