add autocomplete for mastodon instances

This commit is contained in:
codl 2017-08-23 11:42:32 +02:00
parent 5e250a4d03
commit 1dfb728805
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
5 changed files with 80 additions and 9 deletions

View File

@ -0,0 +1,40 @@
"""add mastodon_instances
Revision ID: f8a153bc809b
Revises: 5fec5f5e8a5e
Create Date: 2017-08-23 11:27:19.223721
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'f8a153bc809b'
down_revision = '5fec5f5e8a5e'
branch_labels = None
depends_on = None
def upgrade():
op.create_table('mastodon_instances',
sa.Column('instance', sa.String(), nullable=False),
sa.Column('popularity', sa.Float(), server_default='10', nullable=False),
sa.PrimaryKeyConstraint('instance', name=op.f('pk_mastodon_instances'))
)
op.execute("""
INSERT INTO mastodon_instances (instance, popularity) VALUES
('mastodon.social', 1000),
('mastodon.cloud', 900),
('social.tchncs.de', 800),
('mastodon.xyz', 700),
('mstdn.io', 600),
('awoo.space', 500),
('cybre.space', 400),
('mastodon.art', 300)
;
""")
def downgrade():
op.drop_table('mastodon_instances')

View File

@ -211,3 +211,18 @@ class MastodonApp(db.Model, TimestampMixin):
client_id = db.Column(db.String, nullable=False) client_id = db.Column(db.String, nullable=False)
client_secret = db.Column(db.String, nullable=False) client_secret = db.Column(db.String, nullable=False)
protocol = db.Column(ProtoEnum, nullable=False) protocol = db.Column(ProtoEnum, nullable=False)
class MastodonInstance(db.Model):
"""
this is for the autocomplete in the mastodon login form
it isn't coupled with anything else so that we can seed it with
some popular instances ahead of time
"""
__tablename__ = 'mastodon_instances'
instance = db.Column(db.String, primary_key=True)
popularity = db.Column(db.Float, server_default='10', nullable=False)
def bump(self):
self.popularity = self.popularity + 1

View File

@ -6,7 +6,7 @@ import lib
from lib.auth import require_auth, require_auth_api from lib.auth import require_auth, require_auth_api
from lib import set_session_cookie from lib import set_session_cookie
from lib import get_viewer_session, get_viewer from lib import get_viewer_session, get_viewer
from model import Account, Session, Post, TwitterArchive, MastodonApp from model import Account, Session, Post, TwitterArchive, MastodonApp, MastodonInstance
from app import app, db, sentry, limiter from app import app, db, sentry, limiter
import tasks import tasks
from zipfile import BadZipFile from zipfile import BadZipFile
@ -217,11 +217,14 @@ def api_viewer_timers():
@app.route('/login/mastodon', methods=('GET', 'POST')) @app.route('/login/mastodon', methods=('GET', 'POST'))
def mastodon_login_step1(): def mastodon_login_step1():
instances = MastodonInstance.query.filter(MastodonInstance.popularity > 1).order_by(db.desc(MastodonInstance.popularity)).limit(16)
if request.method == 'GET': if request.method == 'GET':
return render_template('mastodon_login.html', generic_error = 'error' in request.args) return render_template('mastodon_login.html', instances=instances, generic_error = 'error' in request.args)
if not 'instance_url' in request.form or not request.form['instance_url']: if not 'instance_url' in request.form or not request.form['instance_url']:
return render_template('mastodon_login.html', address_error=True) return render_template('mastodon_login.html', instances=instances, address_error=True)
instance_url = request.form['instance_url'].split("@")[-1].lower() instance_url = request.form['instance_url'].split("@")[-1].lower()
@ -250,6 +253,11 @@ def mastodon_login_step2(instance):
sess = Session(account = account) sess = Session(account = account)
db.session.add(sess) db.session.add(sess)
i=MastodonInstance(instance=instance)
i=db.session.merge(i)
i.bump()
db.session.commit() db.session.commit()
g.viewer = sess g.viewer = sess

View File

@ -2,7 +2,7 @@ from celery import Celery, Task
from app import app as flaskapp from app import app as flaskapp
from app import db from app import db
from model import Session, Account, TwitterArchive, Post, OAuthToken from model import Session, Account, TwitterArchive, Post, OAuthToken, MastodonInstance
import lib.twitter import lib.twitter
import lib.mastodon import lib.mastodon
from mastodon.Mastodon import MastodonRatelimitError from mastodon.Mastodon import MastodonRatelimitError
@ -128,6 +128,11 @@ def periodic_cleanup():
for account in unreachable: for account in unreachable:
account.policy_enabled = False account.policy_enabled = False
# normalise mastodon instance popularity scores
biggest_instance = MastodonInstance.query.order_by(db.desc(MastodonInstance.popularity)).first()
if biggest_instance.popularity > 100:
MastodonInstance.query.update({MastodonInstance.popularity: MastodonInstance.popularity * 100 / biggest_instance.popularity}, syn)
db.session.commit() db.session.commit()
@app.task @app.task

View File

@ -12,13 +12,16 @@
{% endif %} {% endif %}
<form method='post'> <form method='post'>
<label>Your mastodon address: <label>Mastodon instance:
<input type='text' name='instance_url' placeholder='example@glitch.social'/> <input type='text' name='instance_url' list='instances' placeholder='social.example.net'/>
</label> </label>
<datalist id='instances'>
<option value=''>
{% for instance in instances %}
<option value='{{instance.instance}}'>
{% endfor %}
</datalist>
<input name='confirm' value='Log in' type='submit'/> <input name='confirm' value='Log in' type='submit'/>
<aside>
Your mastodon address is your username, followed by an @ sign and the address of your mastodon instance.
</aside>
</form> </form>
</div> </div>