@@ -12,7 +12,9 @@ defmodule GroupherServer.Delivery.Delegate.Notification do
1212 alias GroupherServer . { Accounts , Delivery , Repo }
1313 alias Delivery.Model.Notification
1414 alias Accounts.Model.User
15+
1516 alias Helper.ORM
17+ alias Ecto.Multi
1618
1719 @ notify_actions get_config ( :general , :nofity_actions )
1820 @ notify_group_interval_hour get_config ( :general , :notify_group_interval_hour )
@@ -21,12 +23,23 @@ defmodule GroupherServer.Delivery.Delegate.Notification do
2123 with true <- action in @ notify_actions ,
2224 true <- is_valid? ( attrs ) ,
2325 true <- user_id !== from_user . id do
24- from_user = from_user |> Map . take ( [ :login , :nickname ] ) |> Map . put ( :user_id , from_user . id )
25-
26- case find_exist_notify ( attrs , :latest_peroid ) do
27- { :ok , notify } -> merge_notification ( notify , from_user )
28- { :error , _ } -> create_notification ( attrs , from_user )
29- end
26+ Multi . new ( )
27+ |> Multi . run ( :upsert_notifications , fn _ , _ ->
28+ from_user =
29+ from_user
30+ |> Map . take ( [ :login , :nickname ] )
31+ |> Map . put ( :user_id , from_user . id )
32+
33+ case find_exist_notify ( attrs , :latest_peroid ) do
34+ { :ok , notify } -> merge_notification ( notify , from_user )
35+ { :error , _ } -> create_notification ( attrs , from_user )
36+ end
37+ end )
38+ |> Multi . run ( :update_user_mailbox_status , fn _ , % { upsert_notifications: nofity } ->
39+ Accounts . update_mailbox_status ( nofity . user_id )
40+ end )
41+ |> Repo . transaction ( )
42+ |> result ( )
3043 else
3144 false -> { :error , "invalid args for notification" }
3245 error -> error
@@ -38,19 +51,27 @@ defmodule GroupherServer.Delivery.Delegate.Notification do
3851 attrs = attrs |> Map . put ( :from_user , from_user )
3952
4053 with { :ok , notifications } <- find_exist_notify ( attrs , :all ) do
41- Enum . each ( notifications , fn notify ->
42- case length ( notify . from_users ) == 1 do
43- # 只有一就删除记录
44- true ->
45- ORM . delete ( notify )
46-
47- # 如果是多人集合就在 from_users 中删除该用户
48- false ->
49- from_users = Enum . reject ( notify . from_users , & ( & 1 . login == from_user . login ) )
50- notify |> ORM . update_embed ( :from_users , from_users )
51- end
54+ Multi . new ( )
55+ |> Multi . run ( :revoke_notifications , fn _ , _ ->
56+ Enum . each ( notifications , fn notify ->
57+ case length ( notify . from_users ) == 1 do
58+ # 只有一就删除记录
59+ true ->
60+ ORM . delete ( notify )
61+
62+ # 如果是多人集合就在 from_users 中删除该用户
63+ false ->
64+ from_users = Enum . reject ( notify . from_users , & ( & 1 . login == from_user . login ) )
65+ notify |> ORM . update_embed ( :from_users , from_users )
66+ end
67+ end )
68+ |> done
5269 end )
53- |> done
70+ |> Multi . run ( :update_user_mailbox_status , fn _ , _ ->
71+ Enum . each ( notifications , & Accounts . update_mailbox_status ( & 1 . to_user_id ) ) |> done
72+ end )
73+ |> Repo . transaction ( )
74+ |> result ( )
5475 else
5576 false -> { :ok , :pass }
5677 { :error , _ } -> { :ok , :pass }
@@ -191,4 +212,8 @@ defmodule GroupherServer.Delivery.Delegate.Notification do
191212 defp interval_threshold_time ( ) do
192213 Timex . shift ( Timex . now ( ) , hours: - @ notify_group_interval_hour )
193214 end
215+
216+ defp result ( { :ok , % { upsert_notifications: result } } ) , do: { :ok , result }
217+ defp result ( { :ok , % { revoke_notifications: result } } ) , do: { :ok , result }
218+ defp result ( { :error , _ , result , _steps } ) , do: { :error , result }
194219end
0 commit comments