Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
pepebecker committed Feb 3, 2020
1 parent ee35ff5 commit 96925fa
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { ErrorMessage } from '../error-messages'
import { Event } from '../events'
import { EventType } from '../events/event-type'
import { ListData } from '../events/list-data'
import { Filter } from '../filter/filter'
import { MultiQueueSubject } from '../firebase/rx/multi-queue-subject'
import { FirebaseService } from '../firebase/service/firebase-service'
import { Keys } from '../firebase/service/keys'
import { MuteService } from '../firebase/service/mute-serve'
import { Path } from '../firebase/service/path'
import { Paths } from '../firebase/service/paths'
import { FireStreamStore } from '../firestream-store'
Expand Down Expand Up @@ -399,5 +399,20 @@ export class Chat extends AbstractChat implements IChat {
return new Chat(change.getId())
}

mute(until?: Date): Promise<void> {
return MuteService.mute(this.getId(), until)
}

unmute(): Promise<void> {
return MuteService.unmute(this.getId())
}

mutedUntil(): Date | undefined {
return MuteService.mutedUntil(this.getId())
}

isMuted(): boolean {
return MuteService.isMuted(this.getId())
}

}
9 changes: 9 additions & 0 deletions src/firebase/firestore/firestore-core-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Event } from '../../events'
import { EventType } from '../../events/event-type'
import { ListData } from '../../events/list-data'
import { Consumer } from '../../interfaces/consumer'
import { IJsonObject } from '../../interfaces/json'
import { ISendable } from '../../interfaces/sendable'
import { Sendable } from '../../message/sendable'
import { RxUtils } from '../../utils/rx-utils'
Expand Down Expand Up @@ -208,4 +209,12 @@ export class FirestoreCoreHandler extends FirebaseCoreHandler {
}
}

mute(path: Path, data: IJsonObject): Promise<void> {
return new RxFirestore().set(Ref.document(path), data)
}

unmute(path: Path): Promise<void> {
return new RxFirestore().delete(Ref.document(path))
}

}
8 changes: 8 additions & 0 deletions src/firebase/realtime/realtime-core-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,12 @@ export class RealtimeCoreHandler extends FirebaseCoreHandler {
return users.map(user => provider.data(user))
}

mute(path: Path, data: IJsonObject): Promise<void> {
return new RxRealtime().set(Ref.get(path), data)
}

unmute(path: Path): Promise<void> {
return new RxRealtime().delete(Ref.get(path))
}

}
4 changes: 4 additions & 0 deletions src/firebase/service/firebase-core-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DataProvider, User } from '../../chat/user'
import { Event } from '../../events'
import { ListData } from '../../events/list-data'
import { Consumer } from '../../interfaces/consumer'
import { IJsonObject } from '../../interfaces/json'
import { ISendable } from '../../interfaces/sendable'
import { Path } from './path'

Expand Down Expand Up @@ -92,4 +93,7 @@ export abstract class FirebaseCoreHandler {
*/
abstract messagesOn(messagesPath: Path, newerThan?: Date, limit?: number): Observable<Event<ISendable>>

abstract mute(path: Path, data: IJsonObject): Promise<void>
abstract unmute(path: Path): Promise<void>

}
1 change: 1 addition & 0 deletions src/firebase/service/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ export class Keys {
static Blocked = 'blocked'
static Chats = 'chats'
static Meta = 'meta'
static Muted = 'muted'

}
61 changes: 61 additions & 0 deletions src/firebase/service/mute-serve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { User } from '../../chat'
import { FirebaseService } from './firebase-service'
import { Keys } from './keys'
import { Paths } from './paths'

export class MuteService {

private static instance: MuteService

protected muted: { [key: string]: Date } = {}

static get shared() {
if (!this.instance) {
this.instance = new MuteService()
}
return this.instance
}

static add(id: string, until: Date) {
this.shared.muted[id] = until
}

static remove(id: string) {
delete this.shared.muted[id]
}

static mute(user: User | string, until?: Date): Promise<void> {
if (typeof user === 'string') {
return FirebaseService.core.mute(Paths.userMutedPath().child(user), {
[Keys.Date]: until != null ? until.getTime() : Number.MAX_SAFE_INTEGER
})
} else {
return this.mute(user.getId(), until)
}
}

static unmute(user: User | string): Promise<void> {
if (typeof user === 'string') {
return FirebaseService.core.unmute(Paths.userMutedPath().child(user))
} else {
return this.unmute(user.getId())
}
}

static mutedUntil(user: User | string): Date | undefined {
if (typeof user === 'string') {
return this.shared.muted[user]
} else {
return this.mutedUntil(user.getId())
}
}

static isMuted(user: User | string): boolean {
if (typeof user === 'string') {
return this.mutedUntil(user) != null
} else {
return this.isMuted(user.getId())
}
}

}
4 changes: 4 additions & 0 deletions src/firebase/service/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export class Paths extends Keys {
return this.userPath(this.currentUserId()).child(Keys.Chats)
}

