Skip to content

Commit

Permalink
Use UUID native functions (#8032)
Browse files Browse the repository at this point in the history
  • Loading branch information
larkox authored Jun 20, 2024
1 parent 53b0110 commit b7791ad
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 43 deletions.
11 changes: 2 additions & 9 deletions android/app/src/main/java/com/mattermost/helpers/RandomId.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mattermost.helpers

import kotlin.math.floor
import java.util.UUID

class RandomId {
companion object {
Expand All @@ -9,14 +9,7 @@ class RandomId {
private const val idLength = 16

fun generate(): String {
var id = ""
for (i in 1.rangeTo((idLength / 2))) {
val random = floor(Math.random() * alphabetLength * alphabetLength)
id += alphabet[floor(random / alphabetLength).toInt()]
id += alphabet[(random % alphabetLength).toInt()]
}

return id
return UUID.randomUUID().toString()
}
}
}
4 changes: 3 additions & 1 deletion app/components/formatted_text/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {createElement, isValidElement} from 'react';
import {useIntl} from 'react-intl';
import {type StyleProp, Text, type TextProps, type TextStyle} from 'react-native';

import {generateId} from '@utils/general';

type FormattedTextProps = TextProps & {
id: string;
defaultMessage?: string;
Expand All @@ -24,7 +26,7 @@ const FormattedText = (props: FormattedTextProps) => {
if (values && Object.keys(values).length > 0) {
// Creates a token with a random UID that should not be guessable or
// conflict with other parts of the `message` string.
const uid = Math.floor(Math.random() * 0x10000000000).toString(16);
const uid = generateId();

const generateToken = (() => {
let counter = 0;
Expand Down
18 changes: 2 additions & 16 deletions app/utils/general/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See LICENSE.txt for license information.

import {applicationId} from 'expo-application';
import {randomUUID} from 'expo-crypto';
import {createIntl} from 'react-intl';
import ReactNativeHapticFeedback, {HapticFeedbackTypes} from 'react-native-haptic-feedback';

Expand All @@ -25,22 +26,7 @@ export function emptyFunction(..._args: any[]) {

// Generates a RFC-4122 version 4 compliant globally unique identifier.
export const generateId = (prefix?: string): string => {
// implementation taken from http://stackoverflow.com/a/2117523
let id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
id = id.replace(/[xy]/g, (c) => {
const r = Math.floor(Math.random() * 16);
let v;

if (c === 'x') {
v = r;
} else {
// eslint-disable-next-line no-mixed-operators
v = (r & 0x3) | 0x8;
}

return v.toString(16);
});

const id = randomUUID();
if (prefix) {
return `${prefix}-${id}`;
}
Expand Down
15 changes: 1 addition & 14 deletions ios/Gekidou/Sources/Gekidou/Storage/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,7 @@ public class Database: NSObject {
}

public func generateId() -> String {
let alphabet = Array("0123456789abcdefghijklmnopqrstuvwxyz")
let alphabetLenght = alphabet.count
let idLenght = 16
var id = ""

for _ in 1...(idLenght / 2) {
let random = floor(Double.random(in: 0..<1) * Double(alphabetLenght) * Double(alphabetLenght))
let firstIndex = Int(floor(random / Double(alphabetLenght)))
let lastIndex = Int(random) % alphabetLenght
id += String(alphabet[firstIndex])
id += String(alphabet[lastIndex])
}

return id
return UUID().uuidString.lowercased()
}

public func getOnlyServerUrl() throws -> String {
Expand Down
8 changes: 7 additions & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ PODS:
- ExpoModulesCore
- Expo (51.0.14):
- ExpoModulesCore
- ExpoCrypto (13.0.2):
- ExpoModulesCore
- ExpoDevice (6.0.2):
- ExpoModulesCore
- ExpoFileSystem (17.0.1):
Expand Down Expand Up @@ -1645,6 +1647,7 @@ DEPENDENCIES:
- EXApplication (from `../node_modules/expo-application/ios`)
- EXConstants (from `../node_modules/expo-constants/ios`)
- Expo (from `../node_modules/expo`)
- ExpoCrypto (from `../node_modules/expo-crypto/ios`)
- ExpoDevice (from `../node_modules/expo-device/ios`)
- ExpoFileSystem (from `../node_modules/expo-file-system/ios`)
- ExpoImage (from `../node_modules/expo-image/ios`)
Expand Down Expand Up @@ -1775,6 +1778,8 @@ EXTERNAL SOURCES:
:path: "../node_modules/expo-constants/ios"
Expo:
:path: "../node_modules/expo"
ExpoCrypto:
:path: "../node_modules/expo-crypto/ios"
ExpoDevice:
:path: "../node_modules/expo-device/ios"
ExpoFileSystem:
Expand Down Expand Up @@ -1981,6 +1986,7 @@ SPEC CHECKSUMS:
EXApplication: c08200c34daca7af7fd76ac4b9d606077410e8ad
EXConstants: 409690fbfd5afea964e5e9d6c4eb2c2b59222c59
Expo: 9c87c876b45a6934894ba5e9353ee94fdba48125
ExpoCrypto: 156078f266bf28f80ecf5e2a9c3a0d6ffce07a1c
ExpoDevice: fc94f0e42ecdfd897e7590f2874fc64dfa7e9b1c
ExpoFileSystem: 80bfe850b1f9922c16905822ecbf97acd711dc51
ExpoImage: 2ccccff1219ebc765e344f3338f2430af2df4824
Expand All @@ -1992,7 +1998,7 @@ SPEC CHECKSUMS:
FBLazyVector: 4bc164e5b5e6cfc288d2b5ff28643ea15fa1a589
fmt: 4c2741a687cc09f0634a2e2c72a838b99f1ff120
glog: fdfdfe5479092de0c4bdbebedd9056951f092c4f
hermes-engine: 49d04e119f7241031cb4718f6bdea0a536c0ddd9
hermes-engine: 01d3e052018c2a13937aca1860fbedbccd4a41b7
HMSegmentedControl: 34c1f54d822d8308e7b24f5d901ec674dfa31352
JitsiWebRTC: 37fb2fb70d42cac58c06948527a5f9e1b3f50812
libavif: 84bbb62fb232c3018d6f1bab79beea87e35de7b7
Expand Down
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"emoji-regex": "10.3.0",
"expo": "51.0.14",
"expo-application": "5.9.1",
"expo-crypto": "13.0.2",
"expo-device": "6.0.2",
"expo-image": "1.12.12",
"expo-linear-gradient": "13.0.2",
Expand Down
19 changes: 17 additions & 2 deletions test/test_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {SYSTEM_IDENTIFIERS} from '@constants/database';
import {PUSH_PROXY_STATUS_VERIFIED} from '@constants/push_proxy';
import DatabaseManager from '@database/manager';
import {prepareCommonSystemValues} from '@queries/servers/system';
import {generateId} from '@utils/general';

import type {APIClientInterface} from '@mattermost/react-native-network-client';

Expand Down Expand Up @@ -134,7 +133,23 @@ class TestHelper {
};

generateId = () => {
return generateId();
// implementation taken from http://stackoverflow.com/a/2117523
let id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
id = id.replace(/[xy]/g, (c) => {
const r = Math.floor(Math.random() * 16);
let v;

if (c === 'x') {
v = r;
} else {
// eslint-disable-next-line no-mixed-operators
v = (r & 0x3) | 0x8;
}

return v.toString(16);
});

return id;
};

createClient = () => {
Expand Down

0 comments on commit b7791ad

Please sign in to comment.