2017-05-03 10:04:16 +10:00
import React from 'react' ;
2016-11-17 03:20:52 +11:00
import { connect } from 'react-redux' ;
2018-07-30 00:52:06 +10:00
import ImmutablePureComponent from 'react-immutable-pure-component' ;
2017-04-22 04:05:35 +10:00
import PropTypes from 'prop-types' ;
2016-11-17 03:20:52 +11:00
import ImmutablePropTypes from 'react-immutable-proptypes' ;
import LoadingIndicator from '../../components/loading_indicator' ;
import { fetchFavourites } from '../../actions/interactions' ;
2019-10-01 12:57:27 +10:00
import { defineMessages , injectIntl , FormattedMessage } from 'react-intl' ;
2016-11-21 05:39:18 +11:00
import AccountContainer from '../../containers/account_container' ;
2016-11-17 03:20:52 +11:00
import Column from '../ui/components/column' ;
2018-08-27 00:39:37 +10:00
import ScrollableList from '../../components/scrollable_list' ;
2019-10-01 12:57:27 +10:00
import Icon from 'mastodon/components/icon' ;
import ColumnHeader from '../../components/column_header' ;
const messages = defineMessages ( {
refresh : { id : 'refresh' , defaultMessage : 'Refresh' } ,
} ) ;
2016-11-04 23:39:24 +11:00
const mapStateToProps = ( state , props ) => ( {
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 22:53:48 +10:00
accountIds : state . getIn ( [ 'user_lists' , 'favourited_by' , props . params . statusId ] ) ,
2016-11-04 23:39:24 +11:00
} ) ;
2018-09-15 01:59:48 +10:00
export default @ connect ( mapStateToProps )
2019-10-01 12:57:27 +10:00
@ injectIntl
2018-09-15 01:59:48 +10:00
class Favourites extends ImmutablePureComponent {
2016-11-04 23:39:24 +11:00
2017-05-12 22:44:10 +10:00
static propTypes = {
params : PropTypes . object . isRequired ,
dispatch : PropTypes . func . isRequired ,
2017-05-21 01:31:47 +10:00
accountIds : ImmutablePropTypes . list ,
2019-07-19 17:25:22 +10:00
multiColumn : PropTypes . bool ,
2019-10-01 12:57:27 +10:00
intl : PropTypes . object . isRequired ,
2017-05-12 22:44:10 +10:00
} ;
2016-11-04 23:39:24 +11:00
componentWillMount ( ) {
2019-09-30 00:27:00 +10:00
if ( ! this . props . accountIds ) {
this . props . dispatch ( fetchFavourites ( this . props . params . statusId ) ) ;
}
2017-04-22 04:05:35 +10:00
}
2016-11-04 23:39:24 +11:00
2017-05-20 09:28:25 +10:00
componentWillReceiveProps ( nextProps ) {
2016-11-04 23:39:24 +11:00
if ( nextProps . params . statusId !== this . props . params . statusId && nextProps . params . statusId ) {
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 22:53:48 +10:00
this . props . dispatch ( fetchFavourites ( nextProps . params . statusId ) ) ;
2016-11-04 23:39:24 +11:00
}
2017-04-22 04:05:35 +10:00
}
2016-11-04 23:39:24 +11:00
2019-10-01 12:57:27 +10:00
handleRefresh = ( ) => {
this . props . dispatch ( fetchFavourites ( this . props . params . statusId ) ) ;
}
2016-11-04 23:39:24 +11:00
render ( ) {
2021-07-13 23:45:17 +10:00
const { intl , accountIds , multiColumn } = this . props ;
2016-11-04 23:39:24 +11:00
if ( ! accountIds ) {
return (
< Column >
< LoadingIndicator / >
< / C o l u m n >
) ;
}
2018-08-27 00:39:37 +10:00
const emptyMessage = < FormattedMessage id = 'empty_column.favourites' defaultMessage = 'No one has favourited this toot yet. When someone does, they will show up here.' / > ;
2016-11-04 23:39:24 +11:00
return (
2019-10-01 12:57:27 +10:00
< Column bindToDocument = { ! multiColumn } >
< ColumnHeader
showBackButton
multiColumn = { multiColumn }
extraButton = { (
< button className = 'column-header__button' title = { intl . formatMessage ( messages . refresh ) } aria - label = { intl . formatMessage ( messages . refresh ) } onClick = { this . handleRefresh } > < Icon id = 'refresh' / > < / b u t t o n >
) }
/ >
2016-11-04 23:39:24 +11:00
2018-08-27 00:39:37 +10:00
< ScrollableList
scrollKey = 'favourites'
emptyMessage = { emptyMessage }
2019-07-19 17:25:22 +10:00
bindToDocument = { ! multiColumn }
2018-08-27 00:39:37 +10:00
>
{ accountIds . map ( id =>
2020-03-09 02:02:36 +11:00
< AccountContainer key = { id } id = { id } withNote = { false } / > ,
2018-08-27 00:39:37 +10:00
) }
< / S c r o l l a b l e L i s t >
2016-11-04 23:39:24 +11:00
< / C o l u m n >
) ;
}
2017-04-22 04:05:35 +10:00
}