Skip to content

Commit

Permalink
add whatsapp support for editing messages
Browse files Browse the repository at this point in the history
  • Loading branch information
d99kris committed Sep 24, 2023
1 parent 5b28488 commit 0b2f5dc
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 24 deletions.
3 changes: 2 additions & 1 deletion lib/common/src/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ enum ProtocolFeature
FeatureNone = 0,
FeatureAutoGetChatsOnLogin = (1 << 0),
FeatureTypingTimeout = (1 << 1),
FeatureEditMessages = (1 << 2),
FeatureEditMessagesWithinTwoDays = (1 << 2),
FeatureEditMessagesWithinFifteenMins = (1 << 3),
};

class Protocol
Expand Down
2 changes: 1 addition & 1 deletion lib/common/src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

#pragma once

#define NCHAT_VERSION "3.72"
#define NCHAT_VERSION "3.73"
2 changes: 1 addition & 1 deletion lib/tgchat/src/tgchat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ std::string TgChat::Impl::GetProfileId() const

bool TgChat::Impl::HasFeature(ProtocolFeature p_ProtocolFeature) const
{
static int customFeatures = FeatureTypingTimeout | FeatureEditMessages;
static int customFeatures = FeatureTypingTimeout | FeatureEditMessagesWithinTwoDays;
return (p_ProtocolFeature & customFeatures);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/wmchat/go/cgowm.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func CWmGetMessages(connId int, chatId *C.char, limit int, fromMsgId *C.char, ow
}

//export CWmSendMessage
func CWmSendMessage(connId int, chatId *C.char, text *C.char, quotedId *C.char, quotedText *C.char, quotedSender *C.char, filePath *C.char, fileType *C.char) int {
return WmSendMessage(connId, C.GoString(chatId), C.GoString(text), C.GoString(quotedId), C.GoString(quotedText), C.GoString(quotedSender), C.GoString(filePath), C.GoString(fileType))
func CWmSendMessage(connId int, chatId *C.char, text *C.char, quotedId *C.char, quotedText *C.char, quotedSender *C.char, filePath *C.char, fileType *C.char, editMsgId *C.char, editMsgSent int) int {
return WmSendMessage(connId, C.GoString(chatId), C.GoString(text), C.GoString(quotedId), C.GoString(quotedText), C.GoString(quotedSender), C.GoString(filePath), C.GoString(fileType), C.GoString(editMsgId), editMsgSent)
}

//export CWmGetStatus
Expand Down
39 changes: 26 additions & 13 deletions lib/wmchat/go/gowm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1559,7 +1559,7 @@ func WmGetMessages(connId int, chatId string, limit int, fromMsgId string, owner
return -1
}

func WmSendMessage(connId int, chatId string, text string, quotedId string, quotedText string, quotedSender string, filePath string, fileType string) int {
func WmSendMessage(connId int, chatId string, text string, quotedId string, quotedText string, quotedSender string, filePath string, fileType string, editMsgId string, editMsgSent int) int {

LOG_TRACE("send message " + strconv.Itoa(connId) + ", " + chatId + ", " + text)

Expand All @@ -1575,7 +1575,6 @@ func WmSendMessage(connId int, chatId string, text string, quotedId string, quot
// local vars
var sendErr error
var message waProto.Message
var timeStamp time.Time
var sendResponse whatsmeow.SendResponse

// recipient
Expand All @@ -1585,6 +1584,8 @@ func WmSendMessage(connId int, chatId string, text string, quotedId string, quot
return -1
}

isSend := false

// check message type
if len(filePath) == 0 {

Expand Down Expand Up @@ -1620,9 +1621,7 @@ func WmSendMessage(connId int, chatId string, text string, quotedId string, quot
message.Conversation = &text
}

// send message
sendResponse, sendErr = client.SendMessage(context.Background(), chatJid, &message)

isSend = true
} else {

mimeType := strings.Split(fileType, "/")[0] // image, text, application, etc.
Expand Down Expand Up @@ -1655,9 +1654,7 @@ func WmSendMessage(connId int, chatId string, text string, quotedId string, quot

message.ImageMessage = &imageMessage

// send message
sendResponse, sendErr = client.SendMessage(context.Background(), chatJid, &message)

isSend = true
} else {

LOG_TRACE("send document " + fileType)
Expand Down Expand Up @@ -1689,8 +1686,21 @@ func WmSendMessage(connId int, chatId string, text string, quotedId string, quot

message.DocumentMessage = &documentMessage

isSend = true
}
}

if isSend {

if len(editMsgId) > 0 {
// edit message
sendResponse, sendErr =
client.SendMessage(context.Background(), chatJid, client.BuildEdit(chatJid, editMsgId, &message))

} else {
// send message
sendResponse, sendErr = client.SendMessage(context.Background(), chatJid, &message)

}
}

Expand All @@ -1701,16 +1711,19 @@ func WmSendMessage(connId int, chatId string, text string, quotedId string, quot
} else {
LOG_TRACE(fmt.Sprintf("send message ok"))

timeStamp = sendResponse.Timestamp
msgId := sendResponse.ID

// messageInfo
var messageInfo types.MessageInfo
messageInfo.Chat = chatJid
messageInfo.ID = msgId
messageInfo.IsFromMe = true
messageInfo.Sender = *client.Store.ID
messageInfo.Timestamp = timeStamp

if len(editMsgId) > 0 {
messageInfo.ID = editMsgId
messageInfo.Timestamp = time.Unix(int64(editMsgSent), 0)
} else {
messageInfo.ID = sendResponse.ID
messageInfo.Timestamp = sendResponse.Timestamp
}

handler := GetHandler(connId)
handler.HandleMessage(messageInfo, &message, false)
Expand Down
39 changes: 37 additions & 2 deletions lib/wmchat/src/wmchat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ std::string WmChat::GetProfileId() const

bool WmChat::HasFeature(ProtocolFeature p_ProtocolFeature) const
{
ProtocolFeature customFeatures = FeatureNone;
ProtocolFeature customFeatures = FeatureEditMessagesWithinFifteenMins;
return (p_ProtocolFeature & customFeatures);
}

Expand Down Expand Up @@ -278,12 +278,14 @@ void WmChat::PerformRequest(std::shared_ptr<RequestMessage> p_RequestMessage)
std::shared_ptr<SendMessageRequest> sendMessageRequest =
std::static_pointer_cast<SendMessageRequest>(p_RequestMessage);
std::string chatId = sendMessageRequest->chatId;
std::string editMsgId = "";
std::string text = sendMessageRequest->chatMessage.text;
std::string quotedId = sendMessageRequest->chatMessage.quotedId;
std::string quotedText = sendMessageRequest->chatMessage.quotedText;
std::string quotedSender = sendMessageRequest->chatMessage.quotedSender;
std::string filePath;
std::string fileType;
int editMsgSent = 0;
if (!sendMessageRequest->chatMessage.fileInfo.empty())
{
FileInfo fileInfo =
Expand All @@ -296,7 +298,8 @@ void WmChat::PerformRequest(std::shared_ptr<RequestMessage> p_RequestMessage)
CWmSendMessage(m_ConnId, const_cast<char*>(chatId.c_str()), const_cast<char*>(text.c_str()),
const_cast<char*>(quotedId.c_str()), const_cast<char*>(quotedText.c_str()),
const_cast<char*>(quotedSender.c_str()), const_cast<char*>(filePath.c_str()),
const_cast<char*>(fileType.c_str()));
const_cast<char*>(fileType.c_str()), const_cast<char*>(editMsgId.c_str()),
editMsgSent);
Status::Clear(Status::FlagSending);

std::shared_ptr<SendMessageNotify> sendMessageNotify = std::make_shared<SendMessageNotify>(m_ProfileId);
Expand All @@ -307,6 +310,38 @@ void WmChat::PerformRequest(std::shared_ptr<RequestMessage> p_RequestMessage)
}
break;

case EditMessageRequestType:
{
LOG_DEBUG("edit message");
Status::Set(Status::FlagSending);
std::shared_ptr<EditMessageRequest> editMessageRequest =
std::static_pointer_cast<EditMessageRequest>(p_RequestMessage);
std::string chatId = editMessageRequest->chatId;
std::string editMsgId = editMessageRequest->msgId;
std::string text = editMessageRequest->chatMessage.text;
std::string quotedId = editMessageRequest->chatMessage.quotedId;
std::string quotedText = editMessageRequest->chatMessage.quotedText;
std::string quotedSender = editMessageRequest->chatMessage.quotedSender;
std::string filePath;
std::string fileType;
int editMsgSent = editMessageRequest->chatMessage.timeSent / 1000;
if (!editMessageRequest->chatMessage.fileInfo.empty())
{
FileInfo fileInfo =
ProtocolUtil::FileInfoFromHex(editMessageRequest->chatMessage.fileInfo);
filePath = fileInfo.filePath;
fileType = fileInfo.fileType;
}

CWmSendMessage(m_ConnId, const_cast<char*>(chatId.c_str()), const_cast<char*>(text.c_str()),
const_cast<char*>(quotedId.c_str()), const_cast<char*>(quotedText.c_str()),
const_cast<char*>(quotedSender.c_str()), const_cast<char*>(filePath.c_str()),
const_cast<char*>(fileType.c_str()), const_cast<char*>(editMsgId.c_str()),
editMsgSent);
Status::Clear(Status::FlagSending);
}
break;

case MarkMessageReadRequestType:
{
LOG_DEBUG("mark message read");
Expand Down
2 changes: 1 addition & 1 deletion src/nchat.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.\" DO NOT MODIFY THIS FILE! It was generated by help2man.
.TH NCHAT "1" "September 2023" "nchat v3.72" "User Commands"
.TH NCHAT "1" "September 2023" "nchat v3.73" "User Commands"
.SH NAME
nchat \- ncurses chat
.SH SYNOPSIS
Expand Down
20 changes: 17 additions & 3 deletions src/uimodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2677,7 +2677,8 @@ void UiModel::EditMessage()
if (!GetSelectMessageActive() || GetEditMessageActive()) return;

std::string profileId = m_CurrentChat.first;
if (!m_Protocols[profileId]->HasFeature(FeatureEditMessages))
if (!m_Protocols[profileId]->HasFeature(FeatureEditMessagesWithinTwoDays) &&
!m_Protocols[profileId]->HasFeature(FeatureEditMessagesWithinFifteenMins))
{
MessageDialog("Warning", "Protocol does not support editing.", 0.7, 5);
return;
Expand All @@ -2701,12 +2702,22 @@ void UiModel::EditMessage()
const time_t timeNow = time(NULL);
const time_t timeSent = (time_t)(chatMessage.timeSent / 1000);
const time_t messageAgeSec = timeNow - timeSent;
static const time_t maxEditAgeSec = 48 * 3600;
if (messageAgeSec >= maxEditAgeSec)
static const time_t twoDaysSec = 48 * 3600;
static const time_t fifteenMinsSec = 15 * 60;

if (m_Protocols[profileId]->HasFeature(FeatureEditMessagesWithinTwoDays) &&
(messageAgeSec >= twoDaysSec))
{
MessageDialog("Warning", "Messages older than 48 hours cannot be edited.", 0.8, 5);
return;
}
else if (m_Protocols[profileId]->HasFeature(FeatureEditMessagesWithinFifteenMins) &&
(messageAgeSec >= fifteenMinsSec))

{
MessageDialog("Warning", "Messages older than 15 minutes cannot be edited.", 0.8, 5);
return;
}

m_EditMessageId = messageId;
SetEditMessageActive(true);
Expand All @@ -2727,6 +2738,8 @@ void UiModel::SaveEditMessage()
std::string profileId = m_CurrentChat.first;
std::string chatId = m_CurrentChat.second;
std::wstring& entryStr = m_EntryStr[profileId][chatId];
const std::unordered_map<std::string, ChatMessage>& messages = m_Messages[profileId][chatId];
const ChatMessage& chatMessage = messages.at(m_EditMessageId);

if (entryStr.empty()) return;

Expand All @@ -2735,6 +2748,7 @@ void UiModel::SaveEditMessage()
editMessageRequest->chatId = chatId;
editMessageRequest->msgId = m_EditMessageId;
editMessageRequest->chatMessage.text = EntryStrToSendStr(entryStr);
editMessageRequest->chatMessage.timeSent = chatMessage.timeSent;
m_Protocols[profileId]->SendRequest(editMessageRequest);

SetEditMessageActive(false);
Expand Down

0 comments on commit 0b2f5dc

Please sign in to comment.