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.

Commitb1df9c9

Browse files
authored
refactor(follow-viewer-state): debug & re-org (#449)
1 parentb811a0e commitb1df9c9

File tree

17 files changed

+206
-37
lines changed

17 files changed

+206
-37
lines changed

‎lib/groupher_server/accounts/accounts.ex‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ defmodule GroupherServer.Accounts do
2222

2323
defdelegateupdate_profile(user,attrs),to:Profile
2424
defdelegateupdate_geo(user,remote_ip),to:Profile
25-
defdelegateupdate_subscribe_count(user),to:Profile
25+
defdelegateupdate_subscribe_state(user),to:Profile
2626
defdelegategithub_signin(github_user),to:Profile
2727
defdelegatedefault_subscribed_communities(filter),to:Profile
2828
defdelegatesubscribed_communities(user,filter),to:Profile

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule GroupherServer.Accounts.Delegate.Profile do
33
accounts profile
44
"""
55
importEcto.Query,warn:false
6-
importHelper.Utils,only:[done:1,get_config:2]
6+
importHelper.Utils,only:[done:1,get_config:2,ensure:2]
77
importShortMaps
88

99
aliasGroupherServer.{Accounts,CMS,Email,Repo,Statistics}
@@ -80,11 +80,25 @@ defmodule GroupherServer.Accounts.Delegate.Profile do
8080
@doc"""
8181
update user's subscribed communities count
8282
"""
83-
defupdate_subscribe_count(user_id)do
83+
defupdate_subscribe_state(user_id)do
8484
with{:ok,user}<-ORM.find(User,user_id)do
85-
{:ok,count}=from(sinCommunitySubscriber,where:s.user_id==^user.id)|>ORM.count()
86-
87-
user|>ORM.update(%{subscribed_communities_count:count})
85+
query=
86+
from(sinCommunitySubscriber,
87+
where:s.user_id==^user.id,
88+
join:cinassoc(s,:community),
89+
select:c.id
90+
)
91+
92+
subscribed_communities_ids=query|>Repo.all()
93+
subscribed_communities_count=subscribed_communities_ids|>length
94+
95+
user_meta=ensure(user.meta,@default_user_meta)
96+
meta=%{user_meta|subscribed_communities_ids:subscribed_communities_ids}
97+
98+
user
99+
|>ORM.update_meta(meta,
100+
changes:%{subscribed_communities_count:subscribed_communities_count}
101+
)
88102
end
89103
end
90104

‎lib/groupher_server/accounts/models/embeds/user_meta.ex‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ defmodule GroupherServer.Accounts.Model.Embeds.UserMeta do
3838
has_illegal_articles:false,
3939
illegal_articles:[],
4040
has_illegal_comments:false,
41-
illegal_comments:[]
41+
illegal_comments:[],
42+
subscribed_communities_ids:[]
4243
}
4344

4445
@optional_fieldsMap.keys(@general_options)++
@@ -69,6 +70,8 @@ defmodule GroupherServer.Accounts.Model.Embeds.UserMeta do
6970
field(:illegal_articles,{:array,:string},default:[])
7071
field(:illegal_comments,{:array,:string},default:[])
7172

73+
field(:subscribed_communities_ids,{:array,:id},default:[])
74+
7275
published_article_count_fields()
7376
end
7477

‎lib/groupher_server/cms/cms.ex‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ defmodule GroupherServer.CMS do
3535
# Community CURD: editors, thread, tag
3636
defdelegateread_community(args),to:CommunityCURD
3737
defdelegateread_community(args,user),to:CommunityCURD
38+
defdelegatepaged_communities(filter,user),to:CommunityCURD
39+
defdelegatepaged_communities(filter),to:CommunityCURD
3840
defdelegatecreate_community(args),to:CommunityCURD
39-
defdelegateupdate_community(id,args),to:CommunityCURD
4041
defdelegateapply_community(args),to:CommunityCURD
42+
defdelegateupdate_community(id,args),to:CommunityCURD
4143
defdelegateapprove_community_apply(id),to:CommunityCURD
4244
defdelegatedeny_community_apply(id),to:CommunityCURD
4345
defdelegateis_community_exist?(raw),to:CommunityCURD
@@ -52,6 +54,7 @@ defmodule GroupherServer.CMS do
5254
defdelegatecommunity_geo_info(community),to:CommunityCURD
5355
# >> subscribers / editors
5456
defdelegatecommunity_members(type,community,filters),to:CommunityCURD
57+
defdelegatecommunity_members(type,community,filters,user),to:CommunityCURD
5558
# >> category
5659
defdelegatecreate_category(category_attrs,user),to:CommunityCURD
5760
defdelegateupdate_category(category_attrs),to:CommunityCURD
@@ -240,8 +243,10 @@ defmodule GroupherServer.CMS do
240243

241244
# search
242245
defdelegatesearch_articles(thread,args),to:Search
243-
defdelegatesearch_communities(args),to:Search
244-
defdelegatesearch_communities(args,category),to:Search
246+
defdelegatesearch_communities(filter),to:Search
247+
defdelegatesearch_communities(filter,user),to:Search
248+
defdelegatesearch_communities(filter,category),to:Search
249+
defdelegatesearch_communities(filter,category,user),to:Search
245250

246251
# seeds
247252
defdelegateseed_communities(opt),to:Seeds

‎lib/groupher_server/cms/delegates/Seeds/helper.ex‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ defmodule GroupherServer.CMS.Delegate.Seeds.Helper do
8888
ORM.find_all(Category,%{page:1,size:20})
8989
end
9090

91+
defseed_user(name)do
92+
nickname=name
93+
login=name
94+
avatar="https://avatars1.githubusercontent.com/u/6184465?s=460&v=4"
95+
96+
User|>ORM.findby_or_insert(~m(nickname avatar)a,~m(nickname avatar login)a)
97+
end
98+
9199
defseed_bot()do
92100
caseORM.find(User,1)do
93101
{:ok,user}->

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,18 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
6969
update_viewed_user_list(article,user_id)
7070
end)
7171
|>Multi.run(:set_viewer_has_states,fn_,%{normal_read:article}->
72-
article_meta=ifis_nil(article.meta),do:@default_article_meta,else:article.meta
72+
article=Repo.preload(article,:original_community)
73+
article_meta=ensure(article.meta,@default_article_meta)
7374

7475
viewer_has_states=%{
7576
viewer_has_collected:user_idinarticle_meta.collected_user_ids,
7677
viewer_has_upvoted:user_idinarticle_meta.upvoted_user_ids,
7778
viewer_has_reported:user_idinarticle_meta.reported_user_ids
7879
}
7980

80-
article|>Map.merge(viewer_has_states)|>done
81+
article
82+
|>Map.merge(viewer_has_states)
83+
|>done
8184
end)
8285
|>Repo.transaction()
8386
|>result()

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,11 @@ defmodule GroupherServer.CMS.Delegate.CommentCURD do
261261
illegal_comments=user_meta.illegal_comments--illegal_comments
262262
has_illegal_comments=notEnum.empty?(illegal_comments)
263263

264-
meta=
265-
Map.merge(user_meta,%{
266-
has_illegal_comments:has_illegal_comments,
264+
meta=%{
265+
user_meta
266+
|has_illegal_comments:has_illegal_comments,
267267
illegal_comments:illegal_comments
268-
})
268+
}
269269

270270
ORM.update_meta(user,meta)
271271
end

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

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
33
community curd
44
"""
55
importEcto.Query,warn:false
6-
importHelper.Utils,only:[done:1,strip_struct:1,get_config:2,plural:1]
6+
importHelper.Utils,only:[done:1,strip_struct:1,get_config:2,plural:1,ensure:2]
77
importGroupherServer.CMS.Delegate.ArticleCURD,only:[ensure_author_exists:1]
88
importGroupherServer.CMS.Helper.Matcher
99
importShortMaps
@@ -29,6 +29,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
2929
@default_metaEmbeds.CommunityMeta.default_meta()
3030
@article_threadsget_config(:article,:threads)
3131

32+
@default_user_metaAccounts.Model.Embeds.UserMeta.default_meta()
3233
@community_normalConstant.pending(:normal)
3334
@community_applyingConstant.pending(:applying)
3435

@@ -37,6 +38,25 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
3738
defread_community(raw,user),do:read_community(raw)|>viewer_has_states(user)
3839
defread_community(raw),do:do_read_community(raw)
3940

41+
defpaged_communities(filter,%User{id:user_id,meta:meta})do
42+
with{:ok,paged_communtiies}<-paged_communities(filter)do
43+
%{entries:entries}=paged_communtiies
44+
45+
entries=
46+
Enum.map(entries,fncommunity->
47+
viewer_has_subscribed=community.idinmeta.subscribed_communities_ids
48+
%{community|viewer_has_subscribed:viewer_has_subscribed}
49+
end)
50+
51+
%{paged_communtiies|entries:entries}|>done
52+
end
53+
end
54+
55+
defpaged_communities(filter)do
56+
filter=filter|>Enum.reject(fn{_k,v}->is_nil(v)end)|>Enum.into(%{})
57+
Community|>ORM.find_all(filter)
58+
end
59+
4060
@doc"""
4161
create a community
4262
"""
@@ -212,6 +232,22 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
212232
load_community_members(community,CommunityEditor,filters)
213233
end
214234

235+
defcommunity_members(:subscribers,%Community{id:id}=community,filters,%User{meta:meta})
236+
whennotis_nil(id)do
237+
with{:ok,members}<-community_members(:subscribers,community,filters)do
238+
user_meta=ensure(meta,@default_user_meta)
239+
240+
%{entries:entries}=members
241+
242+
entries=
243+
Enum.map(entries,fnmember->
244+
%{member|viewer_has_followed:member.idinuser_meta.following_user_ids}
245+
end)
246+
247+
%{members|entries:entries}|>done
248+
end
249+
end
250+
215251
defcommunity_members(:subscribers,%Community{id:id}=community,filters)
216252
whennotis_nil(id)do
217253
load_community_members(community,CommunitySubscriber,filters)
@@ -295,7 +331,14 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
295331

296332
defpdo_read_community(raw)do
297333
with{:ok,community}<-find_community(raw)do
298-
community|>ORM.read(inc::views)
334+
casecommunity.metado
335+
nil->
336+
{:ok,community}=ORM.update_meta(community,@default_meta)
337+
community|>ORM.read(inc::views)
338+
339+
_->
340+
community|>ORM.read(inc::views)
341+
end
299342
end
300343
end
301344

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
117117
|>Multi.run(:update_community_count,fn_,%{subscribed_community:community}->
118118
CommunityCURD.update_community_count_field(community,user_id,:subscribers_count,:inc)
119119
end)
120-
|>Multi.run(:update_user_subscribe_count,fn_,_->
121-
Accounts.update_subscribe_count(user_id)
120+
|>Multi.run(:update_user_subscribe_state,fn_,_->
121+
Accounts.update_subscribe_state(user_id)
122122
end)
123123
|>Repo.transaction()
124124
|>result()
@@ -137,8 +137,8 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
137137
|>Multi.run(:update_community_count,fn_,%{subscribed_community:community}->
138138
CommunityCURD.update_community_count_field(community,user_id,:subscribers_count,:inc)
139139
end)
140-
|>Multi.run(:update_user_subscribe_count,fn_,_->
141-
Accounts.update_subscribe_count(user_id)
140+
|>Multi.run(:update_user_subscribe_state,fn_,_->
141+
Accounts.update_subscribe_state(user_id)
142142
end)
143143
|>Repo.transaction()
144144
|>result()
@@ -158,8 +158,8 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
158158
|>Multi.run(:update_community_count,fn_,_->
159159
CommunityCURD.update_community_count_field(community,user_id,:subscribers_count,:dec)
160160
end)
161-
|>Multi.run(:update_user_subscribe_count,fn_,_->
162-
Accounts.update_subscribe_count(user_id)
161+
|>Multi.run(:update_user_subscribe_state,fn_,_->
162+
Accounts.update_subscribe_state(user_id)
163163
end)
164164
|>Repo.transaction()
165165
|>result()
@@ -186,8 +186,8 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
186186
|>Multi.run(:update_community_count,fn_,_->
187187
CommunityCURD.update_community_count_field(community,user_id,:subscribers_count,:dec)
188188
end)
189-
|>Multi.run(:update_user_subscribe_count,fn_,_->
190-
Accounts.update_subscribe_count(user_id)
189+
|>Multi.run(:update_user_subscribe_state,fn_,_->
190+
Accounts.update_subscribe_state(user_id)
191191
end)
192192
|>Multi.run(:update_community_geo,fn_,_->
193193
update_community_geo(community_id,user_id,remote_ip,:dec)
@@ -217,8 +217,8 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
217217
|>Multi.run(:update_community_count,fn_,_->
218218
CommunityCURD.update_community_count_field(community,user_id,:subscribers_count,:dec)
219219
end)
220-
|>Multi.run(:update_user_subscribe_count,fn_,_->
221-
Accounts.update_subscribe_count(user_id)
220+
|>Multi.run(:update_user_subscribe_state,fn_,_->
221+
Accounts.update_subscribe_state(user_id)
222222
end)
223223
|>Multi.run(:update_community_geo_city,fn_,_->
224224
update_community_geo_map(community.id,city,:dec)

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ defmodule GroupherServer.CMS.Delegate.Search do
33
search for community, post, job ...
44
"""
55

6-
importHelper.Utils,only:[done:1]
6+
importHelper.Utils,only:[done:1,ensure:2]
77
importEcto.Query,warn:false
88
importGroupherServer.CMS.Helper.Matcher
99

1010
aliasHelper.ORM
11-
aliasGroupherServer.CMS.Model.{Community}
11+
12+
aliasGroupherServer.{Accounts,CMS}
13+
aliasCMS.Model.{Community}
14+
aliasAccounts.Model.{User}
15+
16+
@default_user_metaAccounts.Model.Embeds.UserMeta.default_meta()
1217

1318
@search_items_count15
1419

@@ -19,6 +24,25 @@ defmodule GroupherServer.CMS.Delegate.Search do
1924
do_search_communities(Community,title)
2025
end
2126

27+
defsearch_communities(title,%User{meta:meta})do
28+
with{:ok,communities}<-do_search_communities(Community,title)do
29+
user_meta=ensure(meta,@default_user_meta)
30+
%{entries:entries}=communities
31+
32+
entries=
33+
Enum.map(entries,fncommunity->
34+
viewer_has_subscribed=community.idinuser_meta.subscribed_communities_ids
35+
%{community|viewer_has_subscribed:viewer_has_subscribed}
36+
end)
37+
38+
%{communities|entries:entries}|>done
39+
end
40+
end
41+
42+
defsearch_communities(title,category,%User{meta:meta})do
43+
search_communities(title,category)
44+
end
45+
2246
defsearch_communities(title,category)do
2347
from(
2448
cinCommunity,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp