Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #724: Push search data to server using api/push.json endpoint of server #753

Merged
merged 2 commits into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/app/effects/api-search.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
skip
} from 'rxjs/operators';

import { SearchService, SearchServiceConfig } from '../services';
import { SearchService, SearchServiceConfig, PushService } from '../services';
import * as fromRoot from '../reducers';
import * as apiAction from '../actions/api';
import * as queryAction from '../actions/query';
Expand Down Expand Up @@ -117,6 +117,7 @@ export class ApiSearchEffects {
),
withLatestFrom(this.store$),
map(([action, state]) => {
this.pushService.postData(action['payload']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is otiose to push data to the backend when you are actually getting the data from the backend. It would already contain all the statuses in this case.

The push servlet is supposed to be used when we are fetching Tweets using Twitter API they may be new for the backend.

Copy link
Member Author

@simsausaurabh simsausaurabh Jun 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree it would be more helpful once we complete the twitter api support. But till then it could work I guess, after that we can just push the mapped data from twitter api i.e. the data extracted from twitter api mapped in format of api.loklak. I have made all other changes, and I will surely write the tests in further patches. Thanks @singhpratyush.

return {
doRelocate: state.query.relocateAfter,
relocateTo: state.query.routerString
Expand Down Expand Up @@ -208,6 +209,7 @@ export class ApiSearchEffects {
private store$: Store<fromRoot.State>,
private actions$: Actions,
private apiSearchService: SearchService,
private pushService: PushService,
private location: Location
) { }

Expand Down
1 change: 1 addition & 0 deletions src/app/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './time-bound';
export * from './media-wall-custom';
export * from './media-wall-filters';
export * from './media-wall-direct-url';
export * from './push';
6 changes: 6 additions & 0 deletions src/app/models/push.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface PushApiResponse {
status: string;
records: number;
mps: number;
message: string;
}
1 change: 1 addition & 0 deletions src/app/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './search.service';
export * from './suggest.service';
export * from './user.service';
export * from './speech.service';
export * from './push.service';
21 changes: 21 additions & 0 deletions src/app/services/push.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { TestBed, inject } from '@angular/core/testing';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { PushService } from './push.service';

describe('PushService', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests technically do not test anything significant.

How do you know that proper calls are made and functions are called accordingly? Please try to cover more cases.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to use already existing mock data inside shared/mocks/feedItem.mock.ts, but there comes some issue with some of the parameters inside individual: search_metadata, statuses, and aggregations. I tried to make it work out, but it is taking longer time than expected. Currently the simple test checks that push service is being created. I will add tests in later patches (Because many other parts need new tests since the project is upgraded, and the tests written earlier can't be taken as it is, I have to write new ones). I want to fix sidebar, newsTab issue firstly before this weekend. Hope that works?

beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientModule
],
providers: [
PushService,
HttpClient
]
});
});

it('should be created', inject([PushService], (service: PushService) => {
expect(service).toBeTruthy();
}));
});
49 changes: 49 additions & 0 deletions src/app/services/push.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Injectable } from '@angular/core';
import {
HttpClient,
HttpHeaders,
HttpParams
} from '@angular/common/http';
import { Observable } from 'rxjs';
import { ApiResponse, PushApiResponse } from '../models';
@Injectable({
providedIn: 'root'
})
/*
* The PushService is called on SEARCH_COMPLETE_SUCCESS
* to push the result data back to server in the same
* search result object format obtained from server.
**/
export class PushService {

constructor( private http: HttpClient ) { }

// Returns an Observable of PushApiResponse.
public postData(data: ApiResponse): Observable<PushApiResponse> {

// End point to make a Post to.
const httpUrl = 'https://api.loklak.org/api/push.json';

const headers = new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'cache-control': 'no-cache'
});
// Extracting search_metadata and statuses
// from the data obtained from search result.
const { statuses } = data;
const searchMetadata = data.search_metadata;

// Converting the object to JSON string.
const dataToSend = JSON.stringify({search_metadata: searchMetadata, statuses});

// Setting the data to send in HttpParams()
// with key as 'data'
const body = new HttpParams().set('data', dataToSend);

// Making a Post request to api/push.json endpoint
// of server with required header and data body.
// Response Object is converted to PushApiResponse type.
return this.http.post<PushApiResponse>(httpUrl, body, {headers});
}
}