2020-10-30 13:12:02 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { Q } from '@nozbe/watermelondb';
|
|
|
|
|
|
|
|
import database from '../../lib/database';
|
|
|
|
import { getUserSelector } from '../../selectors/login';
|
|
|
|
import Avatar from './Avatar';
|
|
|
|
|
|
|
|
class AvatarContainer extends React.Component {
|
|
|
|
static propTypes = {
|
2020-10-30 13:51:04 +00:00
|
|
|
rid: PropTypes.string,
|
2020-10-30 13:12:02 +00:00
|
|
|
text: PropTypes.string,
|
2020-10-30 15:54:02 +00:00
|
|
|
type: PropTypes.string,
|
2020-11-04 16:53:44 +00:00
|
|
|
blockUnauthenticatedAccess: PropTypes.bool,
|
|
|
|
serverVersion: PropTypes.string
|
2020-10-30 13:12:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
text: '',
|
|
|
|
type: 'd'
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.mounted = false;
|
|
|
|
this.state = { avatarETag: '' };
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.mounted = true;
|
|
|
|
}
|
|
|
|
|
2020-10-30 13:51:04 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
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';
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2020-10-30 13:12:02 +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();
|
|
|
|
this.subscription = observable.subscribe((r) => {
|
|
|
|
const { avatarETag } = r;
|
|
|
|
if (this.mounted) {
|
|
|
|
this.setState({ avatarETag });
|
|
|
|
} else {
|
|
|
|
this.state.avatarETag = avatarETag;
|
|
|
|
}
|
|
|
|
});
|
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;
|
2020-10-30 13:12:02 +00:00
|
|
|
return (
|
|
|
|
<Avatar
|
|
|
|
avatarETag={avatarETag}
|
2020-11-04 16:53:44 +00:00
|
|
|
serverVersion={serverVersion}
|
2020-10-30 13:12:02 +00:00
|
|
|
{...this.props}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
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:
|
|
|
|
state.share.settings?.Accounts_AvatarBlockUnauthenticatedAccess
|
|
|
|
?? state.settings.Accounts_AvatarBlockUnauthenticatedAccess
|
|
|
|
?? true
|
2020-10-30 13:12:02 +00:00
|
|
|
});
|
|
|
|
export default connect(mapStateToProps)(AvatarContainer);
|