1 /*
2 This file is part of Telegram Desktop,
3 the official desktop application for the Telegram messaging service.
4 
5 For license and copyright information please follow this link:
6 https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
7 */
8 #include "dialogs/dialogs_search_from_controllers.h"
9 
10 #include "lang/lang_keys.h"
11 #include "data/data_peer_values.h"
12 #include "data/data_channel.h"
13 #include "data/data_chat.h"
14 #include "data/data_user.h"
15 #include "main/main_session.h"
16 #include "apiwrap.h"
17 
18 namespace Dialogs {
19 
ShowSearchFromBox(not_null<PeerData * > peer,Fn<void (not_null<PeerData * >)> callback,Fn<void ()> closedCallback)20 void ShowSearchFromBox(
21 		not_null<PeerData*> peer,
22 		Fn<void(not_null<PeerData*>)> callback,
23 		Fn<void()> closedCallback) {
24 	auto createController = [
25 		peer,
26 		callback = std::move(callback)
27 	]() -> std::unique_ptr<PeerListController> {
28 		if (peer && (peer->isChat() || peer->isMegagroup())) {
29 			return std::make_unique<Dialogs::SearchFromController>(
30 				peer,
31 				std::move(callback));
32 		}
33 		return nullptr;
34 	};
35 	if (auto controller = createController()) {
36 		auto subscription = std::make_shared<rpl::lifetime>();
37 		auto box = Ui::show(Box<PeerListBox>(std::move(controller), [subscription](not_null<PeerListBox*> box) {
38 			box->addButton(tr::lng_cancel(), [box, subscription] {
39 				box->closeBox();
40 			});
41 		}), Ui::LayerOption::KeepOther);
42 		box->boxClosing() | rpl::start_with_next(
43 			std::move(closedCallback),
44 			*subscription);
45 	}
46 }
47 
SearchFromController(not_null<PeerData * > peer,Fn<void (not_null<PeerData * >)> callback)48 SearchFromController::SearchFromController(
49 	not_null<PeerData*> peer,
50 	Fn<void(not_null<PeerData*>)> callback)
51 : AddSpecialBoxController(
52 	peer,
53 	ParticipantsBoxController::Role::Members,
54 	AdminDoneCallback(),
55 	BannedDoneCallback())
56 , _callback(std::move(callback)) {
57 	_excludeSelf = false;
58 }
59 
prepare()60 void SearchFromController::prepare() {
61 	AddSpecialBoxController::prepare();
62 	delegate()->peerListSetTitle(tr::lng_search_messages_from());
63 	if (const auto megagroup = peer()->asMegagroup()) {
64 		if (!delegate()->peerListFindRow(megagroup->id.value)) {
65 			delegate()->peerListAppendRow(
66 				std::make_unique<PeerListRow>(megagroup));
67 			setDescriptionText({});
68 			delegate()->peerListRefreshRows();
69 		}
70 	}
71 }
72 
rowClicked(not_null<PeerListRow * > row)73 void SearchFromController::rowClicked(not_null<PeerListRow*> row) {
74 	if (const auto onstack = base::duplicate(_callback)) {
75 		onstack(row->peer());
76 	}
77 }
78 
79 } // namespace Dialogs
80