2024-06-24 14:42:41 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2024-06-24 13:28:46 +00:00
|
|
|
import jwt
|
|
|
|
import firebase_admin
|
2024-06-24 14:42:41 +00:00
|
|
|
from firebase_admin import credentials, auth, firestore
|
|
|
|
from google.api_core.exceptions import NotFound, PermissionDenied, Aborted, ResourceExhausted, OutOfRange, DataLoss
|
2024-06-24 13:28:46 +00:00
|
|
|
|
|
|
|
cred = credentials.Certificate('servii.json')
|
|
|
|
firebase_admin.initialize_app(cred)
|
2024-06-24 14:42:41 +00:00
|
|
|
firestore_database = firestore.client()
|
2024-06-24 13:28:46 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
TODO
|
|
|
|
Write a function that launches upon app's startup, it does check in the firestore for any already running servers.
|
|
|
|
Fetches the PID's.
|
|
|
|
Stops all the current processes,
|
|
|
|
Tell the database they have now stopped running.
|
|
|
|
Also ensure the program can add an additional argument to avoid this checking for scalability.
|
|
|
|
'''
|
|
|
|
|
2024-06-24 14:42:41 +00:00
|
|
|
|
2024-06-24 13:28:46 +00:00
|
|
|
def get_user_from_id(user_id):
|
|
|
|
return auth.get_user(user_id)
|
|
|
|
|
|
|
|
|
|
|
|
def verify_jwt_token(token):
|
|
|
|
try:
|
|
|
|
decoded_token = jwt.decode(token, options={"verify_signature": False})
|
|
|
|
user_id = decoded_token.get('sub')
|
|
|
|
return True, user_id
|
|
|
|
except jwt.ExpiredSignatureError:
|
|
|
|
return False, None
|
|
|
|
except jwt.InvalidTokenError:
|
|
|
|
return False, None
|
2024-06-24 14:42:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_firestore(user_id: str, data: dict) -> bool:
|
|
|
|
doc_ref = firestore_database.collection('users').document(user_id)
|
|
|
|
try:
|
|
|
|
doc_ref.create(data)
|
|
|
|
return True
|
|
|
|
except (NotFound, PermissionDenied, Aborted, ResourceExhausted,
|
|
|
|
OutOfRange, DataLoss, TypeError, Exception, ValueError) as e:
|
|
|
|
log_exception_to_firestore(e, user_id, data)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def update_firestore(user_id: str, data: dict) -> bool:
|
|
|
|
doc_ref = firestore_database.collection('users').document(user_id)
|
|
|
|
try:
|
|
|
|
doc_ref.update(data)
|
|
|
|
return True
|
|
|
|
except (NotFound, PermissionDenied, Aborted, ResourceExhausted,
|
|
|
|
OutOfRange, DataLoss, TypeError, Exception, ValueError) as e:
|
|
|
|
log_exception_to_firestore(e, user_id, data)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def log_exception_to_firestore(exception: Exception = None, user_id: str = None, data: dict = None):
|
|
|
|
new_id: str = datetime.now().strftime('%Y-%m-%d %H:%M:%S %Z%z')
|
|
|
|
log_entry = {
|
|
|
|
'exception_name': str(type(exception).__name__),
|
|
|
|
'exception': str(exception) if exception else 'No exception',
|
|
|
|
'user_id': str(user_id) if user_id else 'No user_id',
|
|
|
|
'data': str(data) if data else 'No data provided',
|
|
|
|
}
|
|
|
|
try:
|
|
|
|
firestore_database.collection('firebase.logs').document(new_id).create(log_entry)
|
|
|
|
print("Log entry added successfully.")
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Failed to add log entry: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
pass
|