-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeed.service.ts
82 lines (66 loc) · 2.53 KB
/
feed.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from 'environments/environment';
import { Observable, Subject, map } from 'rxjs';
import { Feed } from '@models/feed.model';
import { IResponseFeed } from '@interfaces/response.interface';
import Storage from '@utils/storage.util';
import { AuthService } from './auth.service';
const base_url = environment.base_url;
@Injectable({
providedIn: 'root'
})
export class FeedService {
private recentFeeds: Feed[];
public changeNews: Subject<boolean> = new Subject();
constructor(
private http: HttpClient,
private authService: AuthService,
) { }
public changeToExplore(value: boolean): void {
this.changeNews.next(value);
}
get headers() {
return {
headers: {
'x-token': Storage.getItem('x-token'),
},
};
}
getFeeds(skip = 0, limit = 10, isAuthenticated = false): Observable<Feed[]> {
const url = (!isAuthenticated) ?
`${base_url}/feed?skip=${skip}&limit=${limit}` :
`${base_url}/feed/byUser/subscription?skip=${skip}&limit=${limit}`;
const headers = !isAuthenticated ? {} : this.headers;
return this.http.get<IResponseFeed>(url, headers)
.pipe(map(resp => this.mapInUserResource(resp.feeds) as Feed[]));
}
getFeed(id: string): Observable<Feed> {
const url = `${base_url}/feed/${id}`;
return this.http.get<IResponseFeed>(url)
.pipe(map((resp) => this.mapInUserResource(resp.feed) as Feed));
}
getSavedFeeds(skip = 0, limit = 10): Observable<Feed[]> {
const url = `${base_url}/feed/byUser/saved?skip=${skip}&limit=${limit}`;
return this.http.get<IResponseFeed>(url, this.headers)
.pipe(map((resp) => this.mapInUserResource(resp.feeds) as Feed[]));
}
searchFeeds(skip = 0, limit = 10, query: string): Observable<Feed[]> {
const url = `${base_url}/feed/search?skip=${skip}&limit=${limit}&q=${query}`;
return this.http.get<IResponseFeed>(url).pipe(map(resp => resp.feeds));
}
public mapInUserResource(feeds: Feed[] | Feed): Feed[] | Feed {
const userActive = this.authService.getUserActive();
if (userActive) {
const { savedFeeds, likedFeeds } = userActive;
const mapFeed = (feed: Feed): Feed => {
feed.inUser = savedFeeds?.includes(feed._id) || false;
feed.liked = likedFeeds?.includes(feed._id) || false;
feed.likes = feed.likes ?? 0;
return feed;
};
Array.isArray(feeds) ? feeds.forEach(mapFeed) : feeds = mapFeed(feeds);
}
return feeds;
}
}