import type { Member as MemberI , MemberCompositeKey , } from "revolt-api/types/Servers" ; import type { RemoveMemberField , Route } from "../api/routes" ; import type { Attachment } from "revolt-api/types/Autumn" ; import { makeAutoObservable , runInAction , action , computed } from "mobx" ; import isEqual from "lodash.isequal" ; import { Nullable , toNullable } from "../util/null" ; import Collection from "./Collection" ; import { Client , FileArgs } from ".." ; export class Member { client : Client ; _id : MemberCompositeKey ; nickname : Nullable < string > = null ; avatar : Nullable < Attachment > = null ; roles : Nullable < string [ ] > = null ; /**
     * Associated user.
     */ get user ( ) { return this . client . users . get ( this . _id . user ) ; } /**
     * Associated server.
     */ get server ( ) { return this . client . servers . get ( this . _id . server ) ; } constructor ( client , data ) { this . client = client ; this . _id = data . _id ; this . nickname = toNullable ( data . nickname ) ; this . avatar = toNullable ( data . avatar ) ; this . roles = toNullable ( data . roles ) ; makeAutoObservable ( this , { _id : false , client : false , } ) ; } @ action update ( data , clear ? ) { const apply = ( key ) => { if ( typeof data [ key ] !== "undefined" && ! isEqual ( this [ key ] , data [ key ] ) ) { this [ key ] = data [ key ] ; } } ; switch ( clear ) { case "Nickname" : this . nickname = null ; break ; case "Avatar" : this . avatar = null ; break ; } apply ( "nickname" ) ; apply ( "avatar" ) ; apply ( "roles" ) ; } param data returns async edit ( data ) { return await this . client . req ( "PATCH" , `/servers/${ this . _id . server }/members/${ this . _id . user }` as "/servers/id/members/id" , data ) ; } param user_id async kick ( ) { return await this . client . req ( "DELETE" , `/servers/${ this . _id . server }/members/${ this . _id . user }` as "/servers/id/members/id" ) ; } @ computed generateAvatarURL ( ... args ) { return this . client . generateFileURL ( this . avatar ?? undefined , ... args ) ; } } export default class Members extends Collection < string , Member > { constructor ( client ) { super ( client ) ; this . createObj = this . createObj . bind ( this ) ; } static toKey ( id ) { return JSON . stringify ( id , Object . keys ( id ) . sort ( ) ) ; } hasKey ( id ) { return super . has ( Members . toKey ( id ) ) ; } getKey ( id ) { return super . get ( Members . toKey ( id ) ) ; } setKey ( id , member ) { return super . set ( Members . toKey ( id ) , member ) ; } deleteKey ( id ) { return super . delete ( Members . toKey ( id ) ) ; } @ action $get ( id , data ? ) { const member = this . getKey ( id ) ! ; if ( data ) member . update ( data ) ; return member ; } param data param emit returns createObj ( data , emit ? ) { if ( this . hasKey ( data . _id ) ) return this . $get ( data . _id , data ) ; const member = new Member ( this . client , data ) ; runInAction ( ( ) => { this . setKey ( data . _id , member ) ; } ) ; if ( emit === true ) this . client . emit ( "member/join" , member ) ; return member ; } }