From 1aa117bcc2f9a1f43a0fa7f4859772d72e25e7ec Mon Sep 17 00:00:00 2001 From: Amitosh Swain Mahapatra Date: Apr 14 2018 06:10:36 +0000 Subject: Sort posts by their dates --- diff --git a/src/pages/social/social.html b/src/pages/social/social.html index 835a9e5..4cbc272 100644 --- a/src/pages/social/social.html +++ b/src/pages/social/social.html @@ -14,23 +14,23 @@ - - + + - - {{ update.content }} + + {{ post.content }} - diff --git a/src/pages/social/social.ts b/src/pages/social/social.ts index 4af236f..b9d430e 100644 --- a/src/pages/social/social.ts +++ b/src/pages/social/social.ts @@ -11,24 +11,27 @@ import { Tw } from '../../providers/tw/tw'; See http://ionicframework.com/docs/v2/components/#navigation for more info on Ionic pages and navigation. */ +export interface Post { + link:string + content:string, + origin:string, + date: Date +} @Component({ templateUrl: 'social.html', providers: [FB, Tw], }) export class SocialPage { - // TODO: Refactor, we are not supposed to use `any`, rather, ww should define an + // TODO: Refactor, we are not supposed to use `any`, rather, we should define an // interface Post { content:string, date:Date } - private posts:Array; - private tweets:Array; - private updates:Array; + private posts:Post[]; + private USER:any; - constructor(private browser:Browser, private fb:FB, private tw:Tw, + constructor(private browser:Browser, private fb:FB, private twitter:Tw, private socialSharing:SocialSharing) { this.posts = []; - this.tweets = []; - this.updates = []; this.USER = { FB: 'fedoraqa', @@ -40,25 +43,13 @@ export class SocialPage { this.updateUpdates(); } - updateUpdates() { - this.fb - .getPagePosts(this.USER.FB) - .then((posts:Array) => { - this.posts = posts; - this.mergeUpdates(); - }).catch((err) => console.log(err + "kanika")); - - this.tw - .getTimelineTweets(this.USER.TW) - .then((tweets:Array) => { - this.tweets = tweets; - this.mergeUpdates(); - }).catch((err) => console.log(err)); - } + async updateUpdates() { + const posts = await Promise.all([ + this.fb.getPagePosts(this.USER.FB), + this.twitter.getTimelineTweets(this.USER.TW) + ]); - mergeUpdates() { - // TODO: Merge as per ascending order of timestamps? - this.updates = [ ...this.posts, ...this.tweets ]; + this.posts = [...posts[0], ...posts[1]].sort((a:Post,b:Post) => b.date.getTime() - a.date.getTime() ); } openUpdate(event) { diff --git a/src/providers/fb/fb.ts b/src/providers/fb/fb.ts index a593233..d41c208 100644 --- a/src/providers/fb/fb.ts +++ b/src/providers/fb/fb.ts @@ -3,6 +3,7 @@ import 'rxjs/add/operator/map'; import { Facebook } from 'fb'; import * as _ from 'lodash'; +import { Post } from '../../pages/social/social'; const FB_CONFIG = { accessToken: '1242536905766029|8btZ0cyO3iDWxl92d6pgj0xC3a8', @@ -35,7 +36,7 @@ export class FB { }); } - getPagePosts(page) { + getPagePosts(page):Promise { return new Promise((resolve, reject) => { this.api([page, 'posts']).then((res:any) => { var posts = _.compact(_.map(res.data, p => { @@ -44,6 +45,7 @@ export class FB { link: 'https://facebook.com/' + p.id, content: p.message, origin: 'facebook', + date: new Date(p.created_time) }; if (_.isEmpty(post.content)) { diff --git a/src/providers/tw/tw.ts b/src/providers/tw/tw.ts index a99d7ab..5739d26 100644 --- a/src/providers/tw/tw.ts +++ b/src/providers/tw/tw.ts @@ -4,6 +4,7 @@ import 'rxjs/add/operator/map'; import * as _ from 'lodash'; import { Request } from '../request/request'; +import { Post } from '../../pages/social/social'; const API_ENDPOINT = 'https://api.twitter.com/1.1/'; @@ -27,7 +28,7 @@ export class Tw { constructor(private request:Request) { } - getTimelineTweets(user) { + getTimelineTweets(user):Promise { return new Promise((resolve, reject) => { this.request.get( API.timeline, { screen_name: user }, @@ -39,6 +40,7 @@ export class Tw { link: 'https://twitter.com/statuses/' + t.id_str, content: t.text, origin: 'twitter', + date: new Date(t.created_at) }; }); return resolve(tweets);