@@ -102,6 +102,61 @@ defmodule ExRssWeb.FeedLive.Index do
102
102
end
103
103
end
104
104
105
+ def handle_event ( "mark_as_read" , % { "feed-id" => feed_id } , socket ) do
106
+ changeset =
107
+ Repo . get! ( User , socket . assigns . current_user . id )
108
+ |> Ecto . assoc ( :feeds )
109
+ |> Repo . get! ( feed_id )
110
+ |> Repo . preload ( :entries )
111
+ |> Feed . mark_as_read ( )
112
+
113
+ case Repo . update ( changeset ) do
114
+ { :ok , feed } ->
115
+ feed_with_counts_query =
116
+ from (
117
+ f in Feed ,
118
+ join: e in Entry ,
119
+ on: f . id == e . feed_id ,
120
+ group_by: f . id ,
121
+ select: % {
122
+ f
123
+ | unread_entries_count: filter ( count ( e . id ) , e . read == false ) ,
124
+ read_entries_count: filter ( count ( e . id ) , e . read == true ) ,
125
+ has_error: f . retries > 0
126
+ }
127
+ )
128
+
129
+ updated_feed =
130
+ Repo . get! ( feed_with_counts_query , feed . id )
131
+ |> Repo . preload (
132
+ entries: from ( e in Entry , where: e . read == false , order_by: [ desc: e . posted_at ] )
133
+ )
134
+
135
+ update_broadcaster =
136
+ Application . get_env ( :ex_rss , :update_broadcaster , ExRss.Crawler.UpdateBroadcaster )
137
+
138
+ Task.Supervisor . start_child (
139
+ ExRss.TaskSupervisor ,
140
+ update_broadcaster ,
141
+ :broadcast_update ,
142
+ [ updated_feed ]
143
+ )
144
+
145
+ oldest_unread_entry =
146
+ User . oldest_unread_entry ( socket . assigns . current_user . id )
147
+
148
+ socket =
149
+ socket
150
+ |> assign ( :oldest_unread_entry , oldest_unread_entry )
151
+ |> stream_insert ( :feeds , updated_feed )
152
+
153
+ { :noreply , socket }
154
+
155
+ _ ->
156
+ { :noreply , socket }
157
+ end
158
+ end
159
+
105
160
def handle_event ( "pin_feed" , % { "feed-id" => feed_id } , socket ) do
106
161
update_feed_position ( feed_id , 0 , socket )
107
162
end
0 commit comments