mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-02 08:13:34 +03:00
b911208b41
fixes https://github.com/TryGhost/Team/issues/2129 - This changes how the activity feed API parses the filter. - We now parse the filter early to a MongoDB filter, and split it in two. One of the filters is applied to the pageActions, and the other one is used individually for every event type. We now allow to use grouping and OR's inside the filters because of this change. As long as we don't combine filters on 'type' with other filters inside grouped filters or OR, then it is allowed. - We make use of mongoTransformer to manually inject a mongo filter without needing to parse it from a string value again (that would make it a lot harder because we would have to convert the splitted filter back to a string and we currently don't have methods for that). - Added sorting by id for events with the same timestamp (required for reliable pagination) - Added id to each event (required for pagination) - Added more tests for filters - Added test for pagination - Removed unsued getSubscriptions and getVolume methods Used new mongo utility methods introduced here: https://github.com/TryGhost/NQL/pull/49
143 lines
4.3 KiB
JavaScript
143 lines
4.3 KiB
JavaScript
import moment from 'moment-timezone';
|
|
import {Resource} from 'ember-could-get-used-to-this';
|
|
import {TrackedArray} from 'tracked-built-ins';
|
|
import {action} from '@ember/object';
|
|
import {inject as service} from '@ember/service';
|
|
import {task} from 'ember-concurrency';
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
const actions = {
|
|
showPrevious: 'showPrevious',
|
|
showNext: 'showNext'
|
|
};
|
|
|
|
export default class ActivityFeedFetcher extends Resource {
|
|
@service ajax;
|
|
@service ghostPaths;
|
|
@service store;
|
|
|
|
@tracked data = new TrackedArray([]);
|
|
@tracked isLoading = false;
|
|
@tracked isError = false;
|
|
@tracked errorMessage = null;
|
|
|
|
@tracked hasReachedStart = true;
|
|
@tracked hasReachedEnd = true;
|
|
|
|
@tracked shownEvents = 0;
|
|
@tracked totalEvents = 0;
|
|
|
|
// Save the pagination filter for each page so we can return easily
|
|
@tracked eventsBookmarks = [];
|
|
|
|
get value() {
|
|
return {
|
|
isLoading: this.isLoading,
|
|
isError: this.isError,
|
|
errorMessage: this.errorMessage,
|
|
data: this.data,
|
|
loadNextPage: this.loadNextPage,
|
|
loadPreviousPage: this.loadPreviousPage,
|
|
hasReachedStart: this.hasReachedStart,
|
|
hasReachedEnd: this.hasReachedEnd,
|
|
totalEvents: this.totalEvents,
|
|
shownEvents: this.shownEvents,
|
|
previousEvents: this.getAmountOfPreviousEvents()
|
|
};
|
|
}
|
|
|
|
getAmountOfPreviousEvents() {
|
|
return this.shownEvents - this.data.length + 1;
|
|
}
|
|
|
|
async setup() {
|
|
const currentTime = moment.utc().format('YYYY-MM-DD HH:mm:ss');
|
|
let filter = `data.created_at:<'${currentTime}'`;
|
|
this.eventsBookmarks.push(filter);
|
|
|
|
if (this.args.named.filter) {
|
|
filter += `+${this.args.named.filter}`;
|
|
}
|
|
|
|
await this.loadEventsTask.perform({filter}, actions.showNext);
|
|
}
|
|
|
|
@action
|
|
loadNextPage() {
|
|
const lastEvent = this.data[this.data.length - 1];
|
|
const lastEventDate = moment.utc(lastEvent.data.created_at).format('YYYY-MM-DD HH:mm:ss');
|
|
const lastEventId = lastEvent.data.id;
|
|
|
|
let filter = `(data.created_at:<'${lastEventDate}',(data.created_at:'${lastEventDate}'+id:<'${lastEventId}'))`;
|
|
this.eventsBookmarks.push(filter);
|
|
|
|
if (this.args.named.filter) {
|
|
filter += `+${this.args.named.filter}`;
|
|
}
|
|
|
|
this.loadEventsTask.perform({filter}, actions.showNext);
|
|
}
|
|
|
|
@action
|
|
loadPreviousPage() {
|
|
this.eventsBookmarks.pop();
|
|
let filter = this.eventsBookmarks[this.eventsBookmarks.length - 1];
|
|
|
|
if (this.args.named.filter) {
|
|
filter += `+${this.args.named.filter}`;
|
|
}
|
|
|
|
this.shownEvents = this.shownEvents - this.data.length;
|
|
|
|
this.loadEventsTask.perform({filter}, actions.showPrevious);
|
|
}
|
|
|
|
updateState(meta, actionType) {
|
|
if (!this.data.length) {
|
|
return;
|
|
}
|
|
|
|
if (!this.totalEvents) {
|
|
this.totalEvents = meta.pagination.total;
|
|
}
|
|
|
|
if (actionType === actions.showNext) {
|
|
this.shownEvents = this.shownEvents + this.data.length;
|
|
}
|
|
|
|
this.hasReachedStart = this.totalEvents === meta.pagination.total;
|
|
this.hasReachedEnd = this.shownEvents === this.totalEvents;
|
|
|
|
// todo: it's temporarily fix, pagination breaks if few events happen at the same time, easy to reproduce on email clicks
|
|
if ((this.shownEvents < this.totalEvents) && (this.data.length < this.args.named.pageSize)) {
|
|
this.hasReachedEnd = true;
|
|
}
|
|
}
|
|
|
|
@task
|
|
*loadEventsTask(queryParams, actionType) {
|
|
try {
|
|
this.isLoading = true;
|
|
|
|
const url = this.ghostPaths.url.api('members/events');
|
|
const data = Object.assign({}, queryParams, {limit: this.args.named.pageSize});
|
|
const {events, meta} = yield this.ajax.request(url, {data});
|
|
|
|
this.data = events;
|
|
this.updateState(meta, actionType);
|
|
} catch (e) {
|
|
this.isError = true;
|
|
|
|
const errorMessage = e.payload?.errors?.[0]?.message;
|
|
if (errorMessage) {
|
|
this.errorMessage = errorMessage;
|
|
}
|
|
|
|
// TODO: log to Sentry
|
|
console.error(e); // eslint-disable-line
|
|
} finally {
|
|
this.isLoading = false;
|
|
}
|
|
}
|
|
}
|