This commit is contained in:
niten 2024-03-23 11:21:50 -07:00
parent a4d6910037
commit 2e88277401
2 changed files with 40 additions and 40 deletions

View File

@ -27,7 +27,7 @@
default = updateDeps; default = updateDeps;
updateDeps = pkgs.mkShell { updateDeps = pkgs.mkShell {
buildInputs = with helpers.packages."${system}"; buildInputs = with helpers.packages."${system}";
[ updateClojureDeps ]; [ (updateClojureDeps { }) ];
}; };
}; };
}); });

View File

@ -2,7 +2,7 @@
(:require [bebot.model :refer [id user-id new-post to-model mentions-user? channel-last-view from-model created-at]] (:require [bebot.model :refer [id user-id new-post to-model mentions-user? channel-last-view from-model created-at]]
[bebot.api.client :as client] [bebot.api.client :as client]
[bebot.api.channel :as chan] [bebot.api.channel :as chan]
[fudo-clojure.result :refer [let-result exception-failure success failure map-success dispatch-result]] [fudo-clojure.result :as res :refer [let-result exception-failure success failure map-success dispatch-result unwrap]]
[clojure.core.async :as async :refer [go-loop chan timeout >! <!]]) [clojure.core.async :as async :refer [go-loop chan timeout >! <!]])
(:import net.bis5.mattermost.client4.MattermostClient (:import net.bis5.mattermost.client4.MattermostClient
net.bis5.mattermost.model.ChannelView)) net.bis5.mattermost.model.ChannelView))
@ -13,14 +13,11 @@
msg (.getMessage err) msg (.getMessage err)
status-code (.getStatusCode err) status-code (.getStatusCode err)
full-msg (str "[" status-code "] " msg)] full-msg (str "[" status-code "] " msg)]
;; A silly hack to catch the stack trace at this point (throw (ex-info full-msg
(try {:status-code status-code
(throw (ex-info full-msg :error err
{:status-code status-code :message msg})))
:error err})) (to-model (.readEntity resp))))
(catch clojure.lang.ExceptionInfo e
(exception-failure e))))
(success (to-model (.readEntity resp)))))
(defn- to-millis [instant] (defn- to-millis [instant]
(.toEpochMilli instant)) (.toEpochMilli instant))
@ -40,11 +37,9 @@
{:keys [poll-delay buffer-size] {:keys [poll-delay buffer-size]
:or {poll-delay 30 buffer-size 10}}] :or {poll-delay 30 buffer-size 10}}]
(let [out-chan (chan buffer-size)] (let [out-chan (chan buffer-size)]
(go-loop [result (coll-gen)] (go-loop [os (coll-gen)]
(dispatch-result result (doseq [o (sort-by-create-date os)]
([os] (doseq [o (sort-by-create-date os)] (>! out-chan o))
(>! out-chan (success o))))
([e] (>! out-chan (exception-failure e))))
(<! (timeout (* poll-delay 1000))) (<! (timeout (* poll-delay 1000)))
(recur (coll-gen))) (recur (coll-gen)))
out-chan)) out-chan))
@ -60,29 +55,32 @@
(last-read [_] @last-viewed) (last-read [_] @last-viewed)
(get-new-posts! [self] (get-new-posts! [self]
(let-result [msgs (chan/peek-new-posts! self) (let [msgs (chan/peek-new-posts! self)
read-instant (client/mark-read! client (id channel))] read-instant (client/mark-read! client (id channel))]
(do (swap! last-viewed (fn [_] read-instant)) (swap! last-viewed (fn [_] read-instant))
(success (remove-posts-by me msgs))))) (remove-posts-by me msgs)))
(peek-new-posts! [self] (peek-new-posts! [self]
(chan/get-posts-since! self @last-viewed)) (chan/get-posts-since! self @last-viewed))
(get-posts-since! [_ instant] (get-posts-since! [_ instant]
(let-result [msgs (client/get-posts-since! client (id channel) instant)] (->> (client/get-posts-since! client (id channel) instant)
(success (remove-posts-by me msgs)))) (remove-posts-by me)))
(get-new-mentions! [self] (get-new-mentions! [self]
(map-success (chan/get-new-posts! self) (->> self
(partial filter (mentions-user? me)))) (chan/get-new-posts!)
(filter (mentions-user? me))))
(peek-new-mentions! [self] (peek-new-mentions! [self]
(map-success (chan/peek-new-posts! self) (->> self
(partial filter (mentions-user? me)))) (chan/peek-new-posts!)
(filter (mentions-user? me))))
(get-mentions-since! [self instant] (get-mentions-since! [self instant]
(map-success (chan/get-posts-since! self instant) (->> self
(partial filter (mentions-user? me)))) (chan/get-posts-since! instant)
(filter (mentions-user? me))))
(post-channel! [self] (post-channel! [self]
(yield-to-channel (fn [] (chan/get-new-posts! self)) :poll-delay 5)) (yield-to-channel (fn [] (chan/get-new-posts! self)) :poll-delay 5))
@ -105,21 +103,21 @@
(to-result (.getUserByUsername client username nil))) (to-result (.getUserByUsername client username nil)))
(open-channel! [self chan-id] (open-channel! [self chan-id]
(let-result [chan (client/get-channel! self chan-id) (let [chan (client/get-channel! self chan-id)
read-instant (client/mark-read! self chan-id)] read-instant (client/mark-read! self chan-id)]
(success (->BebotChannel self chan (atom read-instant) me)))) (->BebotChannel self chan (atom read-instant) me)))
(open-direct-channel! [self user-id] (open-direct-channel! [self user-id]
(let-result [chan (to-result (.createDirectChannel client (id me) user-id))] (let [chan (to-result (.createDirectChannel client (id me) user-id))]
(success (client/open-channel! self (id chan))))) (client/open-channel! self (id chan))))
(mark-read! [_ chan-id] (mark-read! [_ chan-id]
(let [chan-view (ChannelView. chan-id)] (let [chan-view (ChannelView. chan-id)
(let-result [views (to-result (.viewChannel client (id me) chan-view))] views (to-result (.viewChannel client (id me) chan-view))]
(if-let [view-time (channel-last-view views chan-id)] (if-let [view-time (channel-last-view views chan-id)]
(success view-time) view-time
(failure (str "unable to mark read, not found: " chan-id) (throw (ex-info (str "unable to mark read, not found: " chan-id)
{:channel-views views}))))) {:channel-views views})))))
(get-post! [_ post-id] (get-post! [_ post-id]
(to-result (.getPost client post-id nil))) (to-result (.getPost client post-id nil)))
@ -135,11 +133,13 @@
(get-me! [_] (get-me! [_]
(to-result (.getMe client nil))) (to-result (.getMe client nil)))
(initialize! [self] (initialize! [self]
(map-success (client/get-me! self) (fn [me] (->BebotClient client me))))) (->> self
(client/get-me!)
(->BebotClient client))))
(defn create-connection [url access-token] (defn create-connection [url access-token]
(->BebotClientStub (doto (MattermostClient. url) (->BebotClientStub (doto (MattermostClient. url)
(.setAccessToken access-token)))) (.setAccessToken access-token))))
(defn connect [url access-token] (defn connect [url access-token]
(client/initialize! (create-connection url access-token))) (client/initialize! (create-connection url access-token)))