From 1fe23fe57c25275fbd0bfc51808da9a651e86855 Mon Sep 17 00:00:00 2001 From: virvar Date: Mon, 16 May 2016 15:19:13 +0300 Subject: [PATCH] 'Clear history', 'Leave chat' handlers --- src/syng_im/group_settings/group_settings.cljs | 6 ++---- src/syng_im/handlers.cljs | 13 ++++++++++++- src/syng_im/models/chats.cljs | 9 +++++++++ src/syng_im/models/messages.cljs | 13 +++++++++++-- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/syng_im/group_settings/group_settings.cljs b/src/syng_im/group_settings/group_settings.cljs index 7153ce6207..baba9bc9b1 100644 --- a/src/syng_im/group_settings/group_settings.cljs +++ b/src/syng_im/group_settings/group_settings.cljs @@ -113,14 +113,12 @@ :icon-style {:width 12 :height 12} :title "Clear history" - :subtitle "!not implemented" - :handler nil} + :handler #(dispatch [:clear-history])} {:icon :bin :icon-style {:width 12 :height 18} :title "Delete and leave" - :subtitle "!not implemented" - :handler nil}]] + :handler #(dispatch [:leave-group-chat])}]] [view st/settings-container (for [setting settings] ^{:key setting} [setting-view setting])])) diff --git a/src/syng_im/handlers.cljs b/src/syng_im/handlers.cljs index f73a56a23c..396cf9839c 100644 --- a/src/syng_im/handlers.cljs +++ b/src/syng_im/handlers.cljs @@ -14,7 +14,8 @@ [syng-im.models.messages :refer [save-message update-message! message-by-id - get-messages]] + get-messages + clear-history]] [syng-im.models.commands :as commands :refer [set-chat-command set-response-chat-command set-chat-command-content @@ -41,6 +42,7 @@ chat-add-participants chat-remove-participants set-chat-active + delete-chat re-join-group-chat chat-by-id2] :as chats] [syng-im.models.chat :refer [signal-chat-updated @@ -395,6 +397,13 @@ ((after save-commands-to-realm!)) ((after handle-commands)))) +(register-handler :clear-history + (fn [db [action]] + (log/debug action) + (let [chat-id (current-chat-id db)] + (clear-history chat-id) + (signal-chat-updated db chat-id)))) + (register-handler :leave-group-chat (fn [db [action]] (log/debug action) @@ -402,6 +411,8 @@ (api/leave-group-chat chat-id) (set-chat-active chat-id false) (left-chat-msg chat-id) + (delete-chat chat-id) + (dispatch [:navigate-back]) (signal-chat-updated db chat-id)))) (register-handler :send-group-chat-msg diff --git a/src/syng_im/models/chats.cljs b/src/syng_im/models/chats.cljs index 861d4635da..481322ba55 100644 --- a/src/syng_im/models/chats.cljs +++ b/src/syng_im/models/chats.cljs @@ -186,3 +186,12 @@ (-> (r/get-by-field :chats :chat-id chat-id) (r/single) (aset "is-active" active?))))) + +(defn delete-chat [chat-id] + (r/write + (fn [] + (-> (r/get-by-field :chats :chat-id chat-id) + (r/single) + (r/delete)))) + ;; TODO temp. Update chat in db atom + (dispatch [:initialize-chats])) diff --git a/src/syng_im/models/messages.cljs b/src/syng_im/models/messages.cljs index b8fc9cff57..87517068c6 100644 --- a/src/syng_im/models/messages.cljs +++ b/src/syng_im/models/messages.cljs @@ -1,5 +1,6 @@ (ns syng-im.models.messages (:require [syng-im.persistence.realm :as r] + [re-frame.core :refer [dispatch]] [cljs.reader :refer [read-string]] [syng-im.utils.random :refer [timestamp]] [syng-im.db :as db] @@ -41,8 +42,9 @@ :outgoing outgoing :timestamp (timestamp) :delivery-status nil - :same-author same-author - :same-direction same-direction} true)))))) + ;; TODO 'some?' is temp + :same-author (some? same-author) + :same-direction (some? same-direction)} true)))))) (defn get-messages [chat-id] (->> (-> (r/get-by-field :msgs :chat-id chat-id) @@ -64,3 +66,10 @@ (fn [] (when (r/exists? :msgs :msg-id msg-id) (r/create :msgs msg true))))) + +(defn clear-history [chat-id] + (r/write + (fn [] + (r/delete (r/get-by-field :msgs :chat-id chat-id)))) + ;; TODO temp. Update chat in db atom + (dispatch [:initialize-chats]))