import { ISODateTimeString } from 'polar-shared/src/metadata/ISODateTimeStrings' ; import { GroupIDStr } from '../../Datastore' ; import { Firebase } from '../../../firebase/Firebase' ; import { DocRef } from 'polar-shared/src/groups/DocRef' ; import { Image } from './Images' ; import { Collections , DocumentChange } from './Collections' ; import { Preconditions } from 'polar-shared/src/Preconditions' ; import { Clause } from './Collections' ; import { Logger } from 'polar-shared/src/logger/Logger' ; import { EmailStr , ProfileIDStr } from "polar-firebase/src/firebase/om/Profiles" ; const log = Logger . create ( ) ; export class GroupMemberInvitations { public static readonly COLLECTION = 'group_member_invitation' ; public static async list ( ) { const user = await Firebase . currentUserAsync ( ) ; Preconditions . assertPresent ( user , 'user' ) ; return await Collections . list ( this . COLLECTION , [ [ 'to' , '==' , user ! . email ] ] ) ; } public static async listByGroupID ( groupID ) { const clauses = [ [ 'groupID' , '==' , groupID ] , ] ; return await Collections . list ( this . COLLECTION , clauses ) ; } public static async listByGroupIDAndProfileID ( groupID , profileID ) { const clauses = [ [ 'groupID' , '==' , groupID ] , [ 'from.profileID' , '==' , profileID ] ] ; return await Collections . list ( this . COLLECTION , clauses ) ; } public static async onSnapshot ( delegate ) { const user = await Firebase . currentUserAsync ( ) ; if ( ! user ) { log . warn ( "No user. No notifications will be delivered" ) ; return ; } return await Collections . onQuerySnapshot ( this . COLLECTION , [ [ 'to' , '==' , user ! . email ] ] , delegate ) ; } public static async purge ( ) { await Collections . deleteByID ( this . COLLECTION , ( ) => this . list ( ) ) ; } } export interface GroupMemberInvitationInit { readonly groupID : GroupIDStr ; readonly to : EmailStr ; readonly message : string ; /**
     * We have to keep the sender so that when we go to accept the doc we
     * actually know who it's from.
     */ readonly from : Sender ; /**
     * The actual DocID we're working with.
     */ readonly docs : ReadonlyArray < DocRef > ; } export interface GroupMemberInvitation extends GroupMemberInvitationInit { /**
     * The ID for this entry.
     */ readonly id : string ; readonly created : ISODateTimeString ; } export interface Sender { readonly profileID : ProfileIDStr ; readonly name : string ; readonly email ? : string ; readonly image : Image | null ; }