summaryrefslogtreecommitdiff
path: root/protocols/Steam/src/steam_history.cpp
blob: a897037bae698a0cb81eaee4f5264205facd7314 (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
#include "stdafx.h"

void CSteamProto::OnGotConversations(const JSONNode &root, void*)
{
	// Don't load any messages when we don't have lastMessageTS, as it may cause duplicates
	if (m_lastMessageTS <= 0)
		return;

	if (root.isnull())
		return;

	JSONNode response = root["response"];
	JSONNode sessions = response["message_sessions"].as_array();
	if (sessions.isnull())
		return;

	for (const JSONNode &session : sessions)
	{
		long long accountId = _wtoi64(session["accountid_friend"].as_mstring());
		const char *who = AccountIdToSteamId(accountId);

		time_t lastMessageTS = _wtoi64(session["last_message"].as_mstring());

		/*node = json_get(session, "last_view");
		time_t last_view = _wtoi64(ptrW(json_as_string(node)));

		node = json_get(session, "unread_message_count");
		long unread_count = json_as_int(node);*/

		if (lastMessageTS > m_lastMessageTS)
		{
			ptrA token(getStringA("TokenSecret"));
			ptrA steamId(getStringA("SteamID"));

			PushRequest(
				new GetHistoryMessagesRequest(token, steamId, who, m_lastMessageTS),
				&CSteamProto::OnGotHistoryMessages,
				mir_strdup(who));
		}
	}
}

void CSteamProto::OnGotHistoryMessages(const JSONNode &root, void *arg)
{
	ptrA cSteamId((char*)arg);

	if (root.isnull())
		return;

	JSONNode response = root["response"];
	JSONNode messages = response["messages"].as_array();
	for (size_t i = messages.size(); i > 0; i--)
	{
		JSONNode message = messages[i - 1];

		long long accountId = _wtoi64(message["accountid"].as_mstring());
		const char *authorSteamId = AccountIdToSteamId(accountId);

		json_string text = message["message"].as_string();

		time_t timestamp = _wtoi64(message["timestamp"].as_mstring());

		// Ignore already existing messages
		if (timestamp <= m_lastMessageTS)
			continue;

		PROTORECVEVENT recv = { 0 };
		recv.timestamp = timestamp;
		recv.szMessage = (char*)text.c_str();

		MCONTACT hContact = FindContact(cSteamId);
		if (!hContact)
			return;

		// Self SteamID
		ptrA steamId(getStringA("SteamID"));
		if (strcmp(steamId, authorSteamId))
		{
			// Received message
			ProtoChainRecvMsg(hContact, &recv);
		}
		else
		{
			// Sent message
			recv.flags = PREF_SENT;
			Proto_RecvMessage(hContact, &recv);
		}
	}
}