@@ -47,9 +47,13 @@ defmodule GroupherServer.Delivery.Delegate.Mention do
4747 batch_delete_mentions ( article , from_user )
4848 end )
4949 |> Multi . run ( :batch_insert_mentions , fn _ , _ ->
50- mentions = Enum . map ( mentions , & atom_values_to_upcase ( & 1 ) )
50+ mentions =
51+ mentions
52+ |> Enum . map ( & atom_values_to_upcase ( & 1 ) )
53+ # ignore mention myself
54+ |> Enum . reject ( & ( & 1 . to_user_id == from_user . id ) )
5155
52- case { 0 , nil } !== Repo . insert_all ( Mention , mentions ) do
56+ case Enum . empty? ( mentions ) or { 0 , nil } !== Repo . insert_all ( Mention , mentions ) do
5357 true -> { :ok , :pass }
5458 false -> { :error , "insert mentions error" }
5559 end
@@ -58,6 +62,7 @@ defmodule GroupherServer.Delivery.Delegate.Mention do
5862 |> result ( )
5963 end
6064
65+ @ doc "paged mentions"
6166 def paged_mentions ( % User { } = user , % { page: page , size: size } = filter ) do
6267 read = Map . get ( filter , :read , false )
6368
@@ -69,6 +74,25 @@ defmodule GroupherServer.Delivery.Delegate.Mention do
6974 |> done ( )
7075 end
7176
77+ @ doc "get unread mentions count"
78+ def unread_count ( user_id ) do
79+ Mention
80+ |> where ( [ m ] , m . to_user_id == ^ user_id and m . read == false )
81+ |> Repo . aggregate ( :count )
82+ |> done
83+ end
84+
85+ 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 ] )
89+
90+ case result do
91+ { 0 , nil } -> { :error , "no such mentions found" }
92+ _ -> { :ok , :done }
93+ end
94+ end
95+
7296 defp batch_delete_mentions ( % Comment { } = comment , % User { } = from_user ) do
7397 from ( m in Mention ,
7498 where: m . comment_id == ^ comment . id ,
@@ -100,6 +124,7 @@ defmodule GroupherServer.Delivery.Delegate.Mention do
100124
101125 mention
102126 |> Map . take ( [
127+ :id ,
103128 :thread ,
104129 :article_id ,
105130 :comment_id ,
0 commit comments