From cc641bfe7f53c64ce0e59aa3ecf63de84575caf3 Mon Sep 17 00:00:00 2001 From: erik Date: Thu, 22 Jun 2023 15:44:55 +0200 Subject: [PATCH] Implement simple specs and like map generation --- .../activity_pub_poc/core.clj | 70 +++++++++++++++++-- 1 file changed, 63 insertions(+), 7 deletions(-) diff --git a/src/main/clj/org/domaindrivenarchitecture/activity_pub_poc/core.clj b/src/main/clj/org/domaindrivenarchitecture/activity_pub_poc/core.clj index cb4bf74..01a4b23 100644 --- a/src/main/clj/org/domaindrivenarchitecture/activity_pub_poc/core.clj +++ b/src/main/clj/org/domaindrivenarchitecture/activity_pub_poc/core.clj @@ -4,11 +4,13 @@ [quoll.raphael.core :refer [parse]] [org.domaindrivenarchitecture.activity-pub-poc.common :as cm] [clojure.spec.alpha :as s] + [orchestra.core :refer [defn-spec]] [clojure.inspector :as ins] [hato.client :as hato] [clojure.string :as st] [clojure.walk :as walk] - [clojure.string :as str])) + [clojure.string :as str] + [lambdaisland.uri :as uri])) (def team-url "https://social.meissa-gmbh.de/users/team") (def like-url "https://www.w3.org/ns/activitystreams#Like") @@ -26,17 +28,71 @@ (defn type-string? [input] (re-matches #"[A-Z]" (str (first input)))) +(defn uri-string? [input] + (re-matches #"https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)" input)) + (defn uri-vector? [input] (and (every? #(uri? %) input) (vector? input))) +(def context "@context") +(def type-string "type") +(def id-uri "id") +(def to-uri "to") +(def actor "actor") +(def object "object") + ; Specs for a like -(s/def ::context uri?) -(s/def ::type type-string?) -(s/def ::id uri?) -(s/def ::to uri-vector?) -(s/def ::actor uri?) -(s/def ::object uri?) +(s/def context uri-string?) +(s/def type-string type-string?) +(s/def id-uri uri-string?) +(s/def to-uri uri-vector?) +(s/def actor uri-string?) +(s/def object uri-string?) +(s/def ::to uri-string?) +(s/def ::target string?) +(s/def ::from uri-string?) + +(def like? (s/keys :req-un [context + type-string + id-uri + to-uri + actor + object])) + +(def like-data? (s/keys :req-un [::to ::target ::from])) + +(def activity-streams-context "https://www.w3.org/ns/activitystreams") + +(def like-data + {:to "https://chatty.bla/ben/" + :target "posts/234s23-2g34234-2hhj536" + :from "https://social.bla/alyssa/"}) + +(defn generate-id-from-object [from] + (str from "out" "/" "sdfsd44243g324g3455")) + +(defn-spec generate-like-map like? + [input-map like-data?] + (assoc {} + "@context" activity-streams-context + "type" "Like" + "id" (generate-id-from-object (:from input-map)) + "to" (:to input-map) + "actor" (:from input-map) + "object" (:target input-map))) + + + + + + + + + + + + ; ToDo ; Prädikat das langString parsen/validieren kann, evtl auch für xsd:string