Skip to content

Commit 20ede3e

Browse files
committed
Check in code to clean up reactions
1 parent a4a812a commit 20ede3e

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

repl/emoji_cleanup.clj

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
(ns repl.emoji-cleanup
22
(:require [clojurians-log.datomic :as d]
3+
[clojurians-log.application :as app]
34
[clojurians-log.repl :as repl]
45
[clojurians-log.slack-api :as slack]))
56

7+
(defn conn
8+
"Reach into the system for the datomic connection.
9+
10+
Not 100% kosher but remember this is not production code as such."
11+
[]
12+
(get-in (app/system) [:datomic :conn]))
13+
14+
(defn db []
15+
(d/db (conn)))
16+
617
;; Our :emoji/shortcode property did not originally have an index, meaning we
718
;; kept creating more entities for emojis, instead of upserting them.
819
;;
@@ -11,18 +22,18 @@
1122

1223
;; Do the following before deploying the new schema changes:
1324

14-
(def shortcodes (d/q '[:find ?i ?s :where [?i :emoji/shortcode ?s]] (repl/db)))
25+
(def shortcodes (d/q '[:find ?i ?s :where [?i :emoji/shortcode ?s]] (db)))
1526
(def tx-data (for [[i s] shortcodes]
1627
[:db/retract i :emoji/shortcode s]))
1728

18-
(run! (partial d/transact (repl/conn)) (partition-all 1000 tx-data))
29+
(run! (partial d/transact (conn)) (partition-all 1000 tx-data))
1930

20-
@(d/transact (repl/conn) [{:db/ident :emoji/shortcode
21-
:db/valueType :db.type/string
22-
:db/cardinality :db.cardinality/one
23-
:db/unique :db.unique/identity}])
31+
@(d/transact (conn) [{:db/ident :emoji/shortcode
32+
:db/valueType :db.type/string
33+
:db/cardinality :db.cardinality/one
34+
:db/unique :db.unique/identity}])
2435

25-
(slack/import-emojis! (repl/conn))
36+
(slack/import-emojis! (conn))
2637

2738
;; After deploying the new version, re-import the data to get the emoji reactions in there
2839

@@ -34,9 +45,23 @@
3445
(Thread/sleep 2000))
3546
(println :done))
3647

48+
;; Check vincent's message, should have +4 -1 hearts (= 3 hearts)
49+
3750
(group-by second
3851
(map (juxt :reaction/type
3952
(comp :emoji/shortcode :reaction/emoji))
4053
(:reaction/_message
41-
(datomic.api/entity (repl/db)
54+
(datomic.api/entity (db)
4255
[:message/key "C0GLTDB2T--1608789973.125700"]))))
56+
57+
58+
;; Re-do reactions but now including the key
59+
60+
61+
(def reactions (d/q '[:find [?r ?m] :where [?r :reaction/message ?m]] (db)))
62+
(def tx-data (for [[r m] reactions]
63+
[:db/retract r :reaction/message ?m]))
64+
65+
(run! (partial d/transact (conn)) (partition-all 1000 tx-data))
66+
67+
(count (d/q '[:find [?r ?m] :where [?r :reaction/message ?m]] (db)))

src/clojurians_log/db/import.clj

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,13 @@
9797
;; TODO: deal with reactions on files (I guess this will depend on us actually dealing with files in the first place :))
9898
;; {:reaction "joy", :event_ts "1521818444.000850", :item {:type "file", :file "F9W0B0LHM"}, :user "U06P56UUB", :item_user "U4E5W80P7", :type "reaction_added"}
9999

100-
(defn- reaction-entity [{:keys [user item reaction ts]}]
101-
{:reaction/emoji {:emoji/shortcode reaction}
102-
:reaction/ts ts
103-
:reaction/user [:user/slack-id user]
104-
:reaction/message {:message/key (message-key item)}})
100+
(defn- reaction-entity [{:keys [user item reaction ts type]}]
101+
(let [msg-key (message-key item)]
102+
{:reaction/emoji {:emoji/shortcode reaction}
103+
:reaction/ts ts
104+
:reaction/user [:user/slack-id user]
105+
:reaction/message {:message/key msg-key}
106+
:reaction/key (str message-key "--" user "--" ts "--" reaction "--" type)}))
105107

106108
(defmethod event->tx ["reaction_added" nil] [{:keys [item] :as msg}]
107109
(when (and (:channel item) (:ts item)) ; exclude reactions on things other than messages

src/clojurians_log/db/schema.clj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,12 @@
161161
:db/cardinality :db.cardinality/one}
162162
{:db/ident :reaction/message
163163
:db/valueType :db.type/ref
164-
:db/cardinality :db.cardinality/one}])
164+
:db/cardinality :db.cardinality/one}
165+
{:db/ident :reaction/key
166+
:db/doc "Unique key for a given reaction event, so inserts are idempotent."
167+
:db/valueType :db.type/string
168+
:db/cardinality :db.cardinality/one
169+
:db/unique :db.unique/identity}])
165170

166171
(def full-schema
167172
(concat message-schema

0 commit comments

Comments
 (0)