1 //
2 // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 #pragma once
8 
9 #include "td/db/KeyValueSyncInterface.h"
10 
11 #include "td/utils/SliceBuilder.h"
12 #include "td/utils/Status.h"
13 #include "td/utils/tl_helpers.h"
14 
15 #include <memory>
16 
17 namespace td {
18 
19 class SecretChatDb {
20  public:
21   SecretChatDb(std::shared_ptr<KeyValueSyncInterface> pmc, int32 chat_id);
22 
23   // TODO: some other interface for PFS
24   // two keys should be supported
25   template <class ValueT>
set_value(const ValueT & data)26   void set_value(const ValueT &data) {
27     auto key = PSTRING() << "secret" << chat_id_ << ValueT::key();
28     pmc_->set(std::move(key), serialize(data));
29   }
30   template <class ValueT>
erase_value(const ValueT & data)31   void erase_value(const ValueT &data) {
32     auto key = PSTRING() << "secret" << chat_id_ << ValueT::key();
33     pmc_->erase(std::move(key));
34   }
35   template <class ValueT>
get_value()36   Result<ValueT> get_value() {
37     ValueT value;
38     auto key = PSTRING() << "secret" << chat_id_ << ValueT::key();
39     auto value_str = pmc_->get(std::move(key));
40     TRY_STATUS(unserialize(value, value_str));
41     return std::move(value);
42   }
43 
44  private:
45   std::shared_ptr<KeyValueSyncInterface> pmc_;
46   int32 chat_id_;
47 };
48 
49 }  // namespace td
50