summaryrefslogtreecommitdiff
path: root/src/weepinbell/processor/writer.clj
blob: 5bc672573487d677b5a57ef3bc1955f2ae94c134 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
(ns weepinbell.processor.writer
  (:require [taoensso.timbre :as timbre]
            [weepinbell.storage.spec]
            [clojure.spec.alpha :as s]))

(defn write
  [data storage callbacks]
  (when (:ok? data)
    (let [{:keys [id new? new-data]} data]
      (if new?
        (do ;; append new webmention
          (timbre/info "new webmention" id "processed, writing to storage...")
          ((:append storage) new-data)
          ((:on-create callbacks) new-data))
        (do ;; update old webmention
          (timbre/info "webmention" id "exists, updating...")
          ((:update storage) id new-data)
          ((:on-update callbacks) new-data)))
      (when (not (s/valid? :weepinbell.storage.spec/webmention-wrapped new-data))
        (timbre/warn "webmention" id "data does not match spec before writing to storage"))
      (-> data (assoc :ok? true)))))