Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Api {
constructor(baseUrl) {
this.baseUrl = baseUrl;
this.socket = null;
this.curerntUserId = null;
this.currentUserId = null;
this.responsesPromises = {};
this.onMessageListener = null;
this.onMessageStatusListener = null;
Expand Down Expand Up @@ -40,6 +40,12 @@ class Api {
);
return;
}
if (message.event.conversation_updated) {
this.onConversationCreateListener?.(
message.event.conversation_updated
);
return;
}
if (message.event.conversation_kicked) {
this.onConversationDeleteListener?.(
message.event.conversation_kicked
Expand Down Expand Up @@ -67,7 +73,7 @@ class Api {
if (this.onMessageListener) {
this.onMessageListener(message.message);
}
if (message.message.from.toString() !== this.curerntUserId) {
if (message.message.from !== this.currentUserId) {
EventEmitter.emit("onMessage", message.message);
}
return;
Expand Down Expand Up @@ -158,6 +164,7 @@ class Api {
login: data.ulogin,
password: data.pass,
deviceId: getBrowserFingerprint(true),
application_id: 6783
},
id: getUniqueId("userLogin"),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function ParticipantInChatInfo({
if (isCurrentUser) {
return;
}
navigate(pathname + hash + `/participant?uid=${user._id}`);
navigate(pathname + hash + `/participant?uid=${user.native_id}`);
}}
>
{user.login}
Expand Down
2 changes: 1 addition & 1 deletion src/components/generic/messageComponents/ChatMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function ChatMessage({
return (
<div
className={
fromId === userId.toString() ? "message my-message" : "message"
fromId === userId ? "message my-message" : "message"
}
>
{isNextMessageYours ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function InformativeMessage({
: "informative-message"
}
onClick={() =>
navigate(pathname + hash + `/participant?uid=${params?.user?._id}`)
navigate(pathname + hash + `/participant?uid=${params?.user?.native_id}`)
}
>
{text}
Expand Down
2 changes: 1 addition & 1 deletion src/components/screens/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function Login() {

const { token: userToken, user: userData } = await api.userLogin(data);
localStorage.setItem("sessionId", userToken);
api.curerntUserId = userData._id;
api.currentUserId = userData.native_id;

navigate("/main");
subscribeForNotifications();
Expand Down
6 changes: 3 additions & 3 deletions src/components/screens/chat/ChatList.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default function ChatList() {
? jwtDecode(localStorage.getItem("sessionId"))
: null;
const currentUser = useMemo(
() => (userInfo ? participants[userInfo._id] : {}),
() => (userInfo ? participants[userInfo.native_id] : {}),
[userInfo, participants]
);

Expand Down Expand Up @@ -93,7 +93,7 @@ export default function ChatList() {
const list = [];
for (const obj of conversations) {
const chatName = !obj.name
? obj.owner_id === userInfo?._id
? obj.owner_id === userInfo?.native_id
? participants[obj.opponent_id]?.login
: participants[obj.owner_id]?.login
: obj.name;
Expand All @@ -117,7 +117,7 @@ export default function ChatList() {
countOfNewMessages={obj.unread_messages_count}
chatType={obj.type}
lastMessage={obj.last_message}
uId={userInfo?._id}
uId={userInfo?.native_id}
/>
</NavLink>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/screens/chat/MessagesList.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export default function MessagesList({ scrollRef }) {
<ChatMessage
key={msg._id}
fromId={msg.from}
userId={userInfo._id}
userId={userInfo.native_id}
text={msg.body}
uName={participants[msg.from]?.login}
isPrevMesssageYours={
Expand Down
10 changes: 5 additions & 5 deletions src/components/screens/chat/chatFormBlocks/ChatFormInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default function ChatFormInfo({ closeForm }) {

return (
<p>
{getParticipantName(owner_id === userInfo._id ? opponent_id : owner_id)}
{getParticipantName(owner_id === userInfo.native_id ? opponent_id : owner_id)}
</p>
);
}, [selectedConversation, participants]);
Expand All @@ -64,9 +64,9 @@ export default function ChatFormInfo({ closeForm }) {
return null;
}

return conv.owner_id === userInfo._id
? participants[conv.opponent_id]?._id
: participants[conv.owner_id]?._id;
return conv.owner_id === userInfo.native_id
? participants[conv.opponent_id]?.native_id
: participants[conv.owner_id]?.native_id;
}, [selectedCID, participants]);

const opponentLastActivity = participants[opponentId]?.recent_activity;
Expand Down Expand Up @@ -102,7 +102,7 @@ export default function ChatFormInfo({ closeForm }) {
`/main/#${selectedCID}${
selectedConversation.type === "g"
? "/info"
: "/opponentinfo?uid=" + participants[opponentId]._id
: "/opponentinfo?uid=" + participants[opponentId].native_id
}`
)
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/screens/chat/chatFormBlocks/ChatFormInputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ export default function ChatFormInputs({
}
setIsSendMessageDisable(true);
messageInputEl.current.value = "";
const mid = userInfo._id + Date.now();
const mid = `${userInfo.native_id}${Date.now()}`;
let msg = {
_id: mid,
body: text,
from: userInfo._id,
from: userInfo.native_id,
t: Date.now(),
attachments: files.map((file) => ({
file_id: file.name,
Expand Down Expand Up @@ -114,7 +114,7 @@ export default function ChatFormInputs({
msg = {
_id: response.server_mid,
body: text,
from: userInfo._id,
from: userInfo.native_id,
status: "sent",
t: response.t,
attachments: attachments.map((obj, i) => ({
Expand Down
8 changes: 4 additions & 4 deletions src/components/screens/info/ChatInfoPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function ChatInfoPage() {
if (!userInfo || !selectedConversation) {
return false;
}
return userInfo._id === selectedConversation.owner_id?.toString();
return userInfo.native_id === selectedConversation.owner_id;
}, [userInfo, selectedConversation]);

window.onkeydown = function (event) {
Expand Down Expand Up @@ -71,7 +71,7 @@ export default function ChatInfoPage() {
return null;
}

const isCurrentUser = u._id === userInfo._id;
const isCurrentUser = u.native_id === userInfo.native_id;

const deleteUser = async (event) => {
event.stopPropagation();
Expand All @@ -82,15 +82,15 @@ export default function ChatInfoPage() {

const requestData = {
cid: selectedCID,
participants: { remove: [u._id] },
participants: { remove: [u.native_id] },
};
await api.conversationUpdate(requestData);
//remove user form participants field - redux
};

return (
<ParticipantInChatInfo
key={u._id}
key={`${u.native_id}`}
user={u}
deleteUserFunc={deleteUser}
isCurrentUser={isCurrentUser}
Expand Down
2 changes: 1 addition & 1 deletion src/components/screens/info/UserProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function UserProfile() {
: null;

const currentUser = useMemo(
() => (userInfo ? participants[userInfo._id] : {}),
() => (userInfo ? participants[userInfo.native_id] : {}),
[participants, userInfo]
);

Expand Down
16 changes: 8 additions & 8 deletions src/components/screens/info/UserSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default function UserSearch({ type }) {
if (selectedUsers.length) {
const requestData = {
type: selectedUsers.length > 1 ? "g" : "u", //fix it in future
participants: selectedUsers.map((el) => el._id),
participants: selectedUsers.map((el) => el.native_id),
};
selectedUsers.length > 1 &&
(requestData["name"] = window.prompt("Please enter a chat name."));
Expand All @@ -75,7 +75,7 @@ export default function UserSearch({ type }) {
event.preventDefault();

if (selectedUsers.length) {
const addUsersArr = selectedUsers.map((el) => el._id);
const addUsersArr = selectedUsers.map((el) => el.native_id);
const requestData = {
cid: selectedCID,
participants: { add: addUsersArr },
Expand Down Expand Up @@ -110,12 +110,12 @@ export default function UserSearch({ type }) {
}

setSelectedUsers([...selectedUsers, data]);
setIgnoreIds([...ignoreIds, data._id]);
setSearchedUsers(searchedUsers.filter((el) => el._id !== data._id));
setIgnoreIds([...ignoreIds, data.native_id]);
setSearchedUsers(searchedUsers.filter((el) => el.native_id !== data.native_id));
};
const removeUserToIgnore = async (data) => {
setSelectedUsers(selectedUsers.filter((el) => el._id !== data._id));
setIgnoreIds(ignoreIds.filter((id) => id !== data._id));
setSelectedUsers(selectedUsers.filter((el) => el.native_id !== data.native_id));
setIgnoreIds(ignoreIds.filter((id) => id !== data.native_id));
setSearchedUsers([...searchedUsers, data]);
};

Expand Down Expand Up @@ -172,7 +172,7 @@ export default function UserSearch({ type }) {
{selectedUsers.length
? selectedUsers.map((d) => (
<SelectedUser
key={d._id + "-selected"}
key={`${d.native_id}-selected`}
onClick={() => removeUserToIgnore(d)}
uLogin={d.login}
/>
Expand All @@ -183,7 +183,7 @@ export default function UserSearch({ type }) {
{searchedUsers.length ? (
searchedUsers.map((d) => (
<SearchedUser
key={d._id}
key={`${d.native_id}`}
onClick={() => addUserToIgnore(d)}
uLogin={d.login}
/>
Expand Down
11 changes: 8 additions & 3 deletions src/services/activityService.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ class ActivityService {
constructor() {
api.onUserActivityListener = (user) => {
const uId = Object.keys(user)[0];
store.dispatch(upsertUser({ _id: uId, recent_activity: user[uId] }));
store.dispatch(upsertUser({ native_id: +uId, recent_activity: user[uId] }));
};

store.subscribe(() => {
const { conversations, participants, selectedConversation } =
store.getState();
const selectedConversationId = selectedConversation.value.id;

if (!selectedConversationId) {
this.currentChatId = null
return
}

if (
!conversations.entities[selectedConversationId]?.created_at ||
!participants.ids.length ||
Expand Down Expand Up @@ -48,7 +53,7 @@ class ActivityService {
}

const uId =
this.activeChat.owner_id === userInfo._id
this.activeChat.owner_id === userInfo.native_id
? this.activeChat.opponent_id
: this.activeChat.owner_id;

Expand All @@ -59,7 +64,7 @@ class ActivityService {
api.subscribeToUserActivity(uId).then((activity) => {
store.dispatch(
upsertUser({
_id: uId,
native_id: +uId,
recent_activity: activity[uId],
})
);
Expand Down
2 changes: 1 addition & 1 deletion src/services/autoLoginService.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AutoLoginService {

if (userToken && userToken !== "undefined") {
localStorage.setItem("sessionId", userToken);
api.curerntUserId = userData._id;
api.currentUserId = userData.native_id;

subscribeForNotifications();
store.dispatch(upsertUser(userData));
Expand Down
2 changes: 1 addition & 1 deletion src/services/conversationsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ConversationsService {
...chat,
unread_messages_count: chat.unread_messages_count || 0,
messagesIds: [],
participants: users.map((u) => u._id),
participants: users.map((u) => u.native_id),
})
);
store.dispatch(addUsers(users));
Expand Down
10 changes: 5 additions & 5 deletions src/services/messagesService.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ class MessagesService {
}

const userInfo = jwtDecode(localStorage.getItem("sessionId"));
message.from === userInfo._id && (message["status"] = "sent");
message.from === userInfo.native_id && (message["status"] = "sent");
store.dispatch(addMessage(message));

let countOfNewMessages = 0;
message.cid === this.currentChatId
? api.markConversationAsRead({ cid: this.currentChatId })
: (countOfNewMessages = message.from === userInfo._id ? 0 : 1);
: (countOfNewMessages = message.from === userInfo.native_id ? 0 : 1);
store.dispatch(
updateLastMessageField({
cid: message.cid,
Expand All @@ -66,7 +66,7 @@ class MessagesService {
store.dispatch(
upsertChat({
_id: message.cid,
participants: [...conv.participants, user._id],
participants: [...conv.participants, user.native_id],
})
);
}
Expand All @@ -75,7 +75,7 @@ class MessagesService {
store.dispatch(
upsertChat({
_id: message.cid,
participants: conv.participants.filter((uId) => uId !== user._id),
participants: conv.participants.filter((uId) => uId !== user.native_id),
})
);
}
Expand Down Expand Up @@ -154,7 +154,7 @@ class MessagesService {
store.dispatch(
upsertParticipants({
cid: this.currentChatId,
participants: arr.map((obj) => obj._id),
participants: arr.map((obj) => obj.native_id),
})
)
);
Expand Down
4 changes: 2 additions & 2 deletions src/store/Participants.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createEntityAdapter, createSlice } from "@reduxjs/toolkit";

export const participantsAdapter = createEntityAdapter({
selectId: ({ _id }) => _id,
sortComparer: (a, b) => a._id.localeCompare(b._id),
selectId: ({ _id, native_id }) => native_id,
sortComparer: (a, b) => typeof a.native_id === 'string' ? a.native_id.localeCompare(b.native_id) : a.native_id - b.native_id,
});

export const {
Expand Down