Skip to content
This repository has been archived by the owner on Aug 24, 2024. It is now read-only.

Add initial micropub content and tag update support #8

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
17 changes: 16 additions & 1 deletion app/boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ async def send_vote(
raise ValueError("Object has no context")
context = in_reply_to_object.ap_context

# TODO: ensure the name are valid?
# ensure the name are valid

# Save the answers
in_reply_to_object.voted_for_answers = names
Expand Down Expand Up @@ -819,7 +819,9 @@ async def send_vote(
for rcp in recipients:
await new_outgoing_activity(db_session, rcp, outbox_object.id)

# commit db session
await db_session.commit()

return vote_id


Expand Down Expand Up @@ -871,6 +873,19 @@ async def send_update(
outbox_object.source = source
outbox_object.revisions = revisions

if outbox_object.tags != tags:
# remove all the existing tags
for tag in outbox_object.tags or []:
db_session.delete(tag)

for tag in tags:
if tag["type"] == "Hashtag":
tagged_object = models.TaggedOutboxObject(
tag=tag["name"][1:].lower(),
outbox_object_id=outbox_object.id,
)
db_session.add(tagged_object)

recipients = await _compute_recipients(db_session, note)
for rcp in recipients:
await new_outgoing_activity(db_session, rcp, outbox_object.id)
Expand Down
21 changes: 16 additions & 5 deletions app/micropub.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from app.boxes import get_outbox_object_by_ap_id
from app.boxes import send_create
from app.boxes import send_delete
from app.boxes import send_update
from app.database import AsyncSession
from app.database import get_db_session
from app.indieauth import AccessTokenInfo
Expand Down Expand Up @@ -83,6 +84,7 @@ async def post_micropub_endpoint(

if "action" in form_data:
if form_data["action"] in ["delete", "update"]:
url = form_data["url"]
outbox_object = await get_outbox_object_by_ap_id(
db_session, str(form_data["url"])
)
Expand All @@ -106,11 +108,20 @@ async def post_micropub_endpoint(
if "update" not in access_token_info.scopes:
return insufficient_scope_resp

# TODO(ts): support update
# "replace": {"content": ["new content"]}

logger.info(f"Updating object {outbox_object.ap_id}: {form_data}")
return JSONResponse(content={}, status_code=200)
# TODO(1d): support update properly. Currently only supposed "replace":{"content":<new content>}

if "replace" in form_data:
logger.info(f"Updating object {outbox_object.ap_id}: {form_data}")
await send_update(db_session,outbox_object.ap_id,form_data["replace"]["content"])
return JSONResponse(content={}, status_code=200)
else:
return JSONResponse(
content={
"error": "invalid_request",
"error_description": "Update only supports replace.content.",
},
status_code=400,
)
else:
raise ValueError("Should never happen")
else:
Expand Down