aboutsummaryrefslogtreecommitdiffhomepage
path: root/td/telegram/SecretChatDb.h
blob: 113becf307aced76760448f01491d680428c3c05 (plain)
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
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once

#include "td/db/KeyValueSyncInterface.h"

#include "td/utils/SliceBuilder.h"
#include "td/utils/Status.h"
#include "td/utils/tl_helpers.h"

#include <memory>

namespace td {

class SecretChatDb {
 public:
  SecretChatDb(std::shared_ptr<KeyValueSyncInterface> pmc, int32 chat_id);

  // TODO: some other interface for PFS
  // two keys should be supported
  template <class ValueT>
  void set_value(const ValueT &data) {
    auto key = PSTRING() << "secret" << chat_id_ << ValueT::key();
    pmc_->set(std::move(key), serialize(data));
  }
  template <class ValueT>
  void erase_value(const ValueT &data) {
    auto key = PSTRING() << "secret" << chat_id_ << ValueT::key();
    pmc_->erase(std::move(key));
  }
  template <class ValueT>
  Result<ValueT> get_value() {
    ValueT value;
    auto key = PSTRING() << "secret" << chat_id_ << ValueT::key();
    auto value_str = pmc_->get(std::move(key));
    TRY_STATUS(unserialize(value, value_str));
    return std::move(value);
  }

 private:
  std::shared_ptr<KeyValueSyncInterface> pmc_;
  int32 chat_id_;
};

}  // namespace td