diff --git a/app/controllers/api/v1/timelines/group_collection_controller.rb b/app/controllers/api/v1/timelines/group_collection_controller.rb new file mode 100644 index 00000000..f883c043 --- /dev/null +++ b/app/controllers/api/v1/timelines/group_collection_controller.rb @@ -0,0 +1,95 @@ +# frozen_string_literal: true + +class Api::V1::Timelines::GroupCollectionController < Api::BaseController + before_action :set_collection_type + before_action :set_statuses + + after_action :insert_pagination_headers, unless: -> { @statuses.empty? } + + def show + if current_user + render json: @statuses, + each_serializer: REST::StatusSerializer, + relationships: StatusRelationshipsPresenter.new(@statuses, current_user.account_id) + else + render json: @statuses, each_serializer: REST::StatusSerializer + end + end + + def set_collection_type + @collection_type = nil + @collection_type = params[:id] if ['featured', 'member'].include? params[:id] + + if @collection_type.nil? + return render json: { error: 'Invalid collection type' }, status: 422 + end + + return @collection_type + end + + private + + def set_statuses + @statuses = cached_group_collection_statuses + end + + def cached_group_collection_statuses + cache_collection group_collection_statuses, Status + end + + def group_collection_statuses + statuses = nil + + @groupIds = [] + if @collection_type == 'featured' + @groupIds = Group.where(is_featured: true, is_archived: false).limit(100).all.pluck(:id) + elsif @collection_type == 'member' + @groupIds = current_user.account.groups.pluck(:id) + end + + if current_account + statuses = Status.as_group_collection_timeline(@groupIds).paginate_by_id( + limit_param(DEFAULT_STATUSES_LIMIT), + params_slice(:max_id, :since_id, :min_id) + ).reject { |status| FeedManager.instance.filter?(:home, status, current_account.id) } + else + statuses = Status.as_group_collection_timeline(@groupIds).paginate_by_id( + limit_param(DEFAULT_STATUSES_LIMIT), + params_slice(:max_id, :since_id, :min_id) + ) + end + + if truthy_param?(:only_media) && current_account + # `SELECT DISTINCT id, updated_at` is too slow, so pluck ids at first, and then select id, updated_at with ids. + status_ids = statuses.joins(:media_attachments).distinct(:id).pluck(:id) + statuses.where(id: status_ids) + else + statuses + end + end + + def insert_pagination_headers + set_pagination_headers(next_path, prev_path) + end + + def pagination_params(core_params) + params.slice(:limit).permit(:limit).merge(core_params) + end + + def next_path + api_v1_timelines_group_collection_url pagination_params(max_id: pagination_max_id) + end + + def prev_path + api_v1_timelines_group_collection_url pagination_params(min_id: pagination_since_id) + end + + def pagination_max_id + @statuses.last.id + end + + def pagination_since_id + @statuses.first.id + end + +end diff --git a/app/javascript/gabsocial/actions/streaming.js b/app/javascript/gabsocial/actions/streaming.js index 880acec4..2469970d 100644 --- a/app/javascript/gabsocial/actions/streaming.js +++ b/app/javascript/gabsocial/actions/streaming.js @@ -55,6 +55,7 @@ export const connectProStream = () => connectTimelineStream('pro', 'pro'); export const connectHashtagStream = (id, tag, accept) => connectTimelineStream(`hashtag:${id}`, `hashtag&tag=${tag}`, null, accept); export const connectListStream = id => connectTimelineStream(`list:${id}`, `list&list=${id}`); export const connectGroupStream = id => connectTimelineStream(`group:${id}`, `group&group=${id}`); +export const connectGroupCollectionStream = (id) => connectTimelineStream(`group:collection:${id}`, `group&group:collection=${id}`); export const connectStatusUpdateStream = () => { return connectStream('statuscard', null, (dispatch, getState) => { diff --git a/app/javascript/gabsocial/actions/timelines.js b/app/javascript/gabsocial/actions/timelines.js index 3bbb099b..f1392fd0 100644 --- a/app/javascript/gabsocial/actions/timelines.js +++ b/app/javascript/gabsocial/actions/timelines.js @@ -172,7 +172,8 @@ export const expandAccountTimeline = (accountId, { maxId, withReplies, commentsO export const expandAccountFeaturedTimeline = accountId => expandTimeline(`account:${accountId}:pinned`, `/api/v1/accounts/${accountId}/statuses`, { pinned: true }); export const expandAccountMediaTimeline = (accountId, { maxId, limit, mediaType } = {}) => expandTimeline(`account:${accountId}:media`, `/api/v1/accounts/${accountId}/statuses`, { max_id: maxId, only_media: true, limit: limit || 20, media_type: mediaType }); export const expandListTimeline = (id, { maxId } = {}, done = noOp) => expandTimeline(`list:${id}`, `/api/v1/timelines/list/${id}`, { max_id: maxId }, done); -export const expandGroupTimeline = (id, { maxId } = {}, done = noOp) => expandTimeline(`group:${id}`, `/api/v1/timelines/group/${id}`, { max_id: maxId }, done); +export const expandGroupTimeline = (id, { sortBy, maxId } = {}, done = noOp) => expandTimeline(`group:${id}`, `/api/v1/timelines/group/${id}`, { sort_by: sortBy, max_id: maxId }, done); +export const expandGroupCollectionTimeline = (collectionType, { sortBy, maxId } = {}, done = noOp) => expandTimeline(`group:collection:${collectionType}`, `/api/v1/timelines/group_collection/${collectionType}`, { sort_by: sortBy, max_id: maxId }, done); export const expandHashtagTimeline = (hashtag, { maxId, tags } = {}, done = noOp) => { return expandTimeline(`hashtag:${hashtag}`, `/api/v1/timelines/tag/${hashtag}`, { max_id: maxId, diff --git a/app/javascript/gabsocial/features/group_collection_timeline.js b/app/javascript/gabsocial/features/group_collection_timeline.js new file mode 100644 index 00000000..3f8f12ed --- /dev/null +++ b/app/javascript/gabsocial/features/group_collection_timeline.js @@ -0,0 +1,67 @@ +import { injectIntl, defineMessages } from 'react-intl' +import { me } from '../initial_state' +import { connectGroupCollectionStream } from '../actions/streaming' +import { expandGroupCollectionTimeline } from '../actions/timelines' +import StatusList from '../components/status_list' + +const messages = defineMessages({ + empty: { id: 'empty_column.group_collection_timeline', defaultMessage: 'There are no gabs to display.' }, +}) + +const mapStateToProps = (state) = ({ + sortByValue: state.getIn(['group_lists', 'sortByValue']), + sortByTopValue: state.getIn(['group_lists', 'sortByTopValue']), +}) + +export default +@injectIntl +@connect(mapStateToProps) +class GroupCollectionTimeline extends PureComponent { + + static propTypes = { + params: PropTypes.object.isRequired, + dispatch: PropTypes.func.isRequired, + intl: PropTypes.object.isRequired, + sortByValue: PropTypes.string.isRequired, + sortByTopValue: PropTypes.string, + } + + componentDidMount() { + const { collectionType, dispatch } = this.props + + dispatch(expandGroupCollectionTimeline(collectionType)) + + if (!!me) { + this.disconnect = dispatch(connectGroupCollectionStream(collectionType)) + } + } + + componentWillUnmount() { + if (this.disconnect && !!me) { + this.disconnect() + this.disconnect = null + } + } + + handleLoadMore = (maxId) => { + const { collectionType } = this.props + this.props.dispatch(expandGroupCollectionTimeline(collectionType, { maxId })) + } + + render() { + const { + collectionType, + intl, + } = this.props + + return ( + + ) + } + +} diff --git a/config/routes.rb b/config/routes.rb index 96dc65dd..59ea7573 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -329,6 +329,7 @@ Rails.application.routes.draw do resources :tag, only: :show resources :list, only: :show resources :group, only: :show + resources :group_collection, only: :show end resources :gab_trends, only: [:index]