Skip to content
This repository has been archived by the owner on Nov 26, 2022. It is now read-only.

add import_statuses endpoint #462

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion routes/api.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from app import app, db
from libforget.auth import require_auth_api, get_viewer
from flask import jsonify, redirect, make_response, request, Response
from model import Account
from model import Account, Post
import libforget.settings
import libforget.json
import random
from werkzeug.exceptions import BadRequest

@app.route('/api/health_check')
def health_check():
Expand Down Expand Up @@ -79,3 +80,43 @@ def known_instances():
resp = Response('', 204)
resp.set_cookie('forget_known_instances', '', max_age=0)
return resp

class MalformedStatusList(werkzeug.exceptions.BadRequest):
pass

@app.route('/api/import_statuses', method=('POST',))
@require_auth_api
def import_statuses():
"""
accepts json in the form

[ {id, favourite, has_media, direct, is_reblog}, ... ]
"""
statuses = request.json
viewer = get_viewer()
if not isinstance(statuses, list):
raise MalformedStatusList()

expected_keys = ('id', 'favourite', 'has_media', 'direct', 'is_reblog')
boolean_keys = ('favourite', 'has_media', 'direct', 'is_reblog')

with db.session.no_autoflush:
for post in statuses:
if not isinstance(post, dict) or set(post.keys()) != expected_keys:
raise MalformedStatusList()

for key in boolean_keys:
post[key] = post[key] == 'true'

post['author_id'] = viewer.id

if viewer.service == 'twitter':
post['id'] = "twitter:{}".format(post['id'])
elif viewer.service == 'mastodon':
post['id'] = "mastodon:{}:{}".format(
viewer.mastdon_instance,
post['id'])

db.session.merge(Post(**post))

db.session.commit()