aboutsummaryrefslogtreecommitdiffhomepage
path: root/td/telegram/ToDoItem.cpp
blob: 4b7fe976210f784dc344170ff40999abae289ae4 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// 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)
//
#include "td/telegram/ToDoItem.h"

#include "td/telegram/AuthManager.h"
#include "td/telegram/Dependencies.h"
#include "td/telegram/MessageSender.h"
#include "td/telegram/OptionManager.h"
#include "td/telegram/Td.h"

#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/SliceBuilder.h"
#include "td/utils/utf8.h"

namespace td {

ToDoItem::ToDoItem(const UserManager *user_manager, telegram_api::object_ptr<telegram_api::todoItem> &&item,
                   int32 message_date) {
  CHECK(item != nullptr);
  id_ = item->id_;
  title_ = get_formatted_text(user_manager, std::move(item->title_), true, true, "ToDoItem");
  validate(message_date, "telegram_api::todoItem");
}

Result<ToDoItem> ToDoItem::get_to_do_item(const Td *td, DialogId dialog_id,
                                          td_api::object_ptr<td_api::inputChecklistTask> &&task) {
  if (task == nullptr) {
    return Status::Error(400, "Checklist task must be non-empty");
  }
  TRY_RESULT(title, get_formatted_text(td, dialog_id, std::move(task->text_), td->auth_manager_->is_bot(), false, true,
                                       false));
  auto max_length = td->option_manager_->get_option_integer("checklist_task_text_length_max", 0);
  if (static_cast<int64>(utf8_length(title.text)) > max_length) {
    return Status::Error(400, PSLICE() << "Checklist task text length must not exceed " << max_length);
  }
  if (task->id_ <= 0) {
    return Status::Error(400, "Checklist task identifier must be positive");
  }
  replace_with_spaces(title.text, "\n");
  remove_unsupported_entities(title);
  ToDoItem result;
  result.id_ = task->id_;
  result.title_ = std::move(title);
  return result;
}

telegram_api::object_ptr<telegram_api::todoItem> ToDoItem::get_input_todo_item(const UserManager *user_manager) const {
  return telegram_api::make_object<telegram_api::todoItem>(
      id_, get_input_text_with_entities(user_manager, title_, "get_input_todo_item"));
}

bool ToDoItem::remove_unsupported_entities(FormattedText &text) {
  return remove_unallowed_quote_user_entities(text, true, true);
}

void ToDoItem::validate(int32 message_date, const char *source) {
  if (remove_unsupported_entities(title_) && message_date > 1782000000) {  // approximate fix time
    LOG(ERROR) << "Receive unexpected checklist task entities from " << source;
  }
  if (!check_utf8(title_.text)) {
    LOG(ERROR) << "Receive invalid checklist task from " << source;
    title_ = {};
  }
}

td_api::object_ptr<td_api::checklistTask> ToDoItem::get_checklist_task_object(
    Td *td, const vector<ToDoCompletion> &completions) const {
  auto result = td_api::make_object<td_api::checklistTask>(
      id_, get_formatted_text_object(td->user_manager_.get(), title_, true, -1), nullptr, 0);
  for (auto &completion : completions) {
    if (completion.id_ == id_) {
      result->completed_by_ = get_message_sender_object(td, completion.completed_by_dialog_id_, "checklistTask");
      result->completion_date_ = completion.date_;
    }
  }
  return result;
}

void ToDoItem::add_dependencies(Dependencies &dependencies) const {
  add_formatted_text_dependencies(dependencies, &title_);
}

bool operator==(const ToDoItem &lhs, const ToDoItem &rhs) {
  return lhs.id_ == rhs.id_ && lhs.title_ == rhs.title_;
}

bool operator!=(const ToDoItem &lhs, const ToDoItem &rhs) {
  return !(lhs == rhs);
}

}  // namespace td