Skip to content

Commit 850135a

Browse files
committed
fix: working however with partly firebase namespaced api (--no-verify)
1 parent 66ecff0 commit 850135a

File tree

3 files changed

+96
-122
lines changed

3 files changed

+96
-122
lines changed

src/components/ChatListProvider.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,8 @@ class ChatListProvider extends React.Component<Props, State> {
5454
this.chatListenerRef = userChatsAllRef(firebaseDBRef, uid);
5555
// .limitToLast(5) // TODO pagination
5656

57-
const orderByChildRef = orderByChild("lastMessageCreatedAt");
58-
const queryRef = query(this.chatListenerRef,
59-
// orderByChildRef
60-
);
6157
onValue(
62-
queryRef,
58+
query(this.chatListenerRef, orderByChild("lastMessageCreatedAt")),
6359
(chatsSnapshot) => {
6460
// TODO resolve any
6561
const chatsMetaValues: CollectionObject<any> = chatsSnapshot.val();

src/components/ChatWindowProvider.tsx

Lines changed: 56 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import type {
3030

3131
type Props = {
3232
firebaseDBRef: DatabaseReference;
33+
oldFirebaseDBRef: Object | any;
3334
component: Object;
3435
};
3536

@@ -57,7 +58,8 @@ const emptyFunc = () => null;
5758
const ChatProviderWrapper = (
5859
firebaseDB: DatabaseReference,
5960
ComposedComponent: React.ComponentType<any>,
60-
packageCount: number = MESSAGE_PACKAGE_COUNT
61+
packageCount: number = MESSAGE_PACKAGE_COUNT,
62+
oldFirebaseDB: any
6163
) => {
6264
class ChatProvider extends React.Component<Props, State> {
6365
state = chatDefaultState;
@@ -75,20 +77,14 @@ const ChatProviderWrapper = (
7577
(chatId: string, newChat?: boolean) => {
7678
console.warn("chatListner", chatId);
7779
const { initialLoad } = this.state;
78-
const chatMessages = chatMessagesRef(firebaseDB, chatId);
79-
const orderByCreatedAt = orderByChild("createdAt");
80-
const limitToLastPackageCount = limitToLast(packageCount);
81-
const queryRef = query(
82-
chatMessages,
83-
// orderByCreatedAt,
84-
limitToLastPackageCount
85-
);
8680
// we want to fetch first N messages at once
8781
if (!newChat && initialLoad) {
88-
onValue(
89-
queryRef,
90-
(messagesSnap) => {
91-
console.warn("chatListner", 1, messagesSnap.val(), messagesSnap);
82+
oldFirebaseDB
83+
.child(`chat-messages/${chatId}`)
84+
.orderByChild("createdAt")
85+
.limitToLast(packageCount)
86+
.once("value")
87+
.then((messagesSnap) => {
9288
/*
9389
* Here we fetch first batch of the messages at once and process them for the chat
9490
* component. After that we push them to state, mark initial load as done and unsubsribe
@@ -115,44 +111,42 @@ const ChatProviderWrapper = (
115111
processedMessages.length - this.state.messagesCount,
116112
initialLoad: false,
117113
});
118-
console.warn("chatListner", 2);
119-
},
120-
// ensure to read data only once
121-
{
122-
onlyOnce: true,
123-
}
124-
);
114+
});
125115
} else {
126-
onChildAdded(queryRef, (messageSnippet) => {
127-
console.warn("chatListner", 3, messageSnippet.val());
128-
const message = listnerSingleMessageTransform(
129-
messageSnippet.key,
130-
participants
131-
)(messageSnippet.val());
132-
133-
// We have to check for duplicates since we get already fetched node as well after
134-
// initial load
135-
/* eslint-disable no-underscore-dangle */
136-
if (
137-
this.state.messages.some((m: Message) => m._id === message._id)
138-
) {
139-
console.warn("chatListner", 3.5);
140-
return;
141-
}
116+
oldFirebaseDB
117+
.child(`chat-messages/${chatId}`)
118+
.orderByChild("createdAt")
119+
.limitToLast(packageCount)
120+
.on("child_added", (messageSnippet) => {
121+
console.warn("chatListner", 3, messageSnippet.val());
122+
const message = listnerSingleMessageTransform(
123+
messageSnippet.key,
124+
participants
125+
)(messageSnippet.val());
126+
127+
// We have to check for duplicates since we get already fetched node as well after
128+
// initial load
129+
/* eslint-disable no-underscore-dangle */
130+
if (
131+
this.state.messages.some((m: Message) => m._id === message._id)
132+
) {
133+
console.warn("chatListner", 3.5);
134+
return;
135+
}
136+
137+
console.warn("chatListner", 4);
138+
const updatedMesseges = webMessageTransform
139+
? R.append(message, this.state.messages)
140+
: R.prepend(message, this.state.messages);
142141

143-
console.warn("chatListner", 4);
144-
const updatedMesseges = webMessageTransform
145-
? R.append(message, this.state.messages)
146-
: R.prepend(message, this.state.messages);
142+
this.setState({
143+
messages: updatedMesseges,
144+
messagesCount: this.state.messagesCount + 1,
145+
initialLoad: false,
146+
});
147147

148-
this.setState({
149-
messages: updatedMesseges,
150-
messagesCount: this.state.messagesCount + 1,
151-
initialLoad: false,
148+
console.warn("chatListner", 5, updatedMesseges);
152149
});
153-
154-
console.warn("chatListner", 5);
155-
});
156150
}
157151
};
158152

@@ -186,7 +180,7 @@ const ChatProviderWrapper = (
186180
});
187181

188182
toSendMessage({
189-
firebaseDB,
183+
firebaseDB: oldFirebaseDB,
190184
chatId: newChatId,
191185
userId: uid,
192186
messages,
@@ -219,7 +213,7 @@ const ChatProviderWrapper = (
219213
}) => {
220214
console.warn("onSend", chatId);
221215
toSendMessage({
222-
firebaseDB,
216+
firebaseDB: oldFirebaseDB,
223217
chatId,
224218
userId: uid,
225219
messages,
@@ -263,20 +257,14 @@ const ChatProviderWrapper = (
263257
// fetch last 5/10/15/20/...
264258

265259
this.setState({ isLoadingEarlier: true }, () => {
266-
const chatMessages = chatMessagesRef(firebaseDB, chatId);
267-
const orderByCreatedAt = orderByChild("createdAt");
268-
const limitToLastPackageCount = limitToLast(updatedMsgsCount);
269-
const queryRef = query(
270-
chatMessages,
271-
// orderByCreatedAt,
272-
limitToLastPackageCount
273-
);
260+
oldFirebaseDB
261+
.child(`chat-messages/${chatId}`)
262+
.orderByChild("createdAt")
263+
.limitToLast(updatedMsgsCount)
264+
.once("value") // TODO via on and unsubscription
265+
.then((chatMsgs) => {
266+
const messagesFromDB = chatMsgs.val();
274267

275-
onValue(
276-
queryRef,
277-
(chatMsgs) => {
278-
const messagesFromDB = chatMsgs.val() as CollectionObject<any>;
279-
console.warn("loadMoreMessages messagesFromDB", messagesFromDB);
280268
if (R.values(messagesFromDB).length > this.state.messages.length) {
281269
// add to message's id to other message's object
282270
chatMsgs.forEach((item) => {
@@ -300,12 +288,7 @@ const ChatProviderWrapper = (
300288
callBack || emptyFunc
301289
);
302290
}
303-
},
304-
// ensure to read data only once
305-
{
306-
onlyOnce: true,
307-
}
308-
);
291+
});
309292
});
310293
};
311294

@@ -316,7 +299,7 @@ const ChatProviderWrapper = (
316299
uid: string;
317300
prevMessages: Message[];
318301
}) => {
319-
console.warn("markMessagesRead", uid);
302+
console.warn("markMessagesRead", uid, prevMessages);
320303
const { messages } = this.state;
321304
const messagesIds = getMessagesIds(messages);
322305
const prevMessagesIds = getMessagesIds(prevMessages);
@@ -336,7 +319,7 @@ const ChatProviderWrapper = (
336319
{}
337320
);
338321

339-
update(firebaseDB, unreadMessagesToDeleteUpdate);
322+
oldFirebaseDB.update(unreadMessagesToDeleteUpdate);
340323
}
341324
};
342325

@@ -348,10 +331,7 @@ const ChatProviderWrapper = (
348331
theUserId: string;
349332
uid: string;
350333
eventId: string;
351-
}) => {
352-
console.warn("checkForChat", theUserId);
353-
return checkForChatExistence(firebaseDB, theUserId, uid, eventId);
354-
};
334+
}) => checkForChatExistence(firebaseDB, theUserId, uid, eventId);
355335

356336
getChatParticipantsDetails = async (
357337
participants: CollectionObject<true>
@@ -385,10 +365,8 @@ const ChatProviderWrapper = (
385365
return allChatsParticipants;
386366
};
387367

388-
getGroupChatsByEvent = (eventId: string) => {
389-
console.warn("getGroupChatsByEvent", eventId);
390-
return getGroupChatsByEvent(firebaseDB, eventId);
391-
};
368+
getGroupChatsByEvent = (eventId: string) =>
369+
getGroupChatsByEvent(oldFirebaseDB, eventId);
392370

393371
render() {
394372
return (

src/firebase/calls.ts

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const toSendMessage = ({
3131
meta,
3232
createNewChat,
3333
}: {
34-
firebaseDB: DatabaseReference;
34+
firebaseDB: any;
3535
chatId: string;
3636
userId: string;
3737
messages: Array<Message>;
@@ -120,7 +120,7 @@ export const toSendMessage = ({
120120
...chatMetaUpdate,
121121
};
122122
console.warn("toSendMessage entireUpdate", entireUpdate);
123-
update(firebaseDB, entireUpdate);
123+
firebaseDB.update(entireUpdate);
124124
};
125125

126126
export const createEmptyChat = (
@@ -190,31 +190,31 @@ export const checkForChatExistence = (
190190
new Promise((resolve) => {
191191
console.warn("checkForChatExistence", theUserId, uid, eventId);
192192
const childRef = child(firebaseDB, "chat-metadata");
193-
const orderByChildRef = orderByChild(`users/${theUserId}`);
194-
const startAtRef = startAt(true);
195-
const sortedRef = query(childRef,
196-
// orderByChildRef,
197-
startAtRef
198-
);
199-
200-
onValue(sortedRef, (snap) => {
201-
const chats = R.toPairs(snap.val());
202-
203-
const filtredChats = chats.filter((chat) => {
204-
const chatMetas = chat[1];
205-
const chatParticipants = R.keys(chatMetas.users);
206-
const ind = chatParticipants.indexOf(uid);
207-
208-
return (
209-
ind !== -1 && // chat exists
210-
chatParticipants.length === 2 && // chat is private
211-
eventId === chatMetas.eventId
212-
); // chat is in the event scope
213-
});
214-
215-
off(child(firebaseDB, "chat-metadata"));
216-
resolve(filtredChats);
217-
}, { onlyOnce: true });
193+
onValue(
194+
query(
195+
childRef,
196+
orderByChild(`users/${theUserId}`),
197+
startAt(true)
198+
),
199+
(snap) => {
200+
const chats = R.toPairs(snap.val());
201+
202+
const filtredChats = chats.filter((chat) => {
203+
const chatMetas = chat[1];
204+
const chatParticipants = R.keys(chatMetas.users);
205+
const ind = chatParticipants.indexOf(uid);
206+
207+
return (
208+
ind !== -1 && // chat exists
209+
chatParticipants.length === 2 && // chat is private
210+
eventId === chatMetas.eventId
211+
); // chat is in the event scope
212+
});
213+
214+
off(childRef);
215+
resolve(filtredChats);
216+
}
217+
);
218218
});
219219

220220
export const getGroupChatsByEvent = (
@@ -224,18 +224,18 @@ export const getGroupChatsByEvent = (
224224
new Promise((resolve) => {
225225
console.warn("getGroupChatsByEvent", eventId);
226226
const childRef = child(firebaseDB, "chat-metadata");
227-
const orderByChildRef = orderByChild("eventId");
228-
const equalToRef = equalTo(eventId);
229-
const queryRef = query(childRef,
230-
// orderByChildRef,
231-
equalToRef
232-
);
233-
234-
onValue(queryRef, (snap) => {
235-
const chats = snap.val();
236-
off(childRef);
237-
resolve(chats);
238-
});
227+
onValue(
228+
query(
229+
childRef,
230+
orderByChild("eventId"),
231+
equalTo(eventId)
232+
),
233+
(snap) => {
234+
const chats = snap.val();
235+
off(childRef);
236+
resolve(chats);
237+
}
238+
);
239239
});
240240

241241
export const getChatById = (

0 commit comments

Comments
 (0)