Adding mention notifications

This commit is contained in:
Yusur 2019-10-11 14:53:13 +02:00
parent 510bf923b0
commit bbbc7655a0
3 changed files with 30 additions and 5 deletions

View file

@ -3,10 +3,13 @@
## 0.4 ## 0.4
* Adding quick mention. You can now create a message mentioning another user in one click. * Adding quick mention. You can now create a message mentioning another user in one click.
* Added mention notifications.
* Adding an about section, footer, version number and license. * Adding an about section, footer, version number and license.
* Improved repository with better README, CHANGELOG, requirements.txt and option to specify port on run_example.py * Improved repository with better README, CHANGELOG, requirements.txt and option to specify port on run_example.py
* Split app config from app module. * Split app config from app module.
* Added the capability to specify post privacy. Now you can choose to post your message to the public, to friends (mutual followers) or only you. * Added the capability to specify post privacy. Now you can choose to post your message to the public, to friends (mutual followers) or only you.
* Added the capability to log in specifying email instead of username.
* Added the precise date of a message as a tooltip when hovering over the human-readable date.
* Now Python 3 is enforced. * Now Python 3 is enforced.
## 0.3 ## 0.3

30
app.py
View file

@ -177,6 +177,8 @@ def is_username(username):
return False return False
return all(x and set(x) < _username_characters for x in username_splitted) return all(x and set(x) < _username_characters for x in username_splitted)
_mention_re = r'\+([A-Za-z0-9_]+(?:\.[A-Za-z0-9_]+)*)'
def validate_birthday(date): def validate_birthday(date):
today = datetime.date.today() today = datetime.date.today()
if today.year - date.year > 13: if today.year - date.year > 13:
@ -322,6 +324,8 @@ def object_list(template_name, qr, var_name='object_list', **kwargs):
kwargs[var_name] = qr.paginate(kwargs['page']) kwargs[var_name] = qr.paginate(kwargs['page'])
return render_template(template_name, **kwargs) return render_template(template_name, **kwargs)
### WEB ###
@app.before_request @app.before_request
def before_request(): def before_request():
g.db = database g.db = database
@ -431,7 +435,7 @@ def user_detail(username):
# get all the users messages ordered newest-first -- note how we're accessing # get all the users messages ordered newest-first -- note how we're accessing
# the messages -- user.message_set. could also have written it as: # the messages -- user.message_set. could also have written it as:
# Message.select().where(Message.user == user) # Message.select().where(Message.user == user)
messages = Visibility(user.messages.order_by(Message.pub_date.desc()), True) messages = Visibility(user.messages.order_by(Message.pub_date.desc()))
return object_list('user_detail.html', messages, 'message_list', user=user) return object_list('user_detail.html', messages, 'message_list', user=user)
@app.route('/+<username>/follow/', methods=['POST']) @app.route('/+<username>/follow/', methods=['POST'])
@ -474,14 +478,16 @@ def user_unfollow(username):
def create(): def create():
user = get_current_user() user = get_current_user()
if request.method == 'POST' and request.form['text']: if request.method == 'POST' and request.form['text']:
text = request.form['text']
privacy = int(request.form.get('privacy', '0'))
message = Message.create( message = Message.create(
type='text', type='text',
user=user, user=user,
text=request.form['text'], text=text,
pub_date=datetime.datetime.now()) pub_date=datetime.datetime.now())
MessagePrivacy.create( MessagePrivacy.create(
message=message, message=message,
value=request.form.get('privacy', '0') value=privacy
) )
file = request.files.get('file') file = request.files.get('file')
if file: if file:
@ -492,6 +498,22 @@ def create():
message=message message=message
) )
file.save(UPLOAD_DIRECTORY + str(upload.id) + '.' + ext) file.save(UPLOAD_DIRECTORY + str(upload.id) + '.' + ext)
# 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 posted successfully') flash('Your message has been posted successfully')
return redirect(url_for('user_detail', username=user.username)) return redirect(url_for('user_detail', username=user.username))
return render_template('create.html') return render_template('create.html')
@ -540,7 +562,7 @@ def username_availability(username):
@app.template_filter() @app.template_filter()
def enrich(s): def enrich(s):
'''Filter for mentioning users.''' '''Filter for mentioning users.'''
return Markup(re.sub(r'\+([A-Za-z0-9_]+)', r'<a href="/+\1">\1</a>', s)) return Markup(re.sub(_mention_re, r'<a href="/+\1">\1</a>', s))
@app.template_filter('is_following') @app.template_filter('is_following')
def is_following(from_user, to_user): def is_following(from_user, to_user):

View file

@ -13,5 +13,5 @@
{% elif message.privacy == 3 %} Only me {% elif message.privacy == 3 %} Only me
{% endif %} {% endif %}
- -
<time datetime="{{ message.pub_date.isoformat() }}">{{ message.pub_date | human_date }}</time> <time datetime="{{ message.pub_date.isoformat() }}" title="{{ message.pub_date.ctime() }}">{{ message.pub_date | human_date }}</time>
</p> </p>