summaryrefslogtreecommitdiff
path: root/protocols/FacebookRM/src/requests/history.h
blob: 2e22ca15dd1c1a4eafdc4df92af0c65697659a41 (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
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
/*

Facebook plugin for Miranda Instant Messenger
_____________________________________________

Copyright © 2011-17 Robert Pösel, 2017-18 Miranda NG team

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#ifndef _FACEBOOK_REQUEST_HISTORY_H_
#define _FACEBOOK_REQUEST_HISTORY_H_

// getting thread info and messages
// revised 3.12.2017
class ThreadInfoRequest : public HttpRequest
{
public:
	// Request only messages history
	ThreadInfoRequest(facebook_client *fc, bool isChat, const char *id, int /*offset*/, const char* /*timestamp*/, int limit) :
		HttpRequest(REQUEST_POST, FACEBOOK_SERVER_REGULAR "/api/graphqlbatch/")
	{
		setCommonBody(fc);

		// FIXME: FB removed /ajax/mercury/thread_info requests and now all messaging stuff does through /api/graphqlbatch/ - all loading of threads, (unread) messages, list of contacts in groupchat, etc.
		// All these request must be rewritten to the new request. Preparation is below but unfinished.

		//const char *type = isChat ? "thread_fbids" : "user_ids";
		std::string id_ = id; // FIXME: Rewrite this without std::string...
		if (isChat) {
			// NOTE: Remove "id." prefix as here we need to give threadFbId and not threadId
			if (id_.substr(0, 3) == "id.")
				id_ = id_.substr(3);
		}
		ptrA idEncoded(mir_urlEncode(id_.c_str()));


		JSONNode root, o0, query_params;

		int before = -1;

		query_params
			<< CHAR_PARAM("id", id_.c_str()) // TODO: Do I have to encode the id? And remove that first "id." at the begin as we do above?
			<< INT_PARAM("message_limit", limit)
			<< INT_PARAM("load_messages", 1)
			<< BOOL_PARAM("load_read_receipts", false);

		if (before != -1)
			query_params << INT_PARAM("before", before);
		else
			query_params << NULL_PARAM("before");

		o0 << CHAR_PARAM("doc_id", id) << JSON_PARAM("query_params", query_params);

		root << JSON_PARAM("o0", o0);

		Body
			<< "batch_name=MessengerGraphQLThreadFetcherRe"
			<< CHAR_VALUE("queries", root.write().c_str());

		// example request data we need to send: { "o0":{"doc_id":"456789456123","query_params" : {"id":"123456789","message_limit" : 20,"load_messages" : 1,"load_read_receipts" : false,"before" : null}} }


		/*
		//if (loadMessages) {
			// Grrr, offset doesn't work at all, we need to use timestamps to get back in history...
			// And we don't know, what's timestamp of first message, so we need to get from latest to oldest
			CMStringA begin(::FORMAT, "messages[%s][%s]", type, idEncoded);

			Body
				<< CMStringA(::FORMAT, "%s[offset]=%i", begin.c_str(), offset).c_str()
				<< CMStringA(::FORMAT, "%s[timestamp]=%s", begin.c_str(), timestamp).c_str()
				<< CMStringA(::FORMAT, "%s[limit]=%i", begin.c_str(), limit).c_str();
		//}*/

		/*if (loadThreadInfo) {
			data += "&threads[" + type + "][0]=" + idEncoded;
		}*/
	}

	// Request only thread info // TODO: Make it array of ids
	ThreadInfoRequest(facebook_client *fc, bool isChat, const char *id) :
		HttpRequest(REQUEST_POST, FACEBOOK_SERVER_REGULAR "/api/graphqlbatch/")
	{
		setCommonBody(fc);

		const char *type = isChat ? "thread_fbids" : "user_ids";
		std::string id_ = id; // FIXME: Rewrite this without std::string...
		if (isChat) {
			// NOTE: Remove "id." prefix as here we need to give threadFbId and not threadId
			if (id_.substr(0, 3) == "id.")
				id_ = id_.substr(3);
		}
		ptrA idEncoded(mir_urlEncode(id_.c_str()));

		// Load only thread info
		Body << CMStringA(::FORMAT, "threads[%s][0]=%s", type, idEncoded).c_str();
	}

	// Request both thread info and messages for single contact/chat
	ThreadInfoRequest(facebook_client *fc, bool isChat, const char *id, int limit) :
		HttpRequest(REQUEST_POST, FACEBOOK_SERVER_REGULAR "/api/graphqlbatch/")
	{
		setCommonBody(fc);

		const char *type = isChat ? "thread_fbids" : "user_ids";
		std::string id_ = id; // FIXME: Rewrite this without std::string...
		if (isChat) {
			// NOTE: Remove "id." prefix as here we need to give threadFbId and not threadId
			if (id_.substr(0, 3) == "id.")
				id_ = id_.substr(3);
		}
		ptrA idEncoded(mir_urlEncode(id_.c_str()));

		// Load messages
		CMStringA begin(::FORMAT, "messages[%s][%s]", type, idEncoded);

		Body
			<< CMStringA(::FORMAT, "%s[offset]=%i", begin.c_str(), 0).c_str()
			<< CMStringA(::FORMAT, "%s[timestamp]=%s", begin.c_str(), "").c_str()
			<< CMStringA(::FORMAT, "%s[limit]=%i", begin.c_str(), limit).c_str();

		// Load thread info
		Body << CMStringA(::FORMAT, "threads[%s][0]=%s", type, idEncoded).c_str();
	}

	// Request both thread info and messages for more threads
	ThreadInfoRequest(facebook_client *fc, const LIST<char> &ids, int offset, int limit) :
		HttpRequest(REQUEST_POST, FACEBOOK_SERVER_REGULAR "/api/graphqlbatch/")
	{
		setCommonBody(fc);

		for (int i = 0; i < ids.getCount(); i++) {
			// NOTE: Remove "id." prefix as here we need to give threadFbId and not threadId
			std::string id_ = ids[i]; // FIXME: Rewrite this without std::string...
			if (id_.substr(0, 3) == "id.")
				id_ = id_.substr(3);
			ptrA idEncoded(mir_urlEncode(id_.c_str()));

			// Load messages
			CMStringA begin(::FORMAT, "messages[%s][%s]", "thread_fbids", idEncoded);
			Body
				<< CMStringA(::FORMAT, "%s[offset]=%i", begin.c_str(), offset).c_str()
				//<< CMStringA(::FORMAT, "%s[timestamp]=%s", begin.c_str(), "").c_str()
				<< CMStringA(::FORMAT, "%s[limit]=%i", begin.c_str(), limit).c_str();

			// Load thread info
			Body << CMStringA(::FORMAT, "threads[%s][%i]=%s", "thread_fbids", i, idEncoded).c_str();
		}
	}

