Add pfp caching for users

This commit is contained in:
Andrew 2022-09-12 15:34:47 -04:00
parent 2b35da71fc
commit 82c0452f55
9 changed files with 80 additions and 105 deletions

View File

@ -147,14 +147,24 @@ class UsersController:
return HelperServers.get_total_owned_servers(exec_user_id)
def update_user(self, user_id: str, user_data=None, user_crafty_data=None):
# check if user crafty perms were updated
if user_crafty_data is None:
user_crafty_data = {}
# check if general user data was updated
if user_data is None:
user_data = {}
# get current user data
base_data = HelperUsers.get_user(user_id)
up_data = {}
# check if we updated user email. If so we update gravatar
if user_data["email"]:
pfp = self.helper.get_gravatar_image(user_data["email"])
up_data["pfp"] = pfp
# create sets to store role data
added_roles = set()
removed_roles = set()
# search for changes in user data
for key in user_data:
if key == "user_id":
continue
@ -174,8 +184,10 @@ class UsersController:
up_data["hints"] = user_data["hints"]
elif base_data[key] != user_data[key]:
up_data[key] = user_data[key]
# change last update for user
up_data["last_update"] = self.helper.get_time_as_string()
logger.debug(f"user: {user_data} +role:{added_roles} -role:{removed_roles}")
for role in added_roles:
HelperUsers.get_or_create(user_id=user_id, role_id=role)
permissions_mask = user_crafty_data.get("permissions_mask", "000")

View File

@ -42,6 +42,7 @@ class Users(BaseModel):
preparing = BooleanField(default=False)
hints = BooleanField(default=True)
manager = IntegerField(default=None, null=True)
pfp = CharField(default="/static/assets/images/faces-clipart/pic-3.png")
class Meta:
table_name = "users"
@ -220,6 +221,7 @@ class HelperUsers:
Users.password: pw_enc,
Users.email: email,
Users.enabled: enabled,
Users.pfp: self.helper.get_gravatar_image(email),
Users.superuser: superuser,
Users.created: Helpers.get_time_as_string(),
Users.manager: manager,

View File

@ -20,6 +20,7 @@ import itertools
from datetime import datetime
from socket import gethostname
from contextlib import redirect_stderr, suppress
import libgravatar
from packaging import version as pkg_version
from app.classes.shared.null_writer import NullWriter
@ -658,6 +659,33 @@ class Helpers:
return True
return False
def get_gravatar_image(self, email):
profile_url = "/static/assets/images/faces-clipart/pic-3.png"
# http://en.gravatar.com/site/implement/images/#rating
if self.get_setting("allow_nsfw_profile_pictures"):
rating = "x"
else:
rating = "g"
# Get grvatar hash for profile pictures
if not self.check_internet() or email != "default@example.com" or email != "":
gravatar = libgravatar.Gravatar(libgravatar.sanitize_email(email))
url = gravatar.get_image(
size=80,
default="404",
force_default=False,
rating=rating,
filetype_extension=False,
use_ssl=True,
) # + "?d=404"
try:
if requests.head(url).status_code != 404:
profile_url = url
except:
profile_url = "/static/assets/images/faces-clipart/pic-3.png"
return profile_url
@staticmethod
def get_file_contents(path: str, lines=100):

View File

@ -666,6 +666,12 @@ class TasksManager:
logger.info(
"No updates found! You are on the most up to date Crafty version."
)
logger.info("Refreshing Gravatar PFPs...")
for user in HelperUsers.get_all_users():
if user.email:
HelperUsers.update_user(
user.id, {"pfp": self.helper.get_gravatar_image(user.email)}
)
def log_watcher(self):
self.controller.servers.check_for_old_logs()

View File

@ -8,7 +8,6 @@ import logging
import threading
import shlex
import bleach
import libgravatar
import requests
import tornado.web
import tornado.escape
@ -331,41 +330,6 @@ class PanelHandler(BaseHandler):
"superuser": superuser,
}
# http://en.gravatar.com/site/implement/images/#rating
if self.helper.get_setting("allow_nsfw_profile_pictures"):
rating = "x"
else:
rating = "g"
# Get grvatar hash for profile pictures
if (
not self.helper.check_internet()
or exec_user["email"] != "default@example.com"
or exec_user["email"] != ""
):
gravatar = libgravatar.Gravatar(
libgravatar.sanitize_email(exec_user["email"])
)
url = gravatar.get_image(
size=80,
default="404",
force_default=False,
rating=rating,
filetype_extension=False,
use_ssl=True,
) # + "?d=404"
try:
if requests.head(url).status_code != 404:
profile_url = url
else:
profile_url = "/static/assets/images/faces-clipart/pic-3.png"
except:
profile_url = "/static/assets/images/faces-clipart/pic-3.png"
else:
profile_url = "/static/assets/images/faces-clipart/pic-3.png"
page_data["user_image"] = profile_url
if page == "unauthorized":
template = "panel/denied.html"

