deanpowers commited on
Commit
d7a4fa1
·
1 Parent(s): 2ca5f08

Add global alerts UI + API for registered users

Browse files
Files changed (3) hide show
  1. local_app.py +21 -0
  2. modules/auth.py +27 -0
  3. templates/profile.html +59 -1
local_app.py CHANGED
@@ -1198,6 +1198,27 @@ def prices_api():
1198
  resp.headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0"
1199
  return resp
1200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1201
  @app.route('/api/whoami')
1202
  def whoami_api():
1203
  u = current_user()
 
1198
  resp.headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0"
1199
  return resp
1200
 
1201
+ @app.route('/api/profile/alerts', methods=['GET', 'POST'])
1202
+ def api_profile_alerts():
1203
+ """Get or update the current user's alert preferences."""
1204
+ from modules.auth import get_user_alerts, update_user_alerts
1205
+
1206
+ user = current_user()
1207
+ if not user:
1208
+ return jsonify({"error": "login required"}), 401
1209
+
1210
+ email = user.get("email")
1211
+
1212
+ if request.method == 'GET':
1213
+ prefs = get_user_alerts(email) or {}
1214
+ return jsonify({"status": "ok", "alerts": prefs})
1215
+
1216
+ data = request.get_json(silent=True) or {}
1217
+ ok = update_user_alerts(email, data)
1218
+ if not ok:
1219
+ return jsonify({"error": "update failed"}), 500
1220
+ return jsonify({"status": "ok", "alerts": get_user_alerts(email)})
1221
+
1222
  @app.route('/api/whoami')
1223
  def whoami_api():
1224
  u = current_user()
modules/auth.py CHANGED
@@ -48,6 +48,31 @@ def _load_users_raw():
48
  def _save_users_raw(users):
49
  return save_json(LOCAL_USERS_FILE, {"users": users})
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  # ---------- Lookups ----------
52
  def get_user(email):
53
  email = (email or "").strip().lower()
@@ -109,6 +134,8 @@ def create_user(email, password, first_name="", last_name="", role="user", statu
109
  "recovery_code": recovery,
110
  "role": role,
111
  "status": status,
 
 
112
  "first_name": first_name,
113
  "last_name": last_name,
114
  "reg_message": (reg_message or "").strip()[:80],
 
48
  def _save_users_raw(users):
49
  return save_json(LOCAL_USERS_FILE, {"users": users})
50
 
51
+ def get_user_alerts(email):
52
+ """Return user's alert preferences with defaults for missing fields."""
53
+ u = get_user(email)
54
+ if not u:
55
+ return None
56
+ return {
57
+ "alert_btc_1pct": bool(u.get("alert_btc_1pct", True)),
58
+ "alert_eth_1pct": bool(u.get("alert_eth_1pct", True)),
59
+ }
60
+
61
+
62
+ def update_user_alerts(email, prefs):
63
+ """Update alert preferences for a user. Returns True on success."""
64
+ email = (email or "").strip().lower()
65
+ users = _load_users_raw()
66
+ for u in users:
67
+ if u.get("email", "").lower() == email:
68
+ if "alert_btc_1pct" in prefs:
69
+ u["alert_btc_1pct"] = bool(prefs["alert_btc_1pct"])
70
+ if "alert_eth_1pct" in prefs:
71
+ u["alert_eth_1pct"] = bool(prefs["alert_eth_1pct"])
72
+ return _save_users_raw(users)
73
+ return False
74
+
75
+
76
  # ---------- Lookups ----------
77
  def get_user(email):
78
  email = (email or "").strip().lower()
 
134
  "recovery_code": recovery,
135
  "role": role,
136
  "status": status,
137
+ "alert_btc_1pct": True,
138
+ "alert_eth_1pct": True,
139
  "first_name": first_name,
140
  "last_name": last_name,
141
  "reg_message": (reg_message or "").strip()[:80],
templates/profile.html CHANGED
@@ -53,6 +53,26 @@
53
  <div class="row"><span class="label">Role</span><span id="p-role">—</span></div>
54
  </div>
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  <div class="card">
57
  <h2>Saved Coin Selection</h2>
58
  <div id="orders-container"><div class="empty">Loading…</div></div>
@@ -130,7 +150,45 @@
130
  loadOrders();
131
  });
