-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathComposeWindow.cpp
More file actions
306 lines (287 loc) · 13.9 KB
/
ComposeWindow.cpp
File metadata and controls
306 lines (287 loc) · 13.9 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "ComposeWindow.hpp"
#include "managers/GlobalParameters.hpp"
#include "ChatWindow.hpp"
#include "AttachMenu.hpp"
#include "common-windows/ErrorWindow.hpp"
#include "common-windows/MenuWindowEdit.hpp"
#include "td/telegram/td_api.h"
#include "td/tl/TlObject.h"
#include "td/utils/Status.h"
#include "windows/EditorWindow.hpp"
#include "Outputter.hpp"
#include "TdObjectsOutput.h"
#include "windows/Output.hpp"
#include "windows/TextEdit.hpp"
#include "td/telegram/SynchronousRequests.h"
#include <memory>
#include <vector>
namespace tdcurses {
void ComposeWindow::install_callback() {
class Cb : public windows::EditorWindow::Callback {
public:
Cb(ComposeWindow *self) : self_(self) {
}
void on_answer(windows::EditorWindow *window, std::string text) override {
self_->send_message(std::move(text));
}
void on_abort(windows::EditorWindow *window, std::string text) override {
self_->set_draft(std::move(text));
}
private:
ComposeWindow *self_;
};
editor_window_->install_callback(std::make_unique<Cb>(this));
}
void ComposeWindow::send_message(std::string message) {
td::tl_object_ptr<td::td_api::formattedText> text;
if (enabled_markdown_) {
auto R =
run_request_sync(td::make_tl_object<td::td_api::parseMarkdown>(td::make_tl_object<td::td_api::formattedText>(
message, std::vector<td::tl_object_ptr<td::td_api::textEntity>>{})));
if (R.is_error()) {
root()->spawn_popup_view_window(PSTRING() << "failed to parse markdown: " << R.move_as_error(), 3);
return;
} else {
text = R.move_as_ok();
}
} else {
//formattedText text:string entities:vector<textEntity> = FormattedText;
text = td::make_tl_object<td::td_api::formattedText>(message,
std::vector<td::tl_object_ptr<td::td_api::textEntity>>());
}
if (edit_message_id_) {
//inputMessageText text:formattedText link_preview_options:linkPreviewOptions clear_draft:Bool = InputMessageContent;
auto content = td::make_tl_object<td::td_api::inputMessageText>(std::move(text), nullptr, true);
auto req = td::make_tl_object<td::td_api::editMessageText>(chat_id_, edit_message_id_, nullptr, std::move(content));
chat_window_->send_request(std::move(req), [&](td::Result<td::tl_object_ptr<td::td_api::message>> R) {
if (R.is_ok()) {
chat_window_->process_update_sent_message(R.move_as_ok());
}
});
editor_window_->clear();
edit_message_id_ = 0;
return;
}
//messageSendOptions suggested_post_info:inputSuggestedPostInfo disable_notification:Bool from_background:Bool protect_content:Bool allow_paid_broadcast:Bool paid_message_star_count:int53 update_order_of_installed_sticker_sets:Bool scheduling_state:MessageSchedulingState effect_id:int64 sending_id:int32 only_preview:Bool = MessageSendOptions;
auto send_options = td::make_tl_object<td::td_api::messageSendOptions>(
/*suggested_post_info*/ nullptr, /*disable_notification*/ !no_sound_,
/*from_background*/ false, /*protect_content*/ false, /*allow_paid_broadcast*/ false,
/*paid_message_star_count*/ 0, /*update_order_of_installed_sticker_sets*/ false, /*scheduling_state*/ nullptr,
/*effect_id*/ 0, /*sending_id*/ 0, /*only_preview*/ false);
std::vector<td::tl_object_ptr<td::td_api::InputMessageContent>> content;
switch (attach_type_) {
case AttachType::None:
//inputMessageText text:formattedText link_preview_options:linkPreviewOptions clear_draft:Bool = InputMessageContent;
content.push_back(td::make_tl_object<td::td_api::inputMessageText>(std::move(text), nullptr, true));
break;
case AttachType::Photo:
for (auto &f : attach_files_) {
//inputMessagePhoto photo:InputFile thumbnail:inputThumbnail added_sticker_file_ids:vector<int32> width:int32 height:int32 caption:formattedText show_caption_above_media:Bool self_destruct_type:MessageSelfDestructType has_spoiler:Bool = InputMessageContent;
content.push_back(td::make_tl_object<td::td_api::inputMessagePhoto>(
td::make_tl_object<td::td_api::inputFileLocal>(f), nullptr, std::vector<td::int32>{}, 0, 0, std::move(text),
false, nullptr, false));
}
break;
case AttachType::Audio:
for (auto &f : attach_files_) {
//inputMessageAudio audio:InputFile album_cover_thumbnail:inputThumbnail duration:int32 title:string performer:string caption:formattedText = InputMessageContent;
content.push_back(td::make_tl_object<td::td_api::inputMessageAudio>(
td::make_tl_object<td::td_api::inputFileLocal>(f), nullptr, 0, "", "", std::move(text)));
}
break;
case AttachType::Video:
for (auto &f : attach_files_) {
//inputMessageVideo video:InputFile thumbnail:inputThumbnail cover:InputFile start_timestamp:int32 added_sticker_file_ids:vector<int32> duration:int32 width:int32 height:int32 supports_streaming:Bool caption:formattedText show_caption_above_media:Bool self_destruct_type:MessageSelfDestructType has_spoiler:Bool = InputMessageContent;
content.push_back(td::make_tl_object<td::td_api::inputMessageVideo>(
td::make_tl_object<td::td_api::inputFileLocal>(f), nullptr, nullptr, 0, std::vector<td::int32>{}, 0, 0, 0,
true, std::move(text), false, nullptr, false));
}
break;
case AttachType::Doc:
for (auto &f : attach_files_) {
//inputMessageDocument document:InputFile thumbnail:inputThumbnail disable_content_type_detection:Bool caption:formattedText = InputMessageContent;
content.push_back(td::make_tl_object<td::td_api::inputMessageDocument>(
td::make_tl_object<td::td_api::inputFileLocal>(f), nullptr, false, std::move(text)));
}
break;
case AttachType::Geo:
break;
case AttachType::Contact:
break;
case AttachType::Poll:
break;
}
td::tl_object_ptr<td::td_api::inputMessageReplyToMessage> reply;
if (reply_message_id_) {
//inputMessageReplyToMessage message_id:int53 quote:inputTextQuote checklist_task_id:int32 = InputMessageReplyTo;
reply = td::make_tl_object<td::td_api::inputMessageReplyToMessage>(/*message_id*/ reply_message_id_,
/*quote*/ nullptr, /*checklist_task_id*/ 0);
if (quote_.size() > 0) {
if (enabled_markdown_) {
auto R = run_request_sync(
td::make_tl_object<td::td_api::parseMarkdown>(td::make_tl_object<td::td_api::formattedText>(
quote_, std::vector<td::tl_object_ptr<td::td_api::textEntity>>{})));
if (R.is_error()) {
root()->spawn_popup_view_window(PSTRING() << "failed to parse markdown: " << R.move_as_error(), 3);
return;
} else {
//@description Describes manually chosen quote from another message
//@text Text of the quote; 0-getOption("message_reply_quote_length_max") characters. Only Bold, Italic, Underline, Strikethrough, Spoiler, and CustomEmoji entities are allowed to be kept and must be kept in the quote
//@position Quote position in the original message in UTF-16 code units
//inputTextQuote text:formattedText position:int32 = InputTextQuote;
auto quote_text = R.move_as_ok();
reply->quote_ = td::make_tl_object<td::td_api::inputTextQuote>(std::move(quote_text), 0);
}
}
}
}
if (content.size() == 1) {
//sendMessage chat_id:int53 message_thread_id:int53 reply_to:MessageReplyTo options:messageSendOptions reply_markup:ReplyMarkup input_message_content:InputMessageContent = Message;
auto req = td::make_tl_object<td::td_api::sendMessage>(chat_id_, /*thread_id*/ nullptr, std::move(reply),
std::move(send_options), nullptr, std::move(content[0]));
chat_window_->send_request(std::move(req), [&](td::Result<td::tl_object_ptr<td::td_api::message>> R) {
DROP_IF_DELETED(R);
if (R.is_ok()) {
chat_window_->process_update_sent_message(R.move_as_ok());
} else {
spawn_error_window(*this, R.move_as_error().to_string(), {});
}
});
} else {
//sendMessageAlbum chat_id:int53 message_thread_id:int53 reply_to:InputMessageReplyTo options:messageSendOptions input_message_contents:vector<InputMessageContent> = Messages;
auto req = td::make_tl_object<td::td_api::sendMessageAlbum>(chat_id_, /*thread_id*/ nullptr, std::move(reply),
std::move(send_options), std::move(content));
chat_window_->send_request(std::move(req), [&](td::Result<td::tl_object_ptr<td::td_api::messages>> R) {
DROP_IF_DELETED(R);
if (R.is_ok()) {
auto res = R.move_as_ok();
for (auto &m : res->messages_) {
chat_window_->process_update_sent_message(std::move(m));
}
} else {
spawn_error_window(*this, R.move_as_error().to_string(), {});
}
});
}
editor_window_->clear();
set_reply_message_id(0, "");
attach_type_ = AttachType::None;
attach_files_.clear();
}
void ComposeWindow::set_draft(std::string message) {
//formattedText text:string entities:vector<textEntity> = FormattedText;
auto text =
td::make_tl_object<td::td_api::formattedText>(message, std::vector<td::tl_object_ptr<td::td_api::textEntity>>());
//inputMessageText text:formattedText link_preview_options:linkPreviewOptions clear_draft:Bool = InputMessageContent;
auto content = td::make_tl_object<td::td_api::inputMessageText>(std::move(text), nullptr, false);
//draftMessage reply_to:InputMessageReplyTo date:int32 input_message_text:InputMessageContent effect_id:int64 suggested_post_info:inputSuggestedPostInfo = DraftMessage;
auto draft = td::make_tl_object<td::td_api::draftMessage>(nullptr, 0, std::move(content), 0, nullptr);
//setChatDraftMessage chat_id:int53 message_thread_id:int53 draft_message:draftMessage = Ok;
auto req = td::make_tl_object<td::td_api::setChatDraftMessage>(chat_id_, /*thread_id*/ nullptr, std::move(draft));
send_request(std::move(req), [&](td::Result<td::tl_object_ptr<td::td_api::ok>> R) {});
editor_window_->clear();
root()->close_compose_window();
}
void ComposeWindow::render(windows::WindowOutputter &rb, bool force) {
{
Outputter out;
out << Outputter::NoLb(true);
if (enabled_markdown_) {
out << Outputter::Bold{true} << "[+MARKDOWN] " << Outputter::Bold{Outputter::ChangeBool::Revert};
} else {
out << "[-MARKDOWN] ";
}
if (!no_sound_) {
out << Outputter::Bold{true} << "[+SOUND] " << Outputter::Bold{Outputter::ChangeBool::Revert};
} else {
out << "[-SOUND] ";
}
switch (attach_type_) {
case AttachType::None:
out << "[-ATTACH] ";
break;
case AttachType::Photo:
out << Outputter::Bold{true} << "[+ATTACH: " << attach_files_.size() << " PHOTOS] "
<< Outputter::Bold{Outputter::ChangeBool::Revert};
break;
case AttachType::Audio:
out << Outputter::Bold{true} << "[+ATTACH: " << attach_files_.size() << " AUDIOS] "
<< Outputter::Bold{Outputter::ChangeBool::Revert};
break;
case AttachType::Video:
out << Outputter::Bold{true} << "[+ATTACH: " << attach_files_.size() << " VIDEOS] "
<< Outputter::Bold{Outputter::ChangeBool::Revert};
break;
case AttachType::Doc:
out << Outputter::Bold{true} << "[+ATTACH: " << attach_files_.size() << " DOCS] "
<< Outputter::Bold{Outputter::ChangeBool::Revert};
break;
case AttachType::Geo:
out << Outputter::Bold{true} << "[+ATTACH: " << attach_files_.size() << " LOC] "
<< Outputter::Bold{Outputter::ChangeBool::Revert};
break;
case AttachType::Contact:
out << Outputter::Bold{true} << "[+ATTACH: " << attach_files_.size() << " CONTACT] "
<< Outputter::Bold{Outputter::ChangeBool::Revert};
break;
case AttachType::Poll:
out << Outputter::Bold{true} << "[+ATTACH: " << attach_files_.size() << " POLL] "
<< Outputter::Bold{Outputter::ChangeBool::Revert};
break;
}
if (quote_.size() > 0) {
out << "[+QUOTE]";
} else if (reply_message_id_ != 0) {
out << "[-QUOTE]";
}
windows::TextEdit::render(rb, width(), out.as_cslice(), 0, out.markup(), false, false);
}
if (reply_message_id_) {
rb.translate(1, 0);
auto msg = chat_window_->get_message_as_message(chat_id_, reply_message_id_);
if (!msg) {
rb.erase_yx(0, 0, width());
rb.putstr_yx(0, 0, "reply to: ", 10);
} else {
Outputter out;
out << Outputter::NoLb(true) << "reply to: " << Color::Red << msg->sender_id_ << Color::Revert << " "
<< msg->content_;
windows::TextEdit::render(rb, width(), out.as_cslice(), 0, out.markup(), false, false);
}
rb.untranslate(1, 0);
}
}
void ComposeWindow::handle_input(const windows::InputEvent &info) {
if (info == "M-m") {
enabled_markdown_ = !enabled_markdown_;
set_need_refresh();
return;
}
if (info == "M-s") {
no_sound_ = !no_sound_;
set_need_refresh();
return;
}
if (info == "M-a") {
create_menu_window<AttachMenu>(root(), root_actor_id(), chat_id_, editor_window_->export_data(), attach_type_,
attach_files_, this, window_unique_id());
set_need_refresh();
return;
}
if (info == "M-q" && reply_message_id_) {
spawn_text_edit_window(*this, "select text to quote", quote_,
[self = this, reply_message_id = reply_message_id_](td::Result<std::string> res) mutable {
if (res.is_error()) {
return;
}
self->set_reply_message_id(reply_message_id, res.move_as_ok());
});
return;
}
editor_window_->handle_input(info);
auto chat_window = root()->chat_window();
if (chat_window) {
chat_window->update_visible();
}
}
} // namespace tdcurses