Skip to content

Commit

Permalink
Merge pull request #2658 from GetStream/develop
Browse files Browse the repository at this point in the history
Next Release
  • Loading branch information
khushal87 authored Sep 9, 2024
2 parents 2d596ed + 3a1b3c2 commit 5ee8382
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[![NPM](https://img.shields.io/npm/v/stream-chat-react-native.svg)](https://www.npmjs.com/package/stream-chat-react-native)
[![Build Status](https://github.com/GetStream/stream-chat-react-native/actions/workflows/release.yml/badge.svg)](https://github.com/GetStream/stream-chat-react-native/actions)
[![Component Reference](https://img.shields.io/badge/docs-component%20reference-blue.svg)](https://getstream.io/chat/docs/sdk/reactnative)
![JS Bundle Size](https://img.shields.io/badge/js_bundle_size-439%20KB-blue)
![JS Bundle Size](https://img.shields.io/badge/js_bundle_size-438.6728515625%20KB-blue)

<img align="right" src="https://getstream.imgix.net/images/ios-chat-tutorial/[email protected]?auto=format,enhance" width="50%" />

Expand Down
6 changes: 4 additions & 2 deletions examples/SampleApp/.bundle/config
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
BUNDLE_PATH: "vendor/bundle"
BUNDLE_FORCE_RUBY_PLATFORM: 1
---
BUNDLE_PATH: "/home/runner/work/stream-chat-react-native/stream-chat-react-native/examples/SampleApp/vendor/bundle"
BUNDLE_FORCE_RUBY_PLATFORM: "1"
BUNDLE_DEPLOYMENT: "true"
4 changes: 2 additions & 2 deletions examples/SampleApp/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ GEM
google-apis-firebaseappdistribution_v1 (~> 0.3.0)
google-apis-firebaseappdistribution_v1alpha (~> 0.2.0)
fastlane-plugin-load_json (0.0.1)
fastlane-plugin-stream_actions (0.3.65)
fastlane-plugin-stream_actions (0.3.67)
xctest_list (= 1.2.1)
ffi (1.17.0)
fourflusher (2.3.1)
Expand Down Expand Up @@ -320,7 +320,7 @@ DEPENDENCIES
fastlane
fastlane-plugin-firebase_app_distribution
fastlane-plugin-load_json
fastlane-plugin-stream_actions (= 0.3.65)
fastlane-plugin-stream_actions (= 0.3.67)
rubocop-performance
rubocop-require_tools

Expand Down
1 change: 1 addition & 0 deletions examples/SampleApp/fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ lane :update_img_shields_sdk_sizes do |options|
update_sdk_size_in_readme(
readme_path: '../../README.md',
open_pr: options[:open_pr] || false,
pr_title: 'chore: update sdk size',
sizes: options[:sizes] || frameworks_sizes,
size_ext: sdk_size_ext
)
Expand Down
2 changes: 1 addition & 1 deletion examples/SampleApp/fastlane/Pluginfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

gem 'fastlane-plugin-firebase_app_distribution'
gem 'fastlane-plugin-load_json'
gem 'fastlane-plugin-stream_actions', '0.3.65'
gem 'fastlane-plugin-stream_actions', '0.3.67'
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ describe('useAppSettings', () => {
auto_translation_enabled: true,
}),
),
userID: 'some-user-id',
} as unknown as StreamChat,
isOnline,
false,
Expand Down
45 changes: 36 additions & 9 deletions package/src/components/Chat/hooks/useAppSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,32 @@ export const useAppSettings = <
const isMounted = useIsMountedRef();

useEffect(() => {
async function enforeAppSettings() {
if (!client.userID) return;
/**
* Fetches app settings from the backend when offline support is disabled.
*/
const enforceAppSettingsWithoutOfflineSupport = async () => {
try {
const appSettings = await client.getAppSettings();
if (isMounted.current) {
setAppSettings(appSettings);
}
} catch (error: unknown) {
if (error instanceof Error) {
console.error(`An error occurred while getting app settings: ${error}`);
}
}
};

if (enableOfflineSupport && !initialisedDatabase) return;
/**
* Fetches app settings from the local database when offline support is enabled if internet is off else fetches from the backend.
* Note: We need to set the app settings from the local database when offline as the client will not have the app settings in memory. For this we store it for the `client.userID`.
*
* TODO: Remove client.userID usage for offline support case.
*/
const enforceAppSettingsWithOfflineSupport = async () => {
if (!client.userID) return;

if (!isOnline && enableOfflineSupport) {
if (!isOnline) {
const appSettings = dbApi.getAppSettings({ currentUserId: client.userID });
setAppSettings(appSettings);
return;
Expand All @@ -33,17 +53,24 @@ export const useAppSettings = <
const appSettings = await client.getAppSettings();
if (isMounted.current) {
setAppSettings(appSettings);
enableOfflineSupport &&
dbApi.upsertAppSettings({
appSettings,
currentUserId: client.userID as string,
});
dbApi.upsertAppSettings({
appSettings,
currentUserId: client.userID as string,
});
}
} catch (error: unknown) {
if (error instanceof Error) {
console.error(`An error occurred while getting app settings: ${error}`);
}
}
};

async function enforeAppSettings() {
if (enableOfflineSupport) {
await enforceAppSettingsWithOfflineSupport();
} else {
await enforceAppSettingsWithoutOfflineSupport();
}
}

enforeAppSettings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import truncate from 'lodash/truncate';

import { parseLinksFromText } from './parseLinks';

// If you need to use any of the special characters literally (actually searching for a "*", for instance), you must escape it by putting a backslash in front of it.
function escapeRegExp(text: string) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}
import { escapeRegExp } from '../../../../utils/utils';

export const generateMarkdownText = (text?: string) => {
if (!text) return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { generateMarkdownText } from './generateMarkdownText';
import type { MessageContextValue } from '../../../../contexts/messageContext/MessageContext';
import type { Colors, MarkdownStyle } from '../../../../contexts/themeContext/utils/theme';
import type { DefaultStreamChatGenerics } from '../../../../types/types';
import { escapeRegExp } from '../../../../utils/utils';
import type { MessageType } from '../../../MessageList/hooks/useMessageList';

const defaultMarkdownStyles: MarkdownStyle = {
Expand Down Expand Up @@ -211,10 +212,6 @@ export const renderText = <
);
};

function escapeRegExp(text: string) {
return text.replace(/[-[\]{}()*+?.,/\\^$|#]/g, '\\$&');
}

// take the @ mentions and turn them into markdown?
// translate links
const { mentioned_users } = message;
Expand Down
25 changes: 19 additions & 6 deletions package/src/contexts/messageInputContext/MessageInputContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ import { DEFAULT_BASE_CONTEXT_VALUE } from '../utils/defaultBaseContextValue';
import { getDisplayName } from '../utils/getDisplayName';
import { isTestEnvironment } from '../utils/isTestEnvironment';

/**
* Function to escape special characters except . in a string and replace with '_'
* @param text
* @returns string
*/
function escapeRegExp(text: string) {
return text.replace(/[[\]{}()*+?,\\^$|#\s]/g, '_');
}

export type EmojiSearchIndex = {
search: (query: string) => PromiseLike<Array<Emoji>> | Array<Emoji> | null;
};
Expand Down Expand Up @@ -1149,6 +1158,9 @@ export const MessageInputProvider = <
const uploadFile = async ({ newFile }: { newFile: FileUpload }) => {
const { file, id } = newFile;

// The file name can have special characters, so we escape it.
const filename = escapeRegExp(file.name);

setFileUploads(getUploadSetStateAction(id, FileState.UPLOADING));

let response: Partial<SendFileAPIResponse> = {};
Expand All @@ -1157,17 +1169,17 @@ export const MessageInputProvider = <
response = await value.doDocUploadRequest(file, channel);
} else if (channel && file.uri) {
uploadAbortControllerRef.current.set(
file.name,
filename,
client.createAbortControllerForNextRequest(),
);
// Compress images selected through file picker when uploading them
if (file.mimeType?.includes('image')) {
const compressedUri = await compressedImageURI(file, value.compressImageQuality);
response = await channel.sendFile(compressedUri, file.name, file.mimeType);
response = await channel.sendFile(compressedUri, filename, file.mimeType);
} else {
response = await channel.sendFile(file.uri, file.name, file.mimeType);
response = await channel.sendFile(file.uri, filename, file.mimeType);
}
uploadAbortControllerRef.current.delete(file.name);
uploadAbortControllerRef.current.delete(filename);
}

const extraData: Partial<FileUpload> = {
Expand All @@ -1181,7 +1193,7 @@ export const MessageInputProvider = <
(error.name === 'AbortError' || error.name === 'CanceledError')
) {
// nothing to do
uploadAbortControllerRef.current.delete(file.name);
uploadAbortControllerRef.current.delete(filename);
return;
}
handleFileOrImageUploadError(error, false, id);
Expand All @@ -1198,7 +1210,8 @@ export const MessageInputProvider = <
let response = {} as SendFileAPIResponse;

const uri = file.uri || '';
const filename = file.name ?? getFileNameFromPath(uri);
// The file name can have special characters, so we escape it.
const filename = escapeRegExp(file.name ?? getFileNameFromPath(uri));

try {
const compressedUri = await compressedImageURI(file, value.compressImageQuality);
Expand Down
9 changes: 9 additions & 0 deletions package/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,3 +677,12 @@ export const getDurationLabelFromDuration = (duration: number) => {

return durationLabel;
};

/**
* Utility to escape special characters in a string.
* @param text
* @returns string
*/
export function escapeRegExp(text: string) {
return text.replace(/[-[\]{}()*+?.,/\\^$|#]/g, '\\$&');
}

0 comments on commit 5ee8382

Please sign in to comment.