2020-10-30 13:12:02 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2022-02-01 13:39:09 +00:00
|
|
|
import { Observable, Subscription } from 'rxjs';
|
2020-10-30 13:12:02 +00:00
|
|
|
|
|
|
|
import database from '../../lib/database';
|
|
|
|
import { getUserSelector } from '../../selectors/login';
|
2022-03-29 16:23:45 +00:00
|
|
|
import { IApplicationState, TSubscriptionModel, TUserModel } from '../../definitions';
|
2020-10-30 13:12:02 +00:00
|
|
|
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-01-12 12:54:04 +00:00
|
|
|
class AvatarContainer extends React.Component<IAvatar, any> {
|
2022-02-01 13:39:09 +00:00
|
|
|
private subscription?: Subscription;
|
2020-10-30 13:12:02 +00:00
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
text: '',
|
|
|
|
type: 'd'
|
|
|
|
};
|
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
constructor(props: IAvatar) {
|
2020-10-30 13:12:02 +00:00
|
|
|
super(props);
|
|
|
|
this.state = { avatarETag: '' };
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
2022-03-29 16:23:45 +00:00
|
|
|
componentDidUpdate(prevProps: IAvatar) {
|
2021-02-26 16:01:45 +00:00
|
|
|
const { text, type } = this.props;
|
|
|
|
if (prevProps.text !== text || prevProps.type !== type) {
|
2020-10-30 13:51:04 +00:00
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 20:40:32 +00:00
|
|
|
shouldComponentUpdate(nextProps: IAvatar, nextState: { avatarETag: string }) {
|
|
|
|
const { avatarETag } = this.state;
|
|
|
|
const { text, type } = this.props;
|
|
|
|
if (nextState.avatarETag !== avatarETag) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.text !== text) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.type !== type) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-30 13:12:02 +00:00
|
|
|
componentWillUnmount() {
|
2020-10-30 13:51:04 +00:00
|
|
|
if (this.subscription?.unsubscribe) {
|
|
|
|
this.subscription.unsubscribe();
|
2020-10-30 13:12:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get isDirect() {
|
|
|
|
const { type } = this.props;
|
|
|
|
return type === 'd';
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
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 {
|
|
|
|
if (this.isDirect) {
|
|
|
|
const { text } = this.props;
|
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;
|
|
|
|
} else {
|
|
|
|
const { rid } = this.props;
|
2022-02-01 13:39:09 +00:00
|
|
|
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>;
|
|
|
|
this.subscription = observable.subscribe(r => {
|
2020-10-30 13:51:04 +00:00
|
|
|
const { avatarETag } = r;
|
2022-03-29 16:23:45 +00:00
|
|
|
this.setState({ 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
|
|
|
|
|
|
|
render() {
|
|
|
|
const { avatarETag } = this.state;
|
2020-11-04 16:53:44 +00:00
|
|
|
const { serverVersion } = this.props;
|
2022-01-12 12:54:04 +00:00
|
|
|
return <Avatar {...this.props} avatarETag={avatarETag} serverVersion={serverVersion} />;
|
2020-10-30 13:12:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 16:23:45 +00:00
|
|
|
const mapStateToProps = (state: IApplicationState) => ({
|
2020-10-30 13:12:02 +00:00
|
|
|
user: getUserSelector(state),
|
2020-11-04 16:53:44 +00:00
|
|
|
server: state.share.server.server || state.server.server,
|
|
|
|
serverVersion: state.share.server.version || state.server.version,
|
2020-10-30 15:54:02 +00:00
|
|
|
blockUnauthenticatedAccess:
|
2022-03-29 16:23:45 +00:00
|
|
|
(state.share.settings?.Accounts_AvatarBlockUnauthenticatedAccess as boolean) ??
|
2021-09-13 20:41:05 +00:00
|
|
|
state.settings.Accounts_AvatarBlockUnauthenticatedAccess ??
|
|
|
|
true
|
2020-10-30 13:12:02 +00:00
|
|
|
});
|
|
|
|
export default connect(mapStateToProps)(AvatarContainer);
|