static userMutedPath(): Path {
return this.userPath(this.currentUserId()).child(Keys.Muted)
}

static userGroupChatPath(chatId: string): Path {
return this.userChatsPath().child(chatId)
}
Expand Down
30 changes: 30 additions & 0 deletions src/firestream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { RealtimeChatHandler } from './firebase/realtime/realtime-chat-handler'
import { RealtimeCoreHandler } from './firebase/realtime/realtime-core-handler'
import { MultiQueueSubject } from './firebase/rx/multi-queue-subject'
import { FirebaseService } from './firebase/service/firebase-service'
import { Keys } from './firebase/service/keys'
import { MuteService } from './firebase/service/mute-serve'
import { Path } from './firebase/service/path'
import { Paths } from './firebase/service/paths'
import { FireStreamStore } from './firestream-store'
Expand Down Expand Up @@ -203,6 +205,18 @@ export class FireStream extends AbstractChat implements IFireStream {
}
}))

this.sm.add(this.listChangeOn(Paths.userMutedPath()).subscribe(listDataEvent => {
const id = listDataEvent.get().getId()
if (listDataEvent.typeIs(EventType.Removed)) {
MuteService.remove(id)
} else {
const date = listDataEvent.get().getData()[Keys.Date]
if (date instanceof Date) {
MuteService.add(id, date)
}
}
}))

// Connect to the message events AFTER we have added our events listeners
await super.connect()

Expand Down Expand Up @@ -414,4 +428,20 @@ export class FireStream extends AbstractChat implements IFireStream {
return FirebaseService.shared
}

mute(user: User | string, until?: Date): Promise<void> {
return MuteService.mute(user, until)
}

unmute(user: User | string): Promise<void> {
return MuteService.unmute(user)
}

mutedUntil(user: User | string): Date | undefined {
return MuteService.mutedUntil(user)
}

isMuted(user: User | string): boolean {
return MuteService.isMuted(user)
}

}
31 changes: 31 additions & 0 deletions src/interfaces/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,35 @@ export interface IChat extends IAbstractChat {
markRead(sendable: ISendable): Promise<void>
markRead(sendableId: string): Promise<void>

/**
* Mute notifications for a user
* @return completion
*/
mute(): Promise<void>

/**
* Mute notifications for a user
* @param until mute the thread until this date
* @return completion
*/
mute(until: Date): Promise<void>

/**
* Unmute notifications for a user
* @return completion
*/
unmute(): Promise<void>

/**
* Is a user muted?
* @return true / false
*/
isMuted(): boolean

/**
* Thread is muted until this date
* @return date or null if not muted
*/
mutedUntil(): Date | undefined

}
36 changes: 36 additions & 0 deletions src/interfaces/firestream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,40 @@ export interface IFireStream extends IAbstractChat {
markReceived(fromUserId: string, sendableId: string): Promise<void>
markRead(fromUserId: string, sendableId: string): Promise<void>

/**
* Mute notifications for a user
* @param user to mute
* @return completion
*/
mute(user: User): Promise<void>

/**
* Mute notifications until a future date
* @param user to mute
* @param until to mute until
* @return completion
*/
mute(user: User, until: Date): Promise<void>

/**
* Unmute notifications for a user
* @param user to unmute
* @return completion
*/
unmute(user: User): Promise<void>

/**
* Use this method to find out if the user is muted and until when
* @param user to check
* @return date or null if not muted
*/
mutedUntil(user: User): Date | undefined

/**
* Is a user muted?
* @param user to mute
* @return true / false
*/
isMuted(user: User): boolean

}

0 comments on commit 96925fa

Please sign in to comment.