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

Commit 46c27fc

Browse files
committed
refactor(notify): mark read & re-org ORM functions
1 parent dfba75e commit 46c27fc

File tree

15 files changed

+203
-127
lines changed

15 files changed

+203
-127
lines changed

lib/groupher_server/accounts/delegates/profile.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ defmodule GroupherServer.Accounts.Delegate.Profile do
7272
"""
7373
def update_subscribe_count(user_id) do
7474
with {:ok, user} <- ORM.find(User, user_id) do
75-
count_query = from(s in CommunitySubscriber, where: s.user_id == ^user.id)
76-
count = Repo.aggregate(count_query, :count)
75+
{:ok, count} = from(s in CommunitySubscriber, where: s.user_id == ^user.id) |> ORM.count()
7776

7877
user |> ORM.update(%{subscribed_communities_count: count})
7978
end

lib/groupher_server/cms/delegates/cited_artiment.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ defmodule GroupherServer.CMS.Delegate.CitedArtiment do
7979
# update article/comment 's citting_count in meta
8080
defp update_artiment_citing_count(cited_artiments) do
8181
Enum.all?(cited_artiments, fn cited ->
82-
count_query = from(c in CitedArtiment, where: c.cited_by_id == ^cited.cited_by_id)
83-
count = Repo.aggregate(count_query, :count)
82+
{:ok, count} =
83+
from(c in CitedArtiment, where: c.cited_by_id == ^cited.cited_by_id) |> ORM.count()
8484

8585
artiment = cited.artiment
8686
meta = Map.merge(artiment.meta, %{citing_count: count})

lib/groupher_server/cms/delegates/comment_action.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
4040
{:ok, info} <- match(full_comment.thread) do
4141
Multi.new()
4242
|> Multi.run(:checked_pined_comments_count, fn _, _ ->
43-
count_query =
43+
{:ok, pined_comments_count} =
4444
from(p in PinnedComment,
4545
where: field(p, ^info.foreign_key) == ^full_comment.article.id
4646
)
47-
48-
pined_comments_count = Repo.aggregate(count_query, :count)
47+
|> ORM.count()
4948

5049
case pined_comments_count >= @pinned_comment_limit do
5150
true -> {:error, "max #{@pinned_comment_limit} pinned comment for each article"}
@@ -163,8 +162,9 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
163162
update_upvoted_user_list(comment, user_id, :add)
164163
end)
165164
|> Multi.run(:inc_upvotes_count, fn _, %{add_upvoted_user: comment} ->
166-
count_query = from(c in CommentUpvote, where: c.comment_id == ^comment.id)
167-
upvotes_count = Repo.aggregate(count_query, :count)
165+
{:ok, upvotes_count} =
166+
from(c in CommentUpvote, where: c.comment_id == ^comment.id) |> ORM.count()
167+
168168
ORM.update(comment, %{upvotes_count: upvotes_count})
169169
end)
170170
|> Multi.run(:check_article_author_upvoted, fn _, %{inc_upvotes_count: comment} ->
@@ -202,8 +202,8 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
202202
update_upvoted_user_list(comment, user_id, :remove)
203203
end)
204204
|> Multi.run(:desc_upvotes_count, fn _, %{remove_upvoted_user: comment} ->
205-
count_query = from(c in CommentUpvote, where: c.comment_id == ^comment_id)
206-
upvotes_count = Repo.aggregate(count_query, :count)
205+
{:ok, upvotes_count} =
206+
from(c in CommentUpvote, where: c.comment_id == ^comment_id) |> ORM.count()
207207

208208
ORM.update(comment, %{upvotes_count: Enum.max([upvotes_count - 1, 0])})
209209
end)

lib/groupher_server/cms/delegates/comment_curd.ex

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,9 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
251251
def update_comments_count(%Comment{} = comment, opt) do
252252
with {:ok, article_info} <- match(:comment_article, comment),
253253
{:ok, article} <- ORM.find(article_info.model, article_info.id) do
254-
count_query =
254+
{:ok, cur_count} =
255255
from(c in Comment, where: field(c, ^article_info.foreign_key) == ^article_info.id)
256-
257-
cur_count = Repo.aggregate(count_query, :count)
256+
|> ORM.count()
258257

259258
# dec 是 comment 还没有删除的时候的操作,和 inc 不同
260259
# 因为 dec 操作如果放在 delete 后面,那么 update 会失败
@@ -405,8 +404,11 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
405404

406405
# get next floor under an article's comments list
407406
defp next_floor(article, foreign_key) do
408-
count_query = from(c in Comment, where: field(c, ^foreign_key) == ^article.id)
409-
Repo.aggregate(count_query, :count) + 1
407+
{:ok, cur_count} =
408+
from(c in Comment, where: field(c, ^foreign_key) == ^article.id)
409+
|> ORM.count()
410+
411+
cur_count + 1
410412
end
411413

412414
defp result({:ok, %{set_question_flag_ifneed: result}}), do: {:ok, result}

lib/groupher_server/cms/delegates/community_curd.ex

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
5858
update editors_count of a community
5959
"""
6060
def update_community_count_field(%Community{} = community, user_id, :editors_count, opt) do
61-
count_query = from(s in CommunityEditor, where: s.community_id == ^community.id)
62-
editors_count = Repo.aggregate(count_query, :count)
61+
{:ok, editors_count} =
62+
from(s in CommunityEditor, where: s.community_id == ^community.id)
63+
|> ORM.count()
64+
6365
community_meta = if is_nil(community.meta), do: @default_meta, else: community.meta
6466

