From af299a53c7aafa4ac676128d86dc5972ae05948f Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Sat, 9 Nov 2019 15:00:06 +0100 Subject: [PATCH] Added create2 endpoint --- CHANGELOG.md | 1 + app/api.py | 44 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3828e7c..d8c472c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Schema changes: moved `full_name` field from table `userprofile` to table `user` for search improvement reasons. * Adding `messages_count`, `followers_count` and `following_count` to `profile_info` API endpoint (what I've done to 0.7.1 too). +* Adding `create2` API endpoint that accepts media, due to an issue with the `create` endpoint that would make it incompatible. * Adding media URLs to messages in API. * Added `relationships_follow`, `relationships_unfollow`, `username_availability` and `edit_profile` endpoints to API. * Added `url` utility to model `Upload`. diff --git a/app/api.py b/app/api.py index 95b7c42..a11b5eb 100644 --- a/app/api.py +++ b/app/api.py @@ -1,9 +1,9 @@ from flask import Blueprint, jsonify, request -import sys, datetime, re +import sys, os, datetime, re from functools import wraps from peewee import IntegrityError -from .models import User, Message, Relationship, database, \ - MSGPRV_PUBLIC, MSGPRV_UNLISTED, MSGPRV_FRIENDS, MSGPRV_ONLYME +from .models import User, Message, Upload, Relationship, database, \ + MSGPRV_PUBLIC, MSGPRV_UNLISTED, MSGPRV_FRIENDS, MSGPRV_ONLYME, UPLOAD_DIRECTORY from .utils import check_access_token, Visibility, push_notification, unpush_notification bp = Blueprint('api', __name__, url_prefix='/api/V1') @@ -103,6 +103,44 @@ def create(self): push_notification('mention', mention_user, user=user.id) except User.DoesNotExist: pass + return {} + +@bp.route('/create2', methods=['POST']) +@validate_access +def create2(self): + text = request.form['text'] + privacy = int(request.form.get('privacy', 0)) + message = Message.create( + user=self, + text=text, + pub_date=datetime.datetime.now(), + privacy=privacy) + file = request.files.get('file') + if file: + print('Uploading', file.filename) + ext = file.filename.split('.')[-1] + upload = Upload.create( + type=ext, + message=message + ) + file.save(os.path.join(UPLOAD_DIRECTORY, str(upload.id) + '.' + ext)) + # create mentions + mention_usernames = set() + for mo in re.finditer(r'\+([A-Za-z0-9_]+(?:\.[A-Za-z0-9_]+)*)', text): + mention_usernames.add(mo.group(1)) + # to avoid self mention + mention_usernames.difference_update({self.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(self) and + self.is_following(mention_user)): + push_notification('mention', mention_user, user=user.id) + except User.DoesNotExist: + pass + return {} def get_relationship_info(self, other): if self == other: