|
|
@ -12,9 +12,14 @@ import { User } from 'src/app/models/user';
|
|
|
|
})
|
|
|
|
})
|
|
|
|
export class FeedService {
|
|
|
|
export class FeedService {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public newPostsAvailable = new BehaviorSubject<boolean>(true);
|
|
|
|
|
|
|
|
public topPostsAvailable = new BehaviorSubject<boolean>(true);
|
|
|
|
public posts: BehaviorSubject<Post[]> = new BehaviorSubject(new Array());
|
|
|
|
public posts: BehaviorSubject<Post[]> = new BehaviorSubject(new Array());
|
|
|
|
public newPosts: BehaviorSubject<Post[]> = new BehaviorSubject(new Array());
|
|
|
|
public newPosts: BehaviorSubject<Post[]> = new BehaviorSubject(new Array());
|
|
|
|
public mostLikedPosts: BehaviorSubject<Post[]> = new BehaviorSubject(new Array());
|
|
|
|
public mostLikedPosts: BehaviorSubject<Post[]> = new BehaviorSubject(new Array());
|
|
|
|
|
|
|
|
private activePostList = 'NEW';
|
|
|
|
|
|
|
|
private mostLikedOffset = 0;
|
|
|
|
|
|
|
|
private newOffset = 0;
|
|
|
|
|
|
|
|
|
|
|
|
constructor(private http: Http) { }
|
|
|
|
constructor(private http: Http) { }
|
|
|
|
|
|
|
|
|
|
|
@ -49,7 +54,7 @@ export class FeedService {
|
|
|
|
const updatedposts = this.newPosts.getValue();
|
|
|
|
const updatedposts = this.newPosts.getValue();
|
|
|
|
updatedposts.unshift(this.renderPost(response.json()));
|
|
|
|
updatedposts.unshift(this.renderPost(response.json()));
|
|
|
|
this.newPosts.next(updatedposts);
|
|
|
|
this.newPosts.next(updatedposts);
|
|
|
|
this.posts.next(this.newPosts.getValue());
|
|
|
|
this.setPost('NEW');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -85,7 +90,7 @@ export class FeedService {
|
|
|
|
const updatedposts = this.newPosts.getValue();
|
|
|
|
const updatedposts = this.newPosts.getValue();
|
|
|
|
updatedposts.unshift(this.renderPost(response.json()));
|
|
|
|
updatedposts.unshift(this.renderPost(response.json()));
|
|
|
|
this.newPosts.next(updatedposts);
|
|
|
|
this.newPosts.next(updatedposts);
|
|
|
|
this.posts.next(this.newPosts.getValue());
|
|
|
|
this.setPost('NEW');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -132,59 +137,67 @@ export class FeedService {
|
|
|
|
return this.http.post(environment.graphQLUrl, body);
|
|
|
|
return this.http.post(environment.graphQLUrl, body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public getNewPosts() {
|
|
|
|
public getPosts(sort: string) {
|
|
|
|
if (this.newPosts.getValue().length === 0) {
|
|
|
|
if ((sort === 'NEW' && this.newPosts.getValue().length === 0) ||
|
|
|
|
|
|
|
|
(sort === 'TOP' && this.mostLikedPosts.getValue().length === 0)) {
|
|
|
|
const headers = new Headers();
|
|
|
|
const headers = new Headers();
|
|
|
|
headers.set('Content-Type', 'application/json');
|
|
|
|
headers.set('Content-Type', 'application/json');
|
|
|
|
this.http.post(environment.graphQLUrl, this.buildJsonNew())
|
|
|
|
this.http.post(environment.graphQLUrl, this.buildJson(sort, 0))
|
|
|
|
.subscribe(response => {
|
|
|
|
.subscribe(response => {
|
|
|
|
|
|
|
|
if (sort === 'NEW') {
|
|
|
|
this.newPosts.next(this.renderAllPosts(response.json()));
|
|
|
|
this.newPosts.next(this.renderAllPosts(response.json()));
|
|
|
|
this.posts.next(this.newPosts.getValue());
|
|
|
|
} else if (sort === 'TOP') {
|
|
|
|
|
|
|
|
this.mostLikedPosts.next(this.renderAllPosts(response.json()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setPost(sort);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {this.posts.next(this.newPosts.getValue()); }
|
|
|
|
} this.setPost(sort);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public getMostLikedPosts() {
|
|
|
|
public getNextPosts() {
|
|
|
|
if (this.mostLikedPosts.getValue().length === 0) {
|
|
|
|
if (this.activePostList === 'NEW' && this.newPostsAvailable) {
|
|
|
|
|
|
|
|
this.newOffset += 10;
|
|
|
|
const headers = new Headers();
|
|
|
|
const headers = new Headers();
|
|
|
|
headers.set('Content-Type', 'application/json');
|
|
|
|
headers.set('Content-Type', 'application/json');
|
|
|
|
this.http.post(environment.graphQLUrl, this.buildJsonMostLiked())
|
|
|
|
this.http.post(environment.graphQLUrl, this.buildJson(this.activePostList, this.newOffset))
|
|
|
|
.subscribe(response => {
|
|
|
|
.subscribe(response => {
|
|
|
|
this.mostLikedPosts.next(this.renderAllPosts(response.json()));
|
|
|
|
let updatedposts = this.newPosts.getValue();
|
|
|
|
this.posts.next(this.mostLikedPosts.getValue());
|
|
|
|
updatedposts = updatedposts.concat(this.renderAllPosts(response.json()));
|
|
|
|
|
|
|
|
if (this.renderAllPosts(response.json()).length < 1) {
|
|
|
|
|
|
|
|
this.newPostsAvailable.next(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this.newPosts.next(updatedposts);
|
|
|
|
|
|
|
|
this.setPost('NEW');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} else if (this.activePostList === 'TOP' && this.topPostsAvailable) {
|
|
|
|
|
|
|
|
this.mostLikedOffset += 10;
|
|
|
|
|
|
|
|
const headers = new Headers();
|
|
|
|
|
|
|
|
headers.set('Content-Type', 'application/json');
|
|
|
|
|
|
|
|
this.http.post(environment.graphQLUrl, this.buildJson(this.activePostList, this.mostLikedOffset))
|
|
|
|
|
|
|
|
.subscribe(response => {
|
|
|
|
|
|
|
|
let updatedposts = this.mostLikedPosts.getValue();
|
|
|
|
|
|
|
|
updatedposts = updatedposts.concat(this.renderAllPosts(response.json()));
|
|
|
|
|
|
|
|
if (this.renderAllPosts(response.json()).length < 1) {
|
|
|
|
|
|
|
|
this.topPostsAvailable.next(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this.mostLikedPosts.next(updatedposts);
|
|
|
|
|
|
|
|
this.setPost('TOP');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {this.posts.next(this.mostLikedPosts.getValue()); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
buildJsonNew() {
|
|
|
|
setPost(sort: string) {
|
|
|
|
const body = {query: `{
|
|
|
|
this.activePostList = sort;
|
|
|
|
getPosts (first: 1000, offset: 0) {
|
|
|
|
if (sort === 'NEW') {
|
|
|
|
id,
|
|
|
|
this.posts.next(this.newPosts.getValue());
|
|
|
|
content,
|
|
|
|
} else if (sort === 'TOP') {
|
|
|
|
htmlContent,
|
|
|
|
this.posts.next(this.mostLikedPosts.getValue());
|
|
|
|
upvotes,
|
|
|
|
}
|
|
|
|
downvotes,
|
|
|
|
|
|
|
|
userVote,
|
|
|
|
|
|
|
|
deletable,
|
|
|
|
|
|
|
|
activity{
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
name
|
|
|
|
|
|
|
|
description
|
|
|
|
|
|
|
|
points
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
author{
|
|
|
|
|
|
|
|
name,
|
|
|
|
|
|
|
|
handle,
|
|
|
|
|
|
|
|
id},
|
|
|
|
|
|
|
|
createdAt}
|
|
|
|
|
|
|
|
}`, variables: {
|
|
|
|
|
|
|
|
}};
|
|
|
|
|
|
|
|
return body;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
buildJsonMostLiked() {
|
|
|
|
buildJson(sort: string, offset: number) {
|
|
|
|
const body = {query: `{
|
|
|
|
const body = {query: `query($offset: Int, $sort: SortType){
|
|
|
|
getPosts (first: 1000, offset: 0, sort: TOP) {
|
|
|
|
getPosts (first: 10, offset: $offset, sort: $sort) {
|
|
|
|
id,
|
|
|
|
id,
|
|
|
|
content,
|
|
|
|
content,
|
|
|
|
htmlContent,
|
|
|
|
htmlContent,
|
|
|
@ -204,6 +217,8 @@ export class FeedService {
|
|
|
|
id},
|
|
|
|
id},
|
|
|
|
createdAt}
|
|
|
|
createdAt}
|
|
|
|
}`, variables: {
|
|
|
|
}`, variables: {
|
|
|
|
|
|
|
|
offset,
|
|
|
|
|
|
|
|
sort
|
|
|
|
}};
|
|
|
|
}};
|
|
|
|
return body;
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|