profile.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 { HttpClient } from "@angular/common/http";
  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 http: HttpClient
  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.http
  67. .get("https://ipfs.infura.io:5001/api/v0/cat", {
  68. params: { arg: lastTweetHash }
  69. })
  70. .toPromise();
  71. // const tweet = JSON.parse(res);
  72. console.log(res);
  73. }
  74. }