profile.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { Component } from "@angular/core";
  2. import {
  3. IonicPage,
  4. NavController,
  5. NavParams,
  6. InfiniteScroll
  7. } from "ionic-angular";
  8. import { TwitterApiProvider } from "../../providers/twitter-api/twitter-api";
  9. import { P2pStorageIpfsProvider } from "../../providers/p2p-storage-ipfs/p2p-storage-ipfs";
  10. import Gun from "gun";
  11. /**
  12. * Generated class for the ProfilePage page.
  13. *
  14. * See https://ionicframework.com/docs/components/#navigation for more info on
  15. * Ionic pages and navigation.
  16. */
  17. @IonicPage()
  18. @Component({
  19. selector: "page-profile",
  20. templateUrl: "profile.html"
  21. })
  22. export class ProfilePage {
  23. userId: string;
  24. user: any = [];
  25. gun;
  26. tweets: any[];
  27. constructor(
  28. public navCtrl: NavController,
  29. public navParams: NavParams,
  30. private twitter: TwitterApiProvider,
  31. private ipfs: P2pStorageIpfsProvider
  32. ) {
  33. this.gun = Gun();
  34. }
  35. ionViewDidLoad() {
  36. this.userId = this.navParams.get("userId");
  37. this.twitter.fetchUser(this.userId).then(res => (this.user = res));
  38. this.twitter
  39. .fetchUserTimeline(this.userId)
  40. .then(res => (this.tweets = res));
  41. this.checkP2pTweets(this.userId);
  42. }
  43. doRefresh(refresher) {
  44. this.twitter.fetchUserTimeline(this.userId).then(res => {
  45. this.tweets = res;
  46. refresher.complete();
  47. });
  48. }
  49. loadMore(infiniteScroll: InfiniteScroll) {
  50. this.twitter
  51. .fetchUserTimelineSince(
  52. this.userId,
  53. this.tweets[this.tweets.length - 1].id
  54. )
  55. .then(res => {
  56. this.tweets = this.tweets.concat(res);
  57. infiniteScroll.complete();
  58. });
  59. }
  60. private async checkP2pTweets(userId) {
  61. let lastTweetHash;
  62. this.gun
  63. .get("username")
  64. .get("lastTweet")
  65. .once(lastTweet => (lastTweetHash = lastTweet));
  66. const res = await this.ipfs.fetchTweet(lastTweetHash);
  67. console.log(res);
  68. }
  69. }