1 | 'use strict'
|
2 |
|
3 | const ChatServiceError = require('./ChatServiceError')
|
4 | const Promise = require('bluebird')
|
5 | const { mixin } = require('es6-mixin')
|
6 | const { run } = require('./utils')
|
7 |
|
8 | class DirectMessagingPermissions {
|
9 |
|
10 | constructor (userName, directMessagingState) {
|
11 | this.userName = userName
|
12 | this.directMessagingState = directMessagingState
|
13 | }
|
14 |
|
15 | checkList (author, listName) {
|
16 | return this.directMessagingState.checkList(listName)
|
17 | }
|
18 |
|
19 | checkListValues (author, listName, values) {
|
20 | return this.checkList(author, listName).then(() => {
|
21 | for (let name of values) {
|
22 | if (name !== this.userName) { continue }
|
23 | return Promise.reject(new ChatServiceError('notAllowed'))
|
24 | }
|
25 | return Promise.resolve()
|
26 | })
|
27 | }
|
28 |
|
29 | checkAcess (userName, bypassPermissions) {
|
30 | if (userName === this.userName) {
|
31 | return Promise.reject(new ChatServiceError('notAllowed'))
|
32 | }
|
33 | if (bypassPermissions) { return Promise.resolve() }
|
34 | return run(this, function * () {
|
35 | let blacklisted = yield this.directMessagingState.hasInList(
|
36 | 'blacklist', userName)
|
37 | if (blacklisted) {
|
38 | return Promise.reject(new ChatServiceError('notAllowed'))
|
39 | }
|
40 | let whitelistOnly = yield this.directMessagingState.whitelistOnlyGet()
|
41 | if (!whitelistOnly) { return Promise.resolve() }
|
42 | let whitelisted = yield this.directMessagingState.hasInList(
|
43 | 'whitelist', userName)
|
44 | if (whitelisted) { return Promise.resolve() }
|
45 | return Promise.reject(new ChatServiceError('notAllowed'))
|
46 | })
|
47 | }
|
48 | }
|
49 |
|
50 |
|
51 |
|
52 | class DirectMessaging {
|
53 |
|
54 | constructor (server, userName) {
|
55 | this.server = server
|
56 | this.userName = userName
|
57 | this.listSizeLimit = this.server.directListSizeLimit
|
58 | let State = this.server.state.DirectMessagingState
|
59 | this.directMessagingState = new State(this.server, this.userName)
|
60 | mixin(this, DirectMessagingPermissions,
|
61 | this.userName, this.directMessagingState)
|
62 | }
|
63 |
|
64 | initState (state) {
|
65 | return this.directMessagingState.initState(state)
|
66 | }
|
67 |
|
68 | removeState () {
|
69 | return this.directMessagingState.removeState()
|
70 | }
|
71 |
|
72 | message (author, msg, bypassPermissions) {
|
73 | return this.checkAcess(author, bypassPermissions)
|
74 | }
|
75 |
|
76 | getList (author, listName) {
|
77 | return this.checkList(author, listName)
|
78 | .then(() => this.directMessagingState.getList(listName))
|
79 | }
|
80 |
|
81 | addToList (author, listName, values) {
|
82 | return this.checkListValues(author, listName, values)
|
83 | .then(() => this.directMessagingState.addToList(
|
84 | listName, values, this.listSizeLimit))
|
85 | }
|
86 |
|
87 | removeFromList (author, listName, values) {
|
88 | return this.checkListValues(author, listName, values)
|
89 | .then(() => this.directMessagingState.removeFromList(listName, values))
|
90 | }
|
91 |
|
92 | hasInList (listName, item) {
|
93 | return this.directMessagingState.hasInList(listName, item)
|
94 | }
|
95 |
|
96 | getMode (author) {
|
97 | return this.directMessagingState.whitelistOnlyGet()
|
98 | }
|
99 |
|
100 | changeMode (author, mode) {
|
101 | return this.directMessagingState.whitelistOnlySet(mode)
|
102 | }
|
103 | }
|
104 |
|
105 | module.exports = DirectMessaging
|