private:
	void setCommonBody(facebook_client *fc)
	{
		Body
			<< CHAR_VALUE("__user", fc->self_.user_id.c_str())
			<< CHAR_VALUE("__dyn", fc->__dyn())
			<< CHAR_VALUE("__req", fc->__req())
			<< CHAR_VALUE("__rev", fc->__rev())
			<< CHAR_VALUE("fb_dtsg", fc->dtsg_.c_str())
			<< CHAR_VALUE("ttstamp", fc->ttstamp_.c_str())
			<< "__a=1"
			<< "__pc=PHASED:DEFAULT"
			<< "__be=1"
			<< "jazoest="
			<< "__spin_r="
			<< "__spin_b="
			<< "__spin_t=";
	}
};

// getting unread threads
// revised 17.8.2016
class UnreadThreadsRequest : public HttpRequest
{
public:
	UnreadThreadsRequest(facebook_client *fc) :
		HttpRequest(REQUEST_POST, FACEBOOK_SERVER_REGULAR "/ajax/mercury/unread_threads.php")
	{
		Url
			<< "dpr=1";

		Body
			<< "folders[0]=inbox"
			<< "folders[1]=other" // TODO: "other" is probably unused, and there is now "pending" instead
			<< "client=mercury"
			<< CHAR_VALUE("__user", fc->self_.user_id.c_str())
			<< CHAR_VALUE("__dyn", fc->__dyn())
			<< CHAR_VALUE("__req", fc->__req())
			<< CHAR_VALUE("__rev", fc->__rev())
			<< CHAR_VALUE("fb_dtsg", fc->dtsg_.c_str())
			<< CHAR_VALUE("ttstamp", fc->ttstamp_.c_str())
			<< "__a=1"
			<< "__pc=PHASED:DEFAULT"
			<< "__be=-1";

		//queries={"o0":{"doc_id":"2003371749678240","query_params":{"limit":99,"before":null,"tags":["PENDING","unread"],"includeDeliveryReceipts":true,"includeSeqID":false}}}
	}
};

#endif //_FACEBOOK_REQUEST_HISTORY_H_