Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated firestore #421

Merged
merged 2 commits into from
Sep 15, 2024
Merged
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
10 changes: 9 additions & 1 deletion apps/acf-extension/src/background/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { initializeApp } from 'firebase/app';
import { getAuth, indexedDBLocalPersistence } from 'firebase/auth/web-extension';
import { connectAuthEmulator, getAuth, indexedDBLocalPersistence, signInWithEmailAndPassword } from 'firebase/auth/web-extension';
import { connectFirestoreEmulator, getFirestore } from 'firebase/firestore';
import { connectStorageEmulator, getStorage } from 'firebase/storage';
import { FIREBASE_API_KEY, FIREBASE_BUCKET, FIREBASE_DATABASE_URL, FIREBASE_PROJECT_ID } from '../common/environments';

const firebase = initializeApp({
Expand All @@ -11,4 +13,10 @@ const firebase = initializeApp({
firebase.automaticDataCollectionEnabled = false;
const auth = getAuth(firebase);
auth.setPersistence(indexedDBLocalPersistence);
if (process.env.IS_LOCAL === 'true') {
connectAuthEmulator(auth, 'http://localhost:9099');
connectFirestoreEmulator(getFirestore(auth.app), 'localhost', 8080);
connectStorageEmulator(getStorage(auth.app), 'localhost', 9199);
signInWithEmailAndPassword(auth, 'new@example.com', 'password123').then(console.log).catch(console.error);
}
export { auth };
23 changes: 12 additions & 11 deletions apps/acf-extension/src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,30 @@ import { MainWorldBackground, RUNTIME_MESSAGE_MAIN_WORLD_MESSAGING } from '@dhru
import { Runtime } from '@dhruv-techapps/core-extension';
import { DiscordMessagingBackground, RUNTIME_MESSAGE_DISCORD_MESSAGING } from '@dhruv-techapps/discord-messaging';
import { DiscordOauth2Background, RUNTIME_MESSAGE_DISCORD_OAUTH } from '@dhruv-techapps/discord-oauth';
import { FirebaseDatabaseBackground, RUNTIME_MESSAGE_FIREBASE_DATABASE } from '@dhruv-techapps/firebase-database';
import { FirebaseFirestoreBackground, RUNTIME_MESSAGE_FIREBASE_FIRESTORE } from '@dhruv-techapps/firebase-firestore';
import { FirebaseFunctionsBackground, RUNTIME_MESSAGE_FIREBASE_FUNCTIONS } from '@dhruv-techapps/firebase-functions';
import { FirebaseOauth2Background, RUNTIME_MESSAGE_FIREBASE_OAUTH } from '@dhruv-techapps/firebase-oauth';
import { RUNTIME_MESSAGE_GOOGLE_ANALYTICS } from '@dhruv-techapps/google-analytics';
import { GoogleDriveBackground, RUNTIME_MESSAGE_GOOGLE_DRIVE } from '@dhruv-techapps/google-drive';
import { GoogleOauth2Background, RUNTIME_MESSAGE_GOOGLE_OAUTH } from '@dhruv-techapps/google-oauth';
import { GoogleSheetsBackground, RUNTIME_MESSAGE_GOOGLE_SHEETS } from '@dhruv-techapps/google-sheets';
import { registerNotifications } from '@dhruv-techapps/notifications';
import XMLHttpRequest from 'xhr-shim';
import { ACTION_POPUP } from '../common/constant';
import { DISCORD_CLIENT_ID, EDGE_OAUTH_CLIENT_ID, OPTIONS_PAGE_URL, UNINSTALL_URL, VARIANT } from '../common/environments';
import { DISCORD_CLIENT_ID, EDGE_OAUTH_CLIENT_ID, FIREBASE_FUNCTIONS_URL, OPTIONS_PAGE_URL, UNINSTALL_URL, VARIANT } from '../common/environments';
import AcfBackup from './acf-backup';
import registerContextMenus from './context-menu';
import { auth } from './firebase';
import { googleAnalytics } from './google-analytics';
import './sync-config';
import { TabsMessenger } from './tab';
import { Update } from './update';
self['XMLHttpRequest'] = XMLHttpRequest;

let firebaseDatabaseBackground: FirebaseDatabaseBackground;
let firebaseFirestoreBackground: FirebaseFirestoreBackground;

try {
firebaseDatabaseBackground = new FirebaseDatabaseBackground(auth, EDGE_OAUTH_CLIENT_ID);
firebaseFirestoreBackground = new FirebaseFirestoreBackground(auth, EDGE_OAUTH_CLIENT_ID, OPTIONS_PAGE_URL);

/**
* Browser Action set to open option page / configuration page
Expand All @@ -46,7 +48,7 @@ try {

auth.onAuthStateChanged((user) => {
if (user) {
Update.discord(firebaseDatabaseBackground);
Update.discord(firebaseFirestoreBackground);
}
});

Expand Down Expand Up @@ -77,13 +79,12 @@ try {
[RUNTIME_MESSAGE_DISCORD_MESSAGING]: new DiscordMessagingBackground(auth, EDGE_OAUTH_CLIENT_ID, VARIANT),
[RUNTIME_MESSAGE_GOOGLE_ANALYTICS]: googleAnalytics,
[RUNTIME_MESSAGE_GOOGLE_OAUTH]: new GoogleOauth2Background(EDGE_OAUTH_CLIENT_ID),
[RUNTIME_MESSAGE_GOOGLE_DRIVE]: new GoogleDriveBackground(auth, EDGE_OAUTH_CLIENT_ID),
[RUNTIME_MESSAGE_ACF.ACF_BACKUP]: new AcfBackup(auth, EDGE_OAUTH_CLIENT_ID),
[RUNTIME_MESSAGE_GOOGLE_SHEETS]: new GoogleSheetsBackground(auth, EDGE_OAUTH_CLIENT_ID),
[RUNTIME_MESSAGE_FIREBASE_DATABASE]: firebaseDatabaseBackground,
[RUNTIME_MESSAGE_GOOGLE_DRIVE]: new GoogleDriveBackground(auth, FIREBASE_FUNCTIONS_URL, EDGE_OAUTH_CLIENT_ID),
[RUNTIME_MESSAGE_ACF.ACF_BACKUP]: new AcfBackup(auth, FIREBASE_FUNCTIONS_URL, EDGE_OAUTH_CLIENT_ID),
[RUNTIME_MESSAGE_GOOGLE_SHEETS]: new GoogleSheetsBackground(auth, FIREBASE_FUNCTIONS_URL, EDGE_OAUTH_CLIENT_ID),
[RUNTIME_MESSAGE_FIREBASE_OAUTH]: new FirebaseOauth2Background(auth, EDGE_OAUTH_CLIENT_ID),
//[RUNTIME_MESSAGE_FIREBASE_FIRESTORE]: new FirebaseFirestoreBackground(auth, EDGE_OAUTH_CLIENT_ID, OPTIONS_PAGE_URL),
[RUNTIME_MESSAGE_FIREBASE_FUNCTIONS]: new FirebaseFunctionsBackground(auth, EDGE_OAUTH_CLIENT_ID),
[RUNTIME_MESSAGE_FIREBASE_FIRESTORE]: new FirebaseFirestoreBackground(auth, EDGE_OAUTH_CLIENT_ID, OPTIONS_PAGE_URL),
[RUNTIME_MESSAGE_FIREBASE_FUNCTIONS]: new FirebaseFunctionsBackground(auth, FIREBASE_FUNCTIONS_URL, EDGE_OAUTH_CLIENT_ID),
};
Runtime.onMessageExternal(onMessageListener);
Runtime.onMessage(onMessageListener);
Expand Down
63 changes: 55 additions & 8 deletions apps/acf-extension/src/background/sync-config.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { Configuration, LOCAL_STORAGE_KEY } from '@dhruv-techapps/acf-common';
import { FirebaseDatabaseBackground, SYNC_ALL_CONFIG_ALARM, SYNC_CONFIG_ALARM } from '@dhruv-techapps/firebase-database';
import { ConfigRequest, FirebaseFirestoreBackground, SYNC_ALL_CONFIG_ALARM, SYNC_CONFIG_ALARM } from '@dhruv-techapps/firebase-firestore';
import { Auth } from '@dhruv-techapps/firebase-oauth';
import { FirebaseStorageBackground } from '@dhruv-techapps/firebase-storage';
import { EDGE_OAUTH_CLIENT_ID } from '../common/environments';
import { auth } from './firebase';
import { googleAnalytics } from './google-analytics';

export const EVENTS_REGEX = new RegExp(
'scrollto|clickevents|mouseevents|touchevents|formevents|keyevents|tabs|keyboardevents|attr|class|copy|paste|windowcommand|locationcommand|func|replace|append|prepend|clipboard',
'scrollto|clickevents|mouseevents|touchevents|formevents|keyevents|tabs|keyboardevents|attr|class|copy|paste|windowcommand|locationcommand|func|replace|append|prepend|clipboard|GoogleSheets',
'i'
);

export const VALUE_MATCHER = new RegExp('<batchRepeat>|<actionRepeat>|<sessionCount>|<random\\(([^)]+)\\)>|<queryParam::([^>]+)>|<api::([^>]+)>', 'i');

export class SyncConfig {
constructor(private auth: Auth) {}

Expand All @@ -22,6 +24,36 @@ export class SyncConfig {
}
}

maskStringWithAsterisks(input: string) {
return input
.split(' ') // Split string into words
.map((word) => '*'.repeat(word.length)) // Replace each word with asterisks of equal length
.join(' '); // Join words back with spaces
}

maskString(input: string) {
let result = '';
let lastIndex = 0;

// Use the value matcher to find matches
let match;
while ((match = VALUE_MATCHER.exec(input)) !== null) {
// Add * for the non-matching part before the current match, but preserve non-alphanumeric characters
result += input.slice(lastIndex, match.index).replace(/[a-zA-Z0-9]/g, '*');

// Add the matching part as is
result += match[0];

// Move the index to after the current match
lastIndex = match.index + match[0].length;
}

// Handle any part of the string after the last match
result += input.slice(lastIndex).replace(/[a-zA-Z0-9]/g, '*');

return result;
}

async reset() {
const storageResult = await chrome.storage.local.get(LOCAL_STORAGE_KEY.CONFIGS);
const configs: Array<Configuration> = storageResult[LOCAL_STORAGE_KEY.CONFIGS] || [];
Expand All @@ -38,13 +70,25 @@ export class SyncConfig {
config.actions.forEach((action, index, actions) => {
const { value } = action;

if (value && !EVENTS_REGEX.test(value)) {
delete action.value;
if (value) {
if (!EVENTS_REGEX.test(value)) {
if (VALUE_MATCHER.test(value)) {
action.value = this.maskString(value);
} else {
action.value = this.maskStringWithAsterisks(value);
}
}
}
if (action.addon) {
const { value } = action.addon;
if (value && !EVENTS_REGEX.test(value)) {
action.addon.value = '';
if (value) {
if (!EVENTS_REGEX.test(value)) {
if (VALUE_MATCHER.test(value)) {
action.addon.value = this.maskString(value);
} else {
action.addon.value = this.maskStringWithAsterisks(value);
}
}
}
}
actions[index] = action;
Expand All @@ -66,8 +110,11 @@ export class SyncConfig {
for (const config of configs) {
try {
// Update Database
const db = { url: config.url, name: config.name, userId: uid };
await new FirebaseDatabaseBackground(this.auth, EDGE_OAUTH_CLIENT_ID).setConfig(db, config.id);
const data: ConfigRequest = { url: config.url, userId: uid };
if (config.name) {
data.name = config.name;
}
await new FirebaseFirestoreBackground(this.auth, EDGE_OAUTH_CLIENT_ID).setConfig(data, config.id);
// Update Storage
const blob = this.getBlob(config);
await new FirebaseStorageBackground(this.auth).uploadFile(blob, `users/${uid}/${config.id}.json`);
Expand Down
6 changes: 3 additions & 3 deletions apps/acf-extension/src/background/update.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { FirebaseDatabaseBackground } from '@dhruv-techapps/firebase-database';
import { FirebaseFirestoreBackground } from '@dhruv-techapps/firebase-firestore';
export const NOTIFICATIONS_TITLE = 'Update';
export const NOTIFICATIONS_ID = 'update';

export class Update {
static async discord(firebaseDatabaseBackground: FirebaseDatabaseBackground) {
static async discord(firebaseFirestoreBackground: FirebaseFirestoreBackground) {
const { discord } = await chrome.storage.local.get('discord');
if (discord) {
firebaseDatabaseBackground.setDiscord(discord).then((response: any) => {
firebaseFirestoreBackground.setDiscord(discord).then((response: any) => {
if (!response?.error) chrome.storage.local.remove('discord');
});
}
Expand Down
1 change: 1 addition & 0 deletions apps/acf-extension/src/background/xhr-shim.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'xhr-shim';
16 changes: 15 additions & 1 deletion apps/acf-extension/src/common/environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,18 @@ const FIREBASE_API_KEY = process.env.NX_PUBLIC_FIREBASE_API_KEY;
const FIREBASE_DATABASE_URL = process.env.NX_PUBLIC_FIREBASE_DATABASE_URL;
const FIREBASE_PROJECT_ID = process.env.NX_PUBLIC_FIREBASE_PROJECT_ID;
const FIREBASE_BUCKET = process.env.NX_PUBLIC_FIREBASE_BUCKET;
export { API_SECRET, DISCORD_CLIENT_ID, EDGE_OAUTH_CLIENT_ID, FIREBASE_API_KEY, FIREBASE_BUCKET, FIREBASE_DATABASE_URL, FIREBASE_PROJECT_ID, MEASUREMENT_ID, OPTIONS_PAGE_URL, UNINSTALL_URL, VARIANT };
const FIREBASE_FUNCTIONS_URL = process.env.NX_PUBLIC_FIREBASE_FUNCTIONS_URL;
export {
API_SECRET,
DISCORD_CLIENT_ID,
EDGE_OAUTH_CLIENT_ID,
FIREBASE_API_KEY,
FIREBASE_BUCKET,
FIREBASE_DATABASE_URL,
FIREBASE_FUNCTIONS_URL,
FIREBASE_PROJECT_ID,
MEASUREMENT_ID,
OPTIONS_PAGE_URL,
UNINSTALL_URL,
VARIANT,
};
4 changes: 2 additions & 2 deletions apps/acf-options-page/src/app/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useTranslation } from 'react-i18next';
import { APP_LANGUAGES, APP_LINK } from '../constants';
import { useAppDispatch, useAppSelector } from '../hooks';
import { SettingsModal } from '../modal';
import { firebaseDatabaseSelector, firebaseSelector } from '../store/firebase';
import { firebaseFirestoreSelector, firebaseSelector } from '../store/firebase';
import { switchSettingsModal } from '../store/settings/settings.slice';
import { switchTheme, themeSelector } from '../store/theme.slice';
import { GearFill, Moon, Sun, ThreeDots } from '../util';
Expand All @@ -15,7 +15,7 @@ function Header() {
const handleClose = () => setShow(false);
const handleShow = () => setShow(true);
const theme = useAppSelector(themeSelector);
const { profile } = useAppSelector(firebaseDatabaseSelector);
const { profile } = useAppSelector(firebaseFirestoreSelector);
const { role } = useAppSelector(firebaseSelector);
const dispatch = useAppDispatch();
const { t, i18n } = useTranslation();
Expand Down
6 changes: 3 additions & 3 deletions apps/acf-options-page/src/app/header_google.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Nav, NavDropdown } from 'react-bootstrap';
import { useAppDispatch, useAppSelector } from '../hooks';
import { firebaseDatabaseSelector, firebaseLogoutAPI, firebaseSelector, switchFirebaseLoginModal } from '../store/firebase';
import { firebaseFirestoreSelector, firebaseLogoutAPI, firebaseSelector, switchFirebaseLoginModal } from '../store/firebase';
import { switchSubscribeModal } from '../store/subscribe';
import { LockFill } from '../util';
import { HeaderProfile } from './header_porfile';

export const HeaderGoogle = () => {
const dispatch = useAppDispatch();
const { user, role } = useAppSelector(firebaseSelector);
const { profile } = useAppSelector(firebaseDatabaseSelector);
const { profile } = useAppSelector(firebaseFirestoreSelector);

/*
const onPortalLink = async () => {
Expand Down Expand Up @@ -54,9 +54,9 @@ export const HeaderGoogle = () => {
Subscribe
</NavDropdown.Item>
<NavDropdown.Divider />
<HeaderProfile />
</>
)}
<HeaderProfile />
<NavDropdown.Item title='logout' onClick={() => dispatch(firebaseLogoutAPI())}>
Logout
</NavDropdown.Item>
Expand Down
4 changes: 2 additions & 2 deletions apps/acf-options-page/src/app/header_porfile.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { NavDropdown } from 'react-bootstrap';
import { useConfirmationModalContext } from '../_providers/confirm.provider';
import { useAppDispatch, useAppSelector } from '../hooks';
import { firebaseDatabaseSelector, profileSetAPI } from '../store/firebase';
import { firebaseFirestoreSelector, profileSetAPI } from '../store/firebase';
import { LockFill, UnLockFill } from '../util';

export const HeaderProfile = () => {
const dispatch = useAppDispatch();
const modalContext = useConfirmationModalContext();
const { profile } = useAppSelector(firebaseDatabaseSelector);
const { profile } = useAppSelector(firebaseFirestoreSelector);

const onSwitchProfile = async () => {
const result = await modalContext.showConfirmation({
Expand Down
2 changes: 1 addition & 1 deletion apps/acf-options-page/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export const APP_LINK = {
DOCS: 'https://getautoclicker.com/docs/4.x/',
BLOG: 'https://blog.getautoclicker.com/',
CONFIGS: 'https://gist.github.com/dharmesh-hemaram',
CONFIGS: 'https://configs.getautoclicker.com/',
TEST: 'https://test.getautoclicker.com/',
ISSUES: 'https://github.com/Dhruv-Techapps/acf-docs/issues',
DISCUSSIONS: 'https://github.com/Dhruv-Techapps/acf-docs/discussions',
Expand Down
4 changes: 2 additions & 2 deletions apps/acf-options-page/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { configureStore } from '@reduxjs/toolkit';
import appReducer from './store/app.slice';
import { blogReducer } from './store/blog';
import { configReducers, configsListenerMiddleware, configsToastListenerMiddleware } from './store/config';
import { firebaseDatabaseReducer, firebaseReducer } from './store/firebase';
import { firebaseFirestoreReducer, firebaseReducer } from './store/firebase';
import { googleDriveReducer, googleReducer } from './store/google';
import { settingsListenerMiddleware, settingsReducer } from './store/settings';
import { subscribeReducer } from './store/subscribe';
Expand All @@ -19,7 +19,7 @@ export const store = configureStore({
google: googleReducer,
googleDrive: googleDriveReducer,
firebase: firebaseReducer,
firebaseDatabase: firebaseDatabaseReducer,
firebaseFirestore: firebaseFirestoreReducer,
subscribe: subscribeReducer,
...configReducers,
},
Expand Down

This file was deleted.

2 changes: 0 additions & 2 deletions apps/acf-options-page/src/store/firebase/database/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { FirebaseFirestoreService } from '@dhruv-techapps/firebase-firestore';
import { createAsyncThunk } from '@reduxjs/toolkit';

export const profileGetAPI = createAsyncThunk('profile/get', async () => {
const result = await FirebaseFirestoreService.getProfile();
return result;
});

export const profileSetAPI = createAsyncThunk('profile/set', async (profile: boolean) => {
const result = await FirebaseFirestoreService.setProfile(profile);
return result;
});
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { RootState } from '@apps/acf-options-page/src/store';
import { createSlice } from '@reduxjs/toolkit';
import { profileGetAPI, profileSetAPI } from './firebase-database.api';
import { profileGetAPI, profileSetAPI } from './firebase-firestore.api';

type FirebaseDatabaseStore = {
type FirebaseFirestoreStore = {
profile: boolean;
};

const initialState: FirebaseDatabaseStore = { profile: false };
const initialState: FirebaseFirestoreStore = { profile: false };

const slice = createSlice({
name: 'firebaseDatabase',
name: 'firebaseFirestore',
initialState,
reducers: {},
extraReducers: (builder) => {
Expand All @@ -22,5 +22,5 @@ const slice = createSlice({
},
});

export const firebaseDatabaseSelector = (state: RootState) => state.firebaseDatabase;
export const firebaseDatabaseReducer = slice.reducer;
export const firebaseFirestoreSelector = (state: RootState) => state.firebaseFirestore;
export const firebaseFirestoreReducer = slice.reducer;
2 changes: 2 additions & 0 deletions apps/acf-options-page/src/store/firebase/firestore/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './firebase-firestore.api';
export * from './firebase-firestore.slice';
2 changes: 1 addition & 1 deletion apps/acf-options-page/src/store/firebase/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './database';
export * from './firebase-login.api';
export * from './firebase-login.slice';
export * from './firestore';
Loading