import { Inject } from '@angular/core' ; import { Client , Upload } from '../../services/api' ; import { UpdateMarkersService } from '../../common/services/update-markers.service' ; import { BehaviorSubject } from 'rxjs' ; export interface MembershipUpdate { show : boolean ; guid : string ; } /**
 * Service for groups.
 */ export class GroupsService { private base : string = 'api/v1/groups/' ; private infiniteInProgress : boolean = false ; private infiniteOffset : any ; group = new BehaviorSubject ( null ) ; $group = this . group . asObservable ( ) ; public membershipUpdate$ : BehaviorSubject < MembershipUpdate > = new BehaviorSubject ( { show : null , guid : null , } ) ; static _ ( client , upload , updateMarkers ) { return new GroupsService ( client , upload , updateMarkers ) ; } constructor ( @ Inject ( Client ) public clientService , @ Inject ( Upload ) public uploadService , @ Inject ( UpdateMarkersService ) private updateMarkers ) { } load ( guid ) { return this . clientService . get ( `${ this . base }group/${ guid }` ) . then ( ( response ) => { if ( response . group ) { this . group . next ( response . group ) ; return response . group ; } throw 'E_LOADING' ; } ) ; } save ( group ) { let endpoint = `${ this . base }group` ; if ( group . guid ) { endpoint += `/${ group . guid }` ; } this . group . next ( group ) ; return this . clientService . post ( endpoint , group ) . then ( ( response ) => { if ( response . guid ) { return response . guid ; } throw 'E_SAVING' ; } ) ; } upload ( group , files ) { let uploads = [ ] ; if ( files . banner ) { uploads . push ( this . uploadService . post ( `${ this . base }group/${ group . guid }/banner` , [ files . banner ] , { banner_position : group . banner_position , } ) ) ; } if ( files . avatar ) { uploads . push ( this . uploadService . post ( `${ this . base }group/${ group . guid }/avatar` , [ files . avatar , ] ) ) ; } return Promise . all ( uploads ) ; } deleteGroup ( group ) { return this . clientService . delete ( `${ this . base }group/${ group . guid }` ) . then ( ( response ) => { this . updateMembership ( false , group . guid ) ; return ! ! response . done ; } ) . catch ( e => { return false ; } ) ; } param { boolean } param { string } updateMembership ( show , guid ) { this . membershipUpdate$ . next ( { show : show , guid : guid , } ) ; } join ( group , target = null ) { let endpoint = `${ this . base }membership/${ group . guid }` ; if ( target ) { endpoint += `/${ target }` ; } return this . clientService . put ( endpoint ) . then ( ( response ) => { if ( response . done ) { this . updateMembership ( true , group . guid ) ; return true ; } throw response . error ? response . error : 'Internal error' ; } ) ; } leave ( group , target = null ) { let endpoint = `${ this . base }membership/${ group . guid }` ; if ( target ) { endpoint += `/${ target }` ; } return this . clientService . delete ( endpoint ) . then ( ( response ) => { if ( response . done ) { this . updateMembership ( false , group . guid ) ; return true ; } throw response . error ? response . error : 'Internal error' ; } ) ; } acceptRequest ( group , target ) { return this . join ( group , target ) ; } rejectRequest ( group , target ) { return this . leave ( group , target ) ; } kick ( group , user ) { return this . clientService . post ( `${ this . base }membership/${ group . guid }/kick` , { user } ) . then ( ( response ) => { return ! ! response . done ; } ) . catch ( e => { return false ; } ) ; } ban ( group , user ) { return this . clientService . post ( `${ this . base }membership/${ group . guid }/ban` , { user } ) . then ( ( response ) => { return ! ! response . done ; } ) . catch ( e => { return false ; } ) ; } cancelRequest ( group ) { return this . clientService . post ( `${ this . base }membership/${ group . guid }/cancel` ) . then ( ( response ) => { return ! ! response . done ; } ) . catch ( e => { return false ; } ) ; } muteNotifications ( group ) { this . updateMarkers . mute ( group . guid ) ; return this . clientService . post ( `${ this . base }notifications/${ group . guid }/mute` ) . then ( ( response ) => { return ! ! response [ 'is:muted' ] ; } ) . catch ( e => { return false ; } ) ; } unmuteNotifications ( group ) { this . updateMarkers . unmute ( group . guid ) ; return this . clientService . post ( `${ this . base }notifications/${ group . guid }/unmute` ) . then ( ( response ) => { return ! ! response [ 'is:muted' ] ; } ) . catch ( e => { return true ; } ) ; } grantOwnership ( group , user ) { return this . clientService . put ( `${ this . base }management/${ group . guid }/${ user }` ) . then ( ( response ) => { return ! ! response . done ; } ) . catch ( e => { return false ; } ) ; } revokeOwnership ( group , user ) { return this . clientService . delete ( `${ this . base }management/${ group . guid }/${ user }` ) . then ( ( response ) => { return ! response . done ; } ) . catch ( e => { return true ; } ) ; } grantModerator ( group , user ) { return this . clientService . put ( `${ this . base }management/${ group . guid }/${ user }/moderator` ) . then ( ( response ) => { return ! ! response . done ; } ) . catch ( e => { return false ; } ) ; } revokeModerator ( group , user ) { return this . clientService . delete ( `${ this . base }management/${ group . guid }/${ user }/moderator` ) . then ( ( response ) => { return ! response . done ; } ) . catch ( e => { return true ; } ) ; } canInvite ( user ) { return this . clientService . post ( `${ this . base }invitations/check` , { user } ) . then ( ( response ) => { if ( response . done ) { return user ; } throw 'E_NOT_DONE' ; } ) ; } invite ( group , invitee ) { return this . clientService . put ( `${ this . base }invitations/${ group . guid }` , { guid : invitee . guid } ) . then ( ( response ) => { if ( response . done ) { return true ; } throw response . error ? response . error : 'Internal error' ; } ) . catch ( e => { throw typeof e === 'string' ? e : 'Connectivity error' ; } ) ; } acceptInvitation ( group ) { return this . clientService . post ( `${ this . base }invitations/${ group . guid }/accept` ) . then ( ( response ) => { return ! ! response . done ; } ) . catch ( e => { return false ; } ) ; } declineInvitation ( group ) { return this . clientService . post ( `${ this . base }invitations/${ group . guid }/decline` ) . then ( ( response ) => { return ! ! response . done ; } ) . catch ( e => { return false ; } ) ; } getReviewCount ( guid ) { return this . clientService . get ( `${ this . base }review/${ guid }/count` ) . then ( ( response ) => { if ( typeof response [ 'adminqueue:count' ] !== 'undefined' ) { return parseInt ( response [ 'adminqueue:count' ] , 10 ) ; } throw 'E_COUNT' ; } ) ; } setExplicit ( guid , value ) { return this . clientService . post ( `api/v1/entities/explicit/${ guid }` , { value } ) . then ( ( response ) => { return ! ! response . done ; } ) ; } async toggleConversation ( guid , enabled ) { const response = await this . clientService . post ( `${ this . base }group/${ guid }` , { conversationDisabled : ! enabled } ) ; return ! ! response . done ; } /**
     * Returns the number of users belonging to a group
     */ countMembers ( guid ) { return this . clientService . get ( `api/v1/groups/membership/${ guid }` ) . then ( res => res [ 'members' ] . length ) ; } }