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,\
MastodonRatelimitError,\
MastodonUnauthorizedError
from model import MastodonApp, Account, OAuthToken, Post
from model import MastodonApp, Account, OAuthToken, Post, MastodonInstance
from requests import head
from app import db, sentry
from libforget.exceptions import TemporaryError
@ -208,3 +208,13 @@ def delete(post):
MastodonNetworkError,
MastodonRatelimitError) as 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
from libforget.auth import require_auth, csrf,\
get_viewer
from model import Session, TwitterArchive, MastodonApp, MastodonInstance
from model import Session, TwitterArchive, MastodonApp
from app import app, db, sentry, imgproxy
import tasks
from zipfile import BadZipFile
@ -34,13 +34,7 @@ def index():
@app.route('/about/')
def about():
instances = (
MastodonInstance.query
.filter(MastodonInstance.popularity > 5)
.order_by(db.desc(MastodonInstance.instance == 'mastodon.social'),
db.desc(MastodonInstance.popularity),
MastodonInstance.instance)
.limit(5).all())
instances = libforget.mastodon.suggested_instances()
return render_template(
'about.html',
mastodon_instances=instances,
@ -213,12 +207,10 @@ def mastodon_login_step1(instance=None):
or request.form.get('instance_url', None))
if not instance_url:
instances = (
MastodonInstance
.query.filter(MastodonInstance.popularity > 1)
.order_by(db.desc(MastodonInstance.popularity),
MastodonInstance.instance)
.limit(30))
instances = libforget.mastodon.suggested_instances(
limit=30,
min_popularity=1
)
return render_template(
'mastodon_login.html', instances=instances,
address_error=request.method == 'POST',