You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

376 lines
18 KiB

  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PropTypes from 'prop-types';
  4. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  5. import Button from 'mastodon/components/button';
  6. import ImmutablePureComponent from 'react-immutable-pure-component';
  7. import { autoPlayGif, me, isStaff } from 'mastodon/initial_state';
  8. import classNames from 'classnames';
  9. import Icon from 'mastodon/components/icon';
  10. import IconButton from 'mastodon/components/icon_button';
  11. import Avatar from 'mastodon/components/avatar';
  12. import { counterRenderer } from 'mastodon/components/common_counter';
  13. import ShortNumber from 'mastodon/components/short_number';
  14. import { NavLink } from 'react-router-dom';
  15. import DropdownMenuContainer from 'mastodon/containers/dropdown_menu_container';
  16. import AccountNoteContainer from '../containers/account_note_container';
  17. const messages = defineMessages({
  18. unfollow: { id: 'account.unfollow', defaultMessage: 'Unfollow' },
  19. follow: { id: 'account.follow', defaultMessage: 'Follow' },
  20. cancel_follow_request: { id: 'account.cancel_follow_request', defaultMessage: 'Cancel follow request' },
  21. requested: { id: 'account.requested', defaultMessage: 'Awaiting approval. Click to cancel follow request' },
  22. unblock: { id: 'account.unblock', defaultMessage: 'Unblock @{name}' },
  23. edit_profile: { id: 'account.edit_profile', defaultMessage: 'Edit profile' },
  24. linkVerifiedOn: { id: 'account.link_verified_on', defaultMessage: 'Ownership of this link was checked on {date}' },
  25. account_locked: { id: 'account.locked_info', defaultMessage: 'This account privacy status is set to locked. The owner manually reviews who can follow them.' },
  26. mention: { id: 'account.mention', defaultMessage: 'Mention @{name}' },
  27. direct: { id: 'account.direct', defaultMessage: 'Direct message @{name}' },
  28. unmute: { id: 'account.unmute', defaultMessage: 'Unmute @{name}' },
  29. block: { id: 'account.block', defaultMessage: 'Block @{name}' },
  30. mute: { id: 'account.mute', defaultMessage: 'Mute @{name}' },
  31. report: { id: 'account.report', defaultMessage: 'Report @{name}' },
  32. share: { id: 'account.share', defaultMessage: 'Share @{name}\'s profile' },
  33. media: { id: 'account.media', defaultMessage: 'Media' },
  34. blockDomain: { id: 'account.block_domain', defaultMessage: 'Block domain {domain}' },
  35. unblockDomain: { id: 'account.unblock_domain', defaultMessage: 'Unblock domain {domain}' },
  36. hideReblogs: { id: 'account.hide_reblogs', defaultMessage: 'Hide boosts from @{name}' },
  37. showReblogs: { id: 'account.show_reblogs', defaultMessage: 'Show boosts from @{name}' },
  38. enableNotifications: { id: 'account.enable_notifications', defaultMessage: 'Notify me when @{name} posts' },
  39. disableNotifications: { id: 'account.disable_notifications', defaultMessage: 'Stop notifying me when @{name} posts' },
  40. pins: { id: 'navigation_bar.pins', defaultMessage: 'Pinned toots' },
  41. preferences: { id: 'navigation_bar.preferences', defaultMessage: 'Preferences' },
  42. follow_requests: { id: 'navigation_bar.follow_requests', defaultMessage: 'Follow requests' },
  43. favourites: { id: 'navigation_bar.favourites', defaultMessage: 'Favourites' },
  44. lists: { id: 'navigation_bar.lists', defaultMessage: 'Lists' },
  45. blocks: { id: 'navigation_bar.blocks', defaultMessage: 'Blocked users' },
  46. domain_blocks: { id: 'navigation_bar.domain_blocks', defaultMessage: 'Blocked domains' },
  47. mutes: { id: 'navigation_bar.mutes', defaultMessage: 'Muted users' },
  48. endorse: { id: 'account.endorse', defaultMessage: 'Feature on profile' },
  49. unendorse: { id: 'account.unendorse', defaultMessage: 'Don\'t feature on profile' },
  50. add_or_remove_from_list: { id: 'account.add_or_remove_from_list', defaultMessage: 'Add or Remove from lists' },
  51. admin_account: { id: 'status.admin_account', defaultMessage: 'Open moderation interface for @{name}' },
  52. });
  53. const dateFormatOptions = {
  54. month: 'short',
  55. day: 'numeric',
  56. year: 'numeric',
  57. hour12: false,
  58. hour: '2-digit',
  59. minute: '2-digit',
  60. };
  61. export default @injectIntl
  62. class Header extends ImmutablePureComponent {
  63. static propTypes = {
  64. account: ImmutablePropTypes.map,
  65. identity_props: ImmutablePropTypes.list,
  66. onFollow: PropTypes.func.isRequired,
  67. onBlock: PropTypes.func.isRequired,
  68. onMention: PropTypes.func.isRequired,
  69. onDirect: PropTypes.func.isRequired,
  70. onReblogToggle: PropTypes.func.isRequired,
  71. onNotifyToggle: PropTypes.func.isRequired,
  72. onReport: PropTypes.func.isRequired,
  73. onMute: PropTypes.func.isRequired,
  74. onBlockDomain: PropTypes.func.isRequired,
  75. onUnblockDomain: PropTypes.func.isRequired,
  76. onEndorseToggle: PropTypes.func.isRequired,
  77. onAddToList: PropTypes.func.isRequired,
  78. onEditAccountNote: PropTypes.func.isRequired,
  79. intl: PropTypes.object.isRequired,
  80. domain: PropTypes.string.isRequired,
  81. };
  82. openEditProfile = () => {
  83. window.open('/settings/profile', '_blank');
  84. }
  85. isStatusesPageActive = (match, location) => {
  86. if (!match) {
  87. return false;
  88. }
  89. return !location.pathname.match(/\/(followers|following)\/?$/);
  90. }
  91. _updateEmojis () {
  92. const node = this.node;
  93. if (!node || autoPlayGif) {
  94. return;
  95. }
  96. const emojis = node.querySelectorAll('.custom-emoji');
  97. for (var i = 0; i < emojis.length; i++) {
  98. let emoji = emojis[i];
  99. if (emoji.classList.contains('status-emoji')) {
  100. continue;
  101. }
  102. emoji.classList.add('status-emoji');
  103. emoji.addEventListener('mouseenter', this.handleEmojiMouseEnter, false);
  104. emoji.addEventListener('mouseleave', this.handleEmojiMouseLeave, false);
  105. }
  106. }
  107. componentDidMount () {
  108. this._updateEmojis();
  109. }
  110. componentDidUpdate () {
  111. this._updateEmojis();
  112. }
  113. handleEmojiMouseEnter = ({ target }) => {
  114. target.src = target.getAttribute('data-original');
  115. }
  116. handleEmojiMouseLeave = ({ target }) => {
  117. target.src = target.getAttribute('data-static');
  118. }
  119. setRef = (c) => {
  120. this.node = c;
  121. }
  122. render () {
  123. const { account, intl, domain, identity_proofs } = this.props;
  124. if (!account) {
  125. return null;
  126. }
  127. const suspended = account.get('suspended');
  128. let info = [];
  129. let actionBtn = '';
  130. let bellBtn = '';
  131. let lockedIcon = '';
  132. let menu = [];
  133. if (me !== account.get('id') && account.getIn(['relationship', 'followed_by'])) {
  134. info.push(<span key='followed_by' className='relationship-tag'><FormattedMessage id='account.follows_you' defaultMessage='Follows you' /></span>);
  135. } else if (me !== account.get('id') && account.getIn(['relationship', 'blocking'])) {
  136. info.push(<span key='blocked' className='relationship-tag'><FormattedMessage id='account.blocked' defaultMessage='Blocked' /></span>);
  137. }
  138. if (me !== account.get('id') && account.getIn(['relationship', 'muting'])) {
  139. info.push(<span key='muted' className='relationship-tag'><FormattedMessage id='account.muted' defaultMessage='Muted' /></span>);
  140. } else if (me !== account.get('id') && account.getIn(['relationship', 'domain_blocking'])) {
  141. info.push(<span key='domain_blocked' className='relationship-tag'><FormattedMessage id='account.domain_blocked' defaultMessage='Domain blocked' /></span>);
  142. }
  143. if (account.getIn(['relationship', 'requested']) || account.getIn(['relationship', 'following'])) {
  144. bellBtn = <IconButton icon='bell-o' size={24} active={account.getIn(['relationship', 'notifying'])} title={intl.formatMessage(account.getIn(['relationship', 'notifying']) ? messages.disableNotifications : messages.enableNotifications, { name: account.get('username') })} onClick={this.props.onNotifyToggle} />;
  145. }
  146. if (me !== account.get('id')) {
  147. if (!account.get('relationship')) { // Wait until the relationship is loaded
  148. actionBtn = '';
  149. } else if (account.getIn(['relationship', 'requested'])) {
  150. actionBtn = <Button className={classNames('logo-button', { 'button--with-bell': bellBtn !== '' })} text={intl.formatMessage(messages.cancel_follow_request)} title={intl.formatMessage(messages.requested)} onClick={this.props.onFollow} />;
  151. } else if (!account.getIn(['relationship', 'blocking'])) {
  152. actionBtn = <Button disabled={account.getIn(['relationship', 'blocked_by'])} className={classNames('logo-button', { 'button--destructive': account.getIn(['relationship', 'following']), 'button--with-bell': bellBtn !== '' })} text={intl.formatMessage(account.getIn(['relationship', 'following']) ? messages.unfollow : messages.follow)} onClick={this.props.onFollow} />;
  153. } else if (account.getIn(['relationship', 'blocking'])) {
  154. actionBtn = <Button className='logo-button' text={intl.formatMessage(messages.unblock, { name: account.get('username') })} onClick={this.props.onBlock} />;
  155. }
  156. } else {
  157. actionBtn = <Button className='logo-button' text={intl.formatMessage(messages.edit_profile)} onClick={this.openEditProfile} />;
  158. }
  159. if (account.get('moved') && !account.getIn(['relationship', 'following'])) {
  160. actionBtn = '';
  161. }
  162. if (account.get('locked')) {
  163. lockedIcon = <Icon id='lock' title={intl.formatMessage(messages.account_locked)} />;
  164. }
  165. if (account.get('id') !== me) {
  166. menu.push({ text: intl.formatMessage(messages.mention, { name: account.get('username') }), action: this.props.onMention });
  167. menu.push({ text: intl.formatMessage(messages.direct, { name: account.get('username') }), action: this.props.onDirect });
  168. menu.push(null);
  169. }
  170. if ('share' in navigator) {
  171. menu.push({ text: intl.formatMessage(messages.share, { name: account.get('username') }), action: this.handleShare });
  172. menu.push(null);
  173. }
  174. if (account.get('id') === me) {
  175. menu.push({ text: intl.formatMessage(messages.edit_profile), href: '/settings/profile' });
  176. menu.push({ text: intl.formatMessage(messages.preferences), href: '/settings/preferences' });
  177. menu.push({ text: intl.formatMessage(messages.pins), to: '/pinned' });
  178. menu.push(null);
  179. menu.push({ text: intl.formatMessage(messages.follow_requests), to: '/follow_requests' });
  180. menu.push({ text: intl.formatMessage(messages.favourites), to: '/favourites' });
  181. menu.push({ text: intl.formatMessage(messages.lists), to: '/lists' });
  182. menu.push(null);
  183. menu.push({ text: intl.formatMessage(messages.mutes), to: '/mutes' });
  184. menu.push({ text: intl.formatMessage(messages.blocks), to: '/blocks' });
  185. menu.push({ text: intl.formatMessage(messages.domain_blocks), to: '/domain_blocks' });
  186. } else {
  187. if (account.getIn(['relationship', 'following'])) {
  188. if (!account.getIn(['relationship', 'muting'])) {
  189. if (account.getIn(['relationship', 'showing_reblogs'])) {
  190. menu.push({ text: intl.formatMessage(messages.hideReblogs, { name: account.get('username') }), action: this.props.onReblogToggle });
  191. } else {
  192. menu.push({ text: intl.formatMessage(messages.showReblogs, { name: account.get('username') }), action: this.props.onReblogToggle });
  193. }
  194. }
  195. menu.push({ text: intl.formatMessage(account.getIn(['relationship', 'endorsed']) ? messages.unendorse : messages.endorse), action: this.props.onEndorseToggle });
  196. menu.push({ text: intl.formatMessage(messages.add_or_remove_from_list), action: this.props.onAddToList });
  197. menu.push(null);
  198. }
  199. if (account.getIn(['relationship', 'muting'])) {
  200. menu.push({ text: intl.formatMessage(messages.unmute, { name: account.get('username') }), action: this.props.onMute });
  201. } else {
  202. menu.push({ text: intl.formatMessage(messages.mute, { name: account.get('username') }), action: this.props.onMute });
  203. }
  204. if (account.getIn(['relationship', 'blocking'])) {
  205. menu.push({ text: intl.formatMessage(messages.unblock, { name: account.get('username') }), action: this.props.onBlock });
  206. } else {
  207. menu.push({ text: intl.formatMessage(messages.block, { name: account.get('username') }), action: this.props.onBlock });
  208. }
  209. menu.push({ text: intl.formatMessage(messages.report, { name: account.get('username') }), action: this.props.onReport });
  210. }
  211. if (account.get('acct') !== account.get('username')) {
  212. const domain = account.get('acct').split('@')[1];
  213. menu.push(null);
  214. if (account.getIn(['relationship', 'domain_blocking'])) {
  215. menu.push({ text: intl.formatMessage(messages.unblockDomain, { domain }), action: this.props.onUnblockDomain });
  216. } else {
  217. menu.push({ text: intl.formatMessage(messages.blockDomain, { domain }), action: this.props.onBlockDomain });
  218. }
  219. }
  220. if (account.get('id') !== me && isStaff) {
  221. menu.push(null);
  222. menu.push({ text: intl.formatMessage(messages.admin_account, { name: account.get('username') }), href: `/admin/accounts/${account.get('id')}` });
  223. }
  224. const content = { __html: account.get('note_emojified') };
  225. const displayNameHtml = { __html: account.get('display_name_html') };
  226. const fields = account.get('fields');
  227. const acct = account.get('acct').indexOf('@') === -1 && domain ? `${account.get('acct')}@${domain}` : account.get('acct');
  228. let badge;
  229. if (account.get('bot')) {
  230. badge = (<div className='account-role bot'><FormattedMessage id='account.badges.bot' defaultMessage='Bot' /></div>);
  231. } else if (account.get('group')) {
  232. badge = (<div className='account-role group'><FormattedMessage id='account.badges.group' defaultMessage='Group' /></div>);
  233. } else {
  234. badge = null;
  235. }
  236. return (
  237. <div className={classNames('account__header', { inactive: !!account.get('moved') })} ref={this.setRef}>
  238. <div className='account__header__image'>
  239. <div className='account__header__info'>
  240. {!suspended && info}
  241. </div>
  242. <img src={autoPlayGif ? account.get('header') : account.get('header_static')} alt='' className='parallax' />
  243. </div>
  244. <div className='account__header__bar'>
  245. <div className='account__header__tabs'>
  246. <a className='avatar' href={account.get('url')} rel='noopener noreferrer' target='_blank'>
  247. <Avatar account={account} size={90} />
  248. </a>
  249. <div className='spacer' />
  250. {!suspended && (
  251. <div className='account__header__tabs__buttons'>
  252. {actionBtn}
  253. {bellBtn}
  254. <DropdownMenuContainer items={menu} icon='ellipsis-v' size={24} direction='right' />
  255. </div>
  256. )}
  257. </div>
  258. <div className='account__header__tabs__name'>
  259. <h1>
  260. <span dangerouslySetInnerHTML={displayNameHtml} /> {badge}
  261. <small>@{acct} {lockedIcon}</small>
  262. </h1>
  263. </div>
  264. <div className='account__header__extra'>
  265. <div className='account__header__bio'>
  266. {(fields.size > 0 || identity_proofs.size > 0) && (
  267. <div className='account__header__fields'>
  268. {identity_proofs.map((proof, i) => (
  269. <dl key={i}>
  270. <dt dangerouslySetInnerHTML={{ __html: proof.get('provider') }} />
  271. <dd className='verified'>
  272. <a href={proof.get('proof_url')} target='_blank' rel='noopener noreferrer'><span title={intl.formatMessage(messages.linkVerifiedOn, { date: intl.formatDate(proof.get('updated_at'), dateFormatOptions) })}>
  273. <Icon id='check' className='verified__mark' />
  274. </span></a>
  275. <a href={proof.get('profile_url')} target='_blank' rel='noopener noreferrer'><span dangerouslySetInnerHTML={{ __html: ' '+proof.get('provider_username') }} /></a>
  276. </dd>
  277. </dl>
  278. ))}
  279. {fields.map((pair, i) => (
  280. <dl key={i}>
  281. <dt dangerouslySetInnerHTML={{ __html: pair.get('name_emojified') }} title={pair.get('name')} className='translate' />
  282. <dd className={pair.get('verified_at') && 'verified'} title={pair.get('value_plain')} className='translate'>
  283. {pair.get('verified_at') && <span title={intl.formatMessage(messages.linkVerifiedOn, { date: intl.formatDate(pair.get('verified_at'), dateFormatOptions) })}><Icon id='check' className='verified__mark' /></span>} <span dangerouslySetInnerHTML={{ __html: pair.get('value_emojified') }} />
  284. </dd>
  285. </dl>
  286. ))}
  287. </div>
  288. )}
  289. {account.get('id') !== me && !suspended && <AccountNoteContainer account={account} />}
  290. {account.get('note').length > 0 && account.get('note') !== '<p></p>' && <div className='account__header__content translate' dangerouslySetInnerHTML={content} />}
  291. </div>
  292. {!suspended && (
  293. <div className='account__header__extra__links'>
  294. <NavLink isActive={this.isStatusesPageActive} activeClassName='active' to={`/accounts/${account.get('id')}`} title={intl.formatNumber(account.get('statuses_count'))}>
  295. <ShortNumber
  296. value={account.get('statuses_count')}
  297. renderer={counterRenderer('statuses')}
  298. />
  299. </NavLink>
  300. <NavLink exact activeClassName='active' to={`/accounts/${account.get('id')}/following`} title={intl.formatNumber(account.get('following_count'))}>
  301. <ShortNumber
  302. value={account.get('following_count')}
  303. renderer={counterRenderer('following')}
  304. />
  305. </NavLink>
  306. <NavLink exact activeClassName='active' to={`/accounts/${account.get('id')}/followers`} title={intl.formatNumber(account.get('followers_count'))}>
  307. <ShortNumber
  308. value={account.get('followers_count')}
  309. renderer={counterRenderer('followers')}
  310. />
  311. </NavLink>
  312. </div>
  313. )}
  314. </div>
  315. </div>
  316. </div>
  317. );
  318. }
  319. }