Skip to content

Commit

Permalink
Follows (rauversion#12)
Browse files Browse the repository at this point in the history
* readme

* follows menu resource

* integrate pagination

* menu tweak

* track list components & reposts

* private preview + embed + reposts
  • Loading branch information
michelson authored Jun 27, 2022
1 parent f7167c5 commit eafdb08
Show file tree
Hide file tree
Showing 45 changed files with 779 additions and 241 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,21 @@ Now you can visit [`localhost:4000`](http://localhost:4000) from your browser.
- [x] File uploads Avatar
- [x] User auth
- [x] Tracks
- [x] Add metadata form step
- [x] Embed at /embed/:track_id
- [x] Embed at /embed/:track_id/private with signed_id
- [ ] Paginate profile tracks /:username
- [ ] Paginate /tracks
- [ ] Filter public/private tracks (where: private: true)
- [ ] Add metadata form step**
- [ ] Listening history
- [x] Filter public/private tracks (where: private: true)
- [ ] Give feedback on upload preprosessing
- [ ] A logo for rauversion
- [ ] Reposts
- [x] Reposts
- [ ] Albums
- [ ] Playlists
- [ ] Likes
- [ ] Followers / Followings
- [x] Private preview on show
- [x] Followers / Followings
- [ ] Telemetry for dashboard
- [ ] Centralized player, fixed (bottom) across navigation

Expand Down Expand Up @@ -78,4 +82,7 @@ Photo by <a href="https://unsplash.com/@schluditsch?utm_source=unsplash&utm_medi
Photo by <a href="https://unsplash.com/@dancristianpaduret?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Dan-Cristian Pădureț</a> on <a href="https://unsplash.com/s/photos/music-studio?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>

Photo by <a href="https://unsplash.com/@grittzheng?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Gritt Zheng</a> on <a href="https://unsplash.com/s/photos/music-studio?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>

Photo by <a href="https://unsplash.com/@saiharishk?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Sai Harish</a> on <a href="https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>


23 changes: 16 additions & 7 deletions lib/rauversion/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,27 @@ defmodule Rauversion.Accounts do
{:ok, result}
end

def broadcast_change({:error, result}, event) do
def broadcast_change({:error, result}, _event) do
{:error, result}
end

def list_accounts(limit \\ 20) do
User |> limit(^limit) |> Repo.all()
end

def tracks_count(user) do
user
|> Ecto.assoc(:tracks)
|> Repo.aggregate(:count, :id)
end

def unfollowed_users(user) do
from(u in User,
left_join: m in assoc(u, :followings),
where: is_nil(m.id)
)
end

## Database getters

@doc """
Expand Down Expand Up @@ -156,14 +169,10 @@ defmodule Rauversion.Accounts do
# a = Rauversion.Accounts.get_user_by_username("michelson") |> Rauversion.Repo.preload(:avatar_blob)
case user do
nil ->
Rauversion.BlobUtils.fallback_image(
"/images/dan-cristian-padure-XKCH37dHt7w-unsplash.jpg"
)
Rauversion.BlobUtils.fallback_image("/images/sai-harish-kjNwiW4BjJE-unsplash-sqr.png")

%{avatar_blob: nil} ->
Rauversion.BlobUtils.fallback_image(
"/images/dan-cristian-padure-XKCH37dHt7w-unsplash.jpg"
)
Rauversion.BlobUtils.fallback_image("/images/sai-harish-kjNwiW4BjJE-unsplash-sqr.png")

%{avatar_blob: %ActiveStorage.Blob{} = avatar_blob} ->
avatar_blob |> ActiveStorage.url()
Expand Down
13 changes: 7 additions & 6 deletions lib/rauversion/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ defmodule Rauversion.Accounts.User do
field :confirmed_at, :naive_datetime

has_many :tracks, Rauversion.Tracks.Track
has_many :followings, Rauversion.UserFollows.UserFollow, foreign_key: :following_id
has_many :followers, Rauversion.UserFollows.UserFollow, foreign_key: :follower_id

has_one(:avatar_attachment, ActiveStorage.Attachment,
where: [record_type: "User", name: "avatar"],
Expand All @@ -30,22 +32,21 @@ defmodule Rauversion.Accounts.User do
"User"
end

def profile_changeset(user, attrs, opts \\ []) do
def profile_changeset(user, attrs, _opts \\ []) do
user
|> validate_contact_fields(attrs)
|> process_avatar(attrs)
end

def changeset(user, attrs, opts \\ []) do
user =
user
|> validate_contact_fields(attrs)
|> registration_changeset(attrs, opts)
user
|> validate_contact_fields(attrs)
|> registration_changeset(attrs, opts)
end

def process_avatar(user, attrs) do
case attrs do
%{"avatar" => avatar} ->
%{"avatar" => _avatar} ->
blob =
ActiveStorage.Blob.create_and_upload!(
%ActiveStorage.Blob{},
Expand Down
2 changes: 1 addition & 1 deletion lib/rauversion/blob_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Rauversion.BlobUtils do
def fallback_image(url \\ nil) do
RauversionWeb.Router.Helpers.static_path(
RauversionWeb.Endpoint,
url || "/images/daniel-schludi-mbGxz7pt0jM-unsplash.jpg"
url || "/images/daniel-schludi-mbGxz7pt0jM-unsplash-sqr-s.png"
)
end

Expand Down
2 changes: 2 additions & 0 deletions lib/rauversion/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ defmodule Rauversion.Repo do
use Ecto.Repo,
otp_app: :rauversion,
adapter: Ecto.Adapters.Postgres

use Scrivener, page_size: 10
end
4 changes: 4 additions & 0 deletions lib/rauversion/reposts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ defmodule Rauversion.Reposts do
"""
def get_repost!(id), do: Repo.get!(Repost, id)

def get_repost_by_user_and_track(user_id, track_id) do
Repost |> where(user_id: ^user_id) |> where(track_id: ^track_id) |> Repo.one()
end

@doc """
Creates a repost.
Expand Down
19 changes: 9 additions & 10 deletions lib/rauversion/services/peaks_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule Rauversion.Services.PeaksGenerator do
])

# |> normalize
frames = Jason.decode!(output)["data"]
Jason.decode!(output)["data"]

# audiowaveform -i ~/Desktop/patio/STE-098.mp3

Expand All @@ -40,15 +40,14 @@ defmodule Rauversion.Services.PeaksGenerator do
IO.inspect(cmd)
output = :os.cmd(cmd)

frames =
Jason.decode!(output)["frames"]
|> Enum.map(fn x ->
case get_in(x, ["tags", "lavfi.astats.Overall.Peak_level"]) |> Float.parse() do
{f, _} -> f
_ -> nil
end
end)
|> normalize
Jason.decode!(output)["frames"]
|> Enum.map(fn x ->
case get_in(x, ["tags", "lavfi.astats.Overall.Peak_level"]) |> Float.parse() do
{f, _} -> f
_ -> nil
end
end)
|> normalize
end

def desired_pixels_per_second(desired_pixels, duration) do
Expand Down
13 changes: 12 additions & 1 deletion lib/rauversion/tracks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,21 @@ defmodule Rauversion.Tracks do
{:ok, result}
end

def broadcast_change({:error, result}, event) do
def broadcast_change({:error, result}, _event) do
{:error, result}
end

def signed_id(track) do
token = Phoenix.Token.sign(RauversionWeb.Endpoint, "user auth", track.id)
end

def find_by_signed_id!(token) do
case Phoenix.Token.verify(RauversionWeb.Endpoint, "user auth", token, max_age: 86400) do
{:ok, track_id} -> get_track!(track_id)
_ -> nil
end
end

@doc """
Returns the list of tracks.
Expand Down
6 changes: 3 additions & 3 deletions lib/rauversion/tracks/track_metadata.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ defmodule Rauversion.Tracks.TrackMetadata do
# |> validate_prescence_url_if(["frame", "link", "url"])
end

def form_definitions(type = "metadata") do
def form_definitions(_type = "metadata") do
[
%{
name: :genre,
Expand Down Expand Up @@ -230,7 +230,7 @@ defmodule Rauversion.Tracks.TrackMetadata do
]
end

def form_definitions(type = "common") do
def form_definitions(_type = "common") do
[
%{
name: :attribution,
Expand Down Expand Up @@ -269,7 +269,7 @@ defmodule Rauversion.Tracks.TrackMetadata do

# TODO: move this to another store / module
def genres do
names = [
[
"Alternative Rock",
"Ambient",
"Classical",
Expand Down
130 changes: 130 additions & 0 deletions lib/rauversion/user_follows.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
defmodule Rauversion.UserFollows do
@moduledoc """
The UserFollows context.
"""

import Ecto.Query, warn: false
alias Rauversion.Repo

alias Rauversion.UserFollows.UserFollow

@doc """
Returns the list of user_follows.
## Examples
iex> list_user_follows()
[%UserFollow{}, ...]
"""
def list_user_follows do
Repo.all(UserFollow)
end

def followers_list_for(user) do
UserFollow
|> where(following_id: ^user.id)
|> Repo.all()
|> Repo.preload([:following, :follower])
end

def followers_for(user) do
UserFollow
|> where(following_id: ^user.id)
|> Repo.aggregate(:count, :id)
end

def followings_list_for(user) do
UserFollow
|> where(follower_id: ^user.id)
|> Repo.all()
|> Repo.preload([:following, :follower])
end

def followings_for(user) do
UserFollow
|> where(follower_id: ^user.id)
|> Repo.aggregate(:count, :id)
end

@doc """
Gets a single user_follow.
Raises `Ecto.NoResultsError` if the User follow does not exist.
## Examples
iex> get_user_follow!(123)
%UserFollow{}
iex> get_user_follow!(456)
** (Ecto.NoResultsError)
"""
def get_user_follow!(id), do: Repo.get!(UserFollow, id)

@doc """
Creates a user_follow.
## Examples
iex> create_user_follow(%{field: value})
{:ok, %UserFollow{}}
iex> create_user_follow(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_user_follow(attrs \\ %{}) do
%UserFollow{}
|> UserFollow.changeset(attrs)
|> Repo.insert()
end

@doc """
Updates a user_follow.
## Examples
iex> update_user_follow(user_follow, %{field: new_value})
{:ok, %UserFollow{}}
iex> update_user_follow(user_follow, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_user_follow(%UserFollow{} = user_follow, attrs) do
user_follow
|> UserFollow.changeset(attrs)
|> Repo.update()
end

@doc """
Deletes a user_follow.
## Examples
iex> delete_user_follow(user_follow)
{:ok, %UserFollow{}}
iex> delete_user_follow(user_follow)
{:error, %Ecto.Changeset{}}
"""
def delete_user_follow(%UserFollow{} = user_follow) do
Repo.delete(user_follow)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking user_follow changes.
## Examples
iex> change_user_follow(user_follow)
%Ecto.Changeset{data: %UserFollow{}}
"""
def change_user_follow(%UserFollow{} = user_follow, attrs \\ %{}) do
UserFollow.changeset(user_follow, attrs)
end
end
21 changes: 21 additions & 0 deletions lib/rauversion/user_follows/user_follow.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Rauversion.UserFollows.UserFollow do
use Ecto.Schema
import Ecto.Changeset

schema "user_follows" do
# field :follower_id, :id
# field :following_id, :id

belongs_to :follower, Rauversion.Accounts.User, foreign_key: :follower_id
belongs_to :following, Rauversion.Accounts.User, foreign_key: :following_id

timestamps()
end

@doc false
def changeset(user_follow, attrs) do
user_follow
|> cast(attrs, [:follower_id, :following_id])
|> validate_required([:follower_id, :following_id])
end
end
4 changes: 2 additions & 2 deletions lib/rauversion_web/active_storage/blobs/proxy_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule RauversionWeb.ActiveStorage.Blobs.ProxyController do

def show(conn, %{"signed_id" => signed_id}) do
case conn |> handle_action(signed_id) do
%{assigns: %{blob: blob}} = conn ->
%{assigns: %{blob: _blob}} = conn ->
handle_range_request(conn)

_ ->
Expand All @@ -50,7 +50,7 @@ defmodule RauversionWeb.ActiveStorage.Blobs.ProxyController do
range = {range_start, conn.assigns.blob.byte_size}
send_range(conn, range)

{range_start, range_end} = range ->
{_range_start, _range_end} = range ->
send_range(conn, range)

ranges when is_list(ranges) ->
Expand Down
Loading

0 comments on commit eafdb08

Please sign in to comment.