gourami

[UNMAINTAINED] Activitypub server in Rust
Log | Files | Refs | README | LICENSE

commit 243783153f6d6953e919f0b07df7e2ae1ed01531
parent 2b01812000d12cacf755222c465e61bf1ffcf16e
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Wed, 22 Apr 2020 00:24:22 -0500

make some activitypub progress

Diffstat:
MTODO | 6+++++-
Msrc/ap.rs | 96+++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------
2 files changed, 72 insertions(+), 30 deletions(-)

diff --git a/TODO b/TODO @@ -1,6 +1,10 @@ +A nice big new feature would be event planning + sanitize on write to db -send notes as cleartext and html? +parse markdown? + +send notes as cleartext and html? cleanup html parsing. understand fn vs async fn in tokio diff --git a/src/ap.rs b/src/ap.rs @@ -1,12 +1,21 @@ use log::{debug, info}; use serde_json::{Result, Value}; use std::error::Error; -use activitystreams::activity::{Create, Activity}; +use activitystreams::activity::{Create, Accept, Follow, Reject, Announce, Delete, Activity}; -fn parse_unstructured_ap(message: &str) -> impl Activity { - // try and serialize in a few different ways - let object: Create = serde_json::from_str(message).unwrap(); - object +// gonna be big +fn process_unstructured_ap(message: &str) { + // Actions usually associated with notes + use serde_json::from_str; + // maybe there's a cleaner way to do this. cant iterate over types + // TODO inbox forwarding https://www.w3.org/TR/activitypub/#inbox-forwarding + if let Some(create) = from_str::<Create>(message).ok() { + // create note database object + } + else if let Some(delete) = from_str::<Delete>(message).ok() { + // delete note database object + } + debug!("Unrecognized or invalid activity"); } #[cfg(test)] @@ -14,33 +23,62 @@ mod tests { use super::*; #[test] fn test_empty_string() { - parse_unstructured_ap("1"); + parse_unstructured_ap("{}"); } -} + #[test] + fn test_mastodon_create_status_example() { + let mastodon_create_note_json_string = r#"{ + "id": "https://mastodon.social/users/alexwennerberg/statuses/104028309437021899/activity", + "type": "Create", + "actor": "https://mastodon.social/users/alexwennerberg", + "published": "2020-04-20T01:27:10Z", + "to": [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "cc": [ + "https://mastodon.social/users/alexwennerberg/followers" + ], + "object": { + "id": "https://mastodon.social/users/alexwennerberg/statuses/104028309437021899", + "type": "Note", + "summary": null, + "inReplyTo": null, + "published": "2020-04-20T01:27:10Z", + "url": "https://mastodon.social/@alexwennerberg/104028309437021899", + "attributedTo": "https://mastodon.social/users/alexwennerberg", + "to": [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "cc": [ + "https://mastodon.social/users/alexwennerberg/followers" + ], + "sensitive": false, + "atomUri": "https://mastodon.social/users/alexwennerberg/statuses/104028309437021899", + "inReplyToAtomUri": null, + "conversation": "tag:mastodon.social,2020-04-20:objectId=167583625:objectType=Conversation", + "content": "<p>&lt;a href=&quot;<a href=\"https://google.com\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"><span class=\"invisible\">https://</span><span class=\"\">google.com</span><span class=\"invisible\"></span></a>&quot;&gt;hi&lt;/a&gt;</p>", + "contentMap": { + "en": "<p>&lt;a href=&quot;<a href=\"https://google.com\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"><span class=\"invisible\">https://</span><span class=\"\">google.com</span><span class=\"invisible\"></span></a>&quot;&gt;hi&lt;/a&gt;</p>" + }, + "attachment": [], + "tag": [], + "replies": { + "id": "https://mastodon.social/users/alexwennerberg/statuses/104028309437021899/replies", + "type": "Collection", + "first": { + "type": "CollectionPage", + "next": "https://mastodon.social/users/alexwennerberg/statuses/104028309437021899/replies?only_other_accounts=true&page=true", + "partOf": "https://mastodon.social/users/alexwennerberg/statuses/104028309437021899/replies", + "items": [] + } + } + } + }"#; + parse_unstructured_ap(mastodon_create_note_json_string); + } + } - // Err("Message not an activity"); -// let object = message.get("object")?.get("type")?; -// profiles -// follow -// accept / reject - -// statuses "Notes" -// match message.get("type").lower() { -// "create" => Some(1), -// "delete" => Some(1), -// Announce? -// _ => None -// }; -// main type: Note -// simple https://docs.joinmastodon.org/spec/activitypub/ -// support types: -// article -// page -// event -// -// "get" -> list all jsons. -// match these to notifications pub fn post_user_inbox(user_name: String, message: Value) { }