2020-10-30 13:12:02 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2022-06-06 15:33:36 +00:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
|
|
|
import { shallowEqual, useSelector } from 'react-redux';
|
2022-02-01 13:39:09 +00:00
|
|
|
import { Observable, Subscription } from 'rxjs';
|
2020-10-30 13:12:02 +00:00
|
|
|
|
2022-06-06 15:33:36 +00:00
|
|
|
import { IApplicationState, TSubscriptionModel, TUserModel } from '../../definitions';
|
2020-10-30 13:12:02 +00:00
|
|
|
import database from '../../lib/database';
|
|
|
|
import { getUserSelector } from '../../selectors/login';
|
|
|
|
import Avatar from './Avatar';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { IAvatar } from './interfaces';
|
2020-10-30 13:12:02 +00:00
|
|
|
|
2022-06-06 15:33:36 +00:00
|
|
|
const AvatarContainer = ({
|
|
|
|
style,
|
|
|
|
text = '',
|
|
|
|
avatar,
|
|
|
|
emoji,
|
|
|
|
size,
|
|
|
|
borderRadius,
|
|
|
|
type,
|
|
|
|
children,
|
|
|
|
onPress,
|
|
|
|
getCustomEmoji,
|
|
|
|
isStatic,
|
|
|
|
rid
|
|
|
|
}: IAvatar): React.ReactElement => {
|
|
|
|
const subscription = useRef<Subscription>();
|
|
|
|
const [avatarETag, setAvatarETag] = useState<string | undefined>('');
|
2020-10-30 13:12:02 +00:00
|
|
|
|
2022-06-06 15:33:36 +00:00
|
|
|
const isDirect = () => type === 'd';
|
2020-10-30 13:12:02 +00:00
|
|
|
|
2022-06-06 15:33:36 +00:00
|
|
|
const server = useSelector((state: IApplicationState) => state.share.server.server || state.server.server);
|
|
|
|
const serverVersion = useSelector((state: IApplicationState) => state.share.server.version || state.server.version);
|
|
|
|
const { id, token } = useSelector(
|
|
|
|
(state: IApplicationState) => ({
|
|
|
|
id: getUserSelector(state).id,
|
|
|
|
token: getUserSelector(state).token
|
|
|
|
}),
|
|
|
|
shallowEqual
|
|
|
|
);
|
2020-10-30 13:12:02 +00:00
|
|
|
|
2022-06-06 15:33:36 +00:00
|
|
|
const externalProviderUrl = useSelector(
|
|
|
|
(state: IApplicationState) => state.settings.Accounts_AvatarExternalProviderUrl as string
|
|
|
|
);
|
|
|
|
const blockUnauthenticatedAccess = useSelector(
|
|
|
|
(state: IApplicationState) =>
|
|
|
|
(state.share.settings?.Accounts_AvatarBlockUnauthenticatedAccess as boolean) ??
|
|
|
|
state.settings.Accounts_AvatarBlockUnauthenticatedAccess ??
|
|
|
|
true
|
|
|
|
);
|
2020-10-30 13:51:04 +00:00
|
|
|
|
2022-06-06 15:33:36 +00:00
|
|
|
const init = async () => {
|
2020-10-30 13:51:04 +00:00
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const usersCollection = db.get('users');
|
|
|
|
const subsCollection = db.get('subscriptions');
|
2020-10-30 13:51:04 +00:00
|
|
|
|
|
|
|
let record;
|
|
|
|
try {
|
2022-06-06 15:33:36 +00:00
|
|
|
if (isDirect()) {
|
2022-01-12 12:54:04 +00:00
|
|
|
const [user] = await usersCollection.query(Q.where('username', text)).fetch();
|
2020-10-30 13:51:04 +00:00
|
|
|
record = user;
|
2022-06-06 15:33:36 +00:00
|
|
|
} else if (rid) {
|
|
|
|
record = await subsCollection.find(rid);
|
2020-10-30 13:12:02 +00:00
|
|
|
}
|
2020-10-30 13:51:04 +00:00
|
|
|
} catch {
|
|
|
|
// Record not found
|
|
|
|
}
|
|
|
|
|
|
|
|
if (record) {
|
2022-02-01 13:39:09 +00:00
|
|
|
const observable = record.observe() as Observable<TSubscriptionModel | TUserModel>;
|
2022-06-06 15:33:36 +00:00
|
|
|
subscription.current = observable.subscribe(r => {
|
|
|
|
setAvatarETag(r.avatarETag);
|
2020-10-30 13:51:04 +00:00
|
|
|
});
|
2020-10-30 13:12:02 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-10-30 13:12:02 +00:00
|
|
|
|
2022-06-06 15:33:36 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!avatarETag) {
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
return () => {
|
|
|
|
if (subscription?.current?.unsubscribe) {
|
|
|
|
subscription.current.unsubscribe();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [text, type, size, avatarETag, externalProviderUrl]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Avatar
|
|
|
|
server={server}
|
|
|
|
style={style}
|
|
|
|
text={text}
|
|
|
|
avatar={avatar}
|
|
|
|
emoji={emoji}
|
|
|
|
size={size}
|
|
|
|
borderRadius={borderRadius}
|
|
|
|
type={type}
|
|
|
|
children={children}
|
|
|
|
userId={id}
|
|
|
|
token={token}
|
|
|
|
onPress={onPress}
|
|
|
|
getCustomEmoji={getCustomEmoji}
|
|
|
|
isStatic={isStatic}
|
|
|
|
rid={rid}
|
|
|
|
blockUnauthenticatedAccess={blockUnauthenticatedAccess}
|
|
|
|
externalProviderUrl={externalProviderUrl}
|
|
|
|
avatarETag={avatarETag}
|
|
|
|
serverVersion={serverVersion}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
2020-10-30 13:12:02 +00:00
|
|
|
|
2022-06-06 15:33:36 +00:00
|
|
|
export default AvatarContainer;
|