weverse
Version:
Wrapper for weverse private API
74 lines (73 loc) • 2.04 kB
JavaScript
import { AssignType } from "../client";
export class WeverseCommunity extends AssignType() {
constructor(props, test) {
super(props);
this.posts = [];
this.postsMap = new Map();
this.photos = [];
this.videos = [];
this.artists = [];
this.artistMap = new Map();
this.newPosts = [];
this.media = [];
this.mediaMap = new Map();
}
toString() {
return this.name;
}
addArtists(artists) {
this.artists = artists;
this.artistMap = new Map();
for (const artist of artists) {
this.artistMap.set(artist.id, artist);
}
}
addPosts(posts) {
this.newPosts = [];
posts.forEach(p => {
if (!this.postsMap.has(p.id)) {
this.posts.unshift(p);
this.postsMap.set(p.id, p);
this.newPosts.push(p);
}
});
return this.newPosts;
}
addPhotos(photos) {
const newPhotos = [];
for (const photo of photos) {
if (!this.photos.find(p => p.id === photo.id)) {
this.photos.unshift(photo);
newPhotos.unshift(photo);
}
}
return newPhotos;
}
addVideos(videos) {
const newVideos = [];
for (const video of videos) {
if (!this.videos.find(p => p.thumbnailUrl === video.thumbnailUrl)) {
this.videos.unshift(video);
newVideos.unshift(video);
}
}
return newVideos;
}
addMedia(media) {
if (this.mediaMap.has(media.id))
return null;
this.mediaMap.set(media.id, media);
this.media.push(media);
return media;
}
getArtist(id) {
return this.artistMap.get(id);
}
toJSON() {
const partial = Object.assign({}, this);
delete partial.artistMap;
delete partial.mediaMap;
delete partial.postsMap;
return partial;
}
}