You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			60 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			TypeScript
		
	
		
		
			
		
	
	
			60 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			TypeScript
		
	
| 
											5 years ago
										 | import { OpenGroupV2Info } from './ApiUtil'; | ||
|  | import _ from 'lodash'; | ||
|  | 
 | ||
|  | /** | ||
|  |  * An onion request to the open group api returns something like | ||
|  |  * {result: {status_code:number; whatever: somerandomtype}; } | ||
|  |  * | ||
|  |  * This utility function just extract the status code and returns it. | ||
|  |  * If the status code is not found, this function returns undefined; | ||
|  |  */ | ||
| 
											5 years ago
										 | export const parseStatusCodeFromOnionRequest = (onionResult: any): number | undefined => { | ||
| 
											5 years ago
										 |   if (!onionResult) { | ||
|  |     return undefined; | ||
|  |   } | ||
|  |   const statusCode = onionResult?.result?.status_code; | ||
|  |   if (statusCode) { | ||
|  |     return statusCode; | ||
|  |   } | ||
|  |   return undefined; | ||
|  | }; | ||
|  | 
 | ||
|  | export const parseMemberCount = (onionResult: any): number | undefined => { | ||
|  |   if (!onionResult) { | ||
|  |     return undefined; | ||
|  |   } | ||
|  |   const memberCount = onionResult?.result?.member_count; | ||
|  |   if (memberCount) { | ||
|  |     return memberCount; | ||
|  |   } | ||
|  |   return undefined; | ||
|  | }; | ||
|  | 
 | ||
| 
											5 years ago
										 | export const parseRooms = (onionResult: any): undefined | Array<OpenGroupV2Info> => { | ||
| 
											5 years ago
										 |   if (!onionResult) { | ||
|  |     return undefined; | ||
|  |   } | ||
|  |   const rooms = onionResult?.result?.rooms as Array<any>; | ||
|  |   if (!rooms || !rooms.length) { | ||
| 
											5 years ago
										 |     window?.log?.warn('getAllRoomInfos failed invalid infos'); | ||
| 
											5 years ago
										 |     return []; | ||
|  |   } | ||
|  |   return _.compact( | ||
|  |     rooms.map(room => { | ||
|  |       // check that the room is correctly filled
 | ||
|  |       const { id, name, image_id: imageId } = room; | ||
|  |       if (!id || !name) { | ||
| 
											5 years ago
										 |         window?.log?.info('getAllRoomInfos: Got invalid room details, skipping'); | ||
| 
											5 years ago
										 |         return null; | ||
|  |       } | ||
|  | 
 | ||
|  |       return { id, name, imageId } as OpenGroupV2Info; | ||
|  |     }) | ||
|  |   ); | ||
|  | }; | ||
|  | 
 | ||
| 
											5 years ago
										 | export const parseModerators = (onionResult: any): Array<string> | undefined => { | ||
|  |   const moderatorsGot = onionResult?.result?.moderators as Array<string> | undefined; | ||
| 
											5 years ago
										 |   return moderatorsGot; | ||
|  | }; |