Adding message edit support for API

This commit is contained in:
Yusur 2019-11-18 19:19:06 +01:00
parent 6c128d0567
commit 621d8cf2c8
4 changed files with 48 additions and 36 deletions

View file

@ -9,7 +9,7 @@
* 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 `relationships_follow`, `relationships_unfollow`, `username_availability`, `edit_profile`, `request_edit` and `confirm_edit` endpoints to API.
* Added `url` utility to model `Upload`.
* Changed default `robots.txt`, adding report and admin-related lines.

View file

@ -4,7 +4,8 @@ from functools import wraps
from peewee import IntegrityError
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
from .utils import check_access_token, Visibility, push_notification, unpush_notification, \
create_mentions, is_username
bp = Blueprint('api', __name__, url_prefix='/api/V1')
@ -86,23 +87,8 @@ def create(self):
text=text,
pub_date=datetime.datetime.now(),
privacy=privacy)
# Currently, API does not support files.
# 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
# This API does not support files. Use create2 instead.
create_mentions(self, text)
return {}
@bp.route('/create2', methods=['POST'])
@ -124,22 +110,7 @@ def create2(self):
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
create_mentions(self, text)
return {}
def get_relationship_info(self, other):
@ -306,3 +277,23 @@ def edit_profile(user):
telegram=data['telegram']
).where(UserProfile.user == user).execute()
return {}
@bp.route('/request_edit/<int:id>')
@validate_access
def request_edit(self, id):
message = Message[id]
if message.user != self:
raise ValueError('Attempt to edit a message from another')
return {
'message_info': get_message_info(message)
}
@bp.route('/save_edit/<int:id>', methods=['POST'])
@validate_access
def save_edit(self, id):
message = Message[id]
if message.user != self:
raise ValueError('Attempt to edit a message from another')
data = request.get_json(True)
Message.update(text=data['text'], privacy=data['privacy']).where(Message.id == id).execute()
return {}

View file

@ -212,6 +212,8 @@ REPORT_REASON_SELFINJURY = 7
REPORT_REASON_FIREARMS = 8
REPORT_REASON_DRUGS = 9
REPORT_REASON_UNDERAGE = 10
REPORT_REASON_LEAK = 11
REPORT_REASON_DMCA = 12
report_reasons = [
(REPORT_REASON_SPAM, "It's spam"),

View file

@ -3,7 +3,8 @@ A list of utilities used across modules.
'''
import datetime, re, base64, hashlib, string, sys, json
from .models import User, Notification
from .models import User, Message, Notification, MSGPRV_PUBLIC, MSGPRV_UNLISTED, \
MSGPRV_FRIENDS, MSGPRV_ONLYME
from flask import abort, render_template, request, session
_forbidden_extensions = 'com net org txt'.split()
@ -196,3 +197,21 @@ def check_access_token(token):
h.update(str(user.password).encode('utf-8'))
if h.hexdigest()[:32] == hh:
return user
def create_mentions(cur_user, text):
# 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({cur_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(cur_user) and
cur_user.is_following(mention_user)):
push_notification('mention', mention_user, user=user.id)
except User.DoesNotExist:
pass