2020-10-30 13:12:02 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { Q } from '@nozbe/watermelondb';
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
class AvatarContainer extends React.Component<Partial<IAvatar>, any> {
|
|
|
|
private mounted: boolean;
|
|
|
|
|
|
|
|
private subscription!: any;
|
2020-10-30 13:12:02 +00:00
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
text: '',
|
|
|
|
type: 'd'
|
|
|
|
};
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
constructor(props: Partial<IAvatar>) {
|
2020-10-30 13:12:02 +00:00
|
|
|
super(props);
|
|
|
|
this.mounted = false;
|
|
|
|
this.state = { avatarETag: '' };
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.mounted = true;
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
componentDidUpdate(prevProps: any) {
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2021-09-13 20:41:05 +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;
|
|
|
|
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) {
|
|
|
|
const observable = record.observe();
|
2021-09-13 20:41:05 +00:00
|
|
|
this.subscription = observable.subscribe((r: any) => {
|
2020-10-30 13:51:04 +00:00
|
|
|
const { avatarETag } = r;
|
|
|
|
if (this.mounted) {
|
|
|
|
this.setState({ avatarETag });
|
|
|
|
} else {
|
2021-09-13 20:41:05 +00:00
|
|
|
// @ts-ignore
|
2020-10-30 13:51:04 +00:00
|
|
|
this.state.avatarETag = avatarETag;
|
|
|
|
}
|
|
|
|
});
|
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;
|
2021-09-13 20:41:05 +00:00
|
|
|
return <Avatar avatarETag={avatarETag} serverVersion={serverVersion} {...this.props} />;
|
2020-10-30 13:12:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const mapStateToProps = (state: any) => ({
|
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:
|
2021-09-13 20:41:05 +00:00
|
|
|
state.share.settings?.Accounts_AvatarBlockUnauthenticatedAccess ??
|
|
|
|
state.settings.Accounts_AvatarBlockUnauthenticatedAccess ??
|
|
|
|
true
|
2020-10-30 13:12:02 +00:00
|
|
|
});
|
|
|
|
export default connect(mapStateToProps)(AvatarContainer);
|