View File

@ -1,6 +1,4 @@
import logging
import libgravatar
import requests
from app.classes.web.base_api_handler import BaseApiHandler
logger = logging.getLogger(__name__)
@ -21,33 +19,5 @@ class ApiUsersUserPfpHandler(BaseApiHandler):
f'User {auth_data[4]["user_id"]} is fetching the pfp for user {user_id}'
)
# http://en.gravatar.com/site/implement/images/#rating
if self.helper.get_setting("allow_nsfw_profile_pictures"):
rating = "x"
else:
rating = "g"
# Get grvatar hash for profile pictures
if (
not self.helper.check_internet()
or user["email"] != "default@example.com"
or user["email"] != ""
):
gravatar = libgravatar.Gravatar(libgravatar.sanitize_email(user["email"]))
url = gravatar.get_image(
size=80,
default="404",
force_default=False,
rating=rating,
filetype_extension=False,
use_ssl=True,
)
try:
requests.head(url).raise_for_status()
except requests.HTTPError as e:
logger.debug("Gravatar profile picture not found", exc_info=e)
else:
self.finish_json(200, {"status": "ok", "data": url})
self.finish_json(200, {"status": "ok", "data": user["pfp"]})
return
self.finish_json(200, {"status": "ok", "data": None})

View File

@ -5,8 +5,6 @@ import time
import tornado.web
import tornado.escape
import bleach
import libgravatar
import requests
from app.classes.models.crafty_permissions import EnumPermissionsCrafty
from app.classes.shared.helpers import Helpers
@ -133,38 +131,6 @@ class ServerHandler(BaseHandler):
"superuser": superuser,
}
if self.helper.get_setting("allow_nsfw_profile_pictures"):
rating = "x"
else:
rating = "g"
if (
not self.helper.check_internet()
or exec_user["email"] != "default@example.com"
or exec_user["email"] != ""
):
gravatar = libgravatar.Gravatar(
libgravatar.sanitize_email(exec_user["email"])
)
url = gravatar.get_image(
size=80,
default="404",
force_default=False,
rating=rating,
filetype_extension=False,
use_ssl=True,
) # + "?d=404"
try:
if requests.head(url).status_code != 404:
profile_url = url
else:
profile_url = "/static/assets/images/faces-clipart/pic-3.png"
except:
profile_url = "/static/assets/images/faces-clipart/pic-3.png"
else:
profile_url = "/static/assets/images/faces-clipart/pic-3.png"
page_data["user_image"] = profile_url
if superuser:
page_data["roles"] = list_roles

View File

@ -18,10 +18,10 @@
<li class="nav-item dropdown user-dropdown">
<a class="nav-link dropdown-toggle" id="UserDropdown" href="#" data-toggle="dropdown" aria-expanded="false">
<img class="img-xs rounded-circle profile-picture" src="{{ data['user_image'] }}" alt="Profile image"> </a>
<img class="img-xs rounded-circle profile-picture" onerror="pfpError(this)" src="{{ data['user_data']['pfp'] }}" alt="Profile image"> </a>
<div class="dropdown-menu dropdown-menu-right navbar-dropdown" aria-labelledby="UserDropdown">
<div class="dropdown-header text-center">
<img class="img-md rounded-circle profile-picture" src="{{ data['user_image'] }}" alt="Profile image">
<img class="img-md rounded-circle profile-picture" onerror="pfpError(this)" src="{{ data['user_data']['pfp'] }}" alt="Profile image">
<p class="mb-1 mt-3 font-weight-semibold">{{ data['user_data']['username'] }}</p>
<p class="font-weight-light text-muted mb-0">Roles: </p>
{% for r in data['user_role'] %}
@ -47,3 +47,11 @@
</div>
</li>
</ul>
<script>
function pfpError(image) {
image.onerror = "";
image.src = "/static/assets/images/faces-clipart/pic-3.png";
return true;
}
</script>

View File

@ -0,0 +1,19 @@
# Generated by database migrator
import peewee
def migrate(migrator, database, **kwargs):
migrator.add_columns(
"users",
pfp=peewee.CharField(default="/static/assets/images/faces-clipart/pic-3.png"),
)
"""
Write your migrations here.
"""
def rollback(migrator, database, **kwargs):
migrator.drop_columns("users", ["pfp"])
"""
Write your rollback migrations here.
"""