-
Notifications
You must be signed in to change notification settings - Fork 20
/
import.clj
79 lines (68 loc) · 3.35 KB
/
import.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
(ns clojurians-log.db.import
(:require [clojurians-log.time-util :as time-util]
[java-time :as jt]))
(defn message-key
"Message unique identifier.
Slack doesn't expose a primary key for messages, instead the API refers to
messages by timestamp. These are *not* globally unique. The Slack docs are
phrased ambiguously, but they seem to imply that channel+ts is unique, so we
store that. This will allow us to refer to messages later on, e.g. with lookup
refs.
Example: C073DKH9P--1512384953.000000"
[{:keys [channel ts]}]
{:pre [(string? ts) (string? channel)]}
(str channel "--" ts))
(defmulti event->tx :subtype)
(defmethod event->tx :default [_]
;; return nil by default, this will let us skip events we don't (yet) care
;; about
nil)
(defmethod event->tx nil [{:keys [ts text channel user thread_ts] :as message}]
(when-not (= \D (first channel)) ;; ignore direct messages
(let [inst (time-util/ts->inst ts)
message #:message {:key (message-key message)
:ts ts
#_#_:inst (jt/to-java-date inst)
:day (time-util/format-inst-day inst)
:text text
:channel [:channel/slack-id channel]
:user [:user/slack-id user]}]
(if (nil? thread_ts)
message
(let [thread-inst (time-util/ts->inst thread_ts)]
(merge message
#:message{:thread-ts thread_ts
:thread-inst (jt/to-java-date thread-inst)
:day (time-util/format-inst-day thread-inst)}))))))
(defmethod event->tx "message_deleted" [{:keys [deleted_ts channel] :as message}]
[:db.fn/retractEntity [:message/key (message-key {:channel channel :ts deleted_ts})]])
(defmethod event->tx "message_changed" [{:keys [message channel]}]
(event->tx (assoc message :channel channel)))
(defn user->tx [{:keys [id name real_name is_admin is_owner profile]}]
(let [{:keys [image_512 email first_name real_name_normalized image_48 image_192
real_name image_72 image_24 avatar_hash team image_32 last_name
display_name display_name_normalized]} profile]
(->> (merge #:user {:slack-id id
:name name
:real-name real_name
:admin? is_admin
:owner? is_owner}
#:user-profile {:email email,
:avatar-hash avatar_hash,
:image-32 image_32,
:image-24 image_24,
:image-192 image_192,
:image-48 image_48,
:real-name-normalized real_name_normalized,
:display-name-normalized display_name_normalized,
:display-name display_name,
:image-72 image_72,
:real-name real_name,
:image-512 image_512})
(remove (comp nil? val))
(into {}))))
(defn channel->tx [{:keys [id name created creator]}]
#:channel {:slack-id id
:name name
:created created
:creator [:user/slack-id creator]})