diff options
author | Robert Pösel <robyer@seznam.cz> | 2015-12-21 10:37:41 +0000 |
---|---|---|
committer | Robert Pösel <robyer@seznam.cz> | 2015-12-21 10:37:41 +0000 |
commit | 56b177cff6de7a1cef5e02d508fce4b53baddba1 (patch) | |
tree | af730f9b425c044cebeaddb78f43a5d325aa9693 /protocols/Steam/src/steam_history.cpp | |
parent | f738c6cf01e4508b3af9a7fed4be3d98f9b087a5 (diff) |
Steam: Implement loading history/offline messages at login (finally!)
git-svn-id: http://svn.miranda-ng.org/main/trunk@15923 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'protocols/Steam/src/steam_history.cpp')
-rw-r--r-- | protocols/Steam/src/steam_history.cpp | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/protocols/Steam/src/steam_history.cpp b/protocols/Steam/src/steam_history.cpp new file mode 100644 index 0000000000..dc1f746928 --- /dev/null +++ b/protocols/Steam/src/steam_history.cpp @@ -0,0 +1,121 @@ +#include "stdafx.h" + +void CSteamProto::OnGotConversations(const HttpResponse *response) +{ + if (!ResponseHttpOk(response)) + return; + + JSONROOT root(response->pData); + if (root == NULL) + return; + + JSONNode *node = json_get(root, "response"); + + if (m_lastMessageTS <= 0) + { + // Remember and save actual timestamp (as it is first we've got) + JSONNode *timestampNode = json_get(node, "timestamp"); + m_lastMessageTS = _ttoi64(ptrT(json_as_string(timestampNode))); + setDword("LastMessageTS", m_lastMessageTS); + + // And don't load any messages as it may cause duplicates + return; + } + + JSONNode *sessions = json_get(node, "message_sessions"); + JSONNode *nsessions = json_as_array(sessions); + + if (nsessions != NULL) + { + ptrA token(getStringA("TokenSecret")); + ptrA steamId(getStringA("SteamID")); + + + for (size_t i = 0; i < json_size(nsessions); i++) + { + JSONNode *session = json_at(nsessions, i); + + node = json_get(session, "accountid_friend"); + const char *who = AccountIdToSteamId(_ttoi64(ptrT(json_as_string(node)))); + + node = json_get(session, "last_message"); + time_t lastMessageTS = _ttoi64(ptrT(json_as_string(node))); + + /*node = json_get(session, "last_view"); + time_t last_view = _ttoi64(ptrT(json_as_string(node))); + + node = json_get(session, "unread_message_count"); + long unread_count = json_as_int(node);*/ + + if (lastMessageTS > m_lastMessageTS) + { + PushRequest( + new GetHistoryMessagesRequest(token, steamId, who, m_lastMessageTS), + &CSteamProto::OnGotHistoryMessages, + mir_strdup(who), + MirFreeArg); + } + } + + json_delete(nsessions); + } +} + +void CSteamProto::OnGotHistoryMessages(const HttpResponse *response, void *arg) +{ + MCONTACT hContact = FindContact((char*)arg); + if (!hContact) + return; + + if (!ResponseHttpOk(response)) + return; + + JSONROOT root(response->pData); + if (root == NULL) + return; + + JSONNode *node = json_get(root, "response"); + + JSONNode *messages = json_get(node, "messages"); + JSONNode *nmessages = json_as_array(messages); + + // Self SteamID + ptrA steamId(getStringA("SteamID")); + + for (size_t i = json_size(nmessages) - 1; i >= 0; i--) + { + JSONNode *message = json_at(nmessages, i); + + node = json_get(message, "accountid"); + const char *authorSteamId = AccountIdToSteamId(_ttoi64(ptrT(json_as_string(node)))); + + node = json_get(message, "message"); + ptrT text(json_as_string(node)); + T2Utf szMessage(text); + + node = json_get(message, "timestamp"); + time_t timestamp = _ttoi64(ptrT(json_as_string(node))); + + PROTORECVEVENT recv = { 0 }; + recv.timestamp = timestamp; + recv.szMessage = szMessage; + + if (strcmp(steamId, authorSteamId)) + { + // Received message + ProtoChainRecvMsg(hContact, &recv); + } + else + { + // Sent message + recv.flags = PREF_SENT; + Proto_RecvMessage(hContact, &recv); + } + + // Update last message timestamp + if (timestamp > getDword("LastMessageTS", 0)) + setDword("LastMessageTS", timestamp); + } + + json_delete(nmessages); +} |