From e4bfcdf054857966a9daf7114a84b1911ffbea47 Mon Sep 17 00:00:00 2001 From: noumantahir Date: Mon, 29 Apr 2024 18:53:00 +0500 Subject: [PATCH] passing access token as param to announcement query if available --- src/providers/ecency/ecency.ts | 7 +++++-- src/providers/queries/announcementsQueries.ts | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/providers/ecency/ecency.ts b/src/providers/ecency/ecency.ts index 22cce1a9a9..fef6c3de8d 100644 --- a/src/providers/ecency/ecency.ts +++ b/src/providers/ecency/ecency.ts @@ -964,9 +964,12 @@ export const getCommentHistory = async ( } }; -export const getAnnouncements = async () => { +export const getAnnouncements = async (accessToken: string) => { try { - const res = await ecencyApi.get('/private-api/announcements'); + + const params = accessToken ? { code:accessToken } : null + + const res = await ecencyApi.get('/private-api/announcements', { params }); console.log('announcements fetcehd', res.data); if (!res.data) { throw new Error('No announcements found!'); diff --git a/src/providers/queries/announcementsQueries.ts b/src/providers/queries/announcementsQueries.ts index 0ae76836a4..9c947948e9 100644 --- a/src/providers/queries/announcementsQueries.ts +++ b/src/providers/queries/announcementsQueries.ts @@ -11,6 +11,8 @@ import { handleDeepLink, showActionModal } from '../../redux/actions/uiAction'; import { getPostUrl } from '../../utils/post'; import { delay } from '../../utils/editor'; import parseVersionNumber from '../../utils/parseVersionNumber'; +import { decryptKey } from '../../utils/crypto'; +import { getDigitPinCode } from '../hive/dhive'; const PROMPT_AGAIN_INTERVAL = 48 * 3600 * 1000; // 2 days @@ -18,6 +20,7 @@ export const useAnnouncementsQuery = () => { const intl = useIntl(); const dispatch = useDispatch(); + const pinHash = useAppSelector((state) => state.application.pin); const lastAppVersion = useAppSelector((state) => state.application.lastAppVersion); const appVersion = useMemo(() => VersionNumber.appVersion, []) @@ -25,7 +28,12 @@ export const useAnnouncementsQuery = () => { const currentAccount = useAppSelector((state) => state.account.currentAccount); const announcementsMeta = useAppSelector((state) => state.cache.announcementsMeta); - const announcmentsQuery = useQuery([QUERIES.ANNOUNCEMENTS.GET], getAnnouncements); + + const announcmentsQuery = useQuery([QUERIES.ANNOUNCEMENTS.GET], () => { + const encToken = currentAccount?.local?.accessToken; + const token = !!encToken && decryptKey(encToken, getDigitPinCode(pinHash)); + return getAnnouncements(token) + }); useEffect(() => { @@ -38,8 +46,8 @@ export const useAnnouncementsQuery = () => { } //bypass if logged in user is required for announcement, skip otherwise - const firstAnnounce = announcmentsQuery.data[0]; - if (firstAnnounce.auth && !currentAccount?.username) { + const firstAnnounce = announcementsMeta.data && announcmentsQuery.data[0]; + if (!firstAnnounce || (firstAnnounce?.auth && !currentAccount?.username)) { return; } @@ -101,3 +109,4 @@ export const useAnnouncementsQuery = () => { ); }; }; +