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

Commit df4a280

Browse files
authored
refactor(notification): cut from_users with count (#408)
1 parent 80c4f06 commit df4a280

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

lib/groupher_server/delivery/delegates/notification.ex

+20-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ defmodule GroupherServer.Delivery.Delegate.Notification do
1818

1919
@notify_actions get_config(:general, :nofity_actions)
2020
@notify_group_interval_hour get_config(:general, :notify_group_interval_hour)
21+
@cut_from_users_count 3
2122

2223
def handle(%{action: action, user_id: user_id} = attrs, %User{} = from_user) do
2324
with true <- action in @notify_actions,
@@ -84,9 +85,21 @@ defmodule GroupherServer.Delivery.Delegate.Notification do
8485
Notification
8586
|> where([n], n.user_id == ^user.id and n.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+
defp cut_from_users_ifneed(%{entries: entries} = paged_contents) do
94+
entries =
95+
Enum.map(entries, fn notify ->
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
def unread_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
defp create_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

+1
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

+1
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule GroupherServer.Repo.Migrations.AddFromUsersCountToNotification do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:notifications) do
6+
add(:from_users_count, :integer)
7+
end
8+
end
9+
end

test/groupher_server/delivery/notification_test.exs

+22
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,28 @@ defmodule GroupherServer.Test.Delivery.Notification do
137137
assert user2 |> 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)a do
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+
assert paged_notifies.total_count == 1
151+
notify = paged_notifies.entries |> List.first()
152+
153+
assert notify.from_users_count == 4
154+
assert length(notify.from_users) == 3
155+
156+
assert user5 |> user_exist_in?(notify.from_users)
157+
assert user4 |> user_exist_in?(notify.from_users)
158+
assert user3 |> user_exist_in?(notify.from_users)
159+
assert not user_exist_in?(user2, notify.from_users)
160+
end
161+
140162
test "notify myself got ignored", ~m(user notify_attrs)a do
141163
{:error, _} = Delivery.send(:notify, notify_attrs, user)
142164
end

0 commit comments

Comments
 (0)