Skip to content

Commit

Permalink
Merge branch 'dev' into feature/store-read-unread-inbox-states
Browse files Browse the repository at this point in the history
  • Loading branch information
budnik9 committed Mar 4, 2024
2 parents 9e9bbb0 + ca5b2d0 commit 78117d6
Show file tree
Hide file tree
Showing 13 changed files with 296 additions and 258 deletions.
32 changes: 20 additions & 12 deletions src/pages/App/handlers/WebViewLoginHandler/WebViewLoginHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,28 @@ const WebViewLoginHandler: FC = () => {
const dispatch = useDispatch();

const handleWebviewLogin = React.useCallback(async (event) => {
const data = parseJson(event.data) as FirebaseCredentials;
const user = await firebase.auth().currentUser;
try {
window.ReactNativeWebView.postMessage("toast-receive-data");
const data = parseJson(event.data) as FirebaseCredentials;
window.ReactNativeWebView.postMessage("toast-parse-data");
const user = await firebase.auth().currentUser;
window.ReactNativeWebView.postMessage("toast-get-user");

if (data?.redirectUrl) {
history.push(data?.redirectUrl);
}
if (data?.redirectUrl) {
window.ReactNativeWebView.postMessage("toast-redirect");
history.push(data?.redirectUrl);
}

if (!data?.providerId) {
return;
}
if (!data?.providerId) {
window.ReactNativeWebView.postMessage("toast-failed-provider");
return;
}

if (user) {
window.ReactNativeWebView.postMessage(WebviewActions.loginSuccess);
}
if (user) {
window.ReactNativeWebView.postMessage(WebviewActions.loginSuccess);
}

try {
window.ReactNativeWebView.postMessage("toast-call-weblogin");
dispatch(
webviewLogin.request({
payload: data,
Expand All @@ -40,11 +46,13 @@ const WebViewLoginHandler: FC = () => {
if (isDarkThemePreferred) {
window.ReactNativeWebView.postMessage(Theme.Dark);
}
window.ReactNativeWebView.postMessage("toast-loggedIn");
window.ReactNativeWebView.postMessage(
WebviewActions.loginSuccess,
);
history.push(getInboxPagePath());
} else {
window.ReactNativeWebView.postMessage("toast-loginError");
window.ReactNativeWebView.postMessage(WebviewActions.loginError);
}
},
Expand Down
12 changes: 12 additions & 0 deletions src/pages/Auth/store/saga.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,15 @@ const authorizeUser = async ({
const authorizeUserViaCredentials = async (
data: FirebaseCredentials,
): Promise<firebase.User | null> => {
window.ReactNativeWebView.postMessage("toast-authorizeUserViaCredentials");
let credential;
if (data.customToken) {
window.ReactNativeWebView.postMessage("toast-customToken");
const { user } = await firebase
.auth()
.signInWithCustomToken(data.customToken);

window.ReactNativeWebView.postMessage("toast-return user");
return user;
} else if (data.providerId === AuthProviderID.Apple) {
const provider = new firebase.auth.OAuthProvider(data.providerId);
Expand Down Expand Up @@ -374,27 +377,36 @@ function* webviewLoginSaga({
}: ReturnType<typeof actions.webviewLogin.request>) {
try {
yield put(actions.startAuthLoading());
window.ReactNativeWebView.postMessage("toast-start-auth");

const loggedFirebaseUser = yield call(
authorizeUserViaCredentials,
payload.payload,
);

window.ReactNativeWebView.postMessage("toast-loggedFirebaseUser");

const { user }: { user: User } = yield call(
verifyLoggedInUser,
loggedFirebaseUser,
true,
AUTH_CODE_FOR_SIGN_UP,
);

window.ReactNativeWebView.postMessage("toast-verifyLoggedInUser");
const firebaseUser: User = yield call(getUserData, user.uid ?? "");
window.ReactNativeWebView.postMessage("toast-firebaseUser");
if (firebaseUser) {
window.ReactNativeWebView.postMessage("toast-firebaseUser-success");
yield put(actions.webviewLogin.success(firebaseUser));
}

if (payload.callback) {
window.ReactNativeWebView.postMessage("toast-firebaseUser-callback");
payload.callback(true);
}
} catch (error) {
window.ReactNativeWebView.postMessage("toast-webviewLoginSaga-error");
if (isError(error)) {
yield put(actions.webviewLogin.failure(error));
}
Expand Down
4 changes: 3 additions & 1 deletion src/pages/common/components/ChatComponent/ChatComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,9 @@ export default function ChatComponent({
});
} else {
pendingMessages.forEach((pendingMessage) => {
discussionMessagesData.addDiscussionMessage(pendingMessage);
discussionMessagesData.addDiscussionMessage(pendingMessage, {
showPlainText: true,
});
});
}

Expand Down
95 changes: 41 additions & 54 deletions src/services/User.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
import { stringify } from "query-string";
import { store } from "@/shared/appConfig";
import { ApiEndpoint } from "@/shared/constants";
import { UnsubscribeFunction } from "@/shared/interfaces";
import { GetInboxResponse, UpdateUserDto } from "@/shared/interfaces/api";
import {
ChatChannel,
FeedLayoutItemWithFollowData,
UnsubscribeFunction,
} from "@/shared/interfaces";
import { UpdateUserDto } from "@/shared/interfaces/api";
import {
Collection,
CommonFeed,
FeedItemFollowWithMetadata,
InboxItem,
SubCollections,
Timestamp,
User,
} from "@/shared/models";
import {
convertObjectDatesToFirestoreTimestamps,
convertToTimestamp,
firestoreDataConverter,
transformFirebaseDataList,
} from "@/shared/utils";
import firebase from "@/shared/utils/firebase";
import * as cacheActions from "@/store/states/cache/actions";
import Api from "./Api";
import { waitForUserToBeLoaded } from "./utils";
import { addMetadataToItemsBatch, waitForUserToBeLoaded } from "./utils";

const converter = firestoreDataConverter<User>();
const inboxConverter = firestoreDataConverter<InboxItem>();
Expand Down Expand Up @@ -162,66 +159,56 @@ class UserService {
});
};

public getInboxItemsWithMetadata = async (
options: {
startAfter?: Timestamp | null;
limit?: number;
unread?: boolean;
} = {},
): Promise<{
data: GetInboxResponse["data"]["inboxWithMetadata"];
public getInboxItemsWithMetadata = async (options: {
userId: string;
startAfter?: Timestamp | null;
limit?: number;
unread?: boolean;
}): Promise<{
data: FeedLayoutItemWithFollowData[];
firstDocTimestamp: Timestamp | null;
lastDocTimestamp: Timestamp | null;
hasMore: boolean;
}> => {
const { startAfter, limit = 10 } = options;
const queryParams: Record<string, unknown> = {
limit,
};
const { userId, startAfter, limit = 10, unread } = options;
let query = this.getInboxSubCollection(userId).orderBy(
"itemUpdatedAt",
"desc",
);

if (unread) {
query = query.where("unread", "==", true);
}
if (startAfter) {
queryParams.startAfter = startAfter.toDate().toISOString();
query = query.startAfter(startAfter);
}

if (options.unread) {
queryParams.unread = true;
if (limit) {
query = query.limit(limit);
}

const {
data: { data },
} = await Api.get<GetInboxResponse>(
`${ApiEndpoint.GetInbox}?${stringify(queryParams)}`,
);
const inboxItems: GetInboxResponse["data"]["inboxWithMetadata"] = {
chatChannels: data.inboxWithMetadata.chatChannels.map((item) =>
convertObjectDatesToFirestoreTimestamps<ChatChannel>(item, [
"lastMessage.createdAt",
]),
),
feedItemFollows: data.inboxWithMetadata.feedItemFollows.map((item) =>
convertObjectDatesToFirestoreTimestamps<FeedItemFollowWithMetadata>(
{
...item,
feedItem: convertObjectDatesToFirestoreTimestamps<CommonFeed>(
item.feedItem,
),
const snapshot = await query.get();
const inboxItems = snapshot.docs.map((doc) => doc.data());
const inboxItemsWithMetadata = (
await addMetadataToItemsBatch(
userId,
inboxItems.map((item) => ({
item,
statuses: {
isAdded: false,
isRemoved: false,
},
["lastSeen", "lastActivity"],
),
),
};
const firstDocTimestamp =
(data.firstDocTimestamp && convertToTimestamp(data.firstDocTimestamp)) ||
null;
})),
)
).map(({ item }) => item);
const firstDocTimestamp = inboxItems[0]?.itemUpdatedAt || null;
const lastDocTimestamp =
(data.lastDocTimestamp && convertToTimestamp(data.lastDocTimestamp)) ||
null;
inboxItems[inboxItems.length - 1]?.itemUpdatedAt || null;

return {
data: inboxItems,
data: inboxItemsWithMetadata,
firstDocTimestamp,
lastDocTimestamp,
hasMore: data.hasMore,
hasMore: inboxItems.length === limit,
};
};

Expand Down
Loading

0 comments on commit 78117d6

Please sign in to comment.