From 13af58da7a7f924ade5dda8bea97463d0b7a31fa Mon Sep 17 00:00:00 2001 From: mgabdev <> Date: Fri, 24 Jul 2020 18:48:31 -0500 Subject: [PATCH] Added bookmarks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Added: - bookmarks for GabPRO members only - migration for creation of StatusBookmarks - all necessary routes, controllers - redux for adding, removing, fetching and displaying bookmarks - bookmark icon - doorkeeper scopes - backend and frontend support Bookmarks behave like likes/favorites, except they aren't shared with other users and do not have an associated counter. https://github.com/tootsuite/mastodon/commit/dfea7368c934f600bd0b6b93b4a6c008a4e265b0 --- .../api/v1/bookmarks_controller.rb | 69 +++++++++++++ .../api/v1/statuses/bookmarks_controller.rb | 47 +++++++++ app/javascript/gabsocial/actions/bookmarks.js | 98 +++++++++++++++++++ .../gabsocial/actions/interactions.js | 82 +++++++++++++++- .../gabsocial/assets/bookmark_icon.js | 24 +++++ app/javascript/gabsocial/components/icon.js | 2 + .../popover/status_options_popover.js | 44 ++++++++- .../gabsocial/features/bookmarked_statuses.js | 59 +++++++++++ app/javascript/gabsocial/features/ui/ui.js | 4 + .../features/ui/util/async_components.js | 1 + .../gabsocial/reducers/status_lists.js | 31 +++++- app/models/concerns/account_associations.rb | 4 + app/models/concerns/account_interactions.rb | 4 + app/models/status.rb | 6 ++ app/models/status_bookmark.rb | 24 +++++ .../status_relationships_presenter.rb | 5 +- app/serializers/rest/status_serializer.rb | 9 ++ app/services/suspend_account_service.rb | 1 + config/initializers/doorkeeper.rb | 2 + config/locales/doorkeeper.en.yml | 2 + config/routes.rb | 4 + .../20200722190627_create_status_bookmark.rb | 11 +++ 22 files changed, 528 insertions(+), 5 deletions(-) create mode 100644 app/controllers/api/v1/bookmarks_controller.rb create mode 100644 app/controllers/api/v1/statuses/bookmarks_controller.rb create mode 100644 app/javascript/gabsocial/actions/bookmarks.js create mode 100644 app/javascript/gabsocial/assets/bookmark_icon.js create mode 100644 app/javascript/gabsocial/features/bookmarked_statuses.js create mode 100644 app/models/status_bookmark.rb create mode 100644 db/migrate/20200722190627_create_status_bookmark.rb diff --git a/app/controllers/api/v1/bookmarks_controller.rb b/app/controllers/api/v1/bookmarks_controller.rb new file mode 100644 index 00000000..b3ef2fd9 --- /dev/null +++ b/app/controllers/api/v1/bookmarks_controller.rb @@ -0,0 +1,69 @@ +# frozen_string_literal: true + +class Api::V1::BookmarksController < Api::BaseController + before_action -> { doorkeeper_authorize! :read, :'read:bookmarks' } + before_action :require_user! + after_action :insert_pagination_headers + + respond_to :json + + def index + @statuses = [] + if current_account.is_pro + @statuses = load_statuses + end + render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id) + end + + private + + def load_statuses + cached_bookmarks + end + + def cached_bookmarks + cache_collection( + Status.reorder(nil).joins(:status_bookmarks).merge(results), + Status + ) + end + + def results + @_results ||= account_bookmarks.paginate_by_id( + limit_param(DEFAULT_STATUSES_LIMIT), + params_slice(:max_id, :since_id, :min_id) + ) + end + + def account_bookmarks + current_account.status_bookmarks + end + + def insert_pagination_headers + set_pagination_headers(next_path, prev_path) + end + + def next_path + api_v1_bookmarks_url pagination_params(max_id: pagination_max_id) if records_continue? + end + + def prev_path + api_v1_bookmarks_url pagination_params(since_id: pagination_since_id) unless results.empty? + end + + def pagination_max_id + results.last.id + end + + def pagination_since_id + results.first.id + end + + def records_continue? + results.size == limit_param(DEFAULT_STATUSES_LIMIT) + end + + def pagination_params(core_params) + params.slice(:limit).permit(:limit).merge(core_params) + end +end \ No newline at end of file diff --git a/app/controllers/api/v1/statuses/bookmarks_controller.rb b/app/controllers/api/v1/statuses/bookmarks_controller.rb new file mode 100644 index 00000000..6dbb74c7 --- /dev/null +++ b/app/controllers/api/v1/statuses/bookmarks_controller.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +class Api::V1::Statuses::BookmarksController < Api::BaseController + include Authorization + + before_action -> { doorkeeper_authorize! :write, :'write:bookmarks' } + before_action :require_user! + + respond_to :json + + def create + if current_user.account.is_pro + @status = bookmarked_status + render json: @status, serializer: REST::StatusSerializer + else + render json: { error: 'You need to be a GabPRO member to access this' }, status: 422 + end + end + + def destroy + if current_user.account.is_pro + @status = requested_status + @bookmarks_map = { @status.id => false } + + bookmark = StatusBookmark.find_by!(account: current_user.account, status: @status) + bookmark.destroy! + + render json: @status, serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new([@status], current_user&.account_id, bookmarks_map: @bookmarks_map) + else + render json: { error: 'You need to be a GabPRO member to access this' }, status: 422 + end + end + + private + + def bookmarked_status + authorize_with current_user.account, requested_status, :show? + + bookmark = StatusBookmark.find_or_create_by!(account: current_user.account, status: requested_status) + + bookmark.status.reload + end + + def requested_status + Status.find(params[:status_id]) + end +end \ No newline at end of file diff --git a/app/javascript/gabsocial/actions/bookmarks.js b/app/javascript/gabsocial/actions/bookmarks.js new file mode 100644 index 00000000..6bbce6dd --- /dev/null +++ b/app/javascript/gabsocial/actions/bookmarks.js @@ -0,0 +1,98 @@ +import api, { getLinks } from '../api' +import { importFetchedStatuses } from './importer' +import { me } from '../initial_state' + +export const BOOKMARKED_STATUSES_FETCH_REQUEST = 'BOOKMARKED_STATUSES_FETCH_REQUEST' +export const BOOKMARKED_STATUSES_FETCH_SUCCESS = 'BOOKMARKED_STATUSES_FETCH_SUCCESS' +export const BOOKMARKED_STATUSES_FETCH_FAIL = 'BOOKMARKED_STATUSES_FETCH_FAIL' + +export const BOOKMARKED_STATUSES_EXPAND_REQUEST = 'BOOKMARKED_STATUSES_EXPAND_REQUEST' +export const BOOKMARKED_STATUSES_EXPAND_SUCCESS = 'BOOKMARKED_STATUSES_EXPAND_SUCCESS' +export const BOOKMARKED_STATUSES_EXPAND_FAIL = 'BOOKMARKED_STATUSES_EXPAND_FAIL' + +export function fetchBookmarkedStatuses() { + return (dispatch, getState) => { + if (!me) return + + if (getState().getIn(['status_lists', 'bookmarks', 'isLoading'])) { + return + } + + dispatch(fetchBookmarkedStatusesRequest()) + + api(getState).get('/api/v1/bookmarks').then(response => { + const next = getLinks(response).refs.find(link => link.rel === 'next') + dispatch(importFetchedStatuses(response.data)) + dispatch(fetchBookmarkedStatusesSuccess(response.data, next ? next.uri : null)) + }).catch(error => { + dispatch(fetchBookmarkedStatusesFail(error)) + }) + } +} + +export function fetchBookmarkedStatusesRequest() { + return { + type: BOOKMARKED_STATUSES_FETCH_REQUEST, + skipLoading: true, + } +} + +export function fetchBookmarkedStatusesSuccess(statuses, next) { + return { + type: BOOKMARKED_STATUSES_FETCH_SUCCESS, + statuses, + next, + skipLoading: true, + } +} + +export function fetchBookmarkedStatusesFail(error) { + return { + type: BOOKMARKED_STATUSES_FETCH_FAIL, + error, + skipLoading: true, + } +} + +export function expandBookmarkedStatuses() { + return (dispatch, getState) => { + if (!me) return + + const url = getState().getIn(['status_lists', 'bookmarks', 'next'], null) + + if (url === null || getState().getIn(['status_lists', 'bookmarks', 'isLoading'])) { + return + } + + dispatch(expandBookmarkedStatusesRequest()) + + api(getState).get(url).then(response => { + const next = getLinks(response).refs.find(link => link.rel === 'next') + dispatch(importFetchedStatuses(response.data)) + dispatch(expandBookmarkedStatusesSuccess(response.data, next ? next.uri : null)) + }).catch(error => { + dispatch(expandBookmarkedStatusesFail(error)) + }) + } +} + +export function expandBookmarkedStatusesRequest() { + return { + type: BOOKMARKED_STATUSES_EXPAND_REQUEST, + } +} + +export function expandBookmarkedStatusesSuccess(statuses, next) { + return { + type: BOOKMARKED_STATUSES_EXPAND_SUCCESS, + statuses, + next, + } +} + +export function expandBookmarkedStatusesFail(error) { + return { + type: BOOKMARKED_STATUSES_EXPAND_FAIL, + error, + } +} diff --git a/app/javascript/gabsocial/actions/interactions.js b/app/javascript/gabsocial/actions/interactions.js index f08421f2..6d3d5f95 100644 --- a/app/javascript/gabsocial/actions/interactions.js +++ b/app/javascript/gabsocial/actions/interactions.js @@ -30,6 +30,14 @@ export const UNPIN_REQUEST = 'UNPIN_REQUEST'; export const UNPIN_SUCCESS = 'UNPIN_SUCCESS'; export const UNPIN_FAIL = 'UNPIN_FAIL'; +export const BOOKMARK_REQUEST = 'BOOKMARK_REQUEST' +export const BOOKMARK_SUCCESS = 'BOOKMARK_SUCCESS' +export const BOOKMARK_FAIL = 'BOOKMARK_FAIL' + +export const UNBOOKMARK_REQUEST = 'UNBOOKMARK_REQUEST' +export const UNBOOKMARK_SUCCESS = 'UNBOOKMARK_SUCCESS' +export const UNBOOKMARK_FAIL = 'UNBOOKMARK_FAIL' + export const LIKES_FETCH_REQUEST = 'LIKES_FETCH_REQUEST'; export const LIKES_FETCH_SUCCESS = 'LIKES_FETCH_SUCCESS'; export const LIKES_FETCH_FAIL = 'LIKES_FETCH_FAIL'; @@ -346,4 +354,76 @@ export function fetchLikesFail(id, error) { type: LIKES_FETCH_FAIL, error, }; -}; \ No newline at end of file +}; + +export function bookmark(status) { + return function (dispatch, getState) { + dispatch(bookmarkRequest(status)); + + api(getState).post(`/api/v1/statuses/${status.get('id')}/bookmark`).then(function (response) { + dispatch(importFetchedStatus(response.data)); + dispatch(bookmarkSuccess(status, response.data)); + }).catch(function (error) { + dispatch(bookmarkFail(status, error)) + }) + } +} + +export function unbookmark(status) { + return (dispatch, getState) => { + dispatch(unbookmarkRequest(status)) + + api(getState).post(`/api/v1/statuses/${status.get('id')}/unbookmark`).then(response => { + dispatch(importFetchedStatus(response.data)) + dispatch(unbookmarkSuccess(status, response.data)) + }).catch(error => { + dispatch(unbookmarkFail(status, error)) + }) + } +} + +export function bookmarkRequest(status) { + return { + type: BOOKMARK_REQUEST, + status: status, + } +} + +export function bookmarkSuccess(status, response) { + return { + type: BOOKMARK_SUCCESS, + status: status, + response: response, + } +} + +export function bookmarkFail(status, error) { + return { + type: BOOKMARK_FAIL, + status: status, + error: error, + } +} + +export function unbookmarkRequest(status) { + return { + type: UNBOOKMARK_REQUEST, + status: status, + } +} + +export function unbookmarkSuccess(status, response) { + return { + type: UNBOOKMARK_SUCCESS, + status: status, + response: response, + } +} + +export function unbookmarkFail(status, error) { + return { + type: UNBOOKMARK_FAIL, + status: status, + error: error, + } +} \ No newline at end of file diff --git a/app/javascript/gabsocial/assets/bookmark_icon.js b/app/javascript/gabsocial/assets/bookmark_icon.js new file mode 100644 index 00000000..b00369be --- /dev/null +++ b/app/javascript/gabsocial/assets/bookmark_icon.js @@ -0,0 +1,24 @@ +const BookmarkIcon = ({ + className = '', + size = '16px', + title = 'Bookmark', +}) => ( + + + + + +) + +export default BookmarkIcon \ No newline at end of file diff --git a/app/javascript/gabsocial/components/icon.js b/app/javascript/gabsocial/components/icon.js index 9408edfe..80a2c522 100644 --- a/app/javascript/gabsocial/components/icon.js +++ b/app/javascript/gabsocial/components/icon.js @@ -11,6 +11,7 @@ import BackIcon from '../assets/back_icon' import BlockIcon from '../assets/block_icon' import BlockquoteIcon from '../assets/blockquote_icon' import BoldIcon from '../assets/bold_icon' +import BookmarkIcon from '../assets/bookmark_icon' import CalendarIcon from '../assets/calendar_icon' import ChatIcon from '../assets/chat_icon' import CheckIcon from '../assets/check_icon' @@ -93,6 +94,7 @@ const ICONS = { 'block': BlockIcon, 'blockquote': BlockquoteIcon, 'bold': BoldIcon, + 'bookmark': BookmarkIcon, 'calendar': CalendarIcon, 'chat': ChatIcon, 'check': CheckIcon, diff --git a/app/javascript/gabsocial/components/popover/status_options_popover.js b/app/javascript/gabsocial/components/popover/status_options_popover.js index 9157dd61..68a3bba8 100644 --- a/app/javascript/gabsocial/components/popover/status_options_popover.js +++ b/app/javascript/gabsocial/components/popover/status_options_popover.js @@ -7,6 +7,8 @@ import { unrepost, pin, unpin, + bookmark, + unbookmark, } from '../../actions/interactions'; import { muteStatus, @@ -24,7 +26,10 @@ import { initMuteModal } from '../../actions/mutes' import { initReport } from '../../actions/reports' import { openModal } from '../../actions/modal' import { closePopover } from '../../actions/popover' -import { MODAL_EMBED } from '../../constants' +import { + MODAL_EMBED, + MODAL_PRO_UPGRADE, +} from '../../constants' import PopoverLayout from './popover_layout' import List from '../list' @@ -49,6 +54,8 @@ const messages = defineMessages({ unmuteConversation: { id: 'status.unmute_conversation', defaultMessage: 'Unmute conversation' }, pin: { id: 'status.pin', defaultMessage: 'Pin on profile' }, unpin: { id: 'status.unpin', defaultMessage: 'Unpin from profile' }, + bookmark: { id: 'status.bookmark', defaultMessage: 'Bookmark status' }, + unbookmark: { id: 'status.unbookmark', defaultMessage: 'Remove bookmark' }, admin_account: { id: 'status.admin_account', defaultMessage: 'Open moderation interface for @{name}' }, admin_status: { id: 'status.admin_status', defaultMessage: 'Open this status in the moderation interface' }, group_remove_account: { id: 'status.remove_account_from_group', defaultMessage: 'Remove account from group' }, @@ -68,6 +75,7 @@ const mapStateToProps = (state, { status }) => { return { groupId, groupRelationships, + isPro: state.getIn(['accounts', me, 'is_pro']), } } @@ -93,6 +101,16 @@ const mapDispatchToProps = (dispatch) => ({ } }, + onBookmark(status) { + dispatch(closePopover()) + + if (status.get('bookmarked')) { + dispatch(unbookmark(status)) + } else { + dispatch(bookmark(status)) + } + }, + onQuote(status, router) { dispatch(closePopover()) @@ -186,6 +204,11 @@ const mapDispatchToProps = (dispatch) => ({ })) }, + onOpenProUpgradeModal() { + dispatch(closePopover()) + dispatch(openModal(MODAL_PRO_UPGRADE)) + }, + onClosePopover: () => dispatch(closePopover()), }) @@ -213,8 +236,10 @@ class StatusOptionsPopover extends ImmutablePureComponent { intl: PropTypes.object.isRequired, onFetchGroupRelationships: PropTypes.func.isRequired, onOpenEmbedModal: PropTypes.func.isRequired, + onOpenProUpgradeModal: PropTypes.func.isRequired, onClosePopover: PropTypes.func.isRequired, isXS: PropTypes.bool, + isPro: PropTypes.bool, } updateOnProps = [ @@ -261,6 +286,14 @@ class StatusOptionsPopover extends ImmutablePureComponent { this.props.onPin(this.props.status) } + handleBookmarkClick = () => { + if (this.props.isPro) { + this.props.onBookmark(this.props.status) + } else { + this.props.onOpenProUpgradeModal() + } + } + handleDeleteClick = () => { this.props.onDelete(this.props.status) } @@ -346,6 +379,13 @@ class StatusOptionsPopover extends ImmutablePureComponent { }) } + menu.push({ + icon: 'bookmark', + hideArrow: true, + title: intl.formatMessage(status.get('bookmarked') ? messages.unbookmark : messages.bookmark), + onClick: this.handleBookmarkClick, + }) + if (status.getIn(['account', 'id']) === me) { if (publicStatus) { menu.push({ @@ -355,7 +395,7 @@ class StatusOptionsPopover extends ImmutablePureComponent { onClick: this.handlePinClick, }) } - + menu.push({ icon: 'trash', hideArrow: true, diff --git a/app/javascript/gabsocial/features/bookmarked_statuses.js b/app/javascript/gabsocial/features/bookmarked_statuses.js new file mode 100644 index 00000000..b5d7edbf --- /dev/null +++ b/app/javascript/gabsocial/features/bookmarked_statuses.js @@ -0,0 +1,59 @@ +import ImmutablePropTypes from 'react-immutable-proptypes' +import { FormattedMessage } from 'react-intl' +import ImmutablePureComponent from 'react-immutable-pure-component' +import debounce from 'lodash.debounce' +import { fetchBookmarkedStatuses, expandBookmarkedStatuses } from '../actions/bookmarks' +import { meUsername } from '../initial_state' +import StatusList from '../components/status_list' +import ColumnIndicator from '../components/column_indicator' + +const mapStateToProps = (state, { params: { username } }) => { + return { + isMyAccount: (username.toLowerCase() === meUsername.toLowerCase()), + statusIds: state.getIn(['status_lists', 'bookmarks', 'items']), + isLoading: state.getIn(['status_lists', 'bookmarks', 'isLoading'], true), + hasMore: !!state.getIn(['status_lists', 'bookmarks', 'next']), + } +} + +export default +@connect(mapStateToProps) +class BookmarkedStatuses extends ImmutablePureComponent { + + static propTypes = { + dispatch: PropTypes.func.isRequired, + statusIds: ImmutablePropTypes.list.isRequired, + hasMore: PropTypes.bool, + isLoading: PropTypes.bool, + isMyAccount: PropTypes.bool.isRequired, + } + + componentWillMount() { + this.props.dispatch(fetchBookmarkedStatuses()) + } + + handleLoadMore = debounce(() => { + this.props.dispatch(expandBookmarkedStatuses()) + }, 300, { leading: true }) + + render() { + const { statusIds, hasMore, isLoading, isMyAccount } = this.props + + if (!isMyAccount) { + return + } + + return ( + } + /> + ) + } + +} + diff --git a/app/javascript/gabsocial/features/ui/ui.js b/app/javascript/gabsocial/features/ui/ui.js index 3fbc3c69..88727766 100644 --- a/app/javascript/gabsocial/features/ui/ui.js +++ b/app/javascript/gabsocial/features/ui/ui.js @@ -48,6 +48,7 @@ import { AccountTimeline, BlockedAccounts, BlockedDomains, + BookmarkedStatuses, CommunityTimeline, Compose, DMCA, @@ -228,6 +229,9 @@ class SwitchingArea extends PureComponent { + + + diff --git a/app/javascript/gabsocial/features/ui/util/async_components.js b/app/javascript/gabsocial/features/ui/util/async_components.js index 1005408e..c2abbade 100644 --- a/app/javascript/gabsocial/features/ui/util/async_components.js +++ b/app/javascript/gabsocial/features/ui/util/async_components.js @@ -3,6 +3,7 @@ export function AccountTimeline() { return import(/* webpackChunkName: "features export function AccountGallery() { return import(/* webpackChunkName: "features/account_gallery" */'../../account_gallery') } export function BlockAccountModal() { return import(/* webpackChunkName: "components/block_account_modal" */'../../../components/modal/block_account_modal') } export function BlockedAccounts() { return import(/* webpackChunkName: "features/blocked_accounts" */'../../blocked_accounts') } +export function BookmarkedStatuses() { return import(/* webpackChunkName: "features/bookmarked_statuses" */'../../bookmarked_statuses') } export function BoostModal() { return import(/* webpackChunkName: "components/boost_modal" */'../../../components/modal/boost_modal') } export function CommentSortingOptionsPopover() { return import(/* webpackChunkName: "components/comment_sorting_options_popover" */'../../../components/popover/comment_sorting_options_popover') } export function CommunityTimeline() { return import(/* webpackChunkName: "features/community_timeline" */'../../community_timeline') } diff --git a/app/javascript/gabsocial/reducers/status_lists.js b/app/javascript/gabsocial/reducers/status_lists.js index d71e3615..c0b9c9ff 100644 --- a/app/javascript/gabsocial/reducers/status_lists.js +++ b/app/javascript/gabsocial/reducers/status_lists.js @@ -6,15 +6,30 @@ import { FAVORITED_STATUSES_EXPAND_SUCCESS, FAVORITED_STATUSES_EXPAND_FAIL, } from '../actions/favorites'; +import { + BOOKMARKED_STATUSES_FETCH_REQUEST, + BOOKMARKED_STATUSES_FETCH_SUCCESS, + BOOKMARKED_STATUSES_FETCH_FAIL, + BOOKMARKED_STATUSES_EXPAND_REQUEST, + BOOKMARKED_STATUSES_EXPAND_SUCCESS, + BOOKMARKED_STATUSES_EXPAND_FAIL, +} from '../actions/bookmarks' import { Map as ImmutableMap, List as ImmutableList } from 'immutable'; import { FAVORITE_SUCCESS, UNFAVORITE_SUCCESS, PIN_SUCCESS, UNPIN_SUCCESS, + BOOKMARK_SUCCESS, + UNBOOKMARK_SUCCESS, } from '../actions/interactions'; const initialState = ImmutableMap({ + bookmarks: ImmutableMap({ + next: null, + loaded: false, + items: ImmutableList(), + }), favorites: ImmutableMap({ next: null, loaded: false, @@ -57,7 +72,17 @@ const removeOneFromList = (state, listType, status) => { }; export default function statusLists(state = initialState, action) { - switch(action.type) { + switch (action.type) { + case BOOKMARKED_STATUSES_FETCH_REQUEST: + case BOOKMARKED_STATUSES_EXPAND_REQUEST: + return state.setIn(['bookmarks', 'isLoading'], true); + case BOOKMARKED_STATUSES_FETCH_FAIL: + case BOOKMARKED_STATUSES_EXPAND_FAIL: + return state.setIn(['bookmarks', 'isLoading'], false); + case BOOKMARKED_STATUSES_FETCH_SUCCESS: + return normalizeList(state, 'bookmarks', action.statuses, action.next); + case BOOKMARKED_STATUSES_EXPAND_SUCCESS: + return appendToList(state, 'bookmarks', action.statuses, action.next); case FAVORITED_STATUSES_FETCH_REQUEST: case FAVORITED_STATUSES_EXPAND_REQUEST: return state.setIn(['favorites', 'isLoading'], true); @@ -76,6 +101,10 @@ export default function statusLists(state = initialState, action) { return prependOneToList(state, 'pins', action.status); case UNPIN_SUCCESS: return removeOneFromList(state, 'pins', action.status); + case BOOKMARK_SUCCESS: + return prependOneToList(state, 'bookmarks', action.status); + case UNBOOKMARK_SUCCESS: + return removeOneFromList(state, 'bookmarks', action.status); default: return state; } diff --git a/app/models/concerns/account_associations.rb b/app/models/concerns/account_associations.rb index b3d20d4b..c6bcc5a3 100644 --- a/app/models/concerns/account_associations.rb +++ b/app/models/concerns/account_associations.rb @@ -19,6 +19,10 @@ module AccountAssociations has_many :conversations, class_name: 'AccountConversation', dependent: :destroy, inverse_of: :account has_many :scheduled_statuses, inverse_of: :account, dependent: :destroy + # Pinned statuses + has_many :status_bookmarks, inverse_of: :account, dependent: :destroy + has_many :bookmarked_statuses, -> { reorder('status_bookmarks.created_at DESC') }, through: :status_bookmarks, class_name: 'Status', source: :status + # Pinned statuses has_many :status_pins, inverse_of: :account, dependent: :destroy has_many :pinned_statuses, -> { reorder('status_pins.created_at DESC') }, through: :status_pins, class_name: 'Status', source: :status diff --git a/app/models/concerns/account_interactions.rb b/app/models/concerns/account_interactions.rb index 395c2079..053960b6 100644 --- a/app/models/concerns/account_interactions.rb +++ b/app/models/concerns/account_interactions.rb @@ -189,6 +189,10 @@ module AccountInteractions status.proper.favourites.where(account: self).exists? end + def bookmarked?(status) + status_bookmarks.where(account: self).exists? + end + def reblogged?(status) status.proper.reblogs.where(account: self).exists? end diff --git a/app/models/status.rb b/app/models/status.rb index 6bec408d..b8f17bdb 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -26,6 +26,7 @@ # quote_of_id :bigint(8) # revised_at :datetime # markdown :text +# expires_at :datetime # class Status < ApplicationRecord @@ -57,6 +58,7 @@ class Status < ApplicationRecord belongs_to :quote, foreign_key: 'quote_of_id', class_name: 'Status', inverse_of: :quotes, optional: true has_many :favourites, inverse_of: :status, dependent: :destroy + has_many :status_bookmarks, inverse_of: :status, dependent: :destroy has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy has_many :quotes, foreign_key: 'quote_of_id', class_name: 'Status', inverse_of: :quote, dependent: :nullify has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :thread @@ -365,6 +367,10 @@ class Status < ApplicationRecord Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).each_with_object({}) { |f, h| h[f.status_id] = true } end + def bookmarks_map(status_ids, account_id) + StatusBookmark.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h + end + def reblogs_map(status_ids, account_id) select('reblog_of_id').where(reblog_of_id: status_ids).where(account_id: account_id).reorder(nil).each_with_object({}) { |s, h| h[s.reblog_of_id] = true } end diff --git a/app/models/status_bookmark.rb b/app/models/status_bookmark.rb new file mode 100644 index 00000000..5af71cf2 --- /dev/null +++ b/app/models/status_bookmark.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true +# == Schema Information +# +# Table name: status_bookmarks +# +# id :integer not null, primary key +# created_at :datetime not null +# updated_at :datetime not null +# account_id :integer not null +# status_id :integer not null +# + +class StatusBookmark < ApplicationRecord + include Paginable + + belongs_to :account, inverse_of: :status_bookmarks + belongs_to :status, inverse_of: :status_bookmarks + + validates :status_id, uniqueness: { scope: :account_id } + + before_validation do + self.status = status.reblog if status&.reblog? + end +end \ No newline at end of file diff --git a/app/presenters/status_relationships_presenter.rb b/app/presenters/status_relationships_presenter.rb index f196f0dc..ce46d7d9 100644 --- a/app/presenters/status_relationships_presenter.rb +++ b/app/presenters/status_relationships_presenter.rb @@ -1,13 +1,15 @@ # frozen_string_literal: true class StatusRelationshipsPresenter - attr_reader :reblogs_map, :favourites_map, :mutes_map, :pins_map + attr_reader :reblogs_map, :favourites_map, :mutes_map, :pins_map, :bookmarks_map + def initialize(statuses, current_account_id = nil, **options) if current_account_id.nil? @reblogs_map = {} @favourites_map = {} @mutes_map = {} + @bookmarks_map = {} @pins_map = {} else statuses = statuses.compact @@ -18,6 +20,7 @@ class StatusRelationshipsPresenter @reblogs_map = Status.reblogs_map(status_ids, current_account_id).merge(options[:reblogs_map] || {}) @favourites_map = Status.favourites_map(status_ids, current_account_id).merge(options[:favourites_map] || {}) @mutes_map = Status.mutes_map(conversation_ids, current_account_id).merge(options[:mutes_map] || {}) + @bookmarks_map = Status.bookmarks_map(status_ids, current_account_id).merge(options[:bookmarks_map] || {}) @pins_map = Status.pins_map(pinnable_status_ids, current_account_id).merge(options[:pins_map] || {}) end end diff --git a/app/serializers/rest/status_serializer.rb b/app/serializers/rest/status_serializer.rb index 939a93a4..ca8f669b 100644 --- a/app/serializers/rest/status_serializer.rb +++ b/app/serializers/rest/status_serializer.rb @@ -9,6 +9,7 @@ class REST::StatusSerializer < ActiveModel::Serializer attribute :favourited, if: :current_user? attribute :reblogged, if: :current_user? attribute :muted, if: :current_user? + attribute :bookmarked, if: :current_user? attribute :pinned, if: :pinnable? attribute :content, unless: :source_requested? @@ -117,6 +118,14 @@ class REST::StatusSerializer < ActiveModel::Serializer end end + def bookmarked + if instance_options && instance_options[:relationships] + instance_options[:relationships].bookmarks_map[object.id] || false + else + current_user.account.bookmarked?(object) + end + end + def pinned if instance_options && instance_options[:relationships] instance_options[:relationships].pins_map[object.id] || false diff --git a/app/services/suspend_account_service.rb b/app/services/suspend_account_service.rb index f7a375ae..411b8595 100644 --- a/app/services/suspend_account_service.rb +++ b/app/services/suspend_account_service.rb @@ -21,6 +21,7 @@ class SuspendAccountService < BaseService passive_relationships report_notes scheduled_statuses + status_bookmarks status_pins stream_entries subscriptions diff --git a/config/initializers/doorkeeper.rb b/config/initializers/doorkeeper.rb index 367eead6..2a963b32 100644 --- a/config/initializers/doorkeeper.rb +++ b/config/initializers/doorkeeper.rb @@ -58,6 +58,7 @@ Doorkeeper.configure do optional_scopes :write, :'write:accounts', :'write:blocks', + :'write:bookmarks', :'write:conversations', :'write:favourites', :'write:filters', @@ -71,6 +72,7 @@ Doorkeeper.configure do :read, :'read:accounts', :'read:blocks', + :'read:bookmarks', :'read:favourites', :'read:filters', :'read:follows', diff --git a/config/locales/doorkeeper.en.yml b/config/locales/doorkeeper.en.yml index 7d9a6976..704785f7 100644 --- a/config/locales/doorkeeper.en.yml +++ b/config/locales/doorkeeper.en.yml @@ -119,6 +119,7 @@ en: read: read all your account's data read:accounts: see accounts information read:blocks: see your blocks + read:bookmarks: see your bookmarks read:favourites: see your favorites read:filters: see your filters read:follows: see your follows @@ -131,6 +132,7 @@ en: write: modify all your account's data write:accounts: modify your profile write:blocks: block accounts and domains + write:bookmarks: bookmark statuses write:favourites: favorite statuses write:filters: create filters write:follows: follow people diff --git a/config/routes.rb b/config/routes.rb index e74c0828..3fff49ab 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -302,6 +302,9 @@ Rails.application.routes.draw do resource :favourite, only: :create post :unfavourite, to: 'favourites#destroy' + resource :bookmark, only: :create + post :unbookmark, to: 'bookmarks#destroy' + resource :mute, only: :create post :unmute, to: 'mutes#destroy' @@ -360,6 +363,7 @@ Rails.application.routes.draw do resources :filters, only: [:index, :create, :show, :update, :destroy] resources :endorsements, only: [:index] resources :shortcuts, only: [:index, :create, :show, :destroy] + resources :bookmarks, only: [:index] namespace :apps do get :verify_credentials, to: 'credentials#show' diff --git a/db/migrate/20200722190627_create_status_bookmark.rb b/db/migrate/20200722190627_create_status_bookmark.rb new file mode 100644 index 00000000..c1de2663 --- /dev/null +++ b/db/migrate/20200722190627_create_status_bookmark.rb @@ -0,0 +1,11 @@ +class CreateStatusBookmark < ActiveRecord::Migration[5.2] + def change + create_table :status_bookmarks do |t| + t.timestamps + t.belongs_to :account, foreign_key: { on_delete: :cascade }, null: false + t.belongs_to :status, foreign_key: { on_delete: :cascade }, null: false + end + + add_index :status_bookmarks, [:account_id, :status_id], unique: true + end +end