闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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