feat: Add profile images taken from the nft
All checks were successful
Build Docker / Build Docker (push) Successful in 26s

This commit is contained in:
Nathan Woodburn 2024-06-14 16:10:18 +10:00
parent ab3ab6d7c4
commit ef40904945
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
2 changed files with 43 additions and 4 deletions

View File

@ -12,6 +12,7 @@ db = SQLAlchemy()
class User(db.Model): class User(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(40), unique=True) username = db.Column(db.String(40), unique=True)
profile_picture = db.Column(db.String(255), nullable=True)
def __str__(self): def __str__(self):
return self.username return self.username
@ -19,6 +20,9 @@ class User(db.Model):
def get_user_id(self): def get_user_id(self):
return self.id return self.id
def get_user_profile_picture(self):
return self.profile_picture
def check_password(self, password): def check_password(self, password):
return password == 'valid' return password == 'valid'

View File

@ -119,7 +119,8 @@ def hnsid_domain(domain):
# Add domain to the session # Add domain to the session
user = User.query.filter_by(username=domain).first() user = User.query.filter_by(username=domain).first()
if not user: if not user:
user = User(username=domain) # Create user with username and profile picture
user = User(username=domain, profile_picture=nft["image_url"])
db.session.add(user) db.session.add(user)
db.session.commit() db.session.commit()
session["id"] = user.id session["id"] = user.id
@ -230,6 +231,11 @@ def revoke_token():
@require_oauth(["profile", "openid"]) @require_oauth(["profile", "openid"])
def api_me(): def api_me():
user = current_token.user user = current_token.user
picture = f"https://login.hns.au/u/{user.username}/avatar.png"
if user.profile_picture:
picture = user.profile_picture
userInfo = { userInfo = {
"id": user.id, "id": user.id,
"uid": user.id, "uid": user.id,
@ -243,7 +249,7 @@ def api_me():
"nickname": user.username, "nickname": user.username,
"preferred_username": user.username, "preferred_username": user.username,
"profile": f"https://login.hns.au/u/{user.username}", "profile": f"https://login.hns.au/u/{user.username}",
"picture": f"https://login.hns.au/u/{user.username}/avatar.png", "picture": picture,
"website": f"https://{user.username}", "website": f"https://{user.username}",
"email_verified": True "email_verified": True
} }
@ -275,10 +281,39 @@ def autodiscovery():
@bp.route("/u/<username>") @bp.route("/u/<username>")
def profile(username): def profile(username):
user = User.query.filter_by(username=username).first() user = User.query.filter_by(username=username).first()
return jsonify({"name": user.username, "id": user.id}) if not user:
return jsonify({"error": "User not found"})
picture = f"https://login.hns.au/u/{user.username}/avatar.png"
if user.profile_picture:
picture = user.profile_picture
userInfo = {
"id": user.id,
"uid": user.id,
"username": user.username,
"email": f"{user.username}@login.hns.au",
"displayName": user.username + "/",
"sub": user.id,
"name": user.username,
"given_name": user.username,
"family_name": user.username,
"nickname": user.username,
"preferred_username": user.username,
"profile": f"https://login.hns.au/u/{user.username}",
"picture": picture,
"website": f"https://{user.username}",
"email_verified": True
}
return jsonify(userInfo)
@bp.route("/u/<username>/avatar.png") @bp.route("/u/<username>/avatar.png")
def avatar(username): def avatar(username):
user = User.query.filter_by(username=username).first()
if not user:
print("User not found")
return send_from_directory("templates", "favicon.png", mimetype="image/png")
# Check if file exists # Check if file exists
if os.path.exists(f"website/avatars/{username}.png"): if os.path.exists(f"website/avatars/{username}.png"):
return send_from_directory("avatars", f"{username}.png", mimetype="image/png") return send_from_directory("avatars", f"{username}.png", mimetype="image/png")