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.

Commitdf4a280

Browse files
authored
refactor(notification): cut from_users with count (#408)
1 parent80c4f06 commitdf4a280

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

‎lib/groupher_server/delivery/delegates/notification.ex‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ defmodule GroupherServer.Delivery.Delegate.Notification do
1818

1919
@notify_actionsget_config(:general,:nofity_actions)
2020
@notify_group_interval_hourget_config(:general,:notify_group_interval_hour)
21+
@cut_from_users_count3
2122

2223
defhandle(%{action:action,user_id:user_id}=attrs,%User{}=from_user)do
2324
withtrue<-actionin@notify_actions,
@@ -84,9 +85,21 @@ defmodule GroupherServer.Delivery.Delegate.Notification do
8485
Notification
8586
|>where([n],n.user_id==^user.idandn.read==^read)
8687
|>ORM.paginater(~m(page size)a)
88+
|>cut_from_users_ifneed
8789
|>done
8890
end
8991

92+
# @cut_from_users_count
93+
defpcut_from_users_ifneed(%{entries:entries}=paged_contents)do
94+
entries=
95+
Enum.map(entries,fnnotify->
96+
from_users=Enum.slice(notify.from_users,0,@cut_from_users_count)
97+
notify|>Map.put(:from_users,from_users)
98+
end)
99+
100+
paged_contents|>Map.put(:entries,entries)
101+
end
102+
90103
# 注意这里并不是准确的 count, 因为可能有短时间内 merge 到一起的通知
91104
defunread_count(user_id)do
92105
Notification
@@ -114,13 +127,18 @@ defmodule GroupherServer.Delivery.Delegate.Notification do
114127
cur_from_users=notify.from_users|>Enum.map(&strip_struct(&1))
115128
from_users=([from_user]++cur_from_users)|>Enum.uniq()
116129

117-
notify|>ORM.update_embed(:from_users,from_users)
130+
notify
131+
|>Ecto.Changeset.change(%{from_users_count:length(from_users)})
132+
|>Ecto.Changeset.put_embed(:from_users,from_users)
133+
|>Repo.update()
118134
end
119135

120136
# 创建通知
121137
defpcreate_notification(attrs,from_user)do
138+
attrs=attrs|>Map.merge(%{from_users_count:1})|>atom_values_to_upcase
139+
122140
%Notification{}
123-
|>Ecto.Changeset.change(atom_values_to_upcase(attrs))
141+
|>Ecto.Changeset.change(attrs)
124142
|>Ecto.Changeset.put_embed(:from_users,[from_user])
125143
|>Repo.insert()
126144
end

‎lib/groupher_server/delivery/models/notification.ex‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ defmodule GroupherServer.Delivery.Model.Notification do
2424
#
2525
field(:action,:string)
2626
embeds_many(:from_users,Embeds.User,on_replace::delete)
27+
field(:from_users_count,:integer)
2728

2829
field(:read,:boolean,default:false)
2930

‎lib/groupher_server_web/schema/account/account_types.ex‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ defmodule GroupherServerWeb.Schema.Account.Types do
122122
field(:read,:boolean)
123123

124124
field(:from_users,list_of(:common_user))
125+
field(:from_users_count,:integer)
125126

126127
timestamp_fields()
127128
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmoduleGroupherServer.Repo.Migrations.AddFromUsersCountToNotificationdo
2+
useEcto.Migration
3+
4+
defchangedo
5+
altertable(:notifications)do
6+
add(:from_users_count,:integer)
7+
end
8+
end
9+
end

‎test/groupher_server/delivery/notification_test.exs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,28 @@ defmodule GroupherServer.Test.Delivery.Notification do
137137
assertuser2|>user_exist_in?(notify2.from_users)
138138
end
139139

140+
test"notify's from_users_count should work",~m(user user2 user3 user4 notify_attrs)ado
141+
{:ok,user5}=db_insert(:user)
142+
143+
{:ok,_notify}=Delivery.send(:notify,notify_attrs,user2)
144+
{:ok,_}=Delivery.send(:notify,notify_attrs,user3)
145+
{:ok,_}=Delivery.send(:notify,notify_attrs,user4)
146+
{:ok,_}=Delivery.send(:notify,notify_attrs,user5)
147+
148+
{:ok,paged_notifies}=Delivery.fetch(:notification,user,%{page:1,size:10})
149+
150+
assertpaged_notifies.total_count==1
151+
notify=paged_notifies.entries|>List.first()
152+
153+
assertnotify.from_users_count==4
154+
assertlength(notify.from_users)==3
155+
156+
assertuser5|>user_exist_in?(notify.from_users)
157+
assertuser4|>user_exist_in?(notify.from_users)
158+
assertuser3|>user_exist_in?(notify.from_users)
159+
assertnotuser_exist_in?(user2,notify.from_users)
160+
end
161+
140162
test"notify myself got ignored",~m(user notify_attrs)ado
141163
{:error,_}=Delivery.send(:notify,notify_attrs,user)
142164
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp