|
@@ -4,8 +4,11 @@ const {connect} = require('mm')
|
|
module.exports = async (dbName='graphql-chat') => {
|
|
module.exports = async (dbName='graphql-chat') => {
|
|
const {Savable, slice} = await connect(dbName)
|
|
const {Savable, slice} = await connect(dbName)
|
|
|
|
|
|
- async function getModels({id, acl}, onMsgSave){
|
|
|
|
- const SlicedSavable = slice(id === 'anon' ? [id] : [...acl])
|
|
|
|
|
|
+ async function getModels({id, acl}, onMsgSave, onChatSave){
|
|
|
|
+ const thisUser = id !== 'anon' && await Savable.m.User.findOne({_id: ObjectID(id)})
|
|
|
|
+ const chatACL = thisUser && thisUser.chats.map(c => c._id.toString())
|
|
|
|
+
|
|
|
|
+ const SlicedSavable = slice(id === 'anon' ? [id] : [...acl, ...chatACL])
|
|
|
|
|
|
class User extends SlicedSavable {
|
|
class User extends SlicedSavable {
|
|
constructor(...params){
|
|
constructor(...params){
|
|
@@ -124,7 +127,14 @@ module.exports = async (dbName='graphql-chat') => {
|
|
this.___permissions.read.push(chatId)
|
|
this.___permissions.read.push(chatId)
|
|
this.___permissions.read = [...new Set(this.___permissions.read)]
|
|
this.___permissions.read = [...new Set(this.___permissions.read)]
|
|
|
|
|
|
|
|
+
|
|
let result = await super.save(...params)
|
|
let result = await super.save(...params)
|
|
|
|
+
|
|
|
|
+ await this.chat
|
|
|
|
+ this.chat.lastModified = this.createdAt
|
|
|
|
+ await this.chat.save()
|
|
|
|
+
|
|
|
|
+
|
|
onMsgSave(this)
|
|
onMsgSave(this)
|
|
return result
|
|
return result
|
|
}
|
|
}
|
|
@@ -143,15 +153,14 @@ module.exports = async (dbName='graphql-chat') => {
|
|
static get defaultPermissions(){
|
|
static get defaultPermissions(){
|
|
return {
|
|
return {
|
|
create: ['user', 'admin'],
|
|
create: ['user', 'admin'],
|
|
- read: ['user', 'owner', 'admin'],
|
|
|
|
|
|
+ read: ['owner', 'admin'],
|
|
write: ['owner','admin'],
|
|
write: ['owner','admin'],
|
|
delete: ['admin']
|
|
delete: ['admin']
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
static get guestRelations(){
|
|
static get guestRelations(){
|
|
- return ['replies']
|
|
|
|
- return ['forwardWith']
|
|
|
|
|
|
+ return ['replies', 'forwardWith']
|
|
}
|
|
}
|
|
}
|
|
}
|
|
SlicedSavable.addClass(Message)
|
|
SlicedSavable.addClass(Message)
|
|
@@ -164,9 +173,12 @@ module.exports = async (dbName='graphql-chat') => {
|
|
|
|
|
|
async save(...params){
|
|
async save(...params){
|
|
if (!this._id){
|
|
if (!this._id){
|
|
|
|
+ this.members.push(thisUser)
|
|
await super.save(...params)
|
|
await super.save(...params)
|
|
- this.___permissions.read.push(this._id)
|
|
|
|
|
|
+ this.lastModified = this.createdAt.toISOString()
|
|
|
|
+ this.___permissions.read.push(this._id.toString())
|
|
}
|
|
}
|
|
|
|
+ onChatSave(this)
|
|
return await super.save(...params)
|
|
return await super.save(...params)
|
|
}
|
|
}
|
|
|
|
|
|
@@ -193,7 +205,6 @@ module.exports = async (dbName='graphql-chat') => {
|
|
}
|
|
}
|
|
SlicedSavable.addClass(Chat)
|
|
SlicedSavable.addClass(Chat)
|
|
|
|
|
|
- const thisUser = id !== 'anon' && await Savable.m.User.findOne({_id: ObjectID(id)})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|