Skip to content

Commit 2f8f06c

Browse files
committed
Mark feed as read
1 parent d0e8d3a commit 2f8f06c

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

lib/ex_rss_web/feed_live/index.ex

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,61 @@ defmodule ExRssWeb.FeedLive.Index do
102102
end
103103
end
104104

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+
105160
def handle_event("pin_feed", %{"feed-id" => feed_id}, socket) do
106161
update_feed_position(feed_id, 0, socket)
107162
end

lib/ex_rss_web/feed_live/index.html.heex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
<.icon name="hero-bookmark-slash-solid" />
3636
</button>
3737
<% end %>
38+
39+
<button aria-label="Mark as read" phx-click="mark_as_read" phx-value-feed-id={feed.id}>
40+
<.icon name="hero-check-circle-solid" />
41+
</button>
3842
</div>
3943
</div>
4044

0 commit comments

Comments
 (0)