132
  </script>
133
- <script src="/css/nav.js"></script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  <script>
135
  window.__et = function(name, props) {
136
  try {
 
53
  <div class="row"><span class="label">Role</span><span id="p-role">—</span></div>
54
  </div>
55
 
56
+ <div class="card" style="margin-top: 20px;">
57
+ <h2>🔔 Global Alerts</h2>
58
+ <p style="color:#8b949e;font-size:13px;margin:8px 0 16px 0;">Receive alerts for major price moves in the last 24 hours.</p>
59
+
60
+ <label style="display:flex;align-items:center;gap:10px;padding:10px 0;border-bottom:1px solid #21262d;cursor:pointer;">
61
+ <input type="checkbox" id="alert-btc-1pct" style="width:18px;height:18px;cursor:pointer;">
62
+ <span style="color:#e6edf3;font-size:14px;">Alert me when <strong>BTC</strong> changes ±1% in 24 hours</span>
63
+ </label>
64
+
65
+ <label style="display:flex;align-items:center;gap:10px;padding:10px 0;border-bottom:1px solid #21262d;cursor:pointer;">
66
+ <input type="checkbox" id="alert-eth-1pct" style="width:18px;height:18px;cursor:pointer;">
67
+ <span style="color:#e6edf3;font-size:14px;">Alert me when <strong>ETH</strong> changes ±1% in 24 hours</span>
68
+ </label>
69
+
70
+ <div id="alerts-status" style="margin-top:12px;font-size:12px;color:#8b949e;"></div>
71
+ <div style="margin-top:8px;font-size:11px;color:#484f58;">
72
+ <strong>Note:</strong> A Telegram handle is required to receive alerts. We'll add Telegram support to profiles soon.
73
+ </div>
74
+ </div>
75
+
76
  <div class="card">
77
  <h2>Saved Coin Selection</h2>
78
  <div id="orders-container"><div class="empty">Loading…</div></div>
 
150
  loadOrders();
151
  });
152
  </script>
153
+ <script>
154
+ (function() {
155
+ function loadAlerts() {
156
+ fetch('/api/profile/alerts?_=' + Date.now(), {cache: 'no-store'})
157
+ .then(function(r){ return r.json(); })
158
+ .then(function(d) {
159
+ if (!d || d.status !== 'ok') return;
160
+ var a = d.alerts || {};
161
+ var btcEl = document.getElementById('alert-btc-1pct');
162
+ var ethEl = document.getElementById('alert-eth-1pct');
163
+ if (btcEl) btcEl.checked = a.alert_btc_1pct !== false;
164
+ if (ethEl) ethEl.checked = a.alert_eth_1pct !== false;
165
+ }).catch(function(){});
166
+ }
167
+ function saveAlerts() {
168
+ var btcEl = document.getElementById('alert-btc-1pct');
169
+ var ethEl = document.getElementById('alert-eth-1pct');
170
+ var statusEl = document.getElementById('alerts-status');
171
+ if (!btcEl || !ethEl) return;
172
+ fetch('/api/profile/alerts', {
173
+ method: 'POST',
174
+ headers: {'Content-Type': 'application/json'},
175
+ body: JSON.stringify({alert_btc_1pct: btcEl.checked, alert_eth_1pct: ethEl.checked})
176
+ }).then(function(r){ return r.json(); }).then(function() {
177
+ if (statusEl) { statusEl.textContent = 'Saved.'; statusEl.style.color = '#4ade80'; setTimeout(function(){ statusEl.textContent = ''; }, 2000); }
178
+ }).catch(function() {
179
+ if (statusEl) { statusEl.textContent = 'Save failed.'; statusEl.style.color = '#f87171'; }
180
+ });
181
+ }
182
+ document.addEventListener('DOMContentLoaded', function() {
183
+ loadAlerts();
184
+ var btcEl = document.getElementById('alert-btc-1pct');
185
+ var ethEl = document.getElementById('alert-eth-1pct');
186
+ if (btcEl) btcEl.addEventListener('change', saveAlerts);
187
+ if (ethEl) ethEl.addEventListener('change', saveAlerts);
188
+ });
189
+ })();
190
+ </script>
191
+ <script src="/css/nav.js"></script>
192
  <script>
193
  window.__et = function(name, props) {
194
  try {