Skip to content

Commit 550e834

Browse files
authored
Merge pull request #104 from errkk/groupfi
handle group create error from api
2 parents d56d572 + 702afb9 commit 550e834

20 files changed

Lines changed: 3155 additions & 1466 deletions

File tree

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ COPY lib lib
4242
RUN mix compile
4343

4444
# copy runtime configuration file
45-
COPY config/releases.exs config/
45+
COPY config/runtime.exs config/
4646
COPY rel rel
4747

4848
# assemble release

config/config.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ config :pr, PRWeb.Endpoint,
2828
pubsub_server: PR.PubSub,
2929
live_view: [
3030
signing_salt: "SECRET_SALT"
31-
]
31+
],
32+
# This is overriden in prod config
33+
force_ssl: false
3234

3335
# Configures Elixir's Logger
3436
config :logger, :console,

config/dev.exs

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import Config
22

33
config :pr,
4-
playlist_name: "PlayRequest"
4+
playlist_name: "PlayRequest",
5+
session_same_site: "Lax",
6+
session_secure: false
57

68
# Configure your database
79
config :pr, PR.Repo,
@@ -28,7 +30,7 @@ config :oauth2, debug: true
2830
# watchers to your application. For example, we use it
2931
# with webpack to recompile .js and .css sources.
3032
config :pr, PRWeb.Endpoint,
31-
http: [port: System.get_env("PORT")],
33+
http: [port: System.get_env("PORT") || 4000, ip: {0, 0, 0, 0}],
3234
debug_errors: true,
3335
code_reloader: true,
3436
check_origin: false,
@@ -78,8 +80,8 @@ config :pr, PRWeb.Endpoint,
7880
]
7981

8082
# Do not include metadata nor timestamps in development logs
81-
# config :logger, :console, format: "[$level] $message\n"
82-
config :logger, level: :info
83+
config :logger, :console, format: "[$level] $message\n", level: :debug
84+
# config :logger, level: :info
8385

8486
# Initialize plugs at runtime for faster development compilation
8587
config :phoenix, :plug_init_mode, :runtime
@@ -90,43 +92,4 @@ config :phoenix, :stacktrace_depth, 20
9092

9193
config :phoenix_live_view, :debug_heex_annotations, true
9294

93-
config :pr, :sonos,
94-
scopes: "playback-control-all",
95-
redirect_uri: "#{System.get_env("REDIRECT_URL_BASE")}/sonos/authorized",
96-
key: System.get_env("SONOS_KEY"),
97-
secret: System.get_env("SONOS_SECRET")
98-
99-
config :pr, :spotify,
100-
scopes:
101-
~w(user-modify-playback-state user-read-currently-playing user-read-playback-state playlist-modify-private playlist-read-private),
102-
redirect_uri: "#{System.get_env("REDIRECT_URL_BASE")}/spotify/authorized",
103-
user_id: System.get_env("SPOTIFY_USER_ID"),
104-
key: System.get_env("SPOTIFY_CLIENT_ID"),
105-
secret: System.get_env("SPOTIFY_SECRET")
106-
107-
config :ueberauth, Ueberauth.Strategy.Google.OAuth,
108-
client_id: System.get_env("GOOGLE_CLIENT_ID"),
109-
client_secret: System.get_env("GOOGLE_CLIENT_SECRET")
110-
111-
config :pr,
112-
allowed_user_domains: System.get_env("ALLOWED_USER_DOMAINS"),
113-
installation_name: System.get_env("INSTALLATION_NAME", "PlayRequest"),
114-
super_likes_allowed: System.get_env("SUPER_LIKES_ALLOWED", "2"),
115-
burns_allowed: System.get_env("BURNS_ALLOWED", "2")
116-
117-
config :pr, :feature_flags,
118-
show_volume: System.get_env("FF_VOLUME", ""),
119-
show_toggle_playback: System.get_env("FF_TOGGLE_PLAYBACK", ""),
120-
show_skip: System.get_env("FF_SKIP", ""),
121-
scale_play_button: System.get_env("FF_SCALE_PLAY_BUTTON", ""),
122-
show_super_like: System.get_env("FF_SUPER_LIKE", ""),
123-
show_burn: System.get_env("FF_BURN", "")
124-
125-
config :sentry,
126-
dsn: System.get_env("SENTRY_DSN"),
127-
environment_name: :dev,
128-
enable_source_code_context: true,
129-
root_source_code_path: File.cwd!(),
130-
tags: %{
131-
env: "dev"
132-
}
95+
# Runtime configuration for development environment has been moved to config/runtime.exs

