2017-05-20 01:28:25 +02:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-12-04 08:26:40 +01:00
|
|
|
import { fetchAccount } from 'flavours/glitch/actions/accounts';
|
2018-05-27 19:10:37 +02:00
|
|
|
import { expandAccountMediaTimeline } from 'flavours/glitch/actions/timelines';
|
2017-12-04 08:26:40 +01:00
|
|
|
import LoadingIndicator from 'flavours/glitch/components/loading_indicator';
|
|
|
|
import Column from 'flavours/glitch/features/ui/components/column';
|
2019-02-27 13:36:40 +01:00
|
|
|
import ProfileColumnHeader from 'flavours/glitch/features/account/components/profile_column_header';
|
2017-05-20 01:28:25 +02:00
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
2017-12-04 08:26:40 +01:00
|
|
|
import { getAccountGallery } from 'flavours/glitch/selectors';
|
2017-05-20 01:28:25 +02:00
|
|
|
import MediaItem from './components/media_item';
|
2017-12-04 08:26:40 +01:00
|
|
|
import HeaderContainer from 'flavours/glitch/features/account_timeline/containers/header_container';
|
2017-10-31 22:58:38 +01:00
|
|
|
import { ScrollContainer } from 'react-router-scroll-4';
|
2017-12-04 08:26:40 +01:00
|
|
|
import LoadMore from 'flavours/glitch/components/load_more';
|
2019-04-09 05:02:48 +02:00
|
|
|
import MissingIndicator from 'flavours/glitch/components/missing_indicator';
|
2019-05-02 08:34:32 +02:00
|
|
|
import { openModal } from 'flavours/glitch/actions/modal';
|
2017-05-20 01:28:25 +02:00
|
|
|
|
|
|
|
const mapStateToProps = (state, props) => ({
|
2019-04-09 05:02:48 +02:00
|
|
|
isAccount: !!state.getIn(['accounts', props.params.accountId]),
|
2019-05-02 08:34:32 +02:00
|
|
|
attachments: getAccountGallery(state, props.params.accountId),
|
Change IDs to strings rather than numbers in API JSON output (#5019)
* Fix JavaScript interface with long IDs
Somewhat predictably, the JS interface handled IDs as numbers, which in
JS are IEEE double-precision floats. This loses some precision when
working with numbers as large as those generated by the new ID scheme,
so we instead handle them here as strings. This is relatively simple,
and doesn't appear to have caused any problems, but should definitely
be tested more thoroughly than the built-in tests. Several days of use
appear to support this working properly.
BREAKING CHANGE:
The major(!) change here is that IDs are now returned as strings by the
REST endpoints, rather than as integers. In practice, relatively few
changes were required to make the existing JS UI work with this change,
but it will likely hit API clients pretty hard: it's an entirely
different type to consume. (The one API client I tested, Tusky, handles
this with no problems, however.)
Twitter ran into this issue when introducing Snowflake IDs, and decided
to instead introduce an `id_str` field in JSON responses. I have opted
to *not* do that, and instead force all IDs to 64-bit integers
represented by strings in one go. (I believe Twitter exacerbated their
problem by rolling out the changes three times: once for statuses, once
for DMs, and once for user IDs, as well as by leaving an integer ID
value in JSON. As they said, "If you’re using the `id` field with JSON
in a Javascript-related language, there is a very high likelihood that
the integers will be silently munged by Javascript interpreters. In most
cases, this will result in behavior such as being unable to load or
delete a specific direct message, because the ID you're sending to the
API is different than the actual identifier associated with the
message." [1]) However, given that this is a significant change for API
users, alternatives or a transition time may be appropriate.
1: https://blog.twitter.com/developer/en_us/a/2011/direct-messages-going-snowflake-on-sep-30-2011.html
* Additional fixes for stringified IDs in JSON
These should be the last two. These were identified using eslint to try
to identify any plain casts to JavaScript numbers. (Some such casts are
legitimate, but these were not.)
Adding the following to .eslintrc.yml will identify casts to numbers:
~~~
no-restricted-syntax:
- warn
- selector: UnaryExpression[operator='+'] > :not(Literal)
message: Avoid the use of unary +
- selector: CallExpression[callee.name='Number']
message: Casting with Number() may coerce string IDs to numbers
~~~
The remaining three casts appear legitimate: two casts to array indices,
one in a server to turn an environment variable into a number.
* Back out RelationshipsController Change
This was made to make a test a bit less flakey, but has nothing to
do with this branch.
* Change internal streaming payloads to stringified IDs as well
Per
https://github.com/tootsuite/mastodon/pull/5019#issuecomment-330736452
we need these changes to send deleted status IDs as strings, not
integers.
2017-09-20 14:53:48 +02:00
|
|
|
isLoading: state.getIn(['timelines', `account:${props.params.accountId}:media`, 'isLoading']),
|
2019-05-02 08:34:32 +02:00
|
|
|
hasMore: state.getIn(['timelines', `account:${props.params.accountId}:media`, 'hasMore']),
|
2017-05-20 01:28:25 +02:00
|
|
|
});
|
|
|
|
|
2018-05-27 19:10:37 +02:00
|
|
|
class LoadMoreMedia extends ImmutablePureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
maxId: PropTypes.string,
|
|
|
|
onLoadMore: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
handleLoadMore = () => {
|
|
|
|
this.props.onLoadMore(this.props.maxId);
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return (
|
|
|
|
<LoadMore
|
|
|
|
disabled={this.props.disabled}
|
2018-12-11 18:51:01 +01:00
|
|
|
onClick={this.handleLoadMore}
|
2018-05-27 19:10:37 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-06-23 19:36:54 +02:00
|
|
|
@connect(mapStateToProps)
|
|
|
|
export default class AccountGallery extends ImmutablePureComponent {
|
2017-05-20 01:28:25 +02:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
params: PropTypes.object.isRequired,
|
|
|
|
dispatch: PropTypes.func.isRequired,
|
2019-05-02 08:34:32 +02:00
|
|
|
attachments: ImmutablePropTypes.list.isRequired,
|
2017-05-20 01:28:25 +02:00
|
|
|
isLoading: PropTypes.bool,
|
|
|
|
hasMore: PropTypes.bool,
|
2019-04-09 05:02:48 +02:00
|
|
|
isAccount: PropTypes.bool,
|
2017-05-20 01:28:25 +02:00
|
|
|
};
|
|
|
|
|
2019-05-02 08:34:32 +02:00
|
|
|
state = {
|
|
|
|
width: 323,
|
|
|
|
};
|
|
|
|
|
2017-05-20 01:28:25 +02:00
|
|
|
componentDidMount () {
|
Change IDs to strings rather than numbers in API JSON output (#5019)
* Fix JavaScript interface with long IDs
Somewhat predictably, the JS interface handled IDs as numbers, which in
JS are IEEE double-precision floats. This loses some precision when
working with numbers as large as those generated by the new ID scheme,
so we instead handle them here as strings. This is relatively simple,
and doesn't appear to have caused any problems, but should definitely
be tested more thoroughly than the built-in tests. Several days of use
appear to support this working properly.
BREAKING CHANGE:
The major(!) change here is that IDs are now returned as strings by the
REST endpoints, rather than as integers. In practice, relatively few
changes were required to make the existing JS UI work with this change,
but it will likely hit API clients pretty hard: it's an entirely
different type to consume. (The one API client I tested, Tusky, handles
this with no problems, however.)
Twitter ran into this issue when introducing Snowflake IDs, and decided
to instead introduce an `id_str` field in JSON responses. I have opted
to *not* do that, and instead force all IDs to 64-bit integers
represented by strings in one go. (I believe Twitter exacerbated their
problem by rolling out the changes three times: once for statuses, once
for DMs, and once for user IDs, as well as by leaving an integer ID
value in JSON. As they said, "If you’re using the `id` field with JSON
in a Javascript-related language, there is a very high likelihood that
the integers will be silently munged by Javascript interpreters. In most
cases, this will result in behavior such as being unable to load or
delete a specific direct message, because the ID you're sending to the
API is different than the actual identifier associated with the
message." [1]) However, given that this is a significant change for API
users, alternatives or a transition time may be appropriate.
1: https://blog.twitter.com/developer/en_us/a/2011/direct-messages-going-snowflake-on-sep-30-2011.html
* Additional fixes for stringified IDs in JSON
These should be the last two. These were identified using eslint to try
to identify any plain casts to JavaScript numbers. (Some such casts are
legitimate, but these were not.)
Adding the following to .eslintrc.yml will identify casts to numbers:
~~~
no-restricted-syntax:
- warn
- selector: UnaryExpression[operator='+'] > :not(Literal)
message: Avoid the use of unary +
- selector: CallExpression[callee.name='Number']
message: Casting with Number() may coerce string IDs to numbers
~~~
The remaining three casts appear legitimate: two casts to array indices,
one in a server to turn an environment variable into a number.
* Back out RelationshipsController Change
This was made to make a test a bit less flakey, but has nothing to
do with this branch.
* Change internal streaming payloads to stringified IDs as well
Per
https://github.com/tootsuite/mastodon/pull/5019#issuecomment-330736452
we need these changes to send deleted status IDs as strings, not
integers.
2017-09-20 14:53:48 +02:00
|
|
|
this.props.dispatch(fetchAccount(this.props.params.accountId));
|
2018-05-27 19:10:37 +02:00
|
|
|
this.props.dispatch(expandAccountMediaTimeline(this.props.params.accountId));
|
2017-05-20 01:28:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
if (nextProps.params.accountId !== this.props.params.accountId && nextProps.params.accountId) {
|
Change IDs to strings rather than numbers in API JSON output (#5019)
* Fix JavaScript interface with long IDs
Somewhat predictably, the JS interface handled IDs as numbers, which in
JS are IEEE double-precision floats. This loses some precision when
working with numbers as large as those generated by the new ID scheme,
so we instead handle them here as strings. This is relatively simple,
and doesn't appear to have caused any problems, but should definitely
be tested more thoroughly than the built-in tests. Several days of use
appear to support this working properly.
BREAKING CHANGE:
The major(!) change here is that IDs are now returned as strings by the
REST endpoints, rather than as integers. In practice, relatively few
changes were required to make the existing JS UI work with this change,
but it will likely hit API clients pretty hard: it's an entirely
different type to consume. (The one API client I tested, Tusky, handles
this with no problems, however.)
Twitter ran into this issue when introducing Snowflake IDs, and decided
to instead introduce an `id_str` field in JSON responses. I have opted
to *not* do that, and instead force all IDs to 64-bit integers
represented by strings in one go. (I believe Twitter exacerbated their
problem by rolling out the changes three times: once for statuses, once
for DMs, and once for user IDs, as well as by leaving an integer ID
value in JSON. As they said, "If you’re using the `id` field with JSON
in a Javascript-related language, there is a very high likelihood that
the integers will be silently munged by Javascript interpreters. In most
cases, this will result in behavior such as being unable to load or
delete a specific direct message, because the ID you're sending to the
API is different than the actual identifier associated with the
message." [1]) However, given that this is a significant change for API
users, alternatives or a transition time may be appropriate.
1: https://blog.twitter.com/developer/en_us/a/2011/direct-messages-going-snowflake-on-sep-30-2011.html
* Additional fixes for stringified IDs in JSON
These should be the last two. These were identified using eslint to try
to identify any plain casts to JavaScript numbers. (Some such casts are
legitimate, but these were not.)
Adding the following to .eslintrc.yml will identify casts to numbers:
~~~
no-restricted-syntax:
- warn
- selector: UnaryExpression[operator='+'] > :not(Literal)
message: Avoid the use of unary +
- selector: CallExpression[callee.name='Number']
message: Casting with Number() may coerce string IDs to numbers
~~~
The remaining three casts appear legitimate: two casts to array indices,
one in a server to turn an environment variable into a number.
* Back out RelationshipsController Change
This was made to make a test a bit less flakey, but has nothing to
do with this branch.
* Change internal streaming payloads to stringified IDs as well
Per
https://github.com/tootsuite/mastodon/pull/5019#issuecomment-330736452
we need these changes to send deleted status IDs as strings, not
integers.
2017-09-20 14:53:48 +02:00
|
|
|
this.props.dispatch(fetchAccount(nextProps.params.accountId));
|
2018-05-27 19:10:37 +02:00
|
|
|
this.props.dispatch(expandAccountMediaTimeline(this.props.params.accountId));
|
2017-05-20 01:28:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-08 20:34:31 +01:00
|
|
|
handleHeaderClick = () => {
|
|
|
|
this.column.scrollTop();
|
|
|
|
}
|
|
|
|
|
2017-05-25 05:22:46 +02:00
|
|
|
handleScrollToBottom = () => {
|
|
|
|
if (this.props.hasMore) {
|
2019-05-02 08:34:32 +02:00
|
|
|
this.handleLoadMore(this.props.attachments.size > 0 ? this.props.attachments.last().getIn(['status', 'id']) : undefined);
|
2017-05-25 05:22:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 08:34:32 +02:00
|
|
|
handleScroll = e => {
|
2017-05-20 01:28:25 +02:00
|
|
|
const { scrollTop, scrollHeight, clientHeight } = e.target;
|
2017-05-25 05:22:46 +02:00
|
|
|
const offset = scrollHeight - scrollTop - clientHeight;
|
2017-05-20 01:28:25 +02:00
|
|
|
|
2017-05-25 05:22:46 +02:00
|
|
|
if (150 > offset && !this.props.isLoading) {
|
|
|
|
this.handleScrollToBottom();
|
2017-05-20 01:28:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 19:10:37 +02:00
|
|
|
handleLoadMore = maxId => {
|
|
|
|
this.props.dispatch(expandAccountMediaTimeline(this.props.params.accountId, { maxId }));
|
|
|
|
};
|
|
|
|
|
2019-05-02 08:34:32 +02:00
|
|
|
handleLoadOlder = e => {
|
2017-05-25 05:22:46 +02:00
|
|
|
e.preventDefault();
|
|
|
|
this.handleScrollToBottom();
|
|
|
|
}
|
|
|
|
|
2018-07-27 17:59:52 +02:00
|
|
|
shouldUpdateScroll = (prevRouterProps, { location }) => {
|
2018-10-06 18:53:49 +02:00
|
|
|
if ((((prevRouterProps || {}).location || {}).state || {}).mastodonModalOpen) return false;
|
|
|
|
return !(location.state && location.state.mastodonModalOpen);
|
2018-07-27 17:59:52 +02:00
|
|
|
}
|
|
|
|
|
2019-05-02 08:34:32 +02:00
|
|
|
setColumnRef = c => {
|
2019-03-08 20:34:31 +01:00
|
|
|
this.column = c;
|
|
|
|
}
|
|
|
|
|
2019-05-02 08:34:32 +02:00
|
|
|
handleOpenMedia = attachment => {
|
|
|
|
if (attachment.get('type') === 'video') {
|
|
|
|
this.props.dispatch(openModal('VIDEO', { media: attachment }));
|
|
|
|
} else {
|
|
|
|
const media = attachment.getIn(['status', 'media_attachments']);
|
|
|
|
const index = media.findIndex(x => x.get('id') === attachment.get('id'));
|
|
|
|
|
|
|
|
this.props.dispatch(openModal('MEDIA', { media, index }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleRef = c => {
|
|
|
|
if (c) {
|
|
|
|
this.setState({ width: c.offsetWidth });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-20 01:28:25 +02:00
|
|
|
render () {
|
2019-05-02 08:34:32 +02:00
|
|
|
const { attachments, isLoading, hasMore, isAccount } = this.props;
|
|
|
|
const { width } = this.state;
|
2019-04-09 05:02:48 +02:00
|
|
|
|
|
|
|
if (!isAccount) {
|
|
|
|
return (
|
|
|
|
<Column>
|
|
|
|
<MissingIndicator />
|
|
|
|
</Column>
|
|
|
|
);
|
|
|
|
}
|
2017-05-25 05:22:46 +02:00
|
|
|
|
2019-05-02 08:34:32 +02:00
|
|
|
if (!attachments && isLoading) {
|
2017-05-20 01:28:25 +02:00
|
|
|
return (
|
|
|
|
<Column>
|
|
|
|
<LoadingIndicator />
|
|
|
|
</Column>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-02 08:34:32 +02:00
|
|
|
let loadOlder = null;
|
|
|
|
|
|
|
|
if (hasMore && !(isLoading && attachments.size === 0)) {
|
2018-12-12 18:06:00 +01:00
|
|
|
loadOlder = <LoadMore visible={!isLoading} onClick={this.handleLoadOlder} />;
|
2017-05-25 05:22:46 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 01:28:25 +02:00
|
|
|
return (
|
2019-05-02 08:34:32 +02:00
|
|
|
<Column ref={this.setColumnRef}>
|
2019-03-08 20:34:31 +01:00
|
|
|
<ProfileColumnHeader onClick={this.handleHeaderClick} />
|
2017-05-20 01:28:25 +02:00
|
|
|
|
2018-07-27 17:59:52 +02:00
|
|
|
<ScrollContainer scrollKey='account_gallery' shouldUpdateScroll={this.shouldUpdateScroll}>
|
2018-12-12 18:06:00 +01:00
|
|
|
<div className='scrollable scrollable--flex' onScroll={this.handleScroll}>
|
2017-05-20 01:28:25 +02:00
|
|
|
<HeaderContainer accountId={this.props.params.accountId} />
|
|
|
|
|
2019-05-02 08:34:32 +02:00
|
|
|
<div role='feed' className='account-gallery__container' ref={this.handleRef}>
|
|
|
|
{attachments.map((attachment, index) => attachment === null ? (
|
|
|
|
<LoadMoreMedia key={'more:' + attachments.getIn(index + 1, 'id')} maxId={index > 0 ? attachments.getIn(index - 1, 'id') : null} onLoadMore={this.handleLoadMore} />
|
2018-05-27 19:10:37 +02:00
|
|
|
) : (
|
2019-05-02 08:34:32 +02:00
|
|
|
<MediaItem key={attachment.get('id')} attachment={attachment} displayWidth={width} onOpenMedia={this.handleOpenMedia} />
|
2018-05-27 19:10:37 +02:00
|
|
|
))}
|
2019-05-02 08:34:32 +02:00
|
|
|
|
2018-05-27 19:10:37 +02:00
|
|
|
{loadOlder}
|
2017-05-20 01:28:25 +02:00
|
|
|
</div>
|
2018-12-12 18:06:00 +01:00
|
|
|
|
2019-05-02 08:34:32 +02:00
|
|
|
{isLoading && attachments.size === 0 && (
|
2018-12-12 18:06:00 +01:00
|
|
|
<div className='scrollable__append'>
|
|
|
|
<LoadingIndicator />
|
|
|
|
</div>
|
|
|
|
)}
|
2017-05-20 01:28:25 +02:00
|
|
|
</div>
|
|
|
|
</ScrollContainer>
|
|
|
|
</Column>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|