summaryrefslogtreecommitdiff
path: root/protocols
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2017-01-23 18:34:15 +0300
committerGeorge Hazan <ghazan@miranda.im>2017-01-23 18:34:15 +0300
commitac6a2d3c5a668324d4115305a621a81e4b5d99fb (patch)
treeb650b4c84c1f7b26d1ca8b1d58776fd65870eec9 /protocols
parent705649686832a6e12d7eea59e20528a7e61abe14 (diff)
automatic channel creation if user has none
Diffstat (limited to 'protocols')
-rw-r--r--protocols/Discord/src/dispatch.cpp10
-rw-r--r--protocols/Discord/src/proto.cpp16
-rw-r--r--protocols/Discord/src/proto.h1
-rw-r--r--protocols/Discord/src/server.cpp16
4 files changed, 36 insertions, 7 deletions
diff --git a/protocols/Discord/src/dispatch.cpp b/protocols/Discord/src/dispatch.cpp
index 102a933aa4..3afa2f7a1c 100644
--- a/protocols/Discord/src/dispatch.cpp
+++ b/protocols/Discord/src/dispatch.cpp
@@ -64,8 +64,10 @@ GatewayHandlerFunc CDiscordProto::GetHandler(const wchar_t *pwszCommand)
void CDiscordProto::OnCommandChannelCreated(const JSONNode &pRoot)
{
- CDiscordUser *pUser = PrepareUser(pRoot["user"]);
- if (pUser != NULL) {
+ const JSONNode &members = pRoot["recipients"];
+ for (auto it = members.begin(); it != members.end(); ++it) {
+ CDiscordUser *pUser = PrepareUser(*it);
+ pUser->lastMessageId = _wtoi64(pRoot["last_message_id"].as_mstring());
pUser->channelId = _wtoi64(pRoot["id"].as_mstring());
setId(pUser->hContact, DB_KEY_CHANNELID, pUser->channelId);
}
@@ -73,9 +75,9 @@ void CDiscordProto::OnCommandChannelCreated(const JSONNode &pRoot)
void CDiscordProto::OnCommandChannelDeleted(const JSONNode &pRoot)
{
- CDiscordUser *pUser = FindUserByChannel(pRoot["channel_id"]);
+ CDiscordUser *pUser = FindUserByChannel(_wtoi64(pRoot["id"].as_mstring()));
if (pUser != NULL) {
- pUser->channelId = 0;
+ pUser->channelId = pUser->lastMessageId = 0;
delSetting(pUser->hContact, DB_KEY_CHANNELID);
}
}
diff --git a/protocols/Discord/src/proto.cpp b/protocols/Discord/src/proto.cpp
index 946a356441..9b4a6fd0ad 100644
--- a/protocols/Discord/src/proto.cpp
+++ b/protocols/Discord/src/proto.cpp
@@ -347,9 +347,23 @@ int CDiscordProto::SendMsg(MCONTACT hContact, int /*flags*/, const char *pszSrc)
return 0;
CDiscordUser *pUser = FindUser(getId(hContact, DB_KEY_ID));
- if (pUser == NULL || pUser->channelId == NULL)
+ if (pUser == NULL || pUser->id == 0)
return 0;
+ // no channel - we need to create one
+ if (pUser->channelId == 0) {
+ JSONNode list(JSON_ARRAY); list.set_name("recipients"); list << INT64_PARAM("", pUser->id);
+ JSONNode body; body << list;
+ CMStringA szUrl(FORMAT, "/users/%lld/channels", m_ownId);
+
+ // theoretically we get the same data from the gateway thread, but there could be a delay
+ // so we bind data analysis to the http packet reply
+ mir_cslock lck(m_csHttpQueue);
+ ExecuteRequest(new AsyncHttpRequest(this, REQUEST_POST, szUrl, &CDiscordProto::OnReceiveCreateChannel, &body));
+ if (pUser->channelId == 0)
+ return 0;
+ }
+
// we generate a random 64-bit integer and pass it to the server
// to distinguish our own messages from these generated by another clients
SnowFlake nonce; Utils_GetRandom(&nonce, sizeof(nonce));
diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h
index f4ec8055bc..f0b76a2e27 100644
--- a/protocols/Discord/src/proto.h
+++ b/protocols/Discord/src/proto.h
@@ -254,6 +254,7 @@ public:
int OnDeleteContact(MCONTACT hContact);
+ void OnReceiveCreateChannel(NETLIBHTTPREQUEST*, AsyncHttpRequest*);
void OnReceiveAuth(NETLIBHTTPREQUEST*, AsyncHttpRequest*);
void OnReceiveChannels(NETLIBHTTPREQUEST*, AsyncHttpRequest*);
void OnReceiveFriends(NETLIBHTTPREQUEST*, AsyncHttpRequest*);
diff --git a/protocols/Discord/src/server.cpp b/protocols/Discord/src/server.cpp
index e8cc6a752f..a818622d67 100644
--- a/protocols/Discord/src/server.cpp
+++ b/protocols/Discord/src/server.cpp
@@ -203,6 +203,18 @@ void CDiscordProto::OnReceiveAuth(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *p
}
/////////////////////////////////////////////////////////////////////////////////////////
+// channels
+
+void CDiscordProto::OnReceiveCreateChannel(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*)
+{
+ if (pReply->resultCode != 200)
+ return;
+
+ JSONNode root = JSONNode::parse(pReply->pData);
+ if (root)
+ OnCommandChannelCreated(root);
+}
+
void CDiscordProto::OnReceiveChannels(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*)
{
@@ -214,9 +226,9 @@ void CDiscordProto::OnReceiveChannels(NETLIBHTTPREQUEST *pReply, AsyncHttpReques
return;
for (auto it = root.begin(); it != root.end(); ++it) {
- JSONNode &p = *it;
+ const JSONNode &p = *it;
- JSONNode &user = p["recipient"];
+ const JSONNode &user = p["recipient"];
if (!user)
continue;