mock-provider.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { Injectable } from "@angular/core";
  2. import { Storage } from "@ionic/storage";
  3. import { Platform,Events } from 'ionic-angular';
  4. import { File } from '@ionic-native/file';
  5. import { HttpClient } from "@angular/common/http";
  6. import * as tweets from './tweet.json';
  7. import * as user from '../../app/user.json';
  8. import * as sampleTweet from './sampleTweet.json';
  9. declare var cordova: any;
  10. @Injectable()
  11. export class MockProvider {
  12. friends;
  13. userId: string;
  14. mockTweets;
  15. sampleTweet;
  16. result;
  17. fileRead: Boolean;
  18. constructor(
  19. private storage: Storage,
  20. private file: File,
  21. public http: HttpClient,
  22. private platform: Platform,
  23. private events: Events
  24. ) {
  25. console.log('tweets are:', tweets);
  26. this.fileRead = false;
  27. this.sampleTweet = sampleTweet;
  28. console.log("sample tweet is;",this.sampleTweet);
  29. this.mockTweets = [...tweets];
  30. console.log(' this.mockTweets are:', this.mockTweets);
  31. this.storage.get("userId").then(userId => (this.userId = userId));
  32. }
  33. /**
  34. * Retrives the public and private tweets for a user
  35. * Since it is loaded in batches of 20 public tweets, public and private tweet are used as reference to load next 20 tweets
  36. * @param userId user id
  37. * @param oldestPublicTweet oldest public tweet
  38. * @param oldestPrivateTweet oldest private tweet
  39. */
  40. public async loadUserTimeline(
  41. userId,
  42. oldestPublicTweet ? ,
  43. oldestPrivateTweet ?
  44. ) {
  45. console.log('user time line called in mockprovider');
  46. await this.readFile().then(tweets => {
  47. console.log('new mocktweets after replace', this.mockTweets);
  48. })
  49. .catch(err => console.error('error in readfile', err));
  50. return this.mockTweets
  51. .sort((a, b) => this.sortByDateAsc(a, b));
  52. }
  53. /**
  54. * Retrieves the home feed for the logged in user
  55. * Since it is loaded in batches of 20 public tweets, public and private tweet are used as reference to load next 20 tweets
  56. * @param oldestPublicTweet oldest public tweet
  57. * @param oldestPrivateTweet oldest private tweet
  58. */
  59. public async loadHomeTimeline(oldestPublicTweet ? , oldestPrivateTweet ? ) {
  60. console.log('hometime line called in mockprovider');
  61. await this.readFile().then(tweets => {
  62. console.log('new mocktweets after replace', this.mockTweets);
  63. })
  64. .catch(err => console.error('error in readfile', err));
  65. return await this.mockTweets
  66. .sort((a, b) => this.sortByDateAsc(a, b));
  67. }
  68. public async replyTweet() {}
  69. public async writeTweet(twt: string, isPrivate: boolean, retwt ?, reply ?) {
  70. console.log('inside writetweet', retwt, reply);
  71. let newTweet:any = Object.assign({}, this.sampleTweet);
  72. console.log("new tweet is:",newTweet);
  73. // let newTweet:any = {};
  74. newTweet.full_text = twt;
  75. newTweet.id = Math.floor(100000 + Math.random() * 900000);
  76. newTweet.id_str = newTweet.id;
  77. newTweet.full_text = twt;
  78. newTweet.created_at = Date.now();
  79. newTweet.is_quote_status = false;
  80. if(retwt){
  81. newTweet.is_quote_status = true;
  82. newTweet.quoted_status = retwt;
  83. newTweet.quoted_status_id = retwt.id;
  84. newTweet.quoted_status_id_str = retwt.id_str;
  85. }
  86. if(reply){
  87. newTweet.in_reply_to_screen_name=reply.user.name;
  88. newTweet.in_reply_to_status_id = reply.id;
  89. newTweet.in_reply_to_status_id_str = reply.id_str;
  90. newTweet.in_reply_to_user_id = reply.user.id;
  91. newTweet.in_reply_to_user_id_str = reply.user.id_str;
  92. }
  93. if (isPrivate) {
  94. newTweet.private_tweet = true;
  95. }
  96. console.log('new tweet ', newTweet);
  97. this.mockTweets.push(newTweet);
  98. // this.events.publish("tweets:changed", this.mockTweets);
  99. await this.writefile(this.mockTweets);
  100. }
  101. public async writefile(mocktweet) {
  102. await this.platform.ready()
  103. .then(_ => console.log('Device ready.'));
  104. //check if file exists
  105. this.result = await this.file.writeFile(this.file.dataDirectory, 'twets.json', JSON.stringify(mocktweet), { replace: true, append: false });
  106. console.log("writing the file:", this.result);
  107. }
  108. public async readFile() {
  109. var readTweets: [];
  110. console.log('in readfile', this.mockTweets);
  111. console.log("this.file.dataDirectory:",this.file.dataDirectory);
  112. await this.file.checkFile(this.file.dataDirectory, 'twets.json')
  113. .then(async() => {
  114. console.log('file exists');
  115. await this.file.readAsText(this.file.dataDirectory, 'twets.json')
  116. .then( data => {
  117. readTweets = JSON.parse(data);
  118. if (!this.fileRead) {
  119. // readTweets.forEach(el=>this.mockTweets.push(el));
  120. this.mockTweets = [...readTweets];
  121. this.fileRead = true;
  122. }
  123. console.log('file is: ', this.mockTweets);
  124. })
  125. .catch(err => console.error('error in reading file', err));
  126. })
  127. .catch(err => {
  128. console.error('File doesnt exist', err);
  129. this.file.writeFile(this.file.dataDirectory, 'twets.json', JSON.stringify(this.mockTweets), { replace: true });
  130. });
  131. return this.mockTweets;
  132. }
  133. public likeTweet(id) {
  134. console.log('fetch tweet id:', id, this.mockTweets);
  135. const result = this.mockTweets.find(x => x.id_str == id);
  136. // this.mockTweets.forEach(el=>{
  137. // console.log("mocktweet el is:",el);
  138. // })
  139. result.favorited = true;
  140. result.favorite_count++;
  141. console.log('tweet found and modified:', result);
  142. return result;
  143. }
  144. public unlikeTweet(id) {
  145. console.log('fetch tweet id:', id, this.mockTweets);
  146. const result = this.mockTweets.find(x => x.id_str == id);
  147. // this.mockTweets.forEach(el=>{
  148. // console.log("mocktweet el is:",el);
  149. // })
  150. result.favorited = false;
  151. result.favorite_count--;
  152. console.log('tweet found and modified:', result);
  153. return result;
  154. }
  155. public fetchTweet(tweetID) {
  156. return this.mockTweets.find(x => x.id_str == tweetID);
  157. }
  158. public async fetchUser(userId) {
  159. return user;
  160. }
  161. private async fetchPrivateTweets(privateTweetsData: object[]) {
  162. }
  163. public async updateProfileDescription(description: string) {
  164. return user;
  165. }
  166. private sortByDateAsc(a, b) {
  167. const dateA = new Date(a.created_at);
  168. const dateB = new Date(b.created_at);
  169. if (dateA > dateB) {
  170. return -1;
  171. } else if (dateA < dateB) {
  172. return 1;
  173. } else {
  174. return 0;
  175. }
  176. }
  177. }