From 81e2fca2aac08638b4198a67b33013499da92507 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 21 Feb 2019 19:26:24 -0500 Subject: [PATCH] Try to handle Deletes (not currently working) --- lib/activitypub/activity.ts | 4 ++++ lib/activitypub/conversation.ts | 1 + lib/activitypub/inbox.ts | 17 ++++++++++++++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/activitypub/activity.ts b/lib/activitypub/activity.ts index a118ef5..dcfb31f 100644 --- a/lib/activitypub/activity.ts +++ b/lib/activitypub/activity.ts @@ -35,6 +35,10 @@ export interface Note extends Activity { conversation: string; } +export interface Delete extends Activity { + object: string; +} + export interface Actor { id: string; inbox: string; diff --git a/lib/activitypub/conversation.ts b/lib/activitypub/conversation.ts index e14cdc7..349a8de 100644 --- a/lib/activitypub/conversation.ts +++ b/lib/activitypub/conversation.ts @@ -14,6 +14,7 @@ export default function conversation(router: Router) { } else { const notes = rows.map(row => { return { + id: row.id, attributedTo: row.attributed_to, content: row.content, published: row.published, diff --git a/lib/activitypub/inbox.ts b/lib/activitypub/inbox.ts index d1ab0fb..2921a17 100644 --- a/lib/activitypub/inbox.ts +++ b/lib/activitypub/inbox.ts @@ -1,7 +1,7 @@ import { Router, Request, Response } from "express"; import uuidv4 from "uuid/v4"; import { fetchActor, signAndSend } from "./federate"; -import { Activity, Follow, Accept, Undo, Create, Note } from "./activity"; +import { Activity, Follow, Accept, Undo, Create, Note, Delete } from "./activity"; import { Database } from "sqlite3"; import { URL } from "url"; import sanitizeHtml from "sanitize-html"; @@ -22,6 +22,8 @@ async function handleInbox(req: Request, res: Response) { handleCreate(activity, req, res); } else if (activity.type === "Undo") { handleUndo(activity, req, res); + } else if (activity.type === "Delete") { + handleDelete(activity, req, res); } else { res.end(); // TODO: handle this better } @@ -95,9 +97,22 @@ async function handleCreateNote(create: Create, req: Request, res: Response) { $published: note.published }, (err) => { if (err) console.error(`Encountered error storing reply ${note.id}`, err); + res.end(); }); } +async function handleDelete(activity: Activity, req: Request, res: Response) { + const deleteActivity = activity as Delete; + const db = req.app.get("db") as Database; + db.run("DELETE FROM notes WHERE id = $id, actor = $actor", { + $id: deleteActivity.object, + $actor: deleteActivity.actor + }, (err) => { + if (err) console.error(`Encountered error deleting ${deleteActivity.object}`, err); + res.end(); + }) +} + async function handleUndo(activity: Activity, req: Request, res: Response) { const undo = activity as Undo; if (undo.object.type === "Follow") {