123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- import { Component, ViewChild } from "@angular/core";
- import {
- IonicPage,
- NavController,
- NavParams,
- InfiniteScroll,
- Content
- } from "ionic-angular";
- import { TwitterApiProvider } from "../../providers/twitter-api/twitter-api";
- import { P2pStorageIpfsProvider } from "../../providers/p2p-storage-ipfs/p2p-storage-ipfs";
- import { P2pDatabaseGunProvider } from "../../providers/p2p-database-gun/p2p-database-gun";
- /**
- * Generated class for the ProfilePage page.
- *
- * See https://ionicframework.com/docs/components/#navigation for more info on
- * Ionic pages and navigation.
- */
- @IonicPage()
- @Component({
- selector: "page-profile",
- templateUrl: "profile.html"
- })
- export class ProfilePage {
- user: any = [];
- tweets: any[];
- oldestLoadedTweetId;
- enableRefresh: boolean = true;
- enableInfiniteScroll: boolean = true;
- @ViewChild(Content)
- content: Content;
- constructor(
- public navCtrl: NavController,
- public navParams: NavParams,
- private twitter: TwitterApiProvider,
- private ipfs: P2pStorageIpfsProvider,
- private gun: P2pDatabaseGunProvider
- ) {}
- ionViewDidLoad() {
- const userId = this.navParams.get("userId");
- this.twitter.fetchUser(userId).then(res => (this.user = res));
- this.loadTimeline(userId, null).then(res => {
- if (res.length > 0) {
- // Save tweets
- this.tweets = res;
- // Save oldest tweet's id for next load more
- this.oldestLoadedTweetId = res
- .filter(tweet => !tweet.private_tweet)
- .reduce((acc, cur) => (acc.id < cur.id ? acc : cur))["id"];
- }
- });
- }
- doRefresh(refresher) {
- this.loadTimeline(this.user.id, null).then(res => {
- if (res.length > 0) {
- // Replace tweets
- this.tweets = res;
- // Save oldest tweet's id for next load more
- this.oldestLoadedTweetId = res
- .filter(tweet => !tweet.private_tweet)
- .reduce((acc, cur) => (acc.id < cur.id ? acc : cur))["id"];
- }
- // Hide loading icon
- refresher.complete();
- });
- }
- loadMore(infiniteScroll: InfiniteScroll) {
- this.loadTimeline(this.user.id, this.oldestLoadedTweetId).then(res => {
- if (res.length > 0) {
- // Append loaded tweets
- this.tweets = this.tweets.concat(res);
- // Save oldest tweet's id for next load more
- this.oldestLoadedTweetId = res
- .filter(tweet => !tweet.private_tweet)
- .reduce((acc, cur) => (acc.id < cur.id ? acc : cur))["id"];
- }
- // Hide loading icon
- infiniteScroll.complete();
- });
- }
- private async loadTimeline(userId, oldestLoadedTweetId) {
- // Fetch tweets from Twitter
- const tweets =
- oldestLoadedTweetId == null
- ? await this.twitter.fetchUserTimeline(userId)
- : await this.twitter.fetchUserTimelineSince(
- userId,
- oldestLoadedTweetId
- );
- // Determine end of time interval to look for private tweets
- let intervalEnd;
- if (tweets.length < 20) {
- // End of timeline is reached
- this.enableInfiniteScroll = false;
- intervalEnd = new Date("2018-04-01T00:00:00");
- } else {
- const lastTweetTimestamp = tweets[tweets.length - 1].created_at;
- intervalEnd = new Date(lastTweetTimestamp);
- }
- // Fetch private tweet hashs from P2P DB for corresponding interval
- const privateTweetHashs: string[] = await this.gun.fetchPrivateTweetHashsForUserInInterval(
- userId,
- new Date(),
- intervalEnd
- );
- // Load private tweets from P2P storage
- let privateTweets = await this.ipfs.fetchTweets(privateTweetHashs);
- // Add user object to private tweets
- privateTweets = await Promise.all(
- privateTweets.map(async tweet => await this.addUserToTweet(tweet))
- );
- // Combine and sort tweets
- return tweets
- .concat(privateTweets)
- .sort((a, b) => this.sortByDateAsc(a, b));
- }
- private sortByDateAsc(a, b) {
- const dateA = new Date(a.created_at);
- const dateB = new Date(b.created_at);
- if (dateA > dateB) {
- return -1;
- } else if (dateA < dateB) {
- return 1;
- } else {
- return 0;
- }
- }
- private async addUserToTweet(tweet) {
- tweet.user = await this.twitter.fetchUser(tweet.user_id);
- return tweet;
- }
- onScroll(event) {
- this.enableRefresh = event.scrollTop === 0;
- }
- }
|