|
|
@ -10,8 +10,8 @@ import {BaseService} from '../base.service';
|
|
|
|
import {formatDate} from '@angular/common';
|
|
|
|
import {formatDate} from '@angular/common';
|
|
|
|
import {IFileUploadResult} from '../../models/interfaces/IFileUploadResult';
|
|
|
|
import {IFileUploadResult} from '../../models/interfaces/IFileUploadResult';
|
|
|
|
|
|
|
|
|
|
|
|
const createPostGqlQuery = `mutation($content: String!) {
|
|
|
|
const createPostGqlQuery = `mutation($content: String!, $type: PostType) {
|
|
|
|
createPost(content: $content) {
|
|
|
|
createPost(content: $content, type: $type) {
|
|
|
|
id,
|
|
|
|
id,
|
|
|
|
content,
|
|
|
|
content,
|
|
|
|
htmlContent,
|
|
|
|
htmlContent,
|
|
|
@ -34,8 +34,8 @@ const createPostGqlQuery = `mutation($content: String!) {
|
|
|
|
createdAt}
|
|
|
|
createdAt}
|
|
|
|
}`;
|
|
|
|
}`;
|
|
|
|
|
|
|
|
|
|
|
|
const createPostActivityGqlQuery = `mutation($content: String!, $id: ID) {
|
|
|
|
const createPostActivityGqlQuery = `mutation($content: String!, $id: ID, $type: PostType) {
|
|
|
|
createPost(content: $content activityId: $id) {
|
|
|
|
createPost(content: $content activityId: $id, type: $type) {
|
|
|
|
id,
|
|
|
|
id,
|
|
|
|
content,
|
|
|
|
content,
|
|
|
|
htmlContent,
|
|
|
|
htmlContent,
|
|
|
@ -70,7 +70,7 @@ const downvotePostGqlQuery = `mutation($postId: ID!) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`;
|
|
|
|
}`;
|
|
|
|
|
|
|
|
|
|
|
|
const getPostGqlQuery = `query($first: Int, $offset: Int, $sort: SortType){
|
|
|
|
const getPostsGqlQuery = `query($first: Int, $offset: Int, $sort: SortType){
|
|
|
|
getPosts (first: $first, offset: $offset, sort: $sort) {
|
|
|
|
getPosts (first: $first, offset: $offset, sort: $sort) {
|
|
|
|
id,
|
|
|
|
id,
|
|
|
|
content,
|
|
|
|
content,
|
|
|
@ -109,6 +109,7 @@ export class FeedService extends BaseService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public postsAvailable = new BehaviorSubject<boolean>(true);
|
|
|
|
public postsAvailable = new BehaviorSubject<boolean>(true);
|
|
|
|
|
|
|
|
public posting = new BehaviorSubject<boolean>(false);
|
|
|
|
public posts: BehaviorSubject<Post[]> = new BehaviorSubject([]);
|
|
|
|
public posts: BehaviorSubject<Post[]> = new BehaviorSubject([]);
|
|
|
|
private activePostList: Sort = Sort.NEW;
|
|
|
|
private activePostList: Sort = Sort.NEW;
|
|
|
|
private offset = 0;
|
|
|
|
private offset = 0;
|
|
|
@ -122,7 +123,7 @@ export class FeedService extends BaseService {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private static buildGetPostBody(sort: string, offset: number, first: number = 10) {
|
|
|
|
private static buildGetPostBody(sort: string, offset: number, first: number = 10) {
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
query: getPostGqlQuery, variables: {
|
|
|
|
query: getPostsGqlQuery, variables: {
|
|
|
|
first,
|
|
|
|
first,
|
|
|
|
offset,
|
|
|
|
offset,
|
|
|
|
sort
|
|
|
|
sort
|
|
|
@ -145,10 +146,15 @@ export class FeedService extends BaseService {
|
|
|
|
* @param file
|
|
|
|
* @param file
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public createPost(pContent: String, file?: File) {
|
|
|
|
public createPost(pContent: String, file?: File) {
|
|
|
|
|
|
|
|
let type: string;
|
|
|
|
|
|
|
|
if (file) { type = 'MEDIA'; } else {
|
|
|
|
|
|
|
|
type = 'TEXT';
|
|
|
|
|
|
|
|
}
|
|
|
|
const body = {
|
|
|
|
const body = {
|
|
|
|
query: createPostGqlQuery,
|
|
|
|
query: createPostGqlQuery,
|
|
|
|
variables: {
|
|
|
|
variables: {
|
|
|
|
content: pContent
|
|
|
|
content: pContent,
|
|
|
|
|
|
|
|
type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
return this.createPostRequest(body, file);
|
|
|
|
return this.createPostRequest(body, file);
|
|
|
@ -161,10 +167,15 @@ export class FeedService extends BaseService {
|
|
|
|
* @param file
|
|
|
|
* @param file
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public createPostActivity(pContent: String, activityId: String, file?: File) {
|
|
|
|
public createPostActivity(pContent: String, activityId: String, file?: File) {
|
|
|
|
|
|
|
|
let type: string;
|
|
|
|
|
|
|
|
if (file) { type = 'MEDIA'; } else {
|
|
|
|
|
|
|
|
type = 'TEXT';
|
|
|
|
|
|
|
|
}
|
|
|
|
const body = {
|
|
|
|
const body = {
|
|
|
|
query: createPostActivityGqlQuery, variables: {
|
|
|
|
query: createPostActivityGqlQuery, variables: {
|
|
|
|
content: pContent,
|
|
|
|
content: pContent,
|
|
|
|
id: activityId
|
|
|
|
id: activityId,
|
|
|
|
|
|
|
|
type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
return this.createPostRequest(body, file);
|
|
|
|
return this.createPostRequest(body, file);
|
|
|
@ -176,27 +187,40 @@ export class FeedService extends BaseService {
|
|
|
|
* @param file - a file that is being uploaded with the post
|
|
|
|
* @param file - a file that is being uploaded with the post
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private createPostRequest(body: { variables: any; query: string }, file?: File) {
|
|
|
|
private createPostRequest(body: { variables: any; query: string }, file?: File) {
|
|
|
|
|
|
|
|
this.posting.next(true);
|
|
|
|
|
|
|
|
if (file) {
|
|
|
|
return this.postGraphql(body, null, 0)
|
|
|
|
return this.postGraphql(body, null, 0)
|
|
|
|
.pipe(tap(response => {
|
|
|
|
.pipe(tap(response => {
|
|
|
|
if (this.activePostList === Sort.NEW) {
|
|
|
|
|
|
|
|
const updatedPosts = this.posts.getValue();
|
|
|
|
const updatedPosts = this.posts.getValue();
|
|
|
|
const post = this.constructPost(response);
|
|
|
|
const post = this.constructPost(response);
|
|
|
|
updatedPosts.unshift(post);
|
|
|
|
|
|
|
|
if (file) {
|
|
|
|
|
|
|
|
this.uploadPostImage(post.id, file).subscribe((result) => {
|
|
|
|
this.uploadPostImage(post.id, file).subscribe((result) => {
|
|
|
|
|
|
|
|
if (this.activePostList === Sort.NEW) {
|
|
|
|
post.mediaUrl = result.fileName;
|
|
|
|
post.mediaUrl = result.fileName;
|
|
|
|
post.mediaType = result.fileName.endsWith('.png') ? 'IMAGE' : 'VIDEO';
|
|
|
|
post.mediaType = result.fileName.endsWith('.png') ? 'IMAGE' : 'VIDEO';
|
|
|
|
|
|
|
|
updatedPosts.unshift(post);
|
|
|
|
this.posts.next(updatedPosts);
|
|
|
|
this.posts.next(updatedPosts);
|
|
|
|
|
|
|
|
this.posting.next(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
}, error => {
|
|
|
|
}, error => {
|
|
|
|
console.error(error);
|
|
|
|
console.error(error);
|
|
|
|
|
|
|
|
this.posting.next(false);
|
|
|
|
this.deletePost(post.id);
|
|
|
|
this.deletePost(post.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
|
|
|
|
this.posts.next(updatedPosts);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
));
|
|
|
|
|
|
|
|
} else if (!file) {
|
|
|
|
|
|
|
|
return this.postGraphql(body, null, 0)
|
|
|
|
|
|
|
|
.pipe(tap(response => {
|
|
|
|
|
|
|
|
this.posting.next(false);
|
|
|
|
|
|
|
|
const updatedPosts = this.posts.getValue();
|
|
|
|
|
|
|
|
if (this.activePostList === Sort.NEW) {
|
|
|
|
|
|
|
|
const post = this.constructPost(response);
|
|
|
|
|
|
|
|
updatedPosts.unshift(post);
|
|
|
|
|
|
|
|
this.posts.next(updatedPosts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Uploads a file for a post
|
|
|
|
* Uploads a file for a post
|
|
|
@ -266,7 +290,7 @@ export class FeedService extends BaseService {
|
|
|
|
{headers: this.headers})
|
|
|
|
{headers: this.headers})
|
|
|
|
.pipe(this.retryRated())
|
|
|
|
.pipe(this.retryRated())
|
|
|
|
.subscribe(response => {
|
|
|
|
.subscribe(response => {
|
|
|
|
this.posts.next(this.constructAllPosts(response));
|
|
|
|
this.posts.next(this.constructAllPosts(response.data.getPosts));
|
|
|
|
this.activePostList = sort;
|
|
|
|
this.activePostList = sort;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -280,7 +304,7 @@ export class FeedService extends BaseService {
|
|
|
|
this.http.post(environment.graphQLUrl, body, {headers: this.headers})
|
|
|
|
this.http.post(environment.graphQLUrl, body, {headers: this.headers})
|
|
|
|
.pipe(this.retryRated())
|
|
|
|
.pipe(this.retryRated())
|
|
|
|
.subscribe(response => {
|
|
|
|
.subscribe(response => {
|
|
|
|
const posts = this.constructAllPosts(response);
|
|
|
|
const posts = this.constructAllPosts(response.data.getPosts);
|
|
|
|
const previousPosts = this.posts.getValue();
|
|
|
|
const previousPosts = this.posts.getValue();
|
|
|
|
for (const post of previousPosts.reverse()) {
|
|
|
|
for (const post of previousPosts.reverse()) {
|
|
|
|
if (!posts.find(p => p.id === post.id)) {
|
|
|
|
if (!posts.find(p => p.id === post.id)) {
|
|
|
@ -337,7 +361,7 @@ export class FeedService extends BaseService {
|
|
|
|
|
|
|
|
|
|
|
|
public constructAllPosts(response: any): Post[] {
|
|
|
|
public constructAllPosts(response: any): Post[] {
|
|
|
|
const posts = new Array<Post>();
|
|
|
|
const posts = new Array<Post>();
|
|
|
|
for (const post of response.data.getPosts) {
|
|
|
|
for (const post of response) {
|
|
|
|
let profilePicture: string;
|
|
|
|
let profilePicture: string;
|
|
|
|
if (post.author.profilePicture) {
|
|
|
|
if (post.author.profilePicture) {
|
|
|
|
profilePicture = environment.greenvironmentUrl + post.author.profilePicture;
|
|
|
|
profilePicture = environment.greenvironmentUrl + post.author.profilePicture;
|
|
|
|