@@ -30,6 +30,22 @@ defmodule Uptimer.Websites do
3030 websites
3131 end
3232
33+ @ doc """
34+ Counts the number of websites for a given user.
35+ """
36+ def count_websites_for_user ( user_id ) do
37+ Repo . one ( from w in Website , where: w . user_id == ^ user_id , select: count ( w . id ) )
38+ end
39+
40+ @ doc """
41+ Counts the number of websites with thumbnails enabled for a given user.
42+ """
43+ def count_thumbnails_for_user ( user_id ) do
44+ Repo . one (
45+ from w in Website , where: w . user_id == ^ user_id and w . thumbnail == true , select: count ( w . id )
46+ )
47+ end
48+
3349 @ doc """
3450 Gets a single website.
3551
@@ -59,28 +75,72 @@ defmodule Uptimer.Websites do
5975
6076 """
6177 def create_website ( attrs \\ % { } , user_id ) do
62- { :ok , response } =
63- Finch . build ( :get , attrs [ "address" ] )
64- |> add_browser_headers ( )
65- |> Finch . request ( Uptimer.Finch )
66-
67- attrs = Map . put ( attrs , "status" , Integer . to_string ( response . status ) )
68- # Add user_id to the attributes
69- attrs = Map . put ( attrs , "user_id" , user_id )
70-
71- result =
72- % Website { }
73- |> Website . changeset ( attrs )
74- |> Repo . insert ( )
75-
76- case result do
77- { :ok , website } ->
78- # Generate thumbnail asynchronously
79- Task . start ( fn -> generate_and_save_thumbnail ( website ) end )
80- result
78+ # Check if the user has reached the maximum number of websites (32)
79+ if count_websites_for_user ( user_id ) >= 32 do
80+ { :error , "Maximum number of websites (32) reached for free accounts" }
81+ else
82+ # Try to make a request to the website, but handle possible connection errors
83+ request_result =
84+ try do
85+ Finch . build ( :get , attrs [ "address" ] )
86+ |> add_browser_headers ( )
87+ |> Finch . request ( Uptimer.Finch )
88+ rescue
89+ _ -> { :error , :invalid_url }
90+ catch
91+ _ -> { :error , :timeout }
92+ end
8193
82- error ->
83- error
94+ # Process the request results
95+ case request_result do
96+ { :ok , response } ->
97+ # Successfully connected to the website
98+ attrs = Map . put ( attrs , "status" , Integer . to_string ( response . status ) )
99+ # Add user_id to the attributes
100+ attrs = Map . put ( attrs , "user_id" , user_id )
101+
102+ # Set thumbnail to false by default to prevent automatic enabling
103+ # Users will need to explicitly enable thumbnails
104+ attrs = Map . put_new ( attrs , "thumbnail" , false )
105+
106+ result =
107+ % Website { }
108+ |> Website . changeset ( attrs )
109+ |> Repo . insert ( )
110+
111+ case result do
112+ { :ok , website } ->
113+ # Generate thumbnail asynchronously if thumbnail is enabled
114+ # However, this should now be false by default
115+ if website . thumbnail do
116+ Task . start ( fn -> generate_and_save_thumbnail ( website ) end )
117+ end
118+
119+ result
120+
121+ error ->
122+ error
123+ end
124+
125+ { :error , % Mint.TransportError { reason: reason } } ->
126+ # Handle specific transport errors
127+ error_message =
128+ case reason do
129+ :nxdomain -> "Domain not found. Please check the URL."
130+ :timeout -> "Connection timed out. Website might be unavailable."
131+ :econnrefused -> "Connection refused. Website might be unavailable."
132+ :closed -> "Connection closed unexpectedly."
133+ _ -> "Error connecting to website: #{ inspect ( reason ) } "
134+ end
135+
136+ { :error , error_message }
137+
138+ { :error , % Mint.HTTPError { } } ->
139+ { :error , "Invalid HTTP response from website" }
140+
141+ { :error , _ } ->
142+ { :error , "Could not connect to website. Please check the URL." }
143+ end
84144 end
85145 end
86146
@@ -204,9 +264,22 @@ defmodule Uptimer.Websites do
204264 {:ok, %Website{}}
205265 """
206266 def toggle_thumbnail ( % Website { } = website ) do
207- update_website ( website , % {
208- "thumbnail" => ! website . thumbnail
209- } )
267+ # If toggling from false to true, check the thumbnail limit
268+ if ! website . thumbnail do
269+ # Check if user already has 4 thumbnails
270+ if count_thumbnails_for_user ( website . user_id ) >= 4 do
271+ { :error , "Maximum number of thumbnails (4) reached for free accounts" }
272+ else
273+ update_website ( website , % {
274+ "thumbnail" => true
275+ } )
276+ end
277+ else
278+ # Disabling a thumbnail is always allowed
279+ update_website ( website , % {
280+ "thumbnail" => false
281+ } )
282+ end
210283 end
211284
212285 def refresh_thumbnail ( % Website { } = website ) do
0 commit comments