status_action_bar.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import { connect } from 'react-redux';
  4. import PropTypes from 'prop-types';
  5. import IconButton from './icon_button';
  6. import DropdownMenuContainer from '../containers/dropdown_menu_container';
  7. import { defineMessages, injectIntl } from 'react-intl';
  8. import ImmutablePureComponent from 'react-immutable-pure-component';
  9. import { me } from '../initial_state';
  10. import classNames from 'classnames';
  11. import { PERMISSION_MANAGE_USERS } from 'mastodon/permissions';
  12. const messages = defineMessages({
  13. delete: { id: 'status.delete', defaultMessage: 'Delete' },
  14. redraft: { id: 'status.redraft', defaultMessage: 'Delete & re-draft' },
  15. edit: { id: 'status.edit', defaultMessage: 'Edit' },
  16. direct: { id: 'status.direct', defaultMessage: 'Direct message @{name}' },
  17. mention: { id: 'status.mention', defaultMessage: 'Mention @{name}' },
  18. mute: { id: 'account.mute', defaultMessage: 'Mute @{name}' },
  19. block: { id: 'account.block', defaultMessage: 'Block @{name}' },
  20. reply: { id: 'status.reply', defaultMessage: 'Reply' },
  21. share: { id: 'status.share', defaultMessage: 'Share' },
  22. more: { id: 'status.more', defaultMessage: 'More' },
  23. replyAll: { id: 'status.replyAll', defaultMessage: 'Reply to thread' },
  24. reblog: { id: 'status.reblog', defaultMessage: 'Boost' },
  25. reblog_private: { id: 'status.reblog_private', defaultMessage: 'Boost with original visibility' },
  26. cancel_reblog_private: { id: 'status.cancel_reblog_private', defaultMessage: 'Unboost' },
  27. cannot_reblog: { id: 'status.cannot_reblog', defaultMessage: 'This post cannot be boosted' },
  28. favourite: { id: 'status.favourite', defaultMessage: 'Favourite' },
  29. bookmark: { id: 'status.bookmark', defaultMessage: 'Bookmark' },
  30. removeBookmark: { id: 'status.remove_bookmark', defaultMessage: 'Remove bookmark' },
  31. open: { id: 'status.open', defaultMessage: 'Expand this status' },
  32. report: { id: 'status.report', defaultMessage: 'Report @{name}' },
  33. muteConversation: { id: 'status.mute_conversation', defaultMessage: 'Mute conversation' },
  34. unmuteConversation: { id: 'status.unmute_conversation', defaultMessage: 'Unmute conversation' },
  35. pin: { id: 'status.pin', defaultMessage: 'Pin on profile' },
  36. unpin: { id: 'status.unpin', defaultMessage: 'Unpin from profile' },
  37. embed: { id: 'status.embed', defaultMessage: 'Embed' },
  38. admin_account: { id: 'status.admin_account', defaultMessage: 'Open moderation interface for @{name}' },
  39. admin_status: { id: 'status.admin_status', defaultMessage: 'Open this status in the moderation interface' },
  40. copy: { id: 'status.copy', defaultMessage: 'Copy link to status' },
  41. hide: { id: 'status.hide', defaultMessage: 'Hide toot' },
  42. blockDomain: { id: 'account.block_domain', defaultMessage: 'Block domain {domain}' },
  43. unblockDomain: { id: 'account.unblock_domain', defaultMessage: 'Unblock domain {domain}' },
  44. unmute: { id: 'account.unmute', defaultMessage: 'Unmute @{name}' },
  45. unblock: { id: 'account.unblock', defaultMessage: 'Unblock @{name}' },
  46. });
  47. const mapStateToProps = (state, { status }) => ({
  48. relationship: state.getIn(['relationships', status.getIn(['account', 'id'])]),
  49. });
  50. export default @connect(mapStateToProps)
  51. @injectIntl
  52. class StatusActionBar extends ImmutablePureComponent {
  53. static contextTypes = {
  54. router: PropTypes.object,
  55. identity: PropTypes.object,
  56. };
  57. static propTypes = {
  58. status: ImmutablePropTypes.map.isRequired,
  59. relationship: ImmutablePropTypes.map,
  60. onReply: PropTypes.func,
  61. onFavourite: PropTypes.func,
  62. onReblog: PropTypes.func,
  63. onDelete: PropTypes.func,
  64. onDirect: PropTypes.func,
  65. onMention: PropTypes.func,
  66. onMute: PropTypes.func,
  67. onUnmute: PropTypes.func,
  68. onBlock: PropTypes.func,
  69. onUnblock: PropTypes.func,
  70. onBlockDomain: PropTypes.func,
  71. onUnblockDomain: PropTypes.func,
  72. onReport: PropTypes.func,
  73. onEmbed: PropTypes.func,
  74. onMuteConversation: PropTypes.func,
  75. onPin: PropTypes.func,
  76. onBookmark: PropTypes.func,
  77. onFilter: PropTypes.func,
  78. withDismiss: PropTypes.bool,
  79. withCounters: PropTypes.bool,
  80. scrollKey: PropTypes.string,
  81. intl: PropTypes.object.isRequired,
  82. };
  83. // Avoid checking props that are functions (and whose equality will always
  84. // evaluate to false. See react-immutable-pure-component for usage.
  85. updateOnProps = [
  86. 'status',
  87. 'relationship',
  88. 'withDismiss',
  89. ]
  90. handleReplyClick = () => {
  91. if (me) {
  92. this.props.onReply(this.props.status, this.context.router.history);
  93. } else {
  94. this._openInteractionDialog('reply');
  95. }
  96. }
  97. handleShareClick = () => {
  98. navigator.share({
  99. text: this.props.status.get('search_index'),
  100. url: this.props.status.get('url'),
  101. }).catch((e) => {
  102. if (e.name !== 'AbortError') console.error(e);
  103. });
  104. }
  105. handleFavouriteClick = () => {
  106. if (me) {
  107. this.props.onFavourite(this.props.status);
  108. } else {
  109. this._openInteractionDialog('favourite');
  110. }
  111. }
  112. handleReblogClick = e => {
  113. if (me) {
  114. this.props.onReblog(this.props.status, e);
  115. } else {
  116. this._openInteractionDialog('reblog');
  117. }
  118. }
  119. _openInteractionDialog = type => {
  120. window.open(`/interact/${this.props.status.get('id')}?type=${type}`, 'mastodon-intent', 'width=445,height=600,resizable=no,menubar=no,status=no,scrollbars=yes');
  121. }
  122. handleBookmarkClick = () => {
  123. this.props.onBookmark(this.props.status);
  124. }
  125. handleDeleteClick = () => {
  126. this.props.onDelete(this.props.status, this.context.router.history);
  127. }
  128. handleRedraftClick = () => {
  129. this.props.onDelete(this.props.status, this.context.router.history, true);
  130. }
  131. handleEditClick = () => {
  132. this.props.onEdit(this.props.status, this.context.router.history);
  133. }
  134. handlePinClick = () => {
  135. this.props.onPin(this.props.status);
  136. }
  137. handleMentionClick = () => {
  138. this.props.onMention(this.props.status.get('account'), this.context.router.history);
  139. }
  140. handleDirectClick = () => {
  141. this.props.onDirect(this.props.status.get('account'), this.context.router.history);
  142. }
  143. handleMuteClick = () => {
  144. const { status, relationship, onMute, onUnmute } = this.props;
  145. const account = status.get('account');
  146. if (relationship && relationship.get('muting')) {
  147. onUnmute(account);
  148. } else {
  149. onMute(account);
  150. }
  151. }
  152. handleBlockClick = () => {
  153. const { status, relationship, onBlock, onUnblock } = this.props;
  154. const account = status.get('account');
  155. if (relationship && relationship.get('blocking')) {
  156. onUnblock(account);
  157. } else {
  158. onBlock(status);
  159. }
  160. }
  161. handleBlockDomain = () => {
  162. const { status, onBlockDomain } = this.props;
  163. const account = status.get('account');
  164. onBlockDomain(account.get('acct').split('@')[1]);
  165. }
  166. handleUnblockDomain = () => {
  167. const { status, onUnblockDomain } = this.props;
  168. const account = status.get('account');
  169. onUnblockDomain(account.get('acct').split('@')[1]);
  170. }
  171. handleOpen = () => {
  172. this.context.router.history.push(`/@${this.props.status.getIn(['account', 'acct'])}/${this.props.status.get('id')}`);
  173. }
  174. handleEmbed = () => {
  175. this.props.onEmbed(this.props.status);
  176. }
  177. handleReport = () => {
  178. this.props.onReport(this.props.status);
  179. }
  180. handleConversationMuteClick = () => {
  181. this.props.onMuteConversation(this.props.status);
  182. }
  183. handleFilter = () => {
  184. this.props.onFilter();
  185. }
  186. handleCopy = () => {
  187. const url = this.props.status.get('url');
  188. const textarea = document.createElement('textarea');
  189. textarea.textContent = url;
  190. textarea.style.position = 'fixed';
  191. document.body.appendChild(textarea);
  192. try {
  193. textarea.select();
  194. document.execCommand('copy');
  195. } catch (e) {
  196. } finally {
  197. document.body.removeChild(textarea);
  198. }
  199. }
  200. handleFilterClick = () => {
  201. this.props.onFilter();
  202. }
  203. render () {
  204. const { status, relationship, intl, withDismiss, withCounters, scrollKey } = this.props;
  205. const anonymousAccess = !me;
  206. const publicStatus = ['public', 'unlisted'].includes(status.get('visibility'));
  207. const pinnableStatus = ['public', 'unlisted', 'private'].includes(status.get('visibility'));
  208. const mutingConversation = status.get('muted');
  209. const account = status.get('account');
  210. const writtenByMe = status.getIn(['account', 'id']) === me;
  211. let menu = [];
  212. menu.push({ text: intl.formatMessage(messages.open), action: this.handleOpen });
  213. if (publicStatus) {
  214. menu.push({ text: intl.formatMessage(messages.copy), action: this.handleCopy });
  215. menu.push({ text: intl.formatMessage(messages.embed), action: this.handleEmbed });
  216. }
  217. menu.push(null);
  218. menu.push({ text: intl.formatMessage(status.get('bookmarked') ? messages.removeBookmark : messages.bookmark), action: this.handleBookmarkClick });
  219. if (writtenByMe && pinnableStatus) {
  220. menu.push({ text: intl.formatMessage(status.get('pinned') ? messages.unpin : messages.pin), action: this.handlePinClick });
  221. }
  222. menu.push(null);
  223. if (writtenByMe || withDismiss) {
  224. menu.push({ text: intl.formatMessage(mutingConversation ? messages.unmuteConversation : messages.muteConversation), action: this.handleConversationMuteClick });
  225. menu.push(null);
  226. }
  227. if (writtenByMe) {
  228. // menu.push({ text: intl.formatMessage(messages.edit), action: this.handleEditClick });
  229. menu.push({ text: intl.formatMessage(messages.delete), action: this.handleDeleteClick });
  230. menu.push({ text: intl.formatMessage(messages.redraft), action: this.handleRedraftClick });
  231. } else {
  232. menu.push({ text: intl.formatMessage(messages.mention, { name: account.get('username') }), action: this.handleMentionClick });
  233. menu.push({ text: intl.formatMessage(messages.direct, { name: account.get('username') }), action: this.handleDirectClick });
  234. menu.push(null);
  235. if (relationship && relationship.get('muting')) {
  236. menu.push({ text: intl.formatMessage(messages.unmute, { name: account.get('username') }), action: this.handleMuteClick });
  237. } else {
  238. menu.push({ text: intl.formatMessage(messages.mute, { name: account.get('username') }), action: this.handleMuteClick });
  239. }
  240. if (relationship && relationship.get('blocking')) {
  241. menu.push({ text: intl.formatMessage(messages.unblock, { name: account.get('username') }), action: this.handleBlockClick });
  242. } else {
  243. menu.push({ text: intl.formatMessage(messages.block, { name: account.get('username') }), action: this.handleBlockClick });
  244. }
  245. menu.push({ text: intl.formatMessage(messages.report, { name: account.get('username') }), action: this.handleReport });
  246. if (account.get('acct') !== account.get('username')) {
  247. const domain = account.get('acct').split('@')[1];
  248. menu.push(null);
  249. if (relationship && relationship.get('domain_blocking')) {
  250. menu.push({ text: intl.formatMessage(messages.unblockDomain, { domain }), action: this.handleUnblockDomain });
  251. } else {
  252. menu.push({ text: intl.formatMessage(messages.blockDomain, { domain }), action: this.handleBlockDomain });
  253. }
  254. }
  255. if ((this.context.identity.permissions & PERMISSION_MANAGE_USERS) === PERMISSION_MANAGE_USERS) {
  256. menu.push(null);
  257. menu.push({ text: intl.formatMessage(messages.admin_account, { name: account.get('username') }), href: `/admin/accounts/${status.getIn(['account', 'id'])}` });
  258. menu.push({ text: intl.formatMessage(messages.admin_status), href: `/admin/accounts/${status.getIn(['account', 'id'])}/statuses?id=${status.get('id')}` });
  259. }
  260. }
  261. let replyIcon;
  262. let replyTitle;
  263. if (status.get('in_reply_to_id', null) === null) {
  264. replyIcon = 'reply';
  265. replyTitle = intl.formatMessage(messages.reply);
  266. } else {
  267. replyIcon = 'reply-all';
  268. replyTitle = intl.formatMessage(messages.replyAll);
  269. }
  270. const reblogPrivate = status.getIn(['account', 'id']) === me && status.get('visibility') === 'private';
  271. let reblogTitle = '';
  272. if (status.get('reblogged')) {
  273. reblogTitle = intl.formatMessage(messages.cancel_reblog_private);
  274. } else if (publicStatus) {
  275. reblogTitle = intl.formatMessage(messages.reblog);
  276. } else if (reblogPrivate) {
  277. reblogTitle = intl.formatMessage(messages.reblog_private);
  278. } else {
  279. reblogTitle = intl.formatMessage(messages.cannot_reblog);
  280. }
  281. const shareButton = ('share' in navigator) && publicStatus && (
  282. <IconButton className='status__action-bar-button' title={intl.formatMessage(messages.share)} icon='share-alt' onClick={this.handleShareClick} />
  283. );
  284. const filterButton = this.props.onFilter && (
  285. <IconButton className='status__action-bar-button' title={intl.formatMessage(messages.hide)} icon='eye' onClick={this.handleFilterClick} />
  286. );
  287. return (
  288. <div className='status__action-bar'>
  289. <IconButton className='status__action-bar-button' title={replyTitle} icon={status.get('in_reply_to_account_id') === status.getIn(['account', 'id']) ? 'reply' : replyIcon} onClick={this.handleReplyClick} counter={status.get('replies_count')} obfuscateCount />
  290. <IconButton className={classNames('status__action-bar-button', { reblogPrivate })} disabled={!publicStatus && !reblogPrivate} active={status.get('reblogged')} pressed={status.get('reblogged')} title={reblogTitle} icon='retweet' onClick={this.handleReblogClick} counter={withCounters ? status.get('reblogs_count') : undefined} />
  291. <IconButton className='status__action-bar-button star-icon' animate active={status.get('favourited')} pressed={status.get('favourited')} title={intl.formatMessage(messages.favourite)} icon='star' onClick={this.handleFavouriteClick} counter={withCounters ? status.get('favourites_count') : undefined} />
  292. {shareButton}
  293. {filterButton}
  294. <div className='status__action-bar-dropdown'>
  295. <DropdownMenuContainer
  296. scrollKey={scrollKey}
  297. disabled={anonymousAccess}
  298. status={status}
  299. items={menu}
  300. icon='ellipsis-h'
  301. size={18}
  302. direction='right'
  303. title={intl.formatMessage(messages.more)}
  304. />
  305. </div>
  306. </div>
  307. );
  308. }
  309. }