#49 [WIP][DO NOT MERGE] Sort posts by their dates
Opened 2 years ago by amitosh. Modified 2 years ago
amitosh/Fedora-app sort-posts  into  master

file modified
+6 -6

@@ -14,23 +14,23 @@ 

  

    <img id="social_logo" src="assets/img/social_icon.svg">

  

-   <ion-list *ngIf="updates.length !== 0" id="social_card">

-     <ion-card *ngFor="let update of updates" >

+   <ion-list *ngIf="posts.length !== 0" id="social_card">

+     <ion-card *ngFor="let post of posts" >

  

          <ion-row>

-         <ion-col tappable (click)="openUpdate(update)">

-           {{ update.content }}

+         <ion-col tappable (click)="openUpdate(post)">

+           {{ post.content }}

          </ion-col>

  

        </ion-row>

        <ion-row >

          <ion-col center >

            <button ion-button small clear disabled class="s-icon">

-             <ion-icon name="logo-{{ update.origin }}"></ion-icon>

+             <ion-icon name="logo-{{ post.origin }}"></ion-icon>

            </button>

          </ion-col>

          <ion-col center>

-           <button ion-button small clear tappable color="dark" (click)="shareUpdate(update)">

+           <button ion-button small clear tappable color="dark" (click)="shareUpdate(post)">

              <ion-icon name="share"></ion-icon>

            </button>

          </ion-col>

file modified
+16 -25

@@ -11,24 +11,27 @@ 

    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<any>;

-   private tweets:Array<any>;

-   private updates:Array<any>;

+   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 @@ 

      this.updateUpdates();

    }

  

-   updateUpdates() {

-     this.fb

-       .getPagePosts(this.USER.FB)

-       .then((posts:Array<any>) => {

-         this.posts = posts;

-         this.mergeUpdates();

-       }).catch((err) => console.log(err + "kanika"));

- 

-     this.tw

-       .getTimelineTweets(this.USER.TW)

-       .then((tweets:Array<any>) => {

-         this.tweets = tweets;

-         this.mergeUpdates();

-       }).catch((err) => console.log(err));

-   }

+   async updateUpdates() {

+     const posts = await Promise.all<Post[],Post[]>([

+       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) {

file modified
+3 -1

@@ -3,6 +3,7 @@ 

  

  import { Facebook } from 'fb';

  import * as _ from 'lodash';

+ import { Post } from '../../pages/social/social';

  

  const FB_CONFIG = {

    accessToken: '1242536905766029|8btZ0cyO3iDWxl92d6pgj0xC3a8',

@@ -35,7 +36,7 @@ 

      });

    }

  

-   getPagePosts(page) {

+   getPagePosts(page):Promise<Post[]> {

      return new Promise((resolve, reject) => {

        this.api([page, 'posts']).then((res:any) => {

          var posts = _.compact(_.map(res.data, p => {

@@ -44,6 +45,7 @@ 

              link: 'https://facebook.com/' + p.id,

              content: p.message,

              origin: 'facebook',

+             date: new Date(p.created_time)

            };

  

            if (_.isEmpty(post.content)) {

file modified
+3 -1

@@ -4,6 +4,7 @@ 

  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 @@ 

  

    constructor(private request:Request) { }

  

-   getTimelineTweets(user) {

+   getTimelineTweets(user):Promise<Post[]> {

      return new Promise((resolve, reject) => {

        this.request.get(

          API.timeline, { screen_name: user },

@@ -39,6 +40,7 @@ 

                link: 'https://twitter.com/statuses/' + t.id_str,

                content: t.text,

                origin: 'twitter',

+               date: new Date(t.created_at)

              };

            });

            return resolve(tweets);

When clubbing the posts from different sources, we lose the chronological order of the posts. We now sort the post array after fetching posts from all the sources to restore the sorted order

Closes #40
Blocked by #55 , #57

@amitosh, the posts array seems to have chronological order, but they are grouped social application wise, ie, like now, the latest post is on facebook, so all post of facebook have appeared in a right order, and then the post of twitter is there in correct order.

You might like to sort the posts array after it containing all posts and before it is being displayed through HTML. You can do it by simply creating a sort function or if you want to experiment more, you can create a custom sort filter.

Just saw, it is WIP PR :p . Hope above points can help you.