config/prod.exs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
import Config
22

3-
config :logger,
4-
backends: [LoggerJSON],
5-
level: :info
3+
config :pr,
4+
session_same_site: "None",
5+
session_secure: true
6+
7+
# For production, don't forget to configure the url host
8+
# to something meaningful, Phoenix uses this information
9+
# when generating URLs.
610

7-
config :pr, PR.Repo, ssl: false, socket_options: [:inet6]
11+
# Runtime configuration (such as database credentials) should be
12+
# placed in config/runtime.exs which is loaded at runtime.
813

14+
# Note we also include the path to a cache manifest
15+
# containing the digested version of static files. This
16+
# manifest is generated by the `mix assets.deploy` task,
17+
# which you should run after static files are built and
18+
# before starting your production server.
919
config :pr, PRWeb.Endpoint,
10-
force_ssl: [rewrite_on: [:x_forwarded_proto]],
11-
server: true,
12-
root: ".",
13-
version: Application.spec(:pr, :vsn)
20+
cache_static_manifest: "priv/static/cache_manifest.json",
21+
force_ssl: [rewrite_on: [:x_forwarded_proto]]
1422

15-
config :pr,
16-
sleep: 60_000
23+
# Do not print debug messages in production
24+
config :logger, level: :info
1725

18-
config :sentry,
19-
enable_source_code_context: true,
20-
root_source_code_path: File.cwd!()
26+
# Runtime configuration moved to config/runtime.exs

config/releases.exs

Lines changed: 0 additions & 71 deletions
This file was deleted.

config/runtime.exs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import Config
2+
3+
# config/runtime.exs is executed for all environments, including
4+
# during releases. It is executed after compilation and before the
5+
# system starts, so it is typically used to load production configuration
6+
# and secrets from environment variables or elsewhere. Do not define
7+
# any compile-time configuration in here, as it won't be applied.
8+
9+
# ## Using releases
10+
#
11+
# If you use `mix release`, you need to explicitly enable the server
12+
# by passing the PHX_SERVER=true when you start it:
13+
#
14+
# PHX_SERVER=true bin/pr start
15+
#
16+
# Alternatively, you can use `mix phx.gen.release` to generate a `bin/server`
17+
# script that automatically sets the env var above.
18+
if System.get_env("PHX_SERVER") do
19+
config :pr, PRWeb.Endpoint, server: true
20+
end
21+
22+
# Common runtime configuration for all environments
23+
config :pr, :sonos,
24+
scopes: "playback-control-all",
25+
redirect_uri: "#{System.get_env("REDIRECT_URL_BASE")}/sonos/authorized",
26+
key: System.get_env("SONOS_KEY"),
27+
secret: System.get_env("SONOS_SECRET")
28+
29+
config :pr, :spotify,
30+
scopes:
31+
~w(user-modify-playback-state user-read-currently-playing user-read-playback-state playlist-modify-private playlist-read-private),
32+
redirect_uri: "#{System.get_env("REDIRECT_URL_BASE")}/spotify/authorized",
33+
key: System.get_env("SPOTIFY_CLIENT_ID"),
34+
secret: System.get_env("SPOTIFY_SECRET")
35+
36+
config :ueberauth, Ueberauth.Strategy.Google.OAuth,
37+
client_id: System.get_env("GOOGLE_CLIENT_ID"),
38+
client_secret: System.get_env("GOOGLE_CLIENT_SECRET")
39+
40+
config :pr,
41+
allowed_user_domains: System.get_env("ALLOWED_USER_DOMAINS", ""),
42+
installation_name: System.get_env("INSTALLATION_NAME", "PlayRequest"),
43+
super_likes_allowed: System.get_env("SUPER_LIKES_ALLOWED", "2"),
44+
burns_allowed: System.get_env("BURNS_ALLOWED", "2")
45+
46+
config :pr, :feature_flags,
47+
show_volume: System.get_env("FF_VOLUME", ""),
48+
show_toggle_playback: System.get_env("FF_TOGGLE_PLAYBACK", ""),
49+
show_skip: System.get_env("FF_SKIP", ""),
50+
scale_play_button: System.get_env("FF_SCALE_PLAY_BUTTON", ""),
51+
show_super_like: System.get_env("FF_SUPER_LIKE", ""),
52+
show_burn: System.get_env("FF_BURN", "")
53+
54+
config :sentry,
55+
dsn: System.get_env("SENTRY_DSN"),
56+
enable_source_code_context: true,
57+
root_source_code_path: File.cwd!()
58+
59+
# Production-specific configuration
60+
if config_env() == :prod do
61+
config :logger,
62+
backends: [LoggerJSON],
63+
level: :info
64+
65+
config :pr, PR.Repo,
66+
url: System.get_env("DATABASE_URL") || raise("DATABASE_URL not available"),
67+
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
68+
ssl: false,
69+
socket_options: [:inet6]
70+
71+
config :pr, PRWeb.Endpoint,
72+
http: [port: String.to_integer(System.get_env("PORT") || "4000")],
73+
url: [host: System.get_env("HOSTNAME"), port: 443, scheme: "https"],
74+
cache_static_manifest: "priv/static/cache_manifest.json",
75+
server: true
76+
77+
# Production requires these environment variables
78+
unless System.get_env("SONOS_KEY"), do: raise("SONOS_KEY not available")
79+
unless System.get_env("SONOS_SECRET"), do: raise("SONOS_SECRET not available")
80+
unless System.get_env("SPOTIFY_CLIENT_ID"), do: raise("SPOTIFY_CLIENT_ID not available")
81+
unless System.get_env("SPOTIFY_SECRET"), do: raise("SPOTIFY_SECRET not available")
82+
unless System.get_env("GOOGLE_CLIENT_ID"), do: raise("GOOGLE_CLIENT_ID not available")
83+
unless System.get_env("GOOGLE_CLIENT_SECRET"), do: raise("GOOGLE_CLIENT_SECRET not available")
84+
85+
config :pr,
86+
sleep: 60_000
87+
88+
app_name =
89+
System.get_env("FLY_APP_NAME") ||
90+
raise "FLY_APP_NAME not available"
91+
92+
config :libcluster,
93+
debug: true,
94+
topologies: [
95+
fly6pn: [
96+
strategy: Cluster.Strategy.DNSPoll,
97+
config: [
98+
polling_interval: 5_000,
99+
query: "#{app_name}.internal",
100+
node_basename: app_name
101+
]
102+
]
103+
]
104+
105+
config :sentry,
106+
tags: %{
107+
env: System.get_env("RELEASE_STAGE") || "dev"
108+
},
109+
environment_name: System.get_env("RELEASE_STAGE") || :prod,
110+
release: System.get_env("APP_REVISION", "dev")
111+
end
112+
113+
# Development-specific configuration
114+
if config_env() == :dev do
115+
config :sentry,
116+
environment_name: :dev,
117+
tags: %{
118+
env: "dev"
119+
}
120+
end

justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,4 @@ remote_production:
6262
fly ssh console -C "/home/elixir/app/bin/{{binname}} remote" --app {{appname}}
6363

6464
tunnel:
65-
ssh -p 443 -R0:localhost:4000 ${PINGY_TOKEN}@a.pinggy.online
65+
cloudflared tunnel run --token ${CLOUDFLARE_TOKEN}

lib/pr/apis/sonos_api.ex

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,17 @@ defmodule PR.SonosAPI do
186186
%Household{household_id: household_id, id: id} ->
187187
%{playerIds: player_ids}
188188
|> post("/households/#{household_id}/groups/createGroup")
189-
|> Map.get(:group)
190-
|> fields_for_group(id)
191-
|> Map.put(:is_active, true)
192-
|> SonosHouseholds.insert_or_update_group()
189+
|> case do
190+
%{group: group} ->
191+
group
192+
|> fields_for_group(id)
193+
|> Map.put(:is_active, true)
194+
|> SonosHouseholds.insert_or_update_group()
195+
196+
{:error, error} ->
197+
Logger.error(error)
198+
{:error, :cant_create_group}
199+
end
193200

194201
_ ->
195202
{:error, :no_household_activated}

lib/pr/music/music.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,13 @@ defmodule PR.Music do
119119
def check_current_playstate(%PlaybackState{state: state}, _mode),
120120
do: {:error, :playstate, state}
121121

122+
def check_current_playstate(%{}, _mode),
123+
do: {:error, :playstate, nil}
124+
122125
def check_unplayed() do
123126
# Check if there's something in the queue
124127
# If might still be marked as playing if its a skip
125-
# but that's probably ok as this check is only really
128+
# but that's probably ok as this check is only really
126129
# of interest for knowing what to do when skip is pressed
127130
cond do
128131
Queue.num_unplayed() == 0 ->

lib/pr_web.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ defmodule PRWeb do
5151
def plug do
5252
quote do
5353
import Plug.Conn
54-
use Phoenix.Controller, namespace: PRWeb
54+
import Phoenix.Controller, only: [redirect: 2]
5555
unquote(verified_routes())
5656
end
5757
end

0 commit comments

Comments
 (0)