move popular instances query to libforget.mastodon

This commit is contained in:
codl 2018-05-07 20:27:02 +02:00
parent 0caac7c679
commit c42b0f7f87
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
2 changed files with 17 additions and 15 deletions

View File

@ -3,7 +3,7 @@ from mastodon.Mastodon import MastodonAPIError,\
MastodonNetworkError,\ MastodonNetworkError,\
MastodonRatelimitError,\ MastodonRatelimitError,\
MastodonUnauthorizedError MastodonUnauthorizedError
from model import MastodonApp, Account, OAuthToken, Post from model import MastodonApp, Account, OAuthToken, Post, MastodonInstance
from requests import head from requests import head
from app import db, sentry from app import db, sentry
from libforget.exceptions import TemporaryError from libforget.exceptions import TemporaryError
@ -208,3 +208,13 @@ def delete(post):
MastodonNetworkError, MastodonNetworkError,
MastodonRatelimitError) as e: MastodonRatelimitError) as e:
raise TemporaryError(e) raise TemporaryError(e)
def suggested_instances(limit=5, min_popularity=5):
return (
MastodonInstance.query
.filter(MastodonInstance.popularity > min_popularity)
.order_by(db.desc(MastodonInstance.instance == 'mastodon.social'),
db.desc(MastodonInstance.popularity),
MastodonInstance.instance)
.limit(limit).all())

View File

@ -5,7 +5,7 @@ import libforget.twitter
import libforget.mastodon import libforget.mastodon
from libforget.auth import require_auth, csrf,\ from libforget.auth import require_auth, csrf,\
get_viewer get_viewer
from model import Session, TwitterArchive, MastodonApp, MastodonInstance from model import Session, TwitterArchive, MastodonApp
from app import app, db, sentry, imgproxy from app import app, db, sentry, imgproxy
import tasks import tasks
from zipfile import BadZipFile from zipfile import BadZipFile
@ -34,13 +34,7 @@ def index():
@app.route('/about/') @app.route('/about/')
def about(): def about():
instances = ( instances = libforget.mastodon.suggested_instances()
MastodonInstance.query
.filter(MastodonInstance.popularity > 5)
.order_by(db.desc(MastodonInstance.instance == 'mastodon.social'),
db.desc(MastodonInstance.popularity),
MastodonInstance.instance)
.limit(5).all())
return render_template( return render_template(
'about.html', 'about.html',
mastodon_instances=instances, mastodon_instances=instances,
@ -213,12 +207,10 @@ def mastodon_login_step1(instance=None):
or request.form.get('instance_url', None)) or request.form.get('instance_url', None))
if not instance_url: if not instance_url:
instances = ( instances = libforget.mastodon.suggested_instances(
MastodonInstance limit=30,
.query.filter(MastodonInstance.popularity > 1) min_popularity=1
.order_by(db.desc(MastodonInstance.popularity), )
MastodonInstance.instance)
.limit(30))
return render_template( return render_template(
'mastodon_login.html', instances=instances, 'mastodon_login.html', instances=instances,
address_error=request.method == 'POST', address_error=request.method == 'POST',