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"}}
:aliases
{:shadow-cljs
......
......@@ -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
"http://localhost:4000/users/alice"
{:username "alice" :password "123"}))
;; ==================== UI =======================
......@@ -149,5 +138,3 @@
(geopub.state/add-rdf-graph! state (get-rdf "https://literatur.social/users/buechergefahr/outbox?page=true"
{: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 [<!]]
[async-error.core]
[rdf.core :as rdf]
[geopub.state]
[geopub.data.rdf :refer [get-rdf]]
[geopub.data.activity :as activity]
[geopub.ns :refer [ldp as]]
[goog.Uri :as uri]))
(defn public-timeline-url [server-url]
......@@ -35,8 +39,31 @@
(public-timeline-url)
(get-rdf {:with-credentials? false})))
(defn login! [state id basic-auth]
(go-try
(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
(comp
#(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)
inbox
outbox))))))
(defn like! [state what]
(print (get-in @state [:account :outbox]))
(-> (activity/like what)
(geopub.data.rdf/post-rdf "http://localhost:4000/users/alice/outbox"
{:basic-auth {:username "alice" :password "123"}})))
(geopub.data.rdf/post-rdf
(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)}
opts)
;; 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]]
(go
(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)
(merge
{:headers {"Content-type" "text/turtle"}
:body body} opts)))))
;; Reagent components
......
......@@ -22,6 +22,7 @@
;; Commonly used namespaces
(defns as "https://www.w3.org/ns/activitystreams#")
(defns ldp "http://www.w3.org/ns/ldp#")
(defns rdfs "http://www.w3.org/2000/01/rdf-schema#")
(defns schema "http://schema.org/")
(defns geo "http://www.w3.org/2003/01/geo/wgs84_pos#")
......@@ -10,6 +10,15 @@
(r/atom
{:graph (rdf.graph.map/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) (rdf.graph.map/graph))
graphs))))
(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)
input))))
......
......@@ -59,7 +59,7 @@
[:button
{:on-click
#(geopub.cpub/like! (get-iri state))} "Like"]
#(geopub.cpub/like! state (get-iri state))} "Like"]
[:button
{:on-click
......
......@@ -204,7 +204,12 @@
(defn description
"Make a new description"
[subject graph]
(->Description subject graph))
(if (and (iri? subject) (graph? graph))
(->Description subject graph)
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)
mapped-graph)))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment