|
|
@ -4,10 +4,11 @@ import {Post} from 'src/app/models/post';
|
|
|
|
import {Author} from 'src/app/models/author';
|
|
|
|
import {Author} from 'src/app/models/author';
|
|
|
|
import {environment} from 'src/environments/environment';
|
|
|
|
import {environment} from 'src/environments/environment';
|
|
|
|
import {Activity} from 'src/app/models/activity';
|
|
|
|
import {Activity} from 'src/app/models/activity';
|
|
|
|
import {BehaviorSubject} from 'rxjs';
|
|
|
|
import {BehaviorSubject, Observable} from 'rxjs';
|
|
|
|
import {tap} from 'rxjs/operators';
|
|
|
|
import {tap} from 'rxjs/operators';
|
|
|
|
import {BaseService} from '../base.service';
|
|
|
|
import {BaseService} from '../base.service';
|
|
|
|
import {formatDate} from '@angular/common';
|
|
|
|
import {formatDate} from '@angular/common';
|
|
|
|
|
|
|
|
import {IFileUploadResult} from '../../models/interfaces/IFileUploadResult';
|
|
|
|
|
|
|
|
|
|
|
|
const createPostGqlQuery = `mutation($content: String!) {
|
|
|
|
const createPostGqlQuery = `mutation($content: String!) {
|
|
|
|
createPost(content: $content) {
|
|
|
|
createPost(content: $content) {
|
|
|
@ -18,6 +19,7 @@ const createPostGqlQuery = `mutation($content: String!) {
|
|
|
|
downvotes,
|
|
|
|
downvotes,
|
|
|
|
userVote,
|
|
|
|
userVote,
|
|
|
|
deletable,
|
|
|
|
deletable,
|
|
|
|
|
|
|
|
media {url, type},
|
|
|
|
activity{
|
|
|
|
activity{
|
|
|
|
id
|
|
|
|
id
|
|
|
|
name
|
|
|
|
name
|
|
|
@ -41,6 +43,7 @@ const createPostActivityGqlQuery = `mutation($content: String!, $id: ID) {
|
|
|
|
downvotes,
|
|
|
|
downvotes,
|
|
|
|
userVote,
|
|
|
|
userVote,
|
|
|
|
deletable,
|
|
|
|
deletable,
|
|
|
|
|
|
|
|
media {url, type},
|
|
|
|
activity{
|
|
|
|
activity{
|
|
|
|
id
|
|
|
|
id
|
|
|
|
name
|
|
|
|
name
|
|
|
@ -76,6 +79,7 @@ const getPostGqlQuery = `query($first: Int, $offset: Int, $sort: SortType){
|
|
|
|
downvotes,
|
|
|
|
downvotes,
|
|
|
|
userVote,
|
|
|
|
userVote,
|
|
|
|
deletable,
|
|
|
|
deletable,
|
|
|
|
|
|
|
|
media {url, type},
|
|
|
|
activity{
|
|
|
|
activity{
|
|
|
|
id
|
|
|
|
id
|
|
|
|
name
|
|
|
|
name
|
|
|
@ -138,47 +142,74 @@ export class FeedService extends BaseService {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Creates a new post
|
|
|
|
* Creates a new post
|
|
|
|
* @param pContent
|
|
|
|
* @param pContent
|
|
|
|
|
|
|
|
* @param file
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public createPost(pContent: String) {
|
|
|
|
public createPost(pContent: String, file?: File) {
|
|
|
|
const body = {
|
|
|
|
const body = {
|
|
|
|
query: createPostGqlQuery,
|
|
|
|
query: createPostGqlQuery,
|
|
|
|
variables: {
|
|
|
|
variables: {
|
|
|
|
content: pContent
|
|
|
|
content: pContent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
return this.createPostRequest(body);
|
|
|
|
return this.createPostRequest(body, file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Creates a post with an activity
|
|
|
|
* Creates a post with an activity
|
|
|
|
* @param pContent
|
|
|
|
* @param pContent
|
|
|
|
* @param activityId
|
|
|
|
* @param activityId
|
|
|
|
|
|
|
|
* @param file
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public createPostActivity(pContent: String, activityId: String) {
|
|
|
|
public createPostActivity(pContent: String, activityId: String, file?: File) {
|
|
|
|
const body = {
|
|
|
|
const body = {
|
|
|
|
query: createPostActivityGqlQuery, variables: {
|
|
|
|
query: createPostActivityGqlQuery, variables: {
|
|
|
|
content: pContent,
|
|
|
|
content: pContent,
|
|
|
|
id: activityId
|
|
|
|
id: activityId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
return this.createPostRequest(body);
|
|
|
|
return this.createPostRequest(body, file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Creates a new post with a given request.
|
|
|
|
* Creates a new post with a given request.
|
|
|
|
* @param body
|
|
|
|
* @param body
|
|
|
|
|
|
|
|
* @param file - a file that is being uploaded with the post
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private createPostRequest(body: { variables: any; query: string }) {
|
|
|
|
private createPostRequest(body: { variables: any; query: string }, file?: File) {
|
|
|
|
return this.postGraphql(body, null, 0)
|
|
|
|
return this.postGraphql(body, null, 0)
|
|
|
|
.pipe(tap(response => {
|
|
|
|
.pipe(tap(response => {
|
|
|
|
if (this.activePostList === Sort.NEW) {
|
|
|
|
if (this.activePostList === Sort.NEW) {
|
|
|
|
const updatedPosts = this.posts.getValue();
|
|
|
|
const updatedPosts = this.posts.getValue();
|
|
|
|
updatedPosts.unshift(this.constructPost(response));
|
|
|
|
const post = this.constructPost(response);
|
|
|
|
this.posts.next(updatedPosts);
|
|
|
|
updatedPosts.unshift(post);
|
|
|
|
|
|
|
|
if (file) {
|
|
|
|
|
|
|
|
this.uploadPostImage(post.id, file).subscribe((result) => {
|
|
|
|
|
|
|
|
post.mediaUrl = result.fileName;
|
|
|
|
|
|
|
|
post.mediaType = result.fileName.endsWith('.png') ? 'IMAGE' : 'VIDEO';
|
|
|
|
|
|
|
|
this.posts.next(updatedPosts);
|
|
|
|
|
|
|
|
}, error => {
|
|
|
|
|
|
|
|
console.error(error);
|
|
|
|
|
|
|
|
this.deletePost(post.id);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
this.posts.next(updatedPosts);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Uploads a file for a post
|
|
|
|
|
|
|
|
* @param postId
|
|
|
|
|
|
|
|
* @param file
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private uploadPostImage(postId: number, file: File): Observable<IFileUploadResult> {
|
|
|
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
|
|
|
formData.append('postMedia', file);
|
|
|
|
|
|
|
|
formData.append('postId', postId.toString());
|
|
|
|
|
|
|
|
return this.post<IFileUploadResult>(environment.greenvironmentUrl + '/upload', formData, null, 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Upvotes a post
|
|
|
|
* Upvotes a post
|
|
|
|
* @param postId
|
|
|
|
* @param postId
|
|
|
@ -300,7 +331,8 @@ export class FeedService extends BaseService {
|
|
|
|
post.deletable,
|
|
|
|
post.deletable,
|
|
|
|
date,
|
|
|
|
date,
|
|
|
|
author,
|
|
|
|
author,
|
|
|
|
activity);
|
|
|
|
activity,
|
|
|
|
|
|
|
|
post.media);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public constructAllPosts(response: any): Post[] {
|
|
|
|
public constructAllPosts(response: any): Post[] {
|
|
|
@ -340,7 +372,8 @@ export class FeedService extends BaseService {
|
|
|
|
post.deletable,
|
|
|
|
post.deletable,
|
|
|
|
date,
|
|
|
|
date,
|
|
|
|
author,
|
|
|
|
author,
|
|
|
|
activity));
|
|
|
|
activity,
|
|
|
|
|
|
|
|
post.media));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return posts;
|
|
|
|
return posts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|