2
0
mirror of https://github.com/ars3niy/tdlib-purple synced 2025-08-31 14:05:10 +00:00

Set correct "who" for file transfers, and verify in tests

This commit is contained in:
Arseniy Lartsev
2020-10-04 18:57:14 +02:00
parent b3baa548f6
commit 089913eb26
11 changed files with 55 additions and 35 deletions

View File

@@ -129,7 +129,7 @@ struct TgMessageInfo {
Other
};
Type type;
std::string sender;
std::string incomingGroupchatSender;
time_t timestamp;
bool outgoing;
bool sentLocally = false; // For outgoing messages, whether sent by this very client

View File

@@ -443,7 +443,7 @@ static void showMessageTextChat(TdAccountData &account, const td::td_api::chat &
} else {
if (purpleId != 0)
serv_got_chat_in(purple_account_get_connection(account.purpleAccount), purpleId,
message.sender.empty() ? "someone" : message.sender.c_str(),
message.incomingGroupchatSender.empty() ? "someone" : message.incomingGroupchatSender.c_str(),
flags, text, message.timestamp);
}
}
@@ -608,8 +608,8 @@ std::string makeBasicDisplayName(const td::td_api::user &user)
return result;
}
std::string getSenderPurpleName(const td::td_api::chat &chat, const td::td_api::message &message,
TdAccountData &account)
std::string getIncomingGroupchatSenderPurpleName(const td::td_api::chat &chat, const td::td_api::message &message,
TdAccountData &account)
{
if (!message.is_outgoing_ && (getBasicGroupId(chat).valid() || getSupergroupId(chat).valid())) {
UserId senderId = getSenderUserId(message);
@@ -959,7 +959,7 @@ std::string getSenderDisplayName(const td::td_api::chat &chat, const TgMessageIn
else if (isPrivateChat(chat))
return chat.title_;
else
return message.sender;
return message.incomingGroupchatSender;
}
std::string makeNoticeWithSender(const td::td_api::chat &chat, const TgMessageInfo &message,

View File

@@ -25,7 +25,7 @@ void updateBasicGroupChat(TdAccountData &account, BasicGroupId gr
void updateSupergroupChat(TdAccountData &account, SupergroupId groupId);
void removeGroupChat(PurpleAccount *purpleAccount, const td::td_api::chat &chat);
std::string makeBasicDisplayName(const td::td_api::user &user);
std::string getSenderPurpleName(const td::td_api::chat &chat, const td::td_api::message &message,
std::string getIncomingGroupchatSenderPurpleName(const td::td_api::chat &chat, const td::td_api::message &message,
TdAccountData &account);
std::string getForwardSource(const td::td_api::messageForwardInfo &forwardInfo,
TdAccountData &accountData);

View File

@@ -147,6 +147,19 @@ static void cancelDownload(PurpleXfer *xfer)
}
}
static std::string getDownloadXferPeerName(ChatId chatId,
const TgMessageInfo &message,
TdAccountData &account)
{
const td::td_api::chat *chat = account.getChat(chatId);
if (chat) {
const td::td_api::user *privateUser = account.getUserByPrivateChat(*chat);
if (privateUser)
return getPurpleBuddyName(*privateUser);
}
return message.incomingGroupchatSender;
}
void startInlineDownloadProgress(DownloadRequest &request, TdTransceiver &transceiver, TdAccountData &account)
{
purple_debug_misc(config::pluginId, "Tracking download progress of file id %d: downloaded %d/%d\n",
@@ -158,7 +171,8 @@ void startInlineDownloadProgress(DownloadRequest &request, TdTransceiver &transc
return;
request.tempFileName = tempFileName;
PurpleXfer *xfer = purple_xfer_new (account.purpleAccount, PURPLE_XFER_RECEIVE, request.message.sender.c_str());
std::string who = getDownloadXferPeerName(request.chatId, request.message, account);
PurpleXfer *xfer = purple_xfer_new (account.purpleAccount, PURPLE_XFER_RECEIVE, who.c_str());
purple_xfer_set_init_fnc(xfer, nop);
purple_xfer_set_cancel_recv_fnc(xfer, nop);
purple_xfer_set_filename(xfer, request.fileDescription.c_str());
@@ -420,10 +434,11 @@ static void startStandardDownload(PurpleXfer *xfer)
}
}
void requestStandardDownload(const TgMessageInfo &message, const std::string &fileName,
void requestStandardDownload(ChatId chatId, const TgMessageInfo &message, const std::string &fileName,
const td::td_api::file &file, TdTransceiver &transceiver, TdAccountData &account)
{
PurpleXfer *xfer = purple_xfer_new (account.purpleAccount, PURPLE_XFER_RECEIVE, message.sender.c_str());
std::string who = getDownloadXferPeerName(chatId, message, account);
PurpleXfer *xfer = purple_xfer_new (account.purpleAccount, PURPLE_XFER_RECEIVE, who.c_str());
purple_xfer_set_init_fnc(xfer, startStandardDownload);
purple_xfer_set_cancel_recv_fnc(xfer, cancelDownload);
purple_xfer_set_filename(xfer, fileName.c_str());

View File

@@ -21,7 +21,7 @@ void updateFileTransferProgress(const td::td_api::file &file, TdTransceiver &tra
TdAccountData &account, TdTransceiver::ResponseCb sendMessageResponse);
void finishInlineDownloadProgress(DownloadRequest &downloadReq, TdAccountData &account);
void requestStandardDownload(const TgMessageInfo &message, const std::string &fileName,
void requestStandardDownload(ChatId chatId, const TgMessageInfo &message, const std::string &fileName,
const td::td_api::file &file, TdTransceiver &transceiver, TdAccountData &account);
std::string getDownloadPath(const td::td_api::Object *downloadResponse);

View File

@@ -1128,7 +1128,7 @@ void PurpleTdClient::showFileMessage(const td::td_api::chat &chat, TgMessageInfo
showFileInline(chat, message, *file, captionStr, fileDescription, nullptr,
&PurpleTdClient::showDownloadedFileInline);
} else
requestStandardDownload(message, fileName, *file, m_transceiver, m_data);
requestStandardDownload(getId(chat), message, fileName, *file, m_transceiver, m_data);
}
}
@@ -1255,7 +1255,7 @@ void PurpleTdClient::showMessage(const td::td_api::chat &chat, td::td_api::messa
TgMessageInfo messageInfo;
messageInfo.type = TgMessageInfo::Type::Other;
messageInfo.sender = getSenderPurpleName(chat, message, m_data);
messageInfo.incomingGroupchatSender = getIncomingGroupchatSenderPurpleName(chat, message, m_data);
messageInfo.timestamp = message.date_;
messageInfo.outgoing = message.is_outgoing_;
messageInfo.sentLocally = (message.sending_state_ != nullptr);

View File

@@ -216,7 +216,7 @@ TEST_F(FileTransferTest, SendFile_ErrorInUploadResponse)
setFakeFileSize(PATH, 9000);
pluginInfo().send_file(connection, purpleUserName(0).c_str(), PATH);
prpl.verifyEvents(XferAcceptedEvent(PATH));
prpl.verifyEvents(XferAcceptedEvent(purpleUserName(0), PATH));
tgl.verifyRequest(uploadFile(
make_object<inputFileLocal>(PATH),
make_object<fileTypeDocument>(),
@@ -235,7 +235,7 @@ TEST_F(FileTransferTest, SendFile_SendMessageResponseError)
setFakeFileSize(PATH, 9000);
pluginInfo().send_file(connection, purpleUserName(0).c_str(), PATH);
prpl.verifyEvents(XferAcceptedEvent(PATH));
prpl.verifyEvents(XferAcceptedEvent(purpleUserName(0), PATH));
tgl.verifyRequest(uploadFile(
make_object<inputFileLocal>(PATH),
make_object<fileTypeDocument>(),
@@ -312,7 +312,7 @@ TEST_F(FileTransferTest, SendFile_UnknownUser)
setFakeFileSize(PATH, 9000);
pluginInfo().send_file(connection, "Antonie van Leeuwenhoek", PATH);
prpl.verifyEvents(
XferAcceptedEvent(PATH),
XferAcceptedEvent("Antonie van Leeuwenhoek", PATH),
XferLocalCancelEvent(PATH)
);
}
@@ -449,7 +449,7 @@ TEST_F(FileTransferTest, Photo_DownloadProgress_StuckAtStart)
tgl.runTimeouts();
std::string tempFileName;
prpl.verifyEvents(
XferAcceptedEvent(&tempFileName)
XferAcceptedEvent(purpleUserName(0), &tempFileName)
);
tgl.update(make_object<updateFile>(make_object<file>(
@@ -548,7 +548,7 @@ TEST_F(FileTransferTest, Photo_DownloadProgress)
tgl.runTimeouts();
std::string tempFileName;
prpl.verifyEvents(
XferAcceptedEvent(&tempFileName),
XferAcceptedEvent(purpleUserName(0), &tempFileName),
XferStartEvent(&tempFileName)
);
@@ -632,7 +632,7 @@ TEST_F(FileTransferTest, Photo_DownloadProgress_StuckAtStart_Cancel)
tgl.runTimeouts();
std::string tempFileName;
prpl.verifyEvents(
XferAcceptedEvent(&tempFileName)
XferAcceptedEvent(purpleUserName(0), &tempFileName)
);
purple_xfer_cancel_local(prpl.getLastXfer());
@@ -699,7 +699,7 @@ TEST_F(FileTransferTest, Photo_DownloadProgress_Cancel)
tgl.runTimeouts();
std::string tempFileName;
prpl.verifyEvents(
XferAcceptedEvent(&tempFileName),
XferAcceptedEvent(purpleUserName(0), &tempFileName),
XferStartEvent(&tempFileName)
);
@@ -786,7 +786,7 @@ TEST_F(FileTransferTest, SendFileToNonContact)
PATH
);
prpl.verifyEvents(
XferAcceptedEvent(PATH)
XferAcceptedEvent(userFirstNames[0] + " " + userLastNames[0], PATH)
);
tgl.verifyRequest(createPrivateChat(userIds[0], false));
@@ -867,7 +867,7 @@ TEST_F(FileTransferTest, SendFileToNonContact_CreatePrivateChatFail)
PATH
);
prpl.verifyEvents(
XferAcceptedEvent(PATH)
XferAcceptedEvent(userFirstNames[1] + " " + userLastNames[1], PATH)
);
tgl.verifyRequest(createPrivateChat(userIds[1], false));
@@ -891,7 +891,7 @@ TEST_F(FileTransferTest, SendFileToNonContact_TurboCancel)
PATH
);
prpl.verifyEvents(
XferAcceptedEvent(PATH)
XferAcceptedEvent(userFirstNames[1] + " " + userLastNames[1], PATH)
);
tgl.verifyRequest(createPrivateChat(userIds[1], false));
@@ -942,12 +942,12 @@ TEST_F(FileTransferTest, ReceiveDocument_StandardTransfer_TinyFile)
true
));
prpl.verifyEvents(
XferRequestEvent(PURPLE_XFER_RECEIVE, "doc.file.name")
XferRequestEvent(PURPLE_XFER_RECEIVE, purpleUserName(0).c_str(), "doc.file.name")
);
purple_xfer_request_accepted(prpl.getLastXfer(), outputFileName);
prpl.verifyEvents(
XferAcceptedEvent(outputFileName),
XferAcceptedEvent(purpleUserName(0), outputFileName),
XferStartEvent(outputFileName)
);
@@ -1015,12 +1015,12 @@ TEST_F(FileTransferTest, ReceiveDocument_StandardTransfer_Progress)
true
));
prpl.verifyEvents(
XferRequestEvent(PURPLE_XFER_RECEIVE, "doc.file.name")
XferRequestEvent(PURPLE_XFER_RECEIVE, purpleUserName(0).c_str(), "doc.file.name")
);
purple_xfer_request_accepted(prpl.getLastXfer(), outputFileName);
prpl.verifyEvents(
XferAcceptedEvent(outputFileName),
XferAcceptedEvent(purpleUserName(0), outputFileName),
XferStartEvent(outputFileName)
);

View File

@@ -1381,7 +1381,7 @@ TEST_F(GroupChatTest, SendFile)
setFakeFileSize(PATH, 10000);
pluginInfo().chat_send_file(connection, purpleChatId, PATH);
prpl.verifyEvents(XferAcceptedEvent(PATH));
prpl.verifyEvents(XferAcceptedEvent(groupChatTitle, PATH));
tgl.verifyRequest(uploadFile(
make_object<inputFileLocal>(PATH),
make_object<fileTypeDocument>(),

View File

@@ -899,7 +899,7 @@ void purple_xfer_unref(PurpleXfer *xfer)
void purple_xfer_request(PurpleXfer *xfer)
{
EVENT(XferRequestEvent, purple_xfer_get_type(xfer), purple_xfer_get_filename(xfer), xfer);
EVENT(XferRequestEvent, purple_xfer_get_type(xfer), xfer->who, purple_xfer_get_filename(xfer), xfer);
}
std::map<std::string, size_t> fakeFiles;

View File

@@ -220,6 +220,8 @@ static void compare(const ChatSetTopicEvent &actual, const ChatSetTopicEvent &ex
static void compare(const XferAcceptedEvent &actual, const XferAcceptedEvent &expected)
{
COMPARE(who);
if (!expected.filename.empty()) {
COMPARE(filename);
}
@@ -273,6 +275,7 @@ static void compare(const XferRemoteCancelEvent &actual, const XferRemoteCancelE
static void compare(const XferRequestEvent &actual, const XferRequestEvent &expected)
{
COMPARE(type);
COMPARE(who);
COMPARE(filename);
}

View File

@@ -436,17 +436,18 @@ struct ChatSetTopicEvent: PurpleEvent {
struct XferAcceptedEvent: PurpleEvent {
PurpleXfer *xfer = NULL;
std::string who;
std::string filename;
std::string *getFileName = NULL;
XferAcceptedEvent(PurpleXfer *xfer, const std::string &filename)
: PurpleEvent(PurpleEventType::XferAccepted), xfer(xfer), filename(filename) {}
: PurpleEvent(PurpleEventType::XferAccepted), xfer(xfer), who(xfer->who), filename(filename) {}
XferAcceptedEvent(const std::string &filename)
: PurpleEvent(PurpleEventType::XferAccepted), filename(filename) {}
XferAcceptedEvent(const std::string &who, const std::string &filename)
: PurpleEvent(PurpleEventType::XferAccepted), who(who), filename(filename) {}
XferAcceptedEvent(std::string *getFileName)
: PurpleEvent(PurpleEventType::XferAccepted), getFileName(getFileName) {}
XferAcceptedEvent(const std::string &who, std::string *getFileName)
: PurpleEvent(PurpleEventType::XferAccepted), who(who), getFileName(getFileName) {}
};
struct XferStartEvent: PurpleEvent {
@@ -501,10 +502,11 @@ struct XferRemoteCancelEvent: PurpleEvent {
struct XferRequestEvent: PurpleEvent {
PurpleXfer *xfer;
PurpleXferType type;
std::string who;
std::string filename;
XferRequestEvent(PurpleXferType type, const char *filename, PurpleXfer *xfer = NULL)
: PurpleEvent(PurpleEventType::XferRequest), xfer(xfer), type(type),
XferRequestEvent(PurpleXferType type, const char *who, const char *filename, PurpleXfer *xfer = NULL)
: PurpleEvent(PurpleEventType::XferRequest), xfer(xfer), type(type), who(who),
filename(filename ? filename : "") {}
};