6567
editors_ids =
@@ -80,8 +82,9 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
8082
update article_tags_count of a community
8183
"""
8284
def update_community_count_field(%Community{} = community, :article_tags_count) do
83-
count_query = from(t in ArticleTag, where: t.community_id == ^community.id)
84-
article_tags_count = Repo.aggregate(count_query, :count)
85+
{:ok, article_tags_count} =
86+
from(t in ArticleTag, where: t.community_id == ^community.id)
87+
|> ORM.count()
8588

8689
community
8790
|> Ecto.Changeset.change(%{article_tags_count: article_tags_count})
@@ -92,8 +95,9 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
9295
update subscribers_count of a community
9396
"""
9497
def update_community_count_field(%Community{} = community, user_id, :subscribers_count, opt) do
95-
count_query = from(s in CommunitySubscriber, where: s.community_id == ^community.id)
96-
subscribers_count = Repo.aggregate(count_query, :count)
98+
{:ok, subscribers_count} =
99+
from(s in CommunitySubscriber, where: s.community_id == ^community.id) |> ORM.count()
100+
97101
community_meta = if is_nil(community.meta), do: @default_meta, else: community.meta
98102

99103
subscribed_user_ids =
@@ -122,22 +126,18 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
122126
"""
123127
def update_community_count_field(%Community{} = community, thread) do
124128
with {:ok, info} <- match(thread) do
125-
count_query =
129+
{:ok, thread_article_count} =
126130
from(a in info.model,
127131
join: c in assoc(a, :communities),
128-
where: a.mark_delete == false,
129-
where: c.id == ^community.id
132+
where: a.mark_delete == false and c.id == ^community.id
130133
)
134+
|> ORM.count()
131135

132-
thread_article_count = Repo.aggregate(count_query, :count)
133136
community_meta = if is_nil(community.meta), do: @default_meta, else: community.meta
134-
135-
meta = community_meta |> Map.put(:"#{thread}s_count", thread_article_count) |> strip_struct
137+
meta = Map.put(community_meta, :"#{thread}s_count", thread_article_count)
136138

137139
community
138-
|> Ecto.Changeset.change(%{articles_count: recount_articles_count(meta)})
139-
|> Ecto.Changeset.put_embed(:meta, meta)
140-
|> Repo.update()
140+
|> ORM.update_meta(meta, changes: %{articles_count: recount_articles_count(meta)})
141141
end
142142
end
143143

lib/groupher_server/cms/delegates/helper.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ defmodule GroupherServer.CMS.Delegate.Helper do
157157
:collects_count -> ArticleCollect
158158
end
159159

160-
count_query = from(u in schema, where: field(u, ^info.foreign_key) == ^article.id)
161-
cur_count = Repo.aggregate(count_query, :count)
160+
{:ok, cur_count} =
161+
from(u in schema, where: field(u, ^info.foreign_key) == ^article.id) |> ORM.count()
162162

163163
case opt do
164164
:inc ->

lib/groupher_server/delivery/delegates/mention.ex

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ defmodule GroupherServer.Delivery.Delegate.Mention do
6767
read = Map.get(filter, :read, false)
6868

6969
Mention
70-
|> where([m], m.to_user_id == ^user.id)
71-
|> where([m], m.read == ^read)
70+
|> where([m], m.to_user_id == ^user.id and m.read == ^read)
7271
|> ORM.paginater(~m(page size)a)
7372
|> extract_mentions
7473
|> done()
@@ -78,19 +77,21 @@ defmodule GroupherServer.Delivery.Delegate.Mention do
7877
def unread_count(user_id) do
7978
Mention
8079
|> where([m], m.to_user_id == ^user_id and m.read == false)
81-
|> Repo.aggregate(:count)
82-
|> done
80+
|> ORM.count()
8381
end
8482

83+
@doc "mark mention in ids as readed"
8584
def mark_read(ids, %User{} = user) when is_list(ids) do
86-
query = Mention |> where([m], m.id in ^ids and m.to_user_id == ^user.id and m.read == false)
87-
88-
result = Repo.update_all(query, set: [read: true])
85+
Mention
86+
|> where([m], m.id in ^ids and m.to_user_id == ^user.id and m.read == false)
87+
|> ORM.mark_read_all()
88+
end
8989

90-
case result do
91-
{0, nil} -> {:error, "no such mentions found"}
92-
_ -> {:ok, :done}
93-
end
90+
@doc "mark all related mentions as readed"
91+
def mark_read_all(%User{} = user) do
92+
Mention
93+
|> where([m], m.to_user_id == ^user.id and m.read == false)
94+
|> ORM.mark_read_all()
9495
end
9596

9697
defp batch_delete_mentions(%Comment{} = comment, %User{} = from_user) do
@@ -138,8 +139,5 @@ defmodule GroupherServer.Delivery.Delegate.Mention do
138139
end
139140

140141
defp result({:ok, %{batch_insert_mentions: result}}), do: {:ok, result}
141-
142-
defp result({:error, _, result, _steps}) do
143-
{:error, result}
144-
end
142+
defp result({:error, _, result, _steps}), do: {:error, result}
145143
end

lib/groupher_server/delivery/delegates/notification.ex

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ defmodule GroupherServer.Delivery.Delegate.Notification do
6161
read = Map.get(filter, :read, false)
6262

6363
Notification
64-
|> where([n], n.user_id == ^user_id)
65-
|> where([n], n.read == ^read)
64+
|> where([n], n.user_id == ^user_id and n.read == ^read)
6665
|> ORM.paginater(~m(page size)a)
6766
|> done
6867
end
@@ -71,8 +70,21 @@ defmodule GroupherServer.Delivery.Delegate.Notification do
7170
def unread_count(user_id) do
7271
Notification
7372
|> where([n], n.user_id == ^user_id and n.read == false)
74-
|> Repo.aggregate(:count)
75-
|> done
73+
|> ORM.count()
74+
end
75+
76+
@doc "mark notification in ids as readed"
77+
def mark_read(ids, %User{} = user) when is_list(ids) do
78+
Notification
79+
|> where([m], m.id in ^ids and m.user_id == ^user.id and m.read == false)
80+
|> ORM.mark_read_all()
81+
end
82+
83+
@doc "mark all related notifications as readed"
84+
def mark_read_all(%User{} = user) do
85+
Notification
86+
|> where([m], m.user_id == ^user.id and m.read == false)
87+
|> ORM.mark_read_all()
7688
end
7789

7890
# 如果在临近时间段内有类似操作,直接将这次的操作人添加到 from_users 中即可
@@ -172,9 +184,7 @@ defmodule GroupherServer.Delivery.Delegate.Notification do
172184

173185
# 确保 key 存在,并且不为 nil
174186
defp all_exist?(attrs, keys) when is_map(attrs) and is_list(keys) do
175-
Enum.all?(keys, fn key ->
176-
Map.has_key?(attrs, key) and not is_nil(attrs[key])
177-
end)
187+
Enum.all?(keys, &(Map.has_key?(attrs, &1) and not is_nil(attrs[&1])))
178188
end
179189

180190
# 此时间段内的相似通知会被 merge

lib/groupher_server/delivery/delegates/postman.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@ defmodule GroupherServer.Delivery.Delegate.Postman do
1111

1212
def send(:notify, attrs, from_user), do: Notification.handle(attrs, from_user)
1313
def revoke(:notify, attrs, from_user), do: Notification.revoke(attrs, from_user)
14+
1415
def fetch(:mention, user, filter), do: Mention.paged_mentions(user, filter)
1516
def fetch(:notification, user, filter), do: Notification.paged_notifications(user, filter)
1617

1718
def unread_count(:mention, user_id), do: Mention.unread_count(user_id)
1819
def unread_count(:notification, user_id), do: Notification.unread_count(user_id)
1920

2021
def mark_read(:mention, ids, user), do: Mention.mark_read(ids, user)
21-
# def mark_read(:mention, ids, user), do: false
22-
# def mark_read_all(:mention, id, user), do: false
22+
def mark_read(:notification, ids, user), do: Notification.mark_read(ids, user)
23+
24+
def mark_read_all(:mention, user), do: Mention.mark_read_all(user)
25+
def mark_read_all(:notification, user), do: Notification.mark_read_all(user)
2326

2427
# def send(_, _, _), do: {:error, "delivery, not such service"}
2528
# def send(_, _, _, _), do: {:error, "delivery, not such service"}

lib/groupher_server/delivery/delivery.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ defmodule GroupherServer.Delivery do
1515
defdelegate unread_count(service, user), to: Postman
1616

1717
defdelegate mark_read(service, ids, user), to: Postman
18+
defdelegate mark_read_all(service, user), to: Postman
1819
# system_notifications
1920
defdelegate publish_system_notification(info), to: Notifications
2021
defdelegate fetch_sys_notifications(user, filter), to: Notifications

0 commit comments

Comments
 (0)