Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

Announcements API tweaks #885

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/constable/models/announcement.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ defmodule Constable.Announcement do
query |> order_by(desc: :last_discussed_at)
end

def oldest_discussed_first(query \\ __MODULE__) do
sharplet marked this conversation as resolved.
Show resolved Hide resolved
query |> order_by(asc: :last_discussed_at)
end

def with_announcement_list_assocs(query \\ __MODULE__) do
from q in query,
preload: [
Expand Down
23 changes: 15 additions & 8 deletions lib/constable_web/controllers/api/announcement_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ defmodule ConstableWeb.Api.AnnouncementController do
cursor = Repo.get(Announcement, after_id)

announcements =
Announcement
|> where([a], a.inserted_at < ^cursor.inserted_at)
|> order_by(desc: :inserted_at)
interesting_announcements(conn.assigns.current_user)
|> where([a], a.last_discussed_at < ^cursor.last_discussed_at)
|> Announcement.with_announcement_list_assocs()
|> Announcement.last_discussed_first()
|> limit(^limit)
|> Repo.all()

Expand All @@ -27,9 +28,10 @@ defmodule ConstableWeb.Api.AnnouncementController do
cursor = Repo.get(Announcement, before_id)

announcements =
Announcement
|> where([a], ^cursor.inserted_at < a.inserted_at)
|> order_by(:inserted_at)
interesting_announcements(conn.assigns.current_user)
|> where([a], ^cursor.last_discussed_at < a.last_discussed_at)
|> Announcement.with_announcement_list_assocs()
|> Announcement.oldest_discussed_first()
|> limit(^limit)
|> Repo.all()
|> Enum.reverse()
Expand All @@ -41,14 +43,19 @@ defmodule ConstableWeb.Api.AnnouncementController do
limit = Map.get(params, "page_size", 50)

announcements =
Announcement
|> order_by(desc: :inserted_at)
interesting_announcements(conn.assigns.current_user)
|> Announcement.with_announcement_list_assocs()
|> Announcement.last_discussed_first()
|> limit(^limit)
|> Repo.all()

render(conn, "index.json", announcements: announcements)
end

defp interesting_announcements(user) do
user |> Ecto.assoc(:interesting_announcements)
end

def create(
conn,
%{"announcement" => announcement_params, "interest_names" => interest_names}
Expand Down
7 changes: 5 additions & 2 deletions lib/constable_web/views/api/announcement_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule ConstableWeb.Api.AnnouncementView do
use ConstableWeb, :view

alias ConstableWeb.Api.CommentView
alias ConstableWeb.Api.InterestView
alias ConstableWeb.Api.UserView

def render("index.json", %{announcements: announcements}) do
announcements = announcements |> Repo.preload([:comments, :interests])
Expand All @@ -22,10 +24,11 @@ defmodule ConstableWeb.Api.AnnouncementView do
title: announcement.title,
body: announcement.body,
inserted_at: announcement.inserted_at,
last_discussed_at: announcement.last_discussed_at,
updated_at: announcement.updated_at,
user_id: announcement.user_id,
user: render_one(announcement.user, UserView, "author.json"),
comments: render_many(announcement.comments, CommentView, "comment.json"),
interest_ids: pluck(announcement.interests, :id),
interests: render_many(announcement.interests, InterestView, "interest.json"),
url:
ConstableWeb.Router.Helpers.announcement_url(ConstableWeb.Endpoint, :show, announcement)
}
Expand Down
9 changes: 9 additions & 0 deletions lib/constable_web/views/api/user_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,13 @@ defmodule ConstableWeb.Api.UserView do
subscriptions: pluck(user.subscriptions, :id)
}
end

def render("author.json", %{user: user}) do
%{
id: user.id,
name: user.name,
profile_image_url: profile_provider().image_url(user),
username: user.username
}
end
end