2019-05-01 15:33:28 +02:00
|
|
|
from flask import (
|
|
|
|
|
Flask, Markup, abort, flash, g, jsonify, redirect, render_template, request,
|
2019-05-02 15:47:23 +02:00
|
|
|
send_from_directory, session, url_for)
|
2019-05-01 15:33:28 +02:00
|
|
|
import hashlib
|
|
|
|
|
from peewee import *
|
2019-10-14 14:30:22 +02:00
|
|
|
import datetime, time, re, os, sys, string, json, html
|
2019-05-01 15:33:28 +02:00
|
|
|
from functools import wraps
|
2019-10-12 19:22:10 +02:00
|
|
|
import argparse
|
|
|
|
|
from flask_login import LoginManager, login_user, logout_user, login_required
|
2019-05-01 15:33:28 +02:00
|
|
|
|
2019-10-16 19:06:09 +02:00
|
|
|
__version__ = '0.6-dev'
|
2019-10-09 19:51:18 +02:00
|
|
|
|
2019-10-11 12:20:40 +02:00
|
|
|
# we want to support Python 3 only.
|
|
|
|
|
# Python 2 has too many caveats.
|
|
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
|
raise RuntimeError('Python 3 required')
|
2019-10-12 19:22:10 +02:00
|
|
|
|
|
|
|
|
arg_parser = argparse.ArgumentParser()
|
|
|
|
|
arg_parser.add_argument('--norun', action='store_true',
|
|
|
|
|
help='Don\'t run the app. Useful for debugging.')
|
|
|
|
|
arg_parser.add_argument('-p', '--port', type=int, default=5000,
|
|
|
|
|
help='The port where to run the app. Defaults to 5000')
|
|
|
|
|
|
2019-05-01 15:33:28 +02:00
|
|
|
app = Flask(__name__)
|
2019-10-10 19:31:36 +02:00
|
|
|
app.config.from_pyfile('config.py')
|
2019-05-01 15:33:28 +02:00
|
|
|
|
2019-10-12 19:22:10 +02:00
|
|
|
login_manager = LoginManager(app)
|
|
|
|
|
|
2019-10-10 21:14:58 +02:00
|
|
|
### DATABASE ###
|
|
|
|
|
|
2019-10-10 19:31:36 +02:00
|
|
|
database = SqliteDatabase(app.config['DATABASE'])
|
2019-05-01 15:33:28 +02:00
|
|
|
|
|
|
|
|
class BaseModel(Model):
|
|
|
|
|
class Meta:
|
|
|
|
|
database = database
|
|
|
|
|
|
|
|
|
|
# A user. The user is separated from its page.
|
|
|
|
|
class User(BaseModel):
|
|
|
|
|
# The unique username.
|
|
|
|
|
username = CharField(unique=True)
|
|
|
|
|
# The password hash.
|
|
|
|
|
password = CharField()
|
|
|
|
|
# An email address.
|
|
|
|
|
email = CharField()
|
|
|
|
|
# The date of birth (required because of Terms of Service)
|
|
|
|
|
birthday = DateField()
|
|
|
|
|
# The date joined
|
|
|
|
|
join_date = DateTimeField()
|
|
|
|
|
# A disabled flag. 0 = active, 1 = disabled by user, 2 = banned
|
|
|
|
|
is_disabled = IntegerField(default=0)
|
|
|
|
|
|
2019-10-12 19:22:10 +02:00
|
|
|
# Helpers for flask_login
|
|
|
|
|
def get_id(self):
|
|
|
|
|
return str(self.id)
|
|
|
|
|
@property
|
|
|
|
|
def is_active(self):
|
|
|
|
|
return not self.is_disabled
|
|
|
|
|
@property
|
|
|
|
|
def is_anonymous(self):
|
|
|
|
|
return False
|
|
|
|
|
@property
|
|
|
|
|
def is_authenticated(self):
|
|
|
|
|
return self == get_current_user()
|
|
|
|
|
|
2019-05-01 15:33:28 +02:00
|
|
|
# it often makes sense to put convenience methods on model instances, for
|
|
|
|
|
# example, "give me all the users this user is following":
|
|
|
|
|
def following(self):
|
|
|
|
|
# query other users through the "relationship" table
|
|
|
|
|
return (User
|
|
|
|
|
.select()
|
|
|
|
|
.join(Relationship, on=Relationship.to_user)
|
|
|
|
|
.where(Relationship.from_user == self)
|
|
|
|
|
.order_by(User.username))
|
|
|
|
|
|
|
|
|
|
def followers(self):
|
|
|
|
|
return (User
|
|
|
|
|
.select()
|
|
|
|
|
.join(Relationship, on=Relationship.from_user)
|
|
|
|
|
.where(Relationship.to_user == self)
|
|
|
|
|
.order_by(User.username))
|
|
|
|
|
|
|
|
|
|
def is_following(self, user):
|
|
|
|
|
return (Relationship
|
|
|
|
|
.select()
|
|
|
|
|
.where(
|
|
|
|
|
(Relationship.from_user == self) &
|
|
|
|
|
(Relationship.to_user == user))
|
|
|
|
|
.exists())
|
|
|
|
|
|
2019-05-05 10:09:27 +02:00
|
|
|
def unseen_notification_count(self):
|
|
|
|
|
return len(Notification
|
|
|
|
|
.select()
|
|
|
|
|
.where(
|
2019-05-22 18:25:08 +02:00
|
|
|
(Notification.target == self) & (Notification.seen == 0)
|
2019-05-05 10:09:27 +02:00
|
|
|
))
|
2019-10-17 14:34:55 +02:00
|
|
|
# user adminship is stored into a separate table; new in 0.6
|
|
|
|
|
@property
|
|
|
|
|
def is_admin(self):
|
|
|
|
|
return UserAdminship.select().where(UserAdminship.user == self).exists()
|
|
|
|
|
# user profile info; new in 0.6
|
|
|
|
|
@property
|
|
|
|
|
def profile(self):
|
|
|
|
|
# lazy initialization; I don't want (and don't know how)
|
|
|
|
|
# to do schema changes.
|
|
|
|
|
try:
|
|
|
|
|
return UserProfile.get(UserProfile.user == self)
|
|
|
|
|
except UserProfile.DoesNotExist:
|
|
|
|
|
return UserProfile.create(user=self, full_name=self.username)
|
|
|
|
|
|
|
|
|
|
# User adminship.
|
|
|
|
|
# A very high privilege where users can review posts.
|
|
|
|
|
# For very few users only; new in 0.6
|
|
|
|
|
class UserAdminship(BaseModel):
|
|
|
|
|
user = ForeignKeyField(User, primary_key=True)
|
|
|
|
|
|
|
|
|
|
# User profile.
|
|
|
|
|
# Additional info for identifying users.
|
|
|
|
|
# New in 0.6
|
|
|
|
|
class UserProfile(BaseModel):
|
|
|
|
|
user = ForeignKeyField(User, primary_key=True)
|
|
|
|
|
full_name = TextField()
|
|
|
|
|
biography = TextField(default='')
|
|
|
|
|
location = IntegerField(null=True)
|
|
|
|
|
year = IntegerField(null=True)
|
|
|
|
|
website = TextField(null=True)
|
|
|
|
|
instagram = TextField(null=True)
|
|
|
|
|
facebook = TextField(null=True)
|
2019-05-05 10:09:27 +02:00
|
|
|
|
2019-10-12 19:22:10 +02:00
|
|
|
# The message privacy values.
|
|
|
|
|
MSGPRV_PUBLIC = 0 # everyone
|
|
|
|
|
MSGPRV_UNLISTED = 1 # everyone, doesn't show up in public timeline
|
|
|
|
|
MSGPRV_FRIENDS = 2 # only accounts which follow each other
|
|
|
|
|
MSGPRV_ONLYME = 3 # only the poster
|
|
|
|
|
|
2019-05-01 15:33:28 +02:00
|
|
|
# A single public message.
|
2019-10-12 19:22:10 +02:00
|
|
|
# New in v0.5: removed type and info fields; added privacy field.
|
2019-05-01 15:33:28 +02:00
|
|
|
class Message(BaseModel):
|
|
|
|
|
# The user who posted the message.
|
|
|
|
|
user = ForeignKeyField(User, backref='messages')
|
|
|
|
|
# The text of the message.
|
|
|
|
|
text = TextField()
|
|
|
|
|
# The posted date.
|
|
|
|
|
pub_date = DateTimeField()
|
2019-10-10 21:14:58 +02:00
|
|
|
# Info about privacy of the message.
|
2019-10-12 19:22:10 +02:00
|
|
|
privacy = IntegerField(default=MSGPRV_PUBLIC)
|
|
|
|
|
|
2019-10-10 21:14:58 +02:00
|
|
|
def is_visible(self, is_public_timeline=False):
|
|
|
|
|
user = self.user
|
|
|
|
|
cur_user = get_current_user()
|
|
|
|
|
privacy = self.privacy
|
|
|
|
|
if user == cur_user:
|
|
|
|
|
# short path
|
2019-10-17 14:34:55 +02:00
|
|
|
# also: don't show user's messages in public timeline
|
|
|
|
|
return not is_public_timeline
|
2019-10-10 21:14:58 +02:00
|
|
|
elif privacy == MSGPRV_PUBLIC:
|
|
|
|
|
return True
|
|
|
|
|
elif privacy == MSGPRV_UNLISTED:
|
|
|
|
|
# even if unlisted
|
|
|
|
|
return not is_public_timeline
|
|
|
|
|
elif privacy == MSGPRV_FRIENDS:
|
|
|
|
|
if cur_user is None:
|
|
|
|
|
return False
|
|
|
|
|
return user.is_following(cur_user) and cur_user.is_following(user)
|
|
|
|
|
else:
|
|
|
|
|
return False
|
|
|
|
|
|
2019-05-01 15:33:28 +02:00
|
|
|
# this model contains two foreign keys to user -- it essentially allows us to
|
|
|
|
|
# model a "many-to-many" relationship between users. by querying and joining
|
|
|
|
|
# on different columns we can expose who a user is "related to" and who is
|
|
|
|
|
# "related to" a given user
|
|
|
|
|
class Relationship(BaseModel):
|
|
|
|
|
from_user = ForeignKeyField(User, backref='relationships')
|
|
|
|
|
to_user = ForeignKeyField(User, backref='related_to')
|
|
|
|
|
created_date = DateTimeField()
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
indexes = (
|
|
|
|
|
# Specify a unique multi-column index on from/to-user.
|
|
|
|
|
(('from_user', 'to_user'), True),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2019-05-02 15:47:23 +02:00
|
|
|
UPLOAD_DIRECTORY = 'uploads/'
|
2019-10-14 21:24:41 +02:00
|
|
|
|
|
|
|
|
# fixing directory name because of imports from other directory
|
|
|
|
|
if __name__ != '__main__':
|
|
|
|
|
UPLOAD_DIRECTORY = os.path.join(os.path.dirname(__file__), UPLOAD_DIRECTORY)
|
2019-05-02 15:47:23 +02:00
|
|
|
class Upload(BaseModel):
|
|
|
|
|
# the extension of the media
|
|
|
|
|
type = TextField()
|
|
|
|
|
# the message bound to this media
|
|
|
|
|
message = ForeignKeyField(Message, backref='uploads')
|
|
|
|
|
# helper to retrieve contents
|
|
|
|
|
def filename(self):
|
|
|
|
|
return str(self.id) + '.' + self.type
|
|
|
|
|
|
2019-05-05 10:09:27 +02:00
|
|
|
class Notification(BaseModel):
|
|
|
|
|
type = TextField()
|
|
|
|
|
target = ForeignKeyField(User, backref='notifications')
|
|
|
|
|
detail = TextField()
|
|
|
|
|
pub_date = DateTimeField()
|
|
|
|
|
seen = IntegerField(default=0)
|
|
|
|
|
|
2019-05-01 15:33:28 +02:00
|
|
|
def create_tables():
|
|
|
|
|
with database:
|
2019-10-10 21:14:58 +02:00
|
|
|
database.create_tables([
|
2019-10-17 14:34:55 +02:00
|
|
|
User, UserAdminship, UserProfile, Message, Relationship,
|
|
|
|
|
Upload, Notification])
|
2019-05-02 15:47:23 +02:00
|
|
|
if not os.path.isdir(UPLOAD_DIRECTORY):
|
|
|
|
|
os.makedirs(UPLOAD_DIRECTORY)
|
2019-05-01 15:33:28 +02:00
|
|
|
|
2019-10-10 21:14:58 +02:00
|
|
|
### UTILS ###
|
|
|
|
|
|
2019-05-01 15:33:28 +02:00
|
|
|
_forbidden_extensions = 'com net org txt'.split()
|
2019-05-02 19:55:53 +02:00
|
|
|
_username_characters = frozenset(string.ascii_letters + string.digits + '_')
|
2019-05-01 15:33:28 +02:00
|
|
|
|
|
|
|
|
def is_username(username):
|
|
|
|
|
username_splitted = username.split('.')
|
|
|
|
|
if username_splitted and username_splitted[-1] in _forbidden_extensions:
|
|
|
|
|
return False
|
2019-05-02 19:55:53 +02:00
|
|
|
return all(x and set(x) < _username_characters for x in username_splitted)
|
2019-05-01 15:33:28 +02:00
|
|
|
|
2019-10-11 14:53:13 +02:00
|
|
|
_mention_re = r'\+([A-Za-z0-9_]+(?:\.[A-Za-z0-9_]+)*)'
|
|
|
|
|
|
2019-05-01 15:33:28 +02:00
|
|
|
def validate_birthday(date):
|
|
|
|
|
today = datetime.date.today()
|
|
|
|
|
if today.year - date.year > 13:
|
|
|
|
|
return True
|
|
|
|
|
if today.year - date.year < 13:
|
|
|
|
|
return False
|
|
|
|
|
if today.month > date.month:
|
|
|
|
|
return True
|
|
|
|
|
if today.month < date.month:
|
|
|
|
|
return False
|
|
|
|
|
if today.day >= date.day:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
2019-10-20 13:14:16 +02:00
|
|
|
def validate_website(website):
|
|
|
|
|
return re.match(r'(?:https?://)?(?:[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*'
|
|
|
|
|
r'|\[[A-Fa-f0-9:]+\])(?::\d+)?(?:/[^\s]*)?(?:\?[^\s]*)?(?:#[^\s]*)?$',
|
|
|
|
|
website)
|
|
|
|
|
|
2019-05-01 15:33:28 +02:00
|
|
|
def human_short_date(timestamp):
|
|
|
|
|
return ''
|
|
|
|
|
|
|
|
|
|
@app.template_filter()
|
|
|
|
|
def human_date(date):
|
|
|
|
|
timestamp = date.timestamp()
|
|
|
|
|
today = int(time.time())
|
|
|
|
|
offset = today - timestamp
|
|
|
|
|
if offset <= 1:
|
|
|
|
|
return '1 second ago'
|
|
|
|
|
elif offset < 60:
|
|
|
|
|
return '%d seconds ago' % offset
|
|
|
|
|
elif offset < 120:
|
|
|
|
|
return '1 minute ago'
|
|
|
|
|
elif offset < 3600:
|
|
|
|
|
return '%d minutes ago' % (offset // 60)
|
|
|
|
|
elif offset < 7200:
|
|
|
|
|
return '1 hour ago'
|
|
|
|
|
elif offset < 86400:
|
|
|
|
|
return '%d hours ago' % (offset // 3600)
|
|
|
|
|
elif offset < 172800:
|
|
|
|
|
return '1 day ago'
|
|
|
|
|
elif offset < 604800:
|
|
|
|
|
return '%d days ago' % (offset // 86400)
|
|
|
|
|
else:
|
|
|
|
|
d = datetime.datetime.fromtimestamp(timestamp)
|
|
|
|
|
return d.strftime('%B %e, %Y')
|
|
|
|
|
|
|
|
|
|
def int_to_b64(n):
|
|
|
|
|
b = int(n).to_bytes(48, 'big')
|
|
|
|
|
return base64.b64encode(b).lstrip(b'A').decode()
|
|
|
|
|
|
|
|
|
|
def pwdhash(s):
|
|
|
|
|
return hashlib.md5((request.form['password']).encode('utf-8')).hexdigest()
|
|
|
|
|
|
|
|
|
|
def get_object_or_404(model, *expressions):
|
|
|
|
|
try:
|
|
|
|
|
return model.get(*expressions)
|
|
|
|
|
except model.DoesNotExist:
|
|
|
|
|
abort(404)
|
|
|
|
|
|
2019-10-10 21:14:58 +02:00
|
|
|
class Visibility(object):
|
|
|
|
|
'''
|
|
|
|
|
Workaround for the visibility problem for posts.
|
|
|
|
|
Cannot be directly resolved with filter().
|
|
|
|
|
|
|
|
|
|
TODO find a better solution, this seems to be too slow.
|
|
|
|
|
'''
|
|
|
|
|
def __init__(self, query, is_public_timeline=False):
|
|
|
|
|
self.query = query
|
|
|
|
|
self.is_public_timeline = is_public_timeline
|
|
|
|
|
def __iter__(self):
|
|
|
|
|
for i in self.query:
|
|
|
|
|
if i.is_visible(self.is_public_timeline):
|
|
|
|
|
yield i
|
|
|
|
|
def count(self):
|
|
|
|
|
counter = 0
|
|
|
|
|
for i in self.query:
|
|
|
|
|
if i.is_visible(self.is_public_timeline):
|
|
|
|
|
counter += 1
|
|
|
|
|
return counter
|
|
|
|
|
def paginate(self, page):
|
|
|
|
|
counter = 0
|
|
|
|
|
pages_no = range((page - 1) * 20, page * 20)
|
|
|
|
|
for i in self.query:
|
|
|
|
|
if i.is_visible(self.is_public_timeline):
|
|
|
|
|
if counter in pages_no:
|
|
|
|
|
yield i
|
|
|
|
|
counter += 1
|
|
|
|
|
|
2019-10-20 13:14:16 +02:00
|
|
|
def get_locations():
|
|
|
|
|
data = {}
|
|
|
|
|
with open('locations.txt') as f:
|
|
|
|
|
for line in f:
|
|
|
|
|
line = line.rstrip()
|
|
|
|
|
if line.startswith('#'):
|
|
|
|
|
continue
|
|
|
|
|
try:
|
|
|
|
|
key, value = line.split(None, 1)
|
|
|
|
|
except ValueError:
|
|
|
|
|
continue
|
|
|
|
|
data[key] = value
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
locations = get_locations()
|
|
|
|
|
except OSError:
|
|
|
|
|
locations = {}
|
|
|
|
|
|
2019-05-01 15:33:28 +02:00
|
|
|
# get the user from the session
|
2019-10-12 19:22:10 +02:00
|
|
|
# changed in 0.5 to comply with flask_login
|
2019-05-01 15:33:28 +02:00
|
|
|
def get_current_user():
|
2019-10-12 19:22:10 +02:00
|
|
|
user_id = session.get('user_id')
|
|
|
|
|
if user_id:
|
|
|
|
|
return User[user_id]
|
|
|
|
|
|
|
|
|
|
login_manager.login_view = 'login'
|
2019-05-01 15:33:28 +02:00
|
|
|
|
2019-05-05 10:09:27 +02:00
|
|
|
def push_notification(type, target, **kwargs):
|
|
|
|
|
try:
|
|
|
|
|
if isinstance(target, str):
|
|
|
|
|
target = User.get(User.username == target)
|
|
|
|
|
Notification.create(
|
|
|
|
|
type=type,
|
|
|
|
|
target=target,
|
|
|
|
|
detail=json.dumps(kwargs),
|
|
|
|
|
pub_date=datetime.datetime.now()
|
|
|
|
|
)
|
|
|
|
|
except Exception:
|
|
|
|
|
sys.excepthook(*sys.exc_info())
|
|
|
|
|
|
|
|
|
|
def unpush_notification(type, target, **kwargs):
|
|
|
|
|
try:
|
|
|
|
|
if isinstance(target, str):
|
|
|
|
|
target = User.get(User.username == target)
|
|
|
|
|
(Notification
|
|
|
|
|
.delete()
|
|
|
|
|
.where(
|
|
|
|
|
(Notification.type == type) &
|
|
|
|
|
(Notification.target == target) &
|
|
|
|
|
(Notification.detail == json.dumps(kwargs))
|
2019-10-11 17:20:24 +02:00
|
|
|
)
|
|
|
|
|
.execute())
|
2019-05-05 10:09:27 +02:00
|
|
|
except Exception:
|
|
|
|
|
sys.excepthook(*sys.exc_info())
|
|
|
|
|
|
2019-05-01 15:33:28 +02:00
|
|
|
# given a template and a SelectQuery instance, render a paginated list of
|
|
|
|
|
# objects from the query inside the template
|
|
|
|
|
def object_list(template_name, qr, var_name='object_list', **kwargs):
|
|
|
|
|
kwargs.update(
|
|
|
|
|
page=int(request.args.get('page', 1)),
|
2019-10-11 12:20:40 +02:00
|
|
|
pages=qr.count() // 20 + 1)
|
2019-05-01 15:33:28 +02:00
|
|
|
kwargs[var_name] = qr.paginate(kwargs['page'])
|
|
|
|
|
return render_template(template_name, **kwargs)
|
|
|
|
|
|
2019-10-11 14:53:13 +02:00
|
|
|
### WEB ###
|
|
|
|
|
|
2019-05-01 15:33:28 +02:00
|
|
|
@app.before_request
|
|
|
|
|
def before_request():
|
|
|
|
|
g.db = database
|
2019-10-14 21:06:53 +02:00
|
|
|
try:
|
|
|
|
|
g.db.connect()
|
|
|
|
|
except OperationalError:
|
|
|
|
|
sys.stderr.write('database connected twice.\n')
|
2019-05-01 15:33:28 +02:00
|
|
|
|
|
|
|
|
@app.after_request
|
|
|
|
|
def after_request(response):
|
|
|
|
|
g.db.close()
|
|
|
|
|
return response
|
|
|
|
|
|
2019-10-15 14:13:55 +02:00
|
|
|
@app.context_processor
|
|
|
|
|
def _inject_variables():
|
2019-10-20 13:14:16 +02:00
|
|
|
return {'site_name': app.config['SITE_NAME'], 'locations': locations}
|
2019-10-15 14:13:55 +02:00
|
|
|
|
2019-10-12 19:22:10 +02:00
|
|
|
@login_manager.user_loader
|
|
|
|
|
def _inject_user(userid):
|
|
|
|
|
return User[userid]
|
2019-05-01 15:33:28 +02:00
|
|
|
|
|
|
|
|
@app.errorhandler(404)
|
|
|
|
|
def error_404(body):
|
|
|
|
|
return render_template('404.html')
|
|
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
|
def homepage():
|
2019-10-12 19:22:10 +02:00
|
|
|
if get_current_user():
|
2019-05-01 15:33:28 +02:00
|
|
|
return private_timeline()
|
|
|
|
|
else:
|
|
|
|
|
return render_template('homepage.html')
|
|
|
|
|
|
|
|
|
|
def private_timeline():
|
2019-10-10 21:14:58 +02:00
|
|
|
# the private timeline (aka feed) exemplifies the use of a subquery -- we are asking for
|
2019-05-01 15:33:28 +02:00
|
|
|
# messages where the person who created the message is someone the current
|
|
|
|
|
# user is following. these messages are then ordered newest-first.
|
|
|
|
|
user = get_current_user()
|
2019-10-10 21:14:58 +02:00
|
|
|
messages = Visibility(Message
|
2019-05-01 15:33:28 +02:00
|
|
|
.select()
|
2019-05-02 15:47:23 +02:00
|
|
|
.where((Message.user << user.following())
|
|
|
|
|
| (Message.user == user))
|
2019-05-01 15:33:28 +02:00
|
|
|
.order_by(Message.pub_date.desc()))
|
2019-10-10 21:14:58 +02:00
|
|
|
# TODO change to "feed.html"
|
2019-05-01 15:33:28 +02:00
|
|
|
return object_list('private_messages.html', messages, 'message_list')
|
|
|
|
|
|
2019-05-22 18:25:08 +02:00
|
|
|
@app.route('/explore/')
|
|
|
|
|
def public_timeline():
|
2019-10-10 21:14:58 +02:00
|
|
|
messages = Visibility(Message
|
2019-05-22 18:25:08 +02:00
|
|
|
.select()
|
2019-10-10 21:14:58 +02:00
|
|
|
.order_by(Message.pub_date.desc()), True)
|
2019-05-22 18:25:08 +02:00
|
|
|
return object_list('explore.html', messages, 'message_list')
|
|
|
|
|
|
2019-05-01 15:33:28 +02:00
|
|
|
@app.route('/signup/', methods=['GET', 'POST'])
|
|
|
|
|
def register():
|
|
|
|
|
if request.method == 'POST' and request.form['username']:
|
|
|
|
|
try:
|
|
|
|
|
birthday = datetime.datetime.fromisoformat(request.form['birthday'])
|
|
|
|
|
except ValueError:
|
|
|
|
|
flash('Invalid date format')
|
|
|
|
|
return render_template('join.html')
|
|
|
|
|
username = request.form['username'].lower()
|
|
|
|
|
if not is_username(username):
|
|
|
|
|
flash('This username is invalid')
|
2019-10-17 14:34:55 +02:00
|
|
|
return render_template('join.html')
|
|
|
|
|
if username == getattr(get_current_user(), 'username', None) and not request.form.get('confirm_another'):
|
|
|
|
|
flash('You are already logged in. Please confirm you want to '
|
|
|
|
|
'create another account by checking the option.')
|
|
|
|
|
return render_template('join.html')
|
2019-05-01 15:33:28 +02:00
|
|
|
try:
|
|
|
|
|
with database.atomic():
|
|
|
|
|
# Attempt to create the user. If the username is taken, due to the
|
|
|
|
|
# unique constraint, the database will raise an IntegrityError.
|
|
|
|
|
user = User.create(
|
|
|
|
|
username=username,
|
|
|
|
|
password=pwdhash(request.form['password']),
|
|
|
|
|
email=request.form['email'],
|
|
|
|
|
birthday=birthday,
|
|
|
|
|
join_date=datetime.datetime.now())
|
2019-10-17 14:34:55 +02:00
|
|
|
UserProfile.create(
|
|
|
|
|
user=user,
|
|
|
|
|
full_name=request.form.get('full_name') or username
|
|
|
|
|
)
|
2019-05-01 15:33:28 +02:00
|
|
|
|
|
|
|
|
# mark the user as being 'authenticated' by setting the session vars
|
2019-10-12 19:22:10 +02:00
|
|
|
login_user(user)
|
2019-10-11 12:57:40 +02:00
|
|
|
return redirect(request.args.get('next','/'))
|
2019-05-01 15:33:28 +02:00
|
|
|
|
|
|
|
|
except IntegrityError:
|
|
|
|
|
flash('That username is already taken')
|
|
|
|
|
|
|
|
|
|
return render_template('join.html')
|
|
|
|
|
|
|
|
|
|
@app.route('/login/', methods=['GET', 'POST'])
|
|
|
|
|
def login():
|
|
|
|
|
if request.method == 'POST' and request.form['username']:
|
|
|
|
|
try:
|
2019-10-11 12:57:40 +02:00
|
|
|
username = request.form['username']
|
2019-05-01 15:33:28 +02:00
|
|
|
pw_hash = pwdhash(request.form['password'])
|
2019-10-11 12:57:40 +02:00
|
|
|
if '@' in username:
|
|
|
|
|
user = User.get(User.email == username)
|
|
|
|
|
else:
|
|
|
|
|
user = User.get(User.username == username)
|
|
|
|
|
if user.password != pw_hash:
|
|
|
|
|
flash('The password entered is incorrect.')
|
|
|
|
|
return render_template('login.html')
|
2019-05-01 15:33:28 +02:00
|
|
|
except User.DoesNotExist:
|
2019-10-11 12:57:40 +02:00
|
|
|
flash('A user with this username or email does not exist.')
|
2019-05-01 15:33:28 +02:00
|
|
|
else:
|
2019-10-12 19:22:10 +02:00
|
|
|
remember_for = int(request.form['remember'])
|
|
|
|
|
if remember_for > 0:
|
|
|
|
|
login_user(user, remember=True,
|
|
|
|
|
duration=datetime.timedelta(days=remember_for))
|
|
|
|
|
else:
|
|
|
|
|
login_user(user)
|
2019-10-11 12:57:40 +02:00
|
|
|
return redirect(request.args.get('next', '/'))
|
2019-05-01 15:33:28 +02:00
|
|
|
return render_template('login.html')
|
|
|
|
|
|
|
|
|
|
@app.route('/logout/')
|
|
|
|
|
def logout():
|
2019-10-12 19:22:10 +02:00
|
|
|
logout_user()
|
2019-05-01 15:33:28 +02:00
|
|
|
flash('You were logged out')
|
2019-10-11 12:57:40 +02:00
|
|
|
return redirect(request.args.get('next','/'))
|
2019-05-01 15:33:28 +02:00
|
|
|
|
|
|
|
|
@app.route('/+<username>/')
|
|
|
|
|
def user_detail(username):
|
|
|
|
|
user = get_object_or_404(User, User.username == username)
|
|
|
|
|
|
|
|
|
|
# get all the users messages ordered newest-first -- note how we're accessing
|
|
|
|
|
# the messages -- user.message_set. could also have written it as:
|
|
|
|
|
# Message.select().where(Message.user == user)
|
2019-10-11 14:53:13 +02:00
|
|
|
messages = Visibility(user.messages.order_by(Message.pub_date.desc()))
|
2019-10-12 19:22:10 +02:00
|
|
|
# TODO change to "profile.html"
|
2019-05-01 15:33:28 +02:00
|
|
|
return object_list('user_detail.html', messages, 'message_list', user=user)
|
|
|
|
|
|
|
|
|
|
@app.route('/+<username>/follow/', methods=['POST'])
|
|
|
|
|
@login_required
|
|
|
|
|
def user_follow(username):
|
2019-05-05 10:09:27 +02:00
|
|
|
cur_user = get_current_user()
|
2019-05-01 15:33:28 +02:00
|
|
|
user = get_object_or_404(User, User.username == username)
|
|
|
|
|
try:
|
|
|
|
|
with database.atomic():
|
|
|
|
|
Relationship.create(
|
2019-05-05 10:09:27 +02:00
|
|
|
from_user=cur_user,
|
2019-05-01 15:33:28 +02:00
|
|
|
to_user=user,
|
|
|
|
|
created_date=datetime.datetime.now())
|
|
|
|
|
except IntegrityError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
flash('You are following %s' % user.username)
|
2019-05-05 10:09:27 +02:00
|
|
|
push_notification('follow', user, user=cur_user.id)
|
2019-05-01 15:33:28 +02:00
|
|
|
return redirect(url_for('user_detail', username=user.username))
|
|
|
|
|
|
|
|
|
|
@app.route('/+<username>/unfollow/', methods=['POST'])
|
|
|
|
|
@login_required
|
|
|
|
|
def user_unfollow(username):
|
2019-05-05 10:09:27 +02:00
|
|
|
cur_user = get_current_user()
|
2019-05-01 15:33:28 +02:00
|
|
|
user = get_object_or_404(User, User.username == username)
|
|
|
|
|
(Relationship
|
|
|
|
|
.delete()
|
|
|
|
|
.where(
|
2019-05-05 10:09:27 +02:00
|
|
|
(Relationship.from_user == cur_user) &
|
2019-05-01 15:33:28 +02:00
|
|
|
(Relationship.to_user == user))
|
|
|
|
|
.execute())
|
|
|
|
|
flash('You are no longer following %s' % user.username)
|
2019-05-05 10:09:27 +02:00
|
|
|
unpush_notification('follow', user, user=cur_user.id)
|
2019-05-01 15:33:28 +02:00
|
|
|
return redirect(url_for('user_detail', username=user.username))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/create/', methods=['GET', 'POST'])
|
|
|
|
|
@login_required
|
|
|
|
|
def create():
|
|
|
|
|
user = get_current_user()
|
|
|
|
|
if request.method == 'POST' and request.form['text']:
|
2019-10-11 14:53:13 +02:00
|
|
|
text = request.form['text']
|
|
|
|
|
privacy = int(request.form.get('privacy', '0'))
|
2019-05-01 15:33:28 +02:00
|
|
|
message = Message.create(
|
|
|
|
|
user=user,
|
2019-10-11 14:53:13 +02:00
|
|
|
text=text,
|
2019-10-12 19:22:10 +02:00
|
|
|
pub_date=datetime.datetime.now(),
|
|
|
|
|
privacy=privacy)
|
2019-05-02 15:47:23 +02:00
|
|
|
file = request.files.get('file')
|
|
|
|
|
if file:
|
2019-05-02 19:50:20 +02:00
|
|
|
print('Uploading', file.filename)
|
|
|
|
|
ext = file.filename.split('.')[-1]
|
2019-05-02 15:47:23 +02:00
|
|
|
upload = Upload.create(
|
|
|
|
|
type=ext,
|
|
|
|
|
message=message
|
|
|
|
|
)
|
|
|
|
|
file.save(UPLOAD_DIRECTORY + str(upload.id) + '.' + ext)
|
2019-10-11 14:53:13 +02:00
|
|
|
# create mentions
|
|
|
|
|
mention_usernames = set()
|
|
|
|
|
for mo in re.finditer(_mention_re, text):
|
|
|
|
|
mention_usernames.add(mo.group(1))
|
|
|
|
|
# to avoid self mention
|
|
|
|
|
mention_usernames.difference_update({user.username})
|
|
|
|
|
for u in mention_usernames:
|
|
|
|
|
try:
|
|
|
|
|
mention_user = User.get(User.username == u)
|
|
|
|
|
if privacy in (MSGPRV_PUBLIC, MSGPRV_UNLISTED) or \
|
|
|
|
|
(privacy == MSGPRV_FRIENDS and
|
|
|
|
|
mention_user.is_following(user) and
|
|
|
|
|
user.is_following(mention_user)):
|
|
|
|
|
push_notification('mention', mention_user, user=user.id)
|
|
|
|
|
except User.DoesNotExist:
|
|
|
|
|
pass
|
2019-05-01 15:33:28 +02:00
|
|
|
flash('Your message has been posted successfully')
|
|
|
|
|
return redirect(url_for('user_detail', username=user.username))
|
|
|
|
|
return render_template('create.html')
|
|
|
|
|
|
2019-10-14 19:53:33 +02:00
|
|
|
@app.route('/edit/<int:id>', methods=['GET', 'POST'])
|
|
|
|
|
@login_required
|
|
|
|
|
def edit(id):
|
|
|
|
|
user = get_current_user()
|
|
|
|
|
message = get_object_or_404(Message, Message.id == id)
|
|
|
|
|
if message.user != user:
|
|
|
|
|
abort(404)
|
|
|
|
|
if request.method == 'POST' and (request.form['text'] != message.text or
|
|
|
|
|
request.form['privacy'] != message.privacy):
|
|
|
|
|
text = request.form['text']
|
|
|
|
|
privacy = int(request.form.get('privacy', '0'))
|
|
|
|
|
Message.update(
|
|
|
|
|
text=text,
|
|
|
|
|
privacy=privacy,
|
|
|
|
|
pub_date=datetime.datetime.now()
|
|
|
|
|
).where(Message.id == id).execute()
|
|
|
|
|
# edit uploads (skipped for now)
|
|
|
|
|
# create mentions
|
|
|
|
|
mention_usernames = set()
|
|
|
|
|
for mo in re.finditer(_mention_re, text):
|
|
|
|
|
mention_usernames.add(mo.group(1))
|
|
|
|
|
# to avoid self mention
|
|
|
|
|
mention_usernames.difference_update({user.username})
|
|
|
|
|
for u in mention_usernames:
|
|
|
|
|
try:
|
|
|
|
|
mention_user = User.get(User.username == u)
|
|
|
|
|
if privacy in (MSGPRV_PUBLIC, MSGPRV_UNLISTED) or \
|
|
|
|
|
(privacy == MSGPRV_FRIENDS and
|
|
|
|
|
mention_user.is_following(user) and
|
|
|
|
|
user.is_following(mention_user)):
|
|
|
|
|
push_notification('mention', mention_user, user=user.id)
|
|
|
|
|
except User.DoesNotExist:
|
|
|
|
|
pass
|
|
|
|
|
flash('Your message has been edited successfully')
|
|
|
|
|
return redirect(url_for('user_detail', username=user.username))
|
|
|
|
|
return render_template('edit.html', message=message)
|
|
|
|
|
|
|
|
|
|
#@app.route('/delete/<int:id>', methods=['GET', 'POST'])
|
|
|
|
|
#def confirm_delete(id):
|
|
|
|
|
# return render_template('confirm_delete.html')
|
|
|
|
|
|
2019-10-17 14:34:55 +02:00
|
|
|
@app.route('/edit_profile/', methods=['GET', 'POST'])
|
|
|
|
|
def edit_profile():
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
user = get_current_user()
|
|
|
|
|
username = request.form['username']
|
2019-10-17 15:21:33 +02:00
|
|
|
if not username:
|
|
|
|
|
# prevent username to be set to empty
|
|
|
|
|
username = user.username
|
2019-10-17 14:34:55 +02:00
|
|
|
if username != user.username:
|
|
|
|
|
User.update(username=username).where(User.id == user.id).execute()
|
2019-10-20 13:14:16 +02:00
|
|
|
website = request.form['website'].strip().replace(' ', '%20')
|
|
|
|
|
if website and not validate_website(website):
|
|
|
|
|
flash('You should enter a valid URL.')
|
|
|
|
|
return render_template('edit_profile.html')
|
|
|
|
|
location = int(request.form.get('location'))
|
|
|
|
|
if location == 0:
|
|
|
|
|
location = None
|
2019-10-17 15:21:33 +02:00
|
|
|
UserProfile.update(
|
|
|
|
|
full_name=request.form['full_name'] or username,
|
|
|
|
|
biography=request.form['biography'],
|
2019-10-20 13:14:16 +02:00
|
|
|
website=website,
|
|
|
|
|
location=location
|
2019-10-17 15:21:33 +02:00
|
|
|
).where(UserProfile.user == user).execute()
|
|
|
|
|
return redirect(url_for('user_detail', username=username))
|
2019-10-17 14:34:55 +02:00
|
|
|
return render_template('edit_profile.html')
|
|
|
|
|
|
2019-05-05 10:09:27 +02:00
|
|
|
@app.route('/notifications/')
|
|
|
|
|
@login_required
|
|
|
|
|
def notifications():
|
|
|
|
|
user = get_current_user()
|
|
|
|
|
notifications = (Notification
|
|
|
|
|
.select()
|
|
|
|
|
.where(Notification.target == user)
|
|
|
|
|
.order_by(Notification.pub_date.desc()))
|
2019-05-22 18:25:08 +02:00
|
|
|
|
|
|
|
|
with database.atomic():
|
|
|
|
|
(Notification
|
|
|
|
|
.update(seen=1)
|
|
|
|
|
.where((Notification.target == user) & (Notification.seen == 0))
|
|
|
|
|
.execute())
|
2019-05-05 10:09:27 +02:00
|
|
|
return object_list('notifications.html', notifications, 'notification_list', json=json, User=User)
|
|
|
|
|
|
2019-10-09 19:51:18 +02:00
|
|
|
@app.route('/about/')
|
|
|
|
|
def about():
|
|
|
|
|
return render_template('about.html', version=__version__)
|
|
|
|
|
|
2019-10-11 17:20:24 +02:00
|
|
|
# The two following routes are mandatory by law.
|
|
|
|
|
@app.route('/terms/')
|
|
|
|
|
def terms():
|
|
|
|
|
return render_template('terms.html')
|
|
|
|
|
|
|
|
|
|
@app.route('/privacy/')
|
|
|
|
|
def privacy():
|
|
|
|
|
return render_template('privacy.html')
|
|
|
|
|
|
|
|
|
|
@app.route('/robots.txt')
|
|
|
|
|
def robots_txt():
|
|
|
|
|
return send_from_directory(os.getcwd(), 'robots.txt')
|
|
|
|
|
|
2019-10-14 21:06:53 +02:00
|
|
|
@app.route('/uploads/<id>.<type>')
|
2019-05-02 15:47:23 +02:00
|
|
|
def uploads(id, type='jpg'):
|
|
|
|
|
return send_from_directory(UPLOAD_DIRECTORY, id + '.' + type)
|
|
|
|
|
|
2019-05-01 15:33:28 +02:00
|
|
|
@app.route('/ajax/username_availability/<username>')
|
|
|
|
|
def username_availability(username):
|
2019-10-12 19:22:10 +02:00
|
|
|
current = get_current_user()
|
|
|
|
|
if current:
|
|
|
|
|
current = current.username
|
2019-05-01 15:33:28 +02:00
|
|
|
else:
|
|
|
|
|
current = None
|
|
|
|
|
is_valid = is_username(username)
|
|
|
|
|
if is_valid:
|
|
|
|
|
try:
|
|
|
|
|
user = User.get(User.username == username)
|
|
|
|
|
is_available = current == user.username
|
|
|
|
|
except User.DoesNotExist:
|
|
|
|
|
is_available = True
|
|
|
|
|
else:
|
|
|
|
|
is_available = False
|
|
|
|
|
return jsonify({'is_valid':is_valid, 'is_available':is_available, 'status':'ok'})
|
|
|
|
|
|
2019-10-20 13:14:16 +02:00
|
|
|
@app.route('/ajax/location_search/<name>')
|
|
|
|
|
def location_search(name):
|
|
|
|
|
results = []
|
|
|
|
|
for key, value in locations.items():
|
|
|
|
|
if value.startswith(name):
|
|
|
|
|
results.append({'value': key, 'display': value})
|
|
|
|
|
return jsonify({'results': results})
|
|
|
|
|
|
2019-10-14 14:30:22 +02:00
|
|
|
_enrich_symbols = [
|
|
|
|
|
(r'\n', 'NEWLINE'),
|
|
|
|
|
(r'https?://(?:[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*|\[[A-Fa-f0-9:]+\])'
|
2019-10-15 16:32:36 +02:00
|
|
|
r'(?::\d+)?(?:/[^\s]*)?(?:\?[^\s]*)?(?:#[^\s]*)?', 'URL'),
|
2019-10-14 14:30:22 +02:00
|
|
|
(_mention_re, 'MENTION'),
|
2019-10-15 14:13:55 +02:00
|
|
|
(r'[^h\n+]+', 'TEXT'),
|
2019-10-14 14:30:22 +02:00
|
|
|
(r'.', 'TEXT')
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
def _tokenize(characters, table):
|
|
|
|
|
pos = 0
|
|
|
|
|
tokens = []
|
|
|
|
|
while pos < len(characters):
|
|
|
|
|
mo = None
|
|
|
|
|
for pattern, tag in table:
|
|
|
|
|
mo = re.compile(pattern).match(characters, pos)
|
|
|
|
|
if mo:
|
|
|
|
|
if tag:
|
|
|
|
|
text = mo.group(0)
|
|
|
|
|
tokens.append((text, tag))
|
|
|
|
|
break
|
|
|
|
|
pos = mo.end(0)
|
|
|
|
|
return tokens
|
|
|
|
|
|
2019-05-01 15:33:28 +02:00
|
|
|
@app.template_filter()
|
|
|
|
|
def enrich(s):
|
2019-10-14 14:30:22 +02:00
|
|
|
tokens = _tokenize(s, _enrich_symbols)
|
|
|
|
|
r = []
|
|
|
|
|
for text, tag in tokens:
|
|
|
|
|
if tag == 'TEXT':
|
|
|
|
|
r.append(html.escape(text))
|
|
|
|
|
elif tag == 'URL':
|
|
|
|
|
r.append('<a href="{0}">{0}</a>'.format(html.escape(text)))
|
|
|
|
|
elif tag == 'MENTION':
|
|
|
|
|
r.append('<span class="weak">+</span><a href="/{0}">{1}</a>'.format(text, text.lstrip('+')))
|
|
|
|
|
elif tag == 'NEWLINE':
|
|
|
|
|
r.append('<br>')
|
|
|
|
|
return Markup(''.join(r))
|
2019-05-01 15:33:28 +02:00
|
|
|
|
|
|
|
|
@app.template_filter('is_following')
|
|
|
|
|
def is_following(from_user, to_user):
|
|
|
|
|
return from_user.is_following(to_user)
|
|
|
|
|
|
2019-10-20 13:14:16 +02:00
|
|
|
@app.template_filter('locationdata')
|
|
|
|
|
def locationdata(key):
|
|
|
|
|
if key > 0:
|
|
|
|
|
return locations[str(key)]
|
2019-05-01 15:33:28 +02:00
|
|
|
|
|
|
|
|
# allow running from the command line
|
|
|
|
|
if __name__ == '__main__':
|
2019-10-12 19:22:10 +02:00
|
|
|
args = arg_parser.parse_args()
|
2019-05-01 15:33:28 +02:00
|
|
|
create_tables()
|
2019-10-12 19:22:10 +02:00
|
|
|
if not args.norun:
|
|
|
|
|
app.run(port=args.port)
|