mirror of
				https://github.com/sb745/NyaaV3.git
				synced 2025-11-04 01:45:46 +02:00 
			
		
		
		
	[Config Change] Various Gravatar-related changes (#467)
* Add config option to enable/disable gravatar This is useful when running a development instance behind a firewall or NAT, where gravatar cannot reach you to serve up the default user avatar. * Pregenerate Gravatar default image URLs If possible (i.e. SERVER_NAME is set), we can pregenerate the constant gravatar default URL once at application startup, and re-use that, as url_for calls are surprisingly expensive. Especially on torrent view pages with lots of comments, this cuts down on url_for calls massively, saving on my system about 0.3 ms per call.
This commit is contained in:
		
							parent
							
								
									e892f358e8
								
							
						
					
					
						commit
						56a670977e
					
				
					 3 changed files with 31 additions and 13 deletions
				
			
		| 
						 | 
				
			
			@ -152,3 +152,7 @@ ES_INDEX_NAME = SITE_FLAVOR
 | 
			
		|||
# Time limit for editing a comment after it has been posted (seconds)
 | 
			
		||||
# Set to 0 to disable
 | 
			
		||||
EDITING_TIME_LIMIT = 0
 | 
			
		||||
 | 
			
		||||
# Whether to use Gravatar or just always use the default avatar
 | 
			
		||||
# (Useful if run as development instance behind NAT/firewall)
 | 
			
		||||
ENABLE_GRAVATAR = True
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -96,4 +96,10 @@ def create_app(config):
 | 
			
		|||
    app.register_blueprint(api_blueprint)
 | 
			
		||||
    register_views(app)
 | 
			
		||||
 | 
			
		||||
    # Pregenerate some URLs to avoid repeat url_for calls
 | 
			
		||||
    if 'SERVER_NAME' in app.config and app.config['SERVER_NAME']:
 | 
			
		||||
        with app.app_context():
 | 
			
		||||
            url = flask.url_for('static', filename='img/avatar/default.png', _external=True)
 | 
			
		||||
            app.config['DEFAULT_GRAVATAR_URL'] = url
 | 
			
		||||
 | 
			
		||||
    return app
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -518,12 +518,18 @@ class User(db.Model):
 | 
			
		|||
        return all(checks)
 | 
			
		||||
 | 
			
		||||
    def gravatar_url(self):
 | 
			
		||||
        if 'DEFAULT_GRAVATAR_URL' in app.config:
 | 
			
		||||
            default_url = app.config['DEFAULT_GRAVATAR_URL']
 | 
			
		||||
        else:
 | 
			
		||||
            default_url = flask.url_for('static', filename='img/avatar/default.png',
 | 
			
		||||
                                        _external=True)
 | 
			
		||||
        if app.config['ENABLE_GRAVATAR']:
 | 
			
		||||
            # from http://en.gravatar.com/site/implement/images/python/
 | 
			
		||||
            params = {
 | 
			
		||||
                # Image size (https://en.gravatar.com/site/implement/images/#size)
 | 
			
		||||
                's': 120,
 | 
			
		||||
                # Default image (https://en.gravatar.com/site/implement/images/#default-image)
 | 
			
		||||
            'd': flask.url_for('static', filename='img/avatar/default.png', _external=True),
 | 
			
		||||
                'd': default_url,
 | 
			
		||||
                # Image rating (https://en.gravatar.com/site/implement/images/#rating)
 | 
			
		||||
                # Nyaa: PG-rated, Sukebei: X-rated
 | 
			
		||||
                'r': 'pg' if app.config['SITE_FLAVOR'] == 'nyaa' else 'x',
 | 
			
		||||
| 
						 | 
				
			
			@ -531,6 +537,8 @@ class User(db.Model):
 | 
			
		|||
            # construct the url
 | 
			
		||||
            return 'https://www.gravatar.com/avatar/{}?{}'.format(
 | 
			
		||||
                md5(self.email.encode('utf-8').lower()).hexdigest(), urlencode(params))
 | 
			
		||||
        else:
 | 
			
		||||
            return default_url
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def userlevel_str(self):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue