Skip to content

Commit

Permalink
chore(cleanup): Simplify code + functionality (#19)
Browse files Browse the repository at this point in the history
* chore(cleanup): cleanup unused code + functionality
  - cleanup old code
  - remove functionality like email
* ver 0.5.0
  • Loading branch information
jerdog authored Dec 20, 2024
1 parent 7fd4127 commit a82d553
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 696 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "bluesky-notify"
version = "0.4.4"
version = "0.5.0"
authors = [
{ name = "Jeremy Meiss", email = "[email protected]" },
]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name="bluesky-notify",
version="0.4.3",
version="0.5.0",
description="Bluesky Notification Manager - Track and receive notifications from Bluesky accounts",
author="Jeremy Meiss",
author_email="[email protected]",
Expand Down
10 changes: 5 additions & 5 deletions src/bluesky_notify/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def add_account():

handle = data['handle']
preferences = data.get('notification_preferences')

with app.app_context():
# Run add_account in event loop
loop = asyncio.new_event_loop()
Expand Down Expand Up @@ -127,18 +127,18 @@ def remove_account_by_did(did):
else:
logger.warning(f"No account found with DID: {did}")
return jsonify({"error": f"Account with DID {did} not found"}), 404

# Try to remove the account
result = notifier.remove_account(did, by_did=True)
logger.info(f"Account removal result: {result}")

if "error" in result:
logger.warning(f"Error removing account: {result['error']}")
return jsonify({"error": result["error"]}), 400

logger.info("Account removed successfully")
return jsonify({"data": result}), 200

except Exception as e:
error_msg = f"Error removing account by DID: {str(e)}"
logger.error(error_msg)
Expand Down
39 changes: 18 additions & 21 deletions src/bluesky_notify/api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
else:
has_websocket = False

app = Flask(__name__,
app = Flask(__name__,
template_folder='../templates',
static_folder='../static')
CORS(app)
Expand Down Expand Up @@ -89,7 +89,7 @@ def get_accounts():
'display_name': account.display_name,
'is_active': account.is_active,
'notification_preferences': {
pref.type: pref.enabled
pref.type: pref.enabled
for pref in account.notification_preferences
}
} for account in accounts])
Expand All @@ -100,24 +100,23 @@ def add_account():
data = request.get_json()
handle = data.get('handle')
desktop = data.get('desktop', True)
email = data.get('email', False)


notifier = BlueSkyNotifier(app=app)
if not notifier.authenticate():
return jsonify({'error': 'Failed to authenticate with Bluesky'}), 401

try:
account_info = notifier.get_account_info(handle)
notification_preferences = {'desktop': desktop, 'email': email}
notification_preferences = {'desktop': desktop}
result = add_monitored_account(
profile_data=account_info,
notification_preferences=notification_preferences
)

if 'error' in result:
return jsonify(result), 400
return jsonify({'message': f'Successfully added {account_info["display_name"] or handle}'}), 201

except Exception as e:
return jsonify({'error': str(e)}), 400

Expand All @@ -142,9 +141,7 @@ def update_preferences(handle):
prefs = {}
if 'desktop' in data:
prefs['desktop'] = data['desktop']
if 'email' in data:
prefs['email'] = data['email']


if update_notification_preferences(handle, prefs):
return jsonify({'message': f'Successfully updated preferences for {handle}'})
return jsonify({'error': f'Failed to update preferences for {handle}'}), 400
Expand All @@ -153,7 +150,7 @@ def broadcast_notification(title, message, url):
"""Broadcast a notification to all connected WebSocket clients."""
if not has_websocket:
return

notification = {
'type': 'notification',
'title': title,
Expand Down Expand Up @@ -219,15 +216,15 @@ def run_server(host='127.0.0.1', port=3000, debug=False):
for env_var in ['WERKZEUG_SERVER_FD', 'WERKZEUG_RUN_MAIN']:
if env_var in os.environ:
del os.environ[env_var]

# Make sure no existing server is running
if server:
shutdown_server()

# Use Flask's built-in server with debug output
logger.info(f"Starting web server on http://{host}:{port}")
app.logger.setLevel(logging.DEBUG if debug else logging.INFO)

# Test routes are accessible
logger.debug("Testing routes...")
test_routes = [
Expand All @@ -239,7 +236,7 @@ def run_server(host='127.0.0.1', port=3000, debug=False):
logger.debug(f"Testing route: {route} -> {endpoint}")
if endpoint not in app.view_functions:
logger.error(f"Route {route} -> {endpoint} not found!")

# Set up routes before running
logger.debug("Setting up routes...")
app.add_url_rule('/', 'index', index)
Expand All @@ -248,12 +245,12 @@ def run_server(host='127.0.0.1', port=3000, debug=False):
app.add_url_rule('/api/accounts/<handle>', 'remove_account', remove_account, methods=['DELETE'])
app.add_url_rule('/api/accounts/<handle>/toggle', 'toggle_account', toggle_account, methods=['POST'])
app.add_url_rule('/api/accounts/<handle>/preferences', 'update_preferences', update_preferences, methods=['PATCH'])

# Run the server
logger.debug("Starting Flask server...")
import werkzeug
werkzeug.serving.is_running_from_reloader = lambda: False

# Create server instance
try:
srv = werkzeug.serving.make_server(
Expand All @@ -267,17 +264,17 @@ def run_server(host='127.0.0.1', port=3000, debug=False):
except Exception as e:
logger.error(f"Failed to create server: {e}")
raise

# Store server instance
server = srv

# Start serving
try:
srv.serve_forever()
except Exception as e:
logger.error(f"Server failed while running: {e}")
raise

except Exception as e:
logger.error(f"Error starting web server: {e}")
import traceback
Expand Down
Loading

0 comments on commit a82d553

Please sign in to comment.