Thanks to visit codestin.com
Credit goes to github.com

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

Commit b1df9c9

Browse files
authored
refactor(follow-viewer-state): debug & re-org (#449)
1 parent b811a0e commit b1df9c9

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
defdelegate update_profile(user, attrs), to: Profile
2424
defdelegate update_geo(user, remote_ip), to: Profile
25-
defdelegate update_subscribe_count(user), to: Profile
25+
defdelegate update_subscribe_state(user), to: Profile
2626
defdelegate github_signin(github_user), to: Profile
2727
defdelegate default_subscribed_communities(filter), to: Profile
2828
defdelegate subscribed_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
import Ecto.Query, warn: false
6-
import Helper.Utils, only: [done: 1, get_config: 2]
6+
import Helper.Utils, only: [done: 1, get_config: 2, ensure: 2]
77
import ShortMaps
88

99
alias GroupherServer.{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-
def update_subscribe_count(user_id) do
83+
def update_subscribe_state(user_id) do
8484
with {:ok, user} <- ORM.find(User, user_id) do
85-
{:ok, count} = from(s in CommunitySubscriber, where: s.user_id == ^user.id) |> ORM.count()
86-
87-
user |> ORM.update(%{subscribed_communities_count: count})
85+
query =
86+
from(s in CommunitySubscriber,
87+
where: s.user_id == ^user.id,
88+
join: c in assoc(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_fields Map.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
defdelegate read_community(args), to: CommunityCURD
3737
defdelegate read_community(args, user), to: CommunityCURD
38+
defdelegate paged_communities(filter, user), to: CommunityCURD
39+
defdelegate paged_communities(filter), to: CommunityCURD
3840
defdelegate create_community(args), to: CommunityCURD
39-
defdelegate update_community(id, args), to: CommunityCURD
4041
defdelegate apply_community(args), to: CommunityCURD
42+
defdelegate update_community(id, args), to: CommunityCURD
4143
defdelegate approve_community_apply(id), to: CommunityCURD
4244
defdelegate deny_community_apply(id), to: CommunityCURD
4345
defdelegate is_community_exist?(raw), to: CommunityCURD
@@ -52,6 +54,7 @@ defmodule GroupherServer.CMS do
5254
defdelegate community_geo_info(community), to: CommunityCURD
5355
# >> subscribers / editors
5456
defdelegate community_members(type, community, filters), to: CommunityCURD
57+
defdelegate community_members(type, community, filters, user), to: CommunityCURD
5558
# >> category
5659
defdelegate create_category(category_attrs, user), to: CommunityCURD
5760
defdelegate update_category(category_attrs), to: CommunityCURD
@@ -240,8 +243,10 @@ defmodule GroupherServer.CMS do
240243

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

246251
# seeds
247252
defdelegate seed_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+
def seed_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
def seed_bot() do
92100
case ORM.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 = if is_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_id in article_meta.collected_user_ids,
7677
viewer_has_upvoted: user_id in article_meta.upvoted_user_ids,
7778
viewer_has_reported: user_id in article_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 = not Enum.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
import Ecto.Query, warn: false
6-
import Helper.Utils, only: [done: 1, strip_struct: 1, get_config: 2, plural: 1]
6+
import Helper.Utils, only: [done: 1, strip_struct: 1, get_config: 2, plural: 1, ensure: 2]
77
import GroupherServer.CMS.Delegate.ArticleCURD, only: [ensure_author_exists: 1]
88
import GroupherServer.CMS.Helper.Matcher
99
import ShortMaps
@@ -29,6 +29,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
2929
@default_meta Embeds.CommunityMeta.default_meta()
3030
@article_threads get_config(:article, :threads)
3131

32+
@default_user_meta Accounts.Model.Embeds.UserMeta.default_meta()
3233
@community_normal Constant.pending(:normal)
3334
@community_applying Constant.pending(:applying)
3435

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

41+
def paged_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, fn community ->
47+
viewer_has_subscribed = community.id in meta.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+
def paged_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+
def community_members(:subscribers, %Community{id: id} = community, filters, %User{meta: meta})
236+
when not is_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, fn member ->
244+
%{member | viewer_has_followed: member.id in user_meta.following_user_ids}
245+
end)
246+
247+
%{members | entries: entries} |> done
248+
end
249+
end
250+
215251
def community_members(:subscribers, %Community{id: id} = community, filters)
216252
when not is_nil(id) do
217253
load_community_members(community, CommunitySubscriber, filters)
@@ -295,7 +331,14 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
295331

296332
defp do_read_community(raw) do
297333
with {:ok, community} <- find_community(raw) do
298-
community |> ORM.read(inc: :views)
334+
case community.meta do
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-
import Helper.Utils, only: [done: 1]
6+
import Helper.Utils, only: [done: 1, ensure: 2]
77
import Ecto.Query, warn: false
88
import GroupherServer.CMS.Helper.Matcher
99

1010
alias Helper.ORM
11-
alias GroupherServer.CMS.Model.{Community}
11+
12+
alias GroupherServer.{Accounts, CMS}
13+
alias CMS.Model.{Community}
14+
alias Accounts.Model.{User}
15+
16+
@default_user_meta Accounts.Model.Embeds.UserMeta.default_meta()
1217

1318
@search_items_count 15
1419

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

27+
def search_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, fn community ->
34+
viewer_has_subscribed = community.id in user_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+
def search_communities(title, category, %User{meta: meta}) do
43+
search_communities(title, category)
44+
end
45+
2246
def search_communities(title, category) do
2347
from(
2448
c in Community,

0 commit comments

Comments
 (0)