Query check using RegExp

Specific results can be obtained in loklak using different combinations of type associated with query (e.g. from:user, @user, #query). For identifying the requirement of user through query and maintaining the flow of API requests to the loklak server, RegExp is being used. Through this blog post, I’ll be discussing about its use in the project.

Using RegExp in Services

Patterns for all possible query types has already been defined in reg-exp typescript file inside utils/ folder. We would need to import these expressions to check type of query anywhere inside the codebase.

The query (q) arguments of search endpoints of api.loklak depends on type associated with query i.e. argument for from:user would be different from that of @user. To keep check on the same, I have used RegExp.

if ( hashtagRegExp.exec(query) !== null ) {
       // Check for hashtag query
       jsonpUrl += ‘&q=%23 + hashtagRegExp.
       exec(query)[1] +  + hashtagRegExp.exec(query)[0];
   } else if ( fromRegExp.exec(query) !== null ) {
       // Check for from user query
       jsonpUrl += ‘&q=from%3A’ + fromRegExp.exec(query)[1];
   } else if ( mentionRegExp.exec(query) !== null ) {
       // Check for mention query
       jsonpUrl += ‘&q=%40 + mentionRegExp.exec(query)[1];
   } else if ( nearRegExp.exec(query) !== null ) {
       // Check for near query
       jsonpUrl += ‘&q=near%3A’ + nearRegExp.exec(query)[1];
   } else {
       // for other queries
       jsonpUrl += ‘&q=’ + query;
   }

 

A similar architecture has been used in UserService and SuggestService.

Using RegExp in info-box

A new feature has been added in loklak to provide RSS and JSON feed through api.loklak.org. It requires a query check which is then passed through anchor tag link to provide RSS and JSON feed based on the query and type associated with it. It is being included under info-box and the query check is done in ngOnChanges() which means it will keep updating itself with updating query.

   this.store.select(fromRoot.getQuery).subscribe(query => this.stringQuery = query.displayString);
   this.parseApiResponseData();
   if ( hashtagRegExp.exec(this.stringQuery) !== null ) {
       // Check for hashtag this.stringQuery
       this.queryString = %23 + hashtagRegExp.
       exec(this.stringQuery)[1] +  + hashtagRegExp.exec(this.stringQuery)[0];
   } else if ( fromRegExp.exec(this.stringQuery) !== null ) {
       // Check for from user this.stringQuery
       this.queryString = from%3A + fromRegExp.exec(this.stringQuery)[1];
   } else if ( mentionRegExp.exec(this.stringQuery) !== null ) {
       // Check for mention this.stringQuery
       this.queryString = %40 + mentionRegExp.exec(this.stringQuery)[1];
   } else {
       // for other queries
       this.queryString = this.stringQuery;
   }
}

 

A similar architecture is followed throughout the project in different features implemented so far. It would not be easy to provide each of those implementation in one blog. For actual implementations, please go through the codebase.

Resources

Continue Reading

Add RSS feed and JSON output based on type specified with query param

The idea behind writing this blog post is to discuss the method on how RSS feed and JSON output sources have been included in loklak to provide respective data sources based on the type specified with query as a parameter.

Accessing Current Query in Info-box

Accessing link to RSS feed and JSON output of loklak requires the Query to be passed as a value with parameter ‘q’ (e.g. api/search.json?q=FOSSASIA or api/search.rss?q=FOSSASIA). In order to represent links as buttons in Info-box at sidebar of loklak.org, current Query needs to be accessed/stored inside Info-box from ngrx store.

public stringQuery;
...
this.store.select(fromRoot.getQuery).subscribe(
    query => this.stringQuery = query.displayString);

 

Firstly stringQuery variable is created to store the current Query from store. As the Query can be changed in store (User might search for several Queries), storing of current Query needs to be done inside ngOnChanges().

Checking type associated with Query

There are various types associated with Query to get different type of results like ‘from:FOSSASIA will give results specifically from ‘FOSSASIA’ which will have different query parameter from other types like ‘@FOSSASIA’ or ‘#FOSSASIA’. To assign appropriate Query parameter to each of these types, we need to check the Query pattern to apply Query param based on the type.

import { hashtagRegExp, fromRegExp, mentionRegExp }
      from ‘../../utils/reg-exp’;
...

if ( hashtagRegExp.exec(this.stringQuery) !== null ) {
	// Check for hashtag this.stringQuery
	this.queryString = ‘%23 + hashtagRegExp.exec(
	this.stringQuery)[1] + '' + hashtagRegExp.exec(
	this.stringQuery)[0];
} else if ( fromRegExp.exec(this.stringQuery) !== null ) {
	// Check for from user this.stringQuery
	this.queryString = ‘from%3A’ + fromRegExp.exec(
	this.stringQuery)[1];
} else if ( mentionRegExp.exec(this.stringQuery) !== null ) {
	// Check for mention this.stringQuery
	this.queryString = ‘%40 + mentionRegExp.exec(
	this.stringQuery)[1];
} else {
	// for other queries
	this.queryString = this.stringQuery;
}

 

Note: hashtagRegExp, fromRegExp, mentionRegExp are the utility functions created to match the pattern of given string (Query) in order to classify the preceding type associated with Query. These are provided here as a reference, which can be used to add more of the types.

Passing current queryString in RSS and JSON link

General link for both RSS and JSON data remains same for each Query passed, only the type associated would be changed in the Query value. So representing the link in an anchor tag in UI would be as –

<a class=“data rss” href=“
		http://api.loklak.org/api/search.
		rss?timezoneOffset=-330&q=
		{{stringQuery}}” target=“_blank”>
</a>
<a class=”data json” href=”http://api.
		loklak.org/api/search
		.json?timezoneOffset=-330&q=
		{{stringQuery}}” target=”_blank“>
</a>

 

{{stringQuery}} is the actual query parameter to be passed to get the required results.

Testing RSS feed and JSON data

Search for a query on loklak, and click on the the RSS or JSON button below sidebar on results page and compare with the results.

RSS and JSON button should be similar to –

Resources

Continue Reading
Close Menu