diff options
Diffstat (limited to 'protocols/Teams/src/teams_profile.cpp')
-rw-r--r-- | protocols/Teams/src/teams_profile.cpp | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/protocols/Teams/src/teams_profile.cpp b/protocols/Teams/src/teams_profile.cpp new file mode 100644 index 0000000000..2402467c51 --- /dev/null +++ b/protocols/Teams/src/teams_profile.cpp @@ -0,0 +1,153 @@ +/* +Copyright (c) 2025 Miranda NG team (https://miranda-ng.org) + +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 version 2 +of the License. + +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/>. +*/ + +#include "stdafx.h" + +void CTeamsProto::UpdateProfileDisplayName(const JSONNode &root, MCONTACT hContact) +{ + ptrW firstname(getWStringA(hContact, "FirstName")); + ptrW lastname(getWStringA(hContact, "LastName")); + if (firstname) { + CMStringW nick(firstname); + if (lastname) + nick.AppendFormat(L" %s", lastname.get()); + setWString(hContact, "Nick", nick); + } + else if (lastname) + setWString(hContact, "Nick", lastname); + else { + const JSONNode &node = root["displayname"]; + SetString(hContact, "Nick", (node) ? node : root["username"]); + } +} + +void CTeamsProto::UpdateProfileGender(const JSONNode &root, MCONTACT hContact) +{ + CMStringW gender = root["gender"].as_mstring(); + if (!gender.IsEmpty() && gender != "null") + setByte(hContact, "Gender", (uint8_t)(_wtoi(gender) == 1 ? 'M' : 'F')); + else + delSetting(hContact, "Gender"); +} + +void CTeamsProto::UpdateProfileBirthday(const JSONNode &root, MCONTACT hContact) +{ + CMStringW birthday = root["birthday"].as_mstring(); + if (!birthday.IsEmpty() && birthday != "null") { + int d, m, y; + if (3 == swscanf(birthday.GetBuffer(), L"%d-%d-%d", &y, &m, &d)) { + Contact::SetBirthday(hContact, d, m, y); + return; + } + } + + delSetting(hContact, "BirthYear"); + delSetting(hContact, "BirthDay"); + delSetting(hContact, "BirthMonth"); +} + +void CTeamsProto::UpdateProfileCountry(const JSONNode &root, MCONTACT hContact) +{ + std::string isocode = root["country"].as_string(); + if (!isocode.empty() && isocode != "null") { + char *country = (char *)CallService(MS_UTILS_GETCOUNTRYBYISOCODE, (WPARAM)isocode.c_str(), 0); + setString(hContact, "Country", country); + } + else delSetting(hContact, "Country"); +} + +void CTeamsProto::UpdateProfileEmails(const JSONNode &root, MCONTACT hContact) +{ + const JSONNode &node = root["emails"]; + if (node) { + const JSONNode &items = node.as_array(); + for (int i = 0; i < min(items.size(), 3); i++) { + const JSONNode &item = items.at(i); + if (!item) + break; + + CMStringA name(FORMAT, "e-mail%d", i); + setWString(hContact, name, item.as_mstring()); + } + } + else { + delSetting(hContact, "e-mail0"); + delSetting(hContact, "e-mail1"); + delSetting(hContact, "e-mail2"); + } +} + +void CTeamsProto::UpdateProfileAvatar(const JSONNode &root, MCONTACT hContact) +{ + CMStringW szUrl = root["avatarUrl"].as_mstring(); + if (!szUrl.IsEmpty() && szUrl != "null") { + SetAvatarUrl(hContact, szUrl); + ReloadAvatarInfo(hContact); + } +} + +///////////////////////////////////////////////////////////////////////////////////////// + +void CTeamsProto::OnGetProfileInfo(MHttpResponse *response, AsyncHttpRequest *pRequest) +{ + MCONTACT hContact = (DWORD_PTR)pRequest->pUserInfo; + + TeamsReply reply(response); + if (reply.error()) { + ProtoBroadcastAck(hContact, ACKTYPE_GETINFO, ACKRESULT_FAILED, 0); + return; + } + + auto &root = reply.data(); + std::string username = root["username"].as_string(); + if (username.empty()) { + ProtoBroadcastAck(hContact, ACKTYPE_GETINFO, ACKRESULT_FAILED, 0); + return; + } + + if (m_szSkypename != username.c_str()) + m_szMyname = username.c_str(); + else + m_szMyname = m_szSkypename; + + SetString(hContact, "City", root["city"]); + SetString(hContact, "About", root["about"]); + SetString(hContact, "Phone", root["phone"]); + SetString(hContact, "State", root["province"]); + SetString(hContact, "Cellular", root["phoneMobile"]); + SetString(hContact, "Homepage", root["homepage"]); + SetString(hContact, "LastName", root["lastname"]); + SetString(hContact, "FirstName", root["firstname"]); + SetString(hContact, "CompanyPhone", root["phoneOffice"]); + + UpdateProfileDisplayName(root, hContact); + UpdateProfileGender(root, hContact); + UpdateProfileBirthday(root, hContact); + UpdateProfileCountry(root, hContact); + UpdateProfileEmails(root, hContact); + UpdateProfileAvatar(root, hContact); + + ProtoBroadcastAck(hContact, ACKTYPE_GETINFO, ACKRESULT_SUCCESS, 0); +} + +void CTeamsProto::GetProfileInfo(MCONTACT hContact) +{ + auto *pReq = new AsyncHttpRequest(REQUEST_GET, HOST_API, 0, &CTeamsProto::OnGetProfileInfo); + pReq->m_szUrl.AppendFormat("/users/%s/profile", (hContact == 0) ? "self" : mir_urlEncode(getId(hContact))); + pReq->pUserInfo = (void *)hContact; + PushRequest(pReq); +} |