File: api_toggling_media.cpp

package info (click to toggle)
telegram-desktop 4.6.5%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 53,300 kB
  • sloc: cpp: 605,857; python: 3,978; ansic: 1,636; sh: 965; makefile: 841; objc: 652; javascript: 187; xml: 165
file content (143 lines) | stat: -rw-r--r-- 3,527 bytes parent folder | download
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
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.

For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "api/api_toggling_media.h"

#include "apiwrap.h"
#include "data/data_document.h"
#include "data/data_file_origin.h"
#include "data/data_session.h"
#include "data/stickers/data_stickers.h"
#include "window/window_session_controller.h"
#include "main/main_session.h"

namespace Api {
namespace {

template <typename ToggleRequestCallback, typename DoneCallback>
void ToggleExistingMedia(
		not_null<DocumentData*> document,
		Data::FileOrigin origin,
		ToggleRequestCallback toggleRequest,
		DoneCallback &&done) {
	const auto api = &document->owner().session().api();

	auto performRequest = [=](const auto &repeatRequest) -> void {
		const auto usedFileReference = document->fileReference();
		api->request(
			toggleRequest()
		).done(done).fail([=](const MTP::Error &error) {
			if (error.code() == 400
				&& error.type().startsWith(u"FILE_REFERENCE_"_q)) {
				auto refreshed = [=](const Data::UpdatedFileReferences &d) {
					if (document->fileReference() != usedFileReference) {
						repeatRequest(repeatRequest);
					}
				};
				api->refreshFileReference(origin, std::move(refreshed));
			}
		}).send();
	};
	performRequest(performRequest);
}

} // namespace

void ToggleFavedSticker(
		not_null<Window::SessionController*> controller,
		not_null<DocumentData*> document,
		Data::FileOrigin origin) {
	ToggleFavedSticker(
		controller,
		document,
		std::move(origin),
		!document->owner().stickers().isFaved(document));
}

void ToggleFavedSticker(
		not_null<Window::SessionController*> controller,
		not_null<DocumentData*> document,
		Data::FileOrigin origin,
		bool faved) {
	if (faved && !document->sticker()) {
		return;
	}
	const auto weak = base::make_weak(controller);
	auto done = [=] {
		document->owner().stickers().setFaved(weak.get(), document, faved);
	};
	ToggleExistingMedia(
		document,
		std::move(origin),
		[=, d = document] {
			return MTPmessages_FaveSticker(d->mtpInput(), MTP_bool(!faved));
		},
		std::move(done));
}

void ToggleRecentSticker(
		not_null<DocumentData*> document,
		Data::FileOrigin origin,
		bool saved) {
	if (!document->sticker()) {
		return;
	}
	auto done = [=] {
		if (!saved) {
			document->owner().stickers().removeFromRecentSet(document);
		}
	};
	ToggleExistingMedia(
		document,
		std::move(origin),
		[=] {
			return MTPmessages_SaveRecentSticker(
				MTP_flags(MTPmessages_SaveRecentSticker::Flag(0)),
				document->mtpInput(),
				MTP_bool(!saved));
		},
		std::move(done));
}

void ToggleSavedGif(
		Window::SessionController *controller,
		not_null<DocumentData*> document,
		Data::FileOrigin origin,
		bool saved) {
	if (saved && !document->isGifv()) {
		return;
	}
	const auto weak = base::make_weak(controller);
	auto done = [=] {
		if (saved) {
			document->owner().stickers().addSavedGif(weak.get(), document);
		}
	};
	ToggleExistingMedia(
		document,
		std::move(origin),
		[=, d = document] {
			return MTPmessages_SaveGif(d->mtpInput(), MTP_bool(!saved));
		},
		std::move(done));
}

void ToggleSavedRingtone(
		not_null<DocumentData*> document,
		Data::FileOrigin origin,
		Fn<void()> &&done,
		bool saved) {
	ToggleExistingMedia(
		document,
		std::move(origin),
		[=, d = document] {
			return MTPaccount_SaveRingtone(d->mtpInput(), MTP_bool(!saved));
		},
		std::move(done));
}

} // namespace Api