Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LRS-72 Handle deduplicated attachment multiparts #81

Merged
merged 14 commits into from
May 12, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
LRS-72 make an xapi fn for pulling att objects generally with stateme…
…nt + path
milt committed May 11, 2022
commit 94a9d44cdfe5a1c1905f7b1b37306ecce8d85f26
84 changes: 58 additions & 26 deletions src/main/com/yetanalytics/lrs/xapi/statements.cljc
Original file line number Diff line number Diff line change
@@ -535,32 +535,6 @@
(defn voiding-statement? [s]
(some-> s (get-in ["verb" "id"]) (= "http://adlnet.gov/expapi/verbs/voided")))

(defn all-attachment-hashes
"For each statement, get any attachment hashes. If skip-file-urls is true,
will only return sha2s from attachments w/o fileURL."
[statements & [skip-file-urls]]
(distinct
(keep
(fn [{:strs [sha2 fileUrl] :as _attachment}]
(if skip-file-urls
(when-not fileUrl
sha2)
sha2))
(mapcat
(fn [{:strs [attachments
object]
:as _statement}]
(concat attachments
(get object "attachments")))
statements))))

(s/fdef all-attachment-hashes
:args (s/cat :statements
(s/coll-of ::xs/statement)
:skip-file-urls
(s/? boolean?))
:ret (s/coll-of :attachment/sha2))

;; A representation of a stored attachment
;; TODO: generalize
(s/def :attachment/content
@@ -594,6 +568,64 @@
(s/def ::attachments
(s/coll-of ::attachment))

(s/def ::attachment-path
(s/cat
:root-or-sub (s/? #{"object"})
:attachment-key #{"attachments"}
:index nat-int?))

(defn all-attachment-objects
"Given a collection of statements, return a lazy seq of maps containing:
:statement - the containing statement
:attachment - the attachment object
:attachment-path - the path of the attachment object in the statement"
[statements]
(mapcat
(fn [{:strs [attachments
object]
:as statement}]
(concat (for [[idx att] (map-indexed vector attachments)]
{:statement statement
:attachment att
:attachment-path ["attachments" idx]})
(for [[idx att] (map-indexed vector
(get object "attachments"))]
{:statement statement
:attachment att
:attachment-path ["object" "attachments" idx]})))
statements))

(s/fdef all-attachment-objects
:args (s/cat :statements
(s/coll-of ::xs/statement))
:ret (s/coll-of
(s/keys :req-un
[::xs/statement
::xs/attachment
::attachment-path])))

(defn all-attachment-hashes
"For each statement, get any attachment hashes. If skip-file-urls is true,
will only return sha2s from attachments w/o fileURL."
[statements & [skip-file-urls]]
(distinct
(keep
(fn [{:strs [sha2 fileUrl] :as _attachment}]
(if skip-file-urls
(when-not fileUrl
sha2)
sha2))
(map
:attachment
(all-attachment-objects statements)))))

(s/fdef all-attachment-hashes
:args (s/cat :statements
(s/coll-of ::xs/statement)
:skip-file-urls
(s/? boolean?))
:ret (s/coll-of :attachment/sha2))

(defn statement-rel-docs
"Get related activities and agents"
[statement]
6 changes: 6 additions & 0 deletions src/test/com/yetanalytics/lrs/xapi/statements_test.cljc
Original file line number Diff line number Diff line change
@@ -137,6 +137,12 @@
(stest/check `ss/statement-ref?
{stc-opts {:num-tests 10 :max-size 3}})))))

(deftest all-attachment-objects-test
(is (empty?
(failures
(stest/check `ss/all-attachment-objects
{stc-opts {:num-tests 2 :max-size 2}})))))

(deftest all-attachment-hashes-test
(is (empty?
(failures