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

fix: use deterministic avatars for channel members #61

Merged
merged 3 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions social-messenger-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@stream-io/stream-chat-css": "^2.9.0",
"@stream-io/stream-chat-css": "^2.10.0",
"lodash.debounce": "^4.0.8",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-file-utils": "^1.1.12",
"stream-chat": "^6.6.0",
"stream-chat-react": "^9.1.0"
"stream-chat-react": "^9.1.2"
},
"devDependencies": {
"@types/jest": "^27.5.2",
Expand Down
31 changes: 21 additions & 10 deletions social-messenger-ts/src/assets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export { SendIcon } from './SendIcon';
export { XButton } from './XButton';
export { XButtonBackground } from './XButtonBackground';

const randomImages = [
const staticImages = [
avatar1,
avatar2,
avatar3,
Expand All @@ -66,27 +66,38 @@ const randomImages = [
avatar24,
];

export const getRandomImage = () => {
const index = Math.floor(Math.random() * 24);
return randomImages[index];
export const getImage = (userId: string) => {
const hash = hashCode(userId);
const index = Math.abs(hash) % staticImages.length;
return staticImages[index];
};

export const getCleanImage = (member: ChannelMemberResponse<StreamChatGenerics>) => {
let cleanImage = member.user?.image || '';

const cleanIndex = randomImages.indexOf(cleanImage);

const cleanIndex = staticImages.indexOf(cleanImage);
if (cleanIndex === -1) {
cleanImage = getRandomImage();
cleanImage = getImage(member.user_id || 'stream-user');
}

if (member.user?.name === 'Jen Alexander') {
cleanImage = randomImages[11];
cleanImage = staticImages[11];
}

if (member.user?.name === 'Kevin Rosen') {
cleanImage = randomImages[23];
cleanImage = staticImages[23];
}

return cleanImage;
};

// https://stackoverflow.com/a/7616484/1270325
const hashCode = (value: string) => {
let hash = 0;
if (value.length === 0) return hash;
for (let i = 0; i < value.length; i++) {
const chr = value.charCodeAt(i);
hash = (hash << 5) - hash + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
4 changes: 2 additions & 2 deletions social-messenger-ts/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ReactDOM from 'react-dom/client';
import './index.css';

import App from './App';
import { getRandomImage } from './assets';
import { getImage } from './assets';
import { getChannelListOptions } from './channelListOptions';

const apiKey = process.env.REACT_APP_STREAM_KEY;
Expand All @@ -19,7 +19,7 @@ const channelListOptions = getChannelListOptions(!!noChannelNameFilter, user);
const userToConnect: { id: string; name?: string; image?: string } = {
id: user!,
name: skipNameImageSet ? undefined : user!,
image: skipNameImageSet ? undefined : getRandomImage(),
image: skipNameImageSet ? undefined : getImage(user!),
};

const container = document.getElementById('root');
Expand Down
32 changes: 10 additions & 22 deletions social-messenger-ts/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2411,15 +2411,10 @@
resolved "https://registry.yarnpkg.com/@stream-io/escape-string-regexp/-/escape-string-regexp-5.0.1.tgz#362505c92799fea6afe4e369993fbbda8690cc37"
integrity sha512-qIaSrzJXieZqo2fZSYTdzwSbZgHHsT3tkd646vvZhh4fr+9nO4NlvqGmPF43Y+OfZiWf+zYDFgNiPGG5+iZulQ==

"@stream-io/stream-chat-css@^2.8.0":
version "2.8.0"
resolved "https://registry.yarnpkg.com/@stream-io/stream-chat-css/-/stream-chat-css-2.8.0.tgz#bff7a6ce53d10f3a6d6a1cab233be82d14e53601"
integrity sha512-0Jgo4ujw7vvUp8GI4UxqrWo/XpAhicHbcOmsEuTItcB+5N9/edJ+Tv/Z8Bm3xyKTN5ztnP2dEa9hDWF7U1A7Hw==

"@stream-io/stream-chat-css@^2.9.0":
version "2.9.0"
resolved "https://registry.yarnpkg.com/@stream-io/stream-chat-css/-/stream-chat-css-2.9.0.tgz#abc3d04d44207d8a372c43e390e633417152a6a0"
integrity sha512-o5fK5bPD2h+WYEkLZoWjUw+7dvHWcPyUPoOp65Aer0ElTPRryaWD00kG7TDKTsPFjQwJptolNnfrw9Yj7r9Z/Q==
"@stream-io/stream-chat-css@^2.10.0":
version "2.10.0"
resolved "https://registry.yarnpkg.com/@stream-io/stream-chat-css/-/stream-chat-css-2.10.0.tgz#3b4e7f9cdda2e71995412202bab5a852749d61a4"
integrity sha512-xPBFe3UKSPtYMffHP1wmRdH/R5Gg2deG64LSFArB0Wg7e+sv+b2ijZIz80jAcjyvIqhV3wFFsJnroxhNNrb2Ww==

"@stream-io/transliterate@^1.5.5":
version "1.5.5"
Expand Down Expand Up @@ -8673,13 +8668,6 @@ react-fast-compare@^3.0.1, react-fast-compare@^3.2.0:
resolved "https://registry.yarnpkg.com/react-fast-compare/-/react-fast-compare-3.2.0.tgz#641a9da81b6a6320f270e89724fb45a0b39e43bb"
integrity sha512-rtGImPZ0YyLrscKI9xTpV8psd6I8VAtjKCzQDlzyDvqJA8XOW78TXYQwNRNd8g8JZnDu8q9Fu/1v4HPAVwVdHA==

react-file-utils@^1.1.11:
version "1.1.11"
resolved "https://registry.yarnpkg.com/react-file-utils/-/react-file-utils-1.1.11.tgz#7ca3391a4c7c4569023adb67bdb33469da53ddb1"
integrity sha512-KKVTGfNn5qyAHC2ib41M87EiTzc/iV6pIA4btXOw8CAwUlTEox8TEHLT3pYSLMvztprqF7rAmxQ8Dqr2qTx0sQ==
dependencies:
react-dropzone "^12.0.5"

react-file-utils@^1.1.12:
version "1.1.12"
resolved "https://registry.yarnpkg.com/react-file-utils/-/react-file-utils-1.1.12.tgz#fb1d04d743e7cfc04f44047374dd4a79119006e6"
Expand Down Expand Up @@ -9478,14 +9466,14 @@ statuses@2.0.1:
resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.5.0.tgz#161c7dac177659fd9811f43771fa99381478628c"
integrity sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=

stream-chat-react@^9.1.0:
version "9.1.0"
resolved "https://registry.yarnpkg.com/stream-chat-react/-/stream-chat-react-9.1.0.tgz#663877122287842389b6a0b9524b6d2a2252f05d"
integrity sha512-RrZuybsp9Ac96wuK/UcAKelz9xsYM7I2ymi9OA+KWcLLuKEfaPO8P6zovJEb8DrJ/p0Y3U7ptGvGn+Bj2CIDzg==
stream-chat-react@^9.1.2:
version "9.1.2"
resolved "https://registry.yarnpkg.com/stream-chat-react/-/stream-chat-react-9.1.2.tgz#0ee2df02553bf10c0aeb45888d8300144b93996b"
integrity sha512-U3HghQWyIHjHuJjdQ7RucqNh72I9jXYg0YfNeuQ1QRkJyI+NG7Em+Q/K+YscVrVND/a79xiLjpi1Fj9ZKJBNFA==
dependencies:
"@braintree/sanitize-url" "6.0.0"
"@juggle/resize-observer" "^3.3.1"
"@stream-io/stream-chat-css" "^2.8.0"
"@stream-io/stream-chat-css" "^2.10.0"
dayjs "^1.10.4"
emoji-mart "3.0.1"
emoji-regex "^9.2.0"
Expand All @@ -9501,7 +9489,7 @@ stream-chat-react@^9.1.0:
pretty-bytes "^5.4.1"
prop-types "^15.7.2"
react-fast-compare "^3.2.0"
react-file-utils "^1.1.11"
react-file-utils "^1.1.12"
react-image-gallery "^1.2.7"
react-is "^18.1.0"
react-markdown "^5.0.3"
Expand Down