Commit 40b53405 authored by -'s avatar -

geopub.cpub: login! function

parent e80c3ea9
......@@ -4,7 +4,8 @@
cljs-http {:mvn/version "0.1.46"}
metosin/reitit-core {:mvn/version "0.4.2"}
metosin/reitit-frontend {:mvn/version "0.4.2"}
org.clojure/core.logic {:mvn/version "1.0.0"}}
org.clojure/core.logic {:mvn/version "1.0.0"}
org.clojars.akiel/async-error {:mvn/version "0.3"}}
......@@ -61,21 +61,10 @@
;; get public timeline
(geopub.state/add-rdf-graph! state
(cpub/get-public-timeline server-url))
;; get actor profile
(geopub.state/add-rdf-graph! state
(get-rdf actor-id
{:auth auth
:with-credentials? false}))
;; get actor inbox TODO: figure out outbox from actor object
(geopub.state/add-rdf-graph! state (get-rdf (str actor-id "/inbox")
{:basic-auth auth
:with-credentials? false
;; get actor outbox
(geopub.state/add-rdf-graph! state (get-rdf (str actor-id "/outbox")
{:basic-auth auth
:with-credentials? false})))
;; Login
(geopub.cpub/login! state
{:username "alice" :password "123"}))
;; ==================== UI =======================
......@@ -149,5 +138,3 @@
(geopub.state/add-rdf-graph! state (get-rdf ""
{:with-credentials? false}))
......@@ -17,11 +17,15 @@
(ns geopub.cpub
"Helpers for interacting with CPub server"
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [cljs.core.async :refer [<!]]
(:require-macros [cljs.core.async.macros :refer [go]]
[async-error.core :refer [<? go-try]])
(:require [cljs.core.async :as async :refer [<!]]
[rdf.core :as rdf]
[ :refer [get-rdf]]
[ :as activity]
[geopub.ns :refer [ldp as]]
[goog.Uri :as uri]))
(defn public-timeline-url [server-url]
......@@ -35,8 +39,31 @@
(get-rdf {:with-credentials? false})))
(defn login! [state id basic-auth]
(let [id (rdf/iri id)
profile (rdf/description
id (<? (get-rdf id {:basic-auth basic-auth})))
inbox (<? (get-rdf
(rdf/description-get-first profile (ldp "inbox"))
{:basic-auth basic-auth}))
outbox (<? (get-rdf
(rdf/description-get-first profile (as "outbox"))
{:basic-auth basic-auth}))]
(defn like! [what]
(swap! state
#(assoc % :account
{:basic-auth basic-auth
:profile (rdf/description-subject profile)
:outbox (rdf/description-get-first profile (as "outbox"))})
(geopub.state/merge-graphs (rdf/description-graph profile)
(defn like! [state what]
(print (get-in @state [:account :outbox]))
(-> (activity/like what)
( "http://localhost:4000/users/alice/outbox"
{:basic-auth {:username "alice" :password "123"}})))
(get-in @state [:account :outbox])
{:basic-auth (get-in @state [:account :basic-auth])})))
......@@ -63,7 +63,7 @@
{"Accept" (clojure.string/join
", " (rdf-parse/content-types))}
:method :get
:url url}
:url (if (rdf/iri? url) (rdf/iri-value url) url)}
;; cljs-http does too much post-processing (such as parsing json)
request (wrap-request cljs-http.core/request)
......@@ -98,9 +98,10 @@
(defn post-rdf [data url & [opts]]
(let [body (<! (n3/encode data))]
(http/post url (merge
{:headers {"Content-type" "text/turtle"}
:body body} opts)))))
(http/post (if (rdf/iri? url) (rdf/iri-value url) url)
{:headers {"Content-type" "text/turtle"}
:body body} opts)))))
;; Reagent components
......@@ -22,6 +22,7 @@
;; Commonly used namespaces
(defns as "")
(defns ldp "")
(defns rdfs "")
(defns schema "")
(defns geo "")
......@@ -10,6 +10,15 @@
{:graph (}))
(defn merge-graphs
"Returns a function that will merge in graphs into state graph. Useful when using swap! on the state."
[& graphs]
(fn [state]
(assoc state :graph
(reduce rdf/graph-merge
(or (:graph state) (
(defn add-rdf-graph!
"Takes RDF graph from a channel and merge with stategraph."
[state chan]
......@@ -21,9 +30,7 @@
(if (rdf/graph? input)
;; swap state graph with graph merged with input graph
(swap! state (fn [state]
(assoc state :graph
(rdf/graph-merge (:graph state) input))))
(swap! state (merge-graphs input))
;; else return whatever input is (probably an error)
......@@ -59,7 +59,7 @@
#(geopub.cpub/like! (get-iri state))} "Like"]
#(geopub.cpub/like! state (get-iri state))} "Like"]
......@@ -204,7 +204,12 @@
(defn description
"Make a new description"
[subject graph]
(->Description subject graph))
(if (and (iri? subject) (graph? graph))
(->Description subject graph)
(defn description? [description]
(instance? Description description))
(defn description-subject
"Get the subject of a description"
......@@ -229,3 +234,15 @@
(:graph description)
(triple (:subject description) predicate (l/lvar)))))
(defn description-get-first
"Get first object for given predicate. Warning: If there are multiple objects the first is note properly defined."
[description predicate]
(first (description-get description predicate)))
(defn description-map-graph
"Returns a description with f applied on the graph. f must return a graph. If not the output of f will be returned."
[f description]
(let [mapped-graph (f (description-graph description))]
(if (graph? mapped-graph)
(description (description-subject description) mapped-graph)
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment