-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
rxCommands.ts
56 lines (53 loc) · 1.61 KB
/
rxCommands.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { BehaviorSubject, empty, ObservableInput } from 'rxjs';
import { rxUser } from './rxUser';
import { filter, switchMap, map, tap } from 'rxjs/operators';
import { firestore } from './firebase';
import { collectionData } from 'rxfire/firestore';
import { ICommand } from '..';
import { Command } from './db/db';
/**
* @description rxCommands is the behavior subject for getting the layout of all commands
* @note this pulls directly from firebase and not local database as it doesn't update very much therefore, should not need to be hindered by using local db like users
*/
export const rxCommands = rxUser.pipe(
filter(x => !!x),
switchMap(
(authUser): ObservableInput<ICommand[]> => {
if (!authUser) {
return empty();
}
const ref = firestore
.collection('users')
.doc(authUser.uid)
.collection('commands');
return collectionData(ref);
}
),
map((commands: ICommand[]) =>
commands.map(
(command): Command => {
let newCommand = new Command(
command.name,
command.name,
command.permissions || [],
command.reply,
command.cost || 0,
command.enabled
);
if (newCommand.name.includes(' ')) {
newCommand.delete();
newCommand = new Command(
command.name.replace(' ', '-'),
command.name.replace(' ', '-'),
command.permissions || [],
command.reply,
command.cost || 0,
command.enabled
);
newCommand.save();
}
return newCommand;
}
)
)
);