import { Component } from '@angular/core'; import { NavController, NavParams } from 'ionic-angular'; import * as pnut from 'pnut-butter'; /** * Generated class for the ProfilePage page. * * See https://ionicframework.com/docs/components/#navigation for more info on * Ionic pages and navigation. */ @Component({ selector: 'page-profile', templateUrl: 'profile.html', }) export class ProfilePage { private user: any; private posts: Array = []; private bookmarks: Array = []; private before_id_post: string; private before_id_stars: string; private myUsername: string; constructor(public navCtrl: NavController, public navParams: NavParams) { this.user = this.navParams.data.user; this.myUsername = this.navParams.data.me; } ionViewDidLoad() { console.log('ionViewDidLoad ProfilePage'); let params = { include_deleted: 0, include_raw: 1, count: 40 }; pnut.postsFrom(this.user.id, params).then(res => { this.posts = this.parseData(res.data); this.before_id_post = res.meta.min_id; }); pnut.bookmarks(this.user.id, params).then(res => { this.bookmarks = this.parseData(res.data); this.before_id_stars = res.meta.min_id; }); } fetchOlderPosts(infiniteScroll, stream) { let before_id = this.before_id_post; let fetcher = pnut.postsFrom; if (stream === 'bookmarks') { before_id = this.before_id_stars; fetcher = pnut.bookmarks; } let params = { include_deleted: 0, include_raw: 1, before_id: before_id, count: 40 }; fetcher(this.user.id, params).then(res => { if (res.data.length > 0) { if (stream === 'posts') { this.posts.push.apply(this.posts, this.parseData(res.data)); this.before_id_post = res.meta.min_id; } else if (stream === 'bookmarks') { this.bookmarks.push.apply(this.bookmarks, this.parseData(res.data)); this.before_id_stars = res.meta.min_id; } } infiniteScroll.complete(); }).catch(err => { console.log(err); }); } parseData(data) { var pdata = []; for (var i = 0; i < data.length; i++) { if (!data[i].is_deleted) { if (data[i]['repost_of']) { data[i] = data[i]['repost_of'] var reposted_by_string = ""; for (var j = 0; j < data[i]['reposted_by'].length; j++) { reposted_by_string = reposted_by_string + data[i]['reposted_by'][j]['username'] + ", "; } // data[i]['reposted_by_string'] = "Reposted by: " + reposted_by_string; } if (data[i].content) { for (var k = 0; k < data[i]['content']['entities']['mentions'].length; k++) { var men = data[i]['content']['entities']['mentions'][k]['text']; if (this.myUsername === men) { data[i]['you_are_mentioned'] = true; } } } pdata.push(data[i]); } } return pdata; } }