Skip to content

Commit

Permalink
fixed edit
Browse files Browse the repository at this point in the history
  • Loading branch information
dendidibe committed Oct 4, 2023
1 parent ca4d422 commit 3eed158
Showing 1 changed file with 27 additions and 67 deletions.
94 changes: 27 additions & 67 deletions client-web/src/xmppHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class XmppHandler {
if (stanza.attrs.id === "sendMessage") {
const body = stanza?.getChild("body");
const data = stanza?.getChild("data");
const replace = stanza?.getChild("replace");
const replace = stanza?.getChild("replaced");
const archived = stanza?.getChild("archived");
const id = stanza.getChild("archived")?.attrs.id;
if (!data || !body || !id) {
Expand All @@ -63,14 +63,6 @@ export class XmppHandler {
return;
}

if (replace) {
const replaceMessageId = Number(replace.attrs.id);
const messageString = body.getText();
useStoreState
.getState()
.replaceMessage(replaceMessageId, messageString);
}

if (data.attrs.isReply) {
const messageId = Number(msg.data?.mainMessage?.id);
useStoreState.getState().setNumberOfReplies(messageId);
Expand Down Expand Up @@ -107,7 +99,6 @@ export class XmppHandler {
stanza.is("message") &&
stanza.children[0].attrs.xmlns === "urn:xmpp:mam:2"
) {
console.log(stanza.toString(), 'historyMessage')
const body = stanza
.getChild("result")
?.getChild("forwarded")
Expand All @@ -126,7 +117,7 @@ export class XmppHandler {
.getChild("result")
?.getChild("forwarded")
?.getChild("message")
?.getChild("replace");
?.getChild("replaced");

const id = stanza.getChild("result")?.attrs.id;
if (!data || !body || !delay || !id) {
Expand All @@ -142,7 +133,7 @@ export class XmppHandler {
}

const msg = createMessage(data, body, id, stanza.attrs.from);

msg.data.isEdited = !!replace;
// console.log('TEST ', data.attrs)
const blackList = useStoreState
.getState()
Expand All @@ -151,57 +142,32 @@ export class XmppHandler {
if (blackList) {
return;
}
//if current stanza has replace tag
if (replace) {
//if message loading
if (this.isGettingMessages) {
const replaceItem: replaceMessageListItemProps = {
replaceMessageId: Number(replace.attrs.id),
replaceMessageText: body.getText(),
};
//add the replace item, which has the id of the main message to be edited, in a temporory array
this.temporaryReplaceMessages.push(replaceItem);
}
//if message loading done
if (!this.isGettingMessages) {
const replaceMessageId = Number(replace.attrs.id);
const messageString = body.getText();
//replace body/text of message id in messageHistory array
useStoreState
.getState()
.replaceMessage(replaceMessageId, messageString);
}
} else {
this.temporaryMessages.push(msg);

if (!this.isGettingMessages) {
//check for messages in temp Replace message array agains the current stanza message id
const replaceItem = this.temporaryReplaceMessages.find(
(item) => item.replaceMessageId === msg.id
);
//if exists then replace the body with current stanza body
if (replaceItem) {
msg.body = replaceItem.replaceMessageText;
}
useStoreState.getState().setNewMessageHistory(msg);
useStoreState.getState().sortMessageHistory();
if (!this.isGettingMessages) {
//check for messages in temp Replace message array agains the current stanza message id
const replaceItem = this.temporaryReplaceMessages.find(
(item) => item.replaceMessageId === msg.id
);
//if exists then replace the body with current stanza body
if (replaceItem) {
msg.body = replaceItem.replaceMessageText;
}
useStoreState.getState().setNewMessageHistory(msg);
useStoreState.getState().sortMessageHistory();
}

const untrackedRoom = useStoreState.getState().currentUntrackedChatRoom;
if (
stanza.attrs.to.split("@")[0] !==
data.attrs.senderJID.split("@")[0] &&
stanza.attrs.from.split("@")[0] !== untrackedRoom.split("@")[0] &&
!this.isGettingFirstMessages &&
data.attrs.roomJid
) {
// useStoreState.getState().updateCounterChatRoom(data.attrs.roomJid);
}
if (data.attrs.isReply) {
const messageid = msg.data.mainMessage?.id;
useStoreState.getState().setNumberOfReplies(messageid);
this.updateTemporaryMessagesRepliesCount(messageid);
}
const untrackedRoom = useStoreState.getState().currentUntrackedChatRoom;
if (
stanza.attrs.to.split("@")[0] !== data.attrs.senderJID.split("@")[0] &&
stanza.attrs.from.split("@")[0] !== untrackedRoom.split("@")[0] &&
!this.isGettingFirstMessages &&
data.attrs.roomJid
) {
// useStoreState.getState().updateCounterChatRoom(data.attrs.roomJid);
}
if (data.attrs.isReply) {
const messageid = msg.data.mainMessage?.id;
useStoreState.getState().setNumberOfReplies(messageid);
this.updateTemporaryMessagesRepliesCount(messageid);
}
}
};
Expand Down Expand Up @@ -249,11 +215,6 @@ export class XmppHandler {
}
});

this.temporaryReplaceMessages.forEach((item) => {
useStoreState
.getState()
.replaceMessage(item.replaceMessageId, item.replaceMessageText);
});
useStoreState.getState().setLoaderArchive(false);
this.temporaryMessages = [];
this.isGettingFirstMessages = false;
Expand Down Expand Up @@ -528,7 +489,6 @@ export class XmppHandler {
const replaceMessage: { text: string; id: string } = stanza.children.find(
(item) => item.name === "replace"
).attrs;
console.log(stanza.toString(), 'editMessage')
const messageString = replaceMessage?.text;
useStoreState
.getState()
Expand Down

0 comments on commit 3eed158

Please sign in to comment.