Commit ce7d236f authored by Mark Harding's avatar Mark Harding

(fix): working with production data

1 merge request!373Refactor/es feeds
Pipeline #67714157 passed with stage
in 7 minutes and 43 seconds
......@@ -31,15 +31,20 @@ export class EntitiesService {
const entities = [];
for (const feedItem of feed) {
if (!this.entities[feed.urn]) {
urnsToFetch.push(feed.urn);
if (feedItem.entity) {
this.entities[feedItem.urn] = feedItem.entity;
}
if (!this.entities[feedItem.urn]) {
urnsToFetch.push(feedItem.urn);
}
}
await this.fetch(urnsToFetch);
if (urnsToFetch.length) {
await this.fetch(urnsToFetch);
}
for (const feedItem of feed) {
entities.push(this.entities[feed.urn]);
entities.push(this.entities[feedItem.urn]);
}
return entities;
......@@ -51,6 +56,10 @@ export class EntitiesService {
* @return Object
*/
async single(urn: string): Promise<Object | false> {
if (urn.indexOf('urn:') < 0) { // not a urn, so treat as a guid
urn = `urn:activity:${urn}`; // and assume activity
}
if (!this.entities[urn]) {
await this.fetch([ urn ]);
}
......@@ -59,9 +68,9 @@ export class EntitiesService {
async fetch(urns: string[]): Promise<Array<Object>> {
const response: any = this.client.get('api/v2/entities/', { urns });
const response: any = await this.client.get('api/v2/entities/', { urns });
for (let entity of response.entities) {
for (const entity of response.entities) {
this.entities[entity.urn] = entity;
}
......
......@@ -13,7 +13,7 @@ import FeedsSync from '../../lib/minds-sync/services/FeedsSync.js';
import hashCode from "../../helpers/hash-code";
import AsyncStatus from "../../helpers/async-status";
import { BehaviorSubject, Observable, of, forkJoin } from "rxjs";
import { take, switchMap, map } from "rxjs/operators";
import { take, switchMap, map, tap } from "rxjs/operators";
export type FeedsServiceGetParameters = {
endpoint: string;
......@@ -43,10 +43,11 @@ export class FeedsService {
limit: BehaviorSubject<number> = new BehaviorSubject(12);
offset: BehaviorSubject<number> = new BehaviorSubject(0);
endpoint: string = '';
params: any = { sync: 1 };
rawFeed: BehaviorSubject<Object[]> = new BehaviorSubject([]);
feed: Observable<Object[]>;
inProgress: BehaviorSubject<boolean> = new BehaviorSubject(false);
inProgress: BehaviorSubject<boolean> = new BehaviorSubject(true);
hasMore: Observable<boolean>;
constructor(
......@@ -56,12 +57,20 @@ export class FeedsService {
protected blockListService: BlockListService,
) {
this.feed = this.rawFeed.pipe(
take(this.limit.getValue() + this.offset.getValue()),
tap(() => {
this.inProgress.next(true);
}),
map(feed => feed.slice(0, this.limit.getValue() + this.offset.getValue())),
switchMap(feed => this.entitiesService.getFromFeed(feed)),
tap(() => {
if (this.offset.getValue() > 0) {
this.inProgress.next(false);
}
}),
);
this.hasMore = this.rawFeed.pipe(
map(feed => {
return (this.limit.getValue() + this.offset.getValue()) < feed.length;
return this.inProgress.getValue() || (this.limit.getValue() + this.offset.getValue()) < feed.length;
}),
);
}
......@@ -76,6 +85,14 @@ export class FeedsService {
return this;
}
setParams(params): FeedsService {
this.params = params;
if (!params.sync) {
this.params.sync = 1;
}
return this;
}
setOffset(offset: number): FeedsService {
this.offset.next(offset);
return this;
......@@ -83,19 +100,24 @@ export class FeedsService {
fetch(): FeedsService {
this.inProgress.next(true);
this.client.get(this.endpoint)
this.client.get(this.endpoint, {...this.params, ...{ limit: 150 }}) // Over 12 scrolls
.then((response: any) => {
this.inProgress.next(false);
this.rawFeed.next(response.entities);
})
.catch(err => {
this.inProgress.next(false);
});
return this;
}
loadMore(): FeedsService {
this.setOffset(this.limit.getValue() + this.offset.getValue());
this.rawFeed.next(this.rawFeed.getValue());
return this;
}
clear(): FeedsService {
this.offset.next(0);
this.inProgress.next(true);
this.rawFeed.next([]);
return this;
}
......
......@@ -205,7 +205,14 @@ export class NewsfeedSortedComponent implements OnInit, OnDestroy {
const nsfw = (this.newsfeedService.nsfw || []).join(',');
this.feedsService
.setEndpoint(`api/v2/feeds/global/${this.algorithm}/${this.customType}?hashtags=${hashtags}&period=${period}&all=${all}&query=${query}&nsfw=${nsfw}`)
.setEndpoint(`api/v2/feeds/global/${this.algorithm}/${this.customType}`)
.setParams({
hashtags,
period,
all,
query,
nsfw,
})
.setLimit(12)
.fetch();
......@@ -217,8 +224,7 @@ export class NewsfeedSortedComponent implements OnInit, OnDestroy {
}
loadMore() {
console.log('loading more');
//this.feedsService.setOffset(this.feedsService.offset.getValue() + 12);
this.feedsService.loadMore();
}
delete(activity) {
......
......@@ -120,7 +120,7 @@ export class NewsfeedSubscribedComponent {
loadNext() {
if (this.featuresService.has('es-feeds')) {
this.feedsService.setOffset(this.feedsService.offset.getValue() + 12);
this.feedsService.loadMore();
} else {
this.loadLegacy();
}
......@@ -142,7 +142,7 @@ export class NewsfeedSubscribedComponent {
try {
this.feedsService
.setEndpoint(`api/v2/feeds/container/subscribed/activities`)
.setEndpoint(`api/v2/feeds/subscribed/activities`)
.setLimit(12)
.fetch();
......@@ -156,14 +156,17 @@ export class NewsfeedSubscribedComponent {
* Load newsfeed
*/
loadLegacy(refresh: boolean = false) {
if (this.feedsService.inProgress.getValue())
return false;
if (this.inProgress)
return;
if (refresh) {
this.offset = '';
this.feedsService.clear();
}
this.feedsService.inProgress.next(true);
this.inProgress = true;
this.client.get('api/v1/newsfeed', { limit: 12, offset: this.offset }, { cache: true })
.then((data: MindsActivityObject) => {
......@@ -172,16 +175,31 @@ export class NewsfeedSubscribedComponent {
this.feedsService.inProgress.next(false);
return false;
}
if (this.newsfeed && !refresh) {
this.feedsService.rawFeed.next([...this.feedsService.rawFeed.getValue(), ...data.activity]);
const feedItems = [];
for (const entity of data.activity) {
feedItems.push({
urn: entity.urn,
guid: entity.guid,
owner_guid: entity.owner_guid,
entity: entity,
});
}
this.feedsService.setOffset(this.feedsService.offset.getValue() + 12); // Hacky!
if (this.feedsService.rawFeed.getValue() && !refresh) {
this.feedsService.rawFeed.next([...this.feedsService.rawFeed.getValue(), ...feedItems]);
} else {
this.feedsService.rawFeed.next(data.activity);
this.feedsService.rawFeed.next(feedItems);
}
this.offset = data['load-next'];
this.feedsService.inProgress.next(false);
this.inProgress = false;
})
.catch((e) => {
this.feedsService.inProgress.next(false);
console.error(e);
this.inProgress = false;
});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment