Skip to content

Commit

Permalink
The list of messages for a thread is now updated using websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaffter committed Aug 24, 2019
1 parent 7389ff4 commit 87d8cad
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class ThreadSidenavComponent implements OnDestroy {
filter(thread => !!thread),
take(1),
// TODO Emit socket event when Message is created or added
tap(thread => this.socketMessagesEventName = `thread:entity:${thread.entityId}:${thread._id}:message`),
tap(thread => this.socketMessagesEventName = `message:entity:${thread.entityId}:${thread._id}`),
switchMap(thread => this.messagingService.getMessagesByThread(thread))
)
.subscribe(messages => {
Expand Down
26 changes: 12 additions & 14 deletions server/api/message/message.events.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,25 @@ var events = {
};

// Register the event emitter to the model events
function registerEvents(Message) {
function registerEvents(Message, autoPopulatePost) {
for (var e in events) {
let event = events[e];
Message.post(e, emitEvent(event));
Message.post(e, emitEvent(event, autoPopulatePost));
}
}

function emitEvent(event) {
function emitEvent(event, autoPopulatePost) {
return function (doc) {
// console.log(`${event}:${doc._id}`);
MessageEvents.emit(`${event}:${doc._id}`, doc);
if (event === 'save') {
autoPopulatePost(doc)
.execPopulate()
.then(document => {
MessageEvents.emit(event, document);
});
return;
}
// MessageEvents.emit(`${event}:${doc._id}`, doc);
MessageEvents.emit(event, doc);
// For thread
// if (doc.thread) {
// console.log(`emitting message:thread:${doc.thread}:${event}`);
// MessageEvents.emit(`thread:${doc.thread}:${event}`, doc);
// }
// if (doc.thread) {
// MessageEvents.emit(`thread:${doc.thread}:${event}:${doc._id}`, doc);
// }
// MessageEvents.emit(event, doc);
};
}

Expand Down
41 changes: 4 additions & 37 deletions server/api/message/message.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
registerEvents
} from './message.events';
import User from '../user/user.model';
// import BaseMessage from './base-message.model';

var MessageSchema = new mongoose.Schema({
body: {
Expand Down Expand Up @@ -37,20 +36,6 @@ var MessageSchema = new mongoose.Schema({
}
});

/**
* Virtuals
*/

// Whether the current user has starred the message.
// MessageSchema
// .virtual('starred')
// .get(() => {
//
// });

// MessageSchema.set('toObject', { getters: true });
// MessageSchema.set('toJSON', { getters: true });

/**
* Middlewares
*/
Expand All @@ -60,36 +45,18 @@ const autoPopulatePre = function (next) {
.populate('createdBy', User.profileProperties)
.populate('updatedBy', User.profileProperties)
.populate('thread');
// .populate('tags');
next();
};
//
// const updateUpdatedAt = function (next) {
// this.updatedAt = Date.now();
// next();
// };

const autoPopulatePost = function (doc) {
return doc
.populate('createdBy', User.profileProperties)
.populate('updatedBy', User.profileProperties)
.populate('thread')
// .populate('tags')
// .execPopulate();

.populate('thread');
};

// MessageSchema.pre('save', function (next) {
// this.updatedAt = Date.now();
// next();
// });

MessageSchema
.pre('find', autoPopulatePre);
// .pre('save', updateUpdatedAt);

MessageSchema
.post('save', autoPopulatePost);
MessageSchema.pre('find', autoPopulatePre);
MessageSchema.post('save', autoPopulatePost);

registerEvents(MessageSchema);
registerEvents(MessageSchema, autoPopulatePost);
export default mongoose.model('Message', MessageSchema);
33 changes: 25 additions & 8 deletions server/api/message/message.socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,46 @@
* Broadcast updates to client when the model changes
*/

import {
hasAccessToEntity,
} from '../../auth/auth';
import MessageEvents from './message.events';
import Thread from '../thread/thread.model';
import config from '../../config/environment';

// Model events to emit
var events = ['save', 'remove'];

export function register(spark) {
// Bind model events to socket events
for (let event of events) {
var listener = createListener(`message:${event}`, spark);
var listener = createListener('message', event, spark, spark);

MessageEvents.on(event, listener);
spark.on('disconnect', removeListener(event, listener));
}
}


function createListener(event, spark) {
function createListener(modelName, event, spark) {
return function (doc) {
if (doc.thread) {
spark.emit(`thread:${doc.thread}:${event}`, doc);
} else {
spark.emit(event, doc);
}
Thread.findById(doc.thread)
.exec()
.then(thread => hasAccessToEntity(spark.userId, [
config.accessTypes.READ.value,
config.accessTypes.WRITE.value,
config.accessTypes.ADMIN.value
], thread.entityId, [
config.inviteStatusTypes.ACCEPTED.value
])
.then(hasAccess => {
if (hasAccess) {
spark.emit(`${modelName}:entity:${thread.entityId}:${thread._id}:${event}`, doc);
}
})
)
.catch(err => {
console.error(err);
});
};
}

Expand Down
27 changes: 0 additions & 27 deletions server/api/thread/thread.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,6 @@ var ThreadSchema = new mongoose.Schema({
}
});

/**
* Virtuals
*/

// Whether the current user has starred the message.
// ThreadSchema
// .virtual('starred')
// .get(() => {
//
// });

// ThreadSchema.set('toObject', { getters: true });
// ThreadSchema.set('toJSON', { getters: true });

/**
* Middlewares
*/
Expand All @@ -58,30 +44,17 @@ const autoPopulatePre = function (next) {
this
.populate('createdBy', User.profileProperties)
.populate('updatedBy', User.profileProperties)
// .populate('tags');
next();
};
//
// const updateUpdatedAt = function (next) {
// this.updatedAt = Date.now();
// next();
// };

const autoPopulatePost = function (doc) {
return doc
.populate('createdBy', User.profileProperties)
.populate('updatedBy', User.profileProperties)
.execPopulate();
};

// ThreadSchema.pre('save', function (next) {
// this.updatedAt = Date.now();
// next();
// });

ThreadSchema
.pre('find', autoPopulatePre);
// .pre('save', updateUpdatedAt);

ThreadSchema
.post('save', autoPopulatePost);
Expand Down
29 changes: 14 additions & 15 deletions server/api/thread/thread.socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
* Broadcast updates to client when the model changes
*/

import {
hasAccessToEntity,
} from '../../auth/auth';
import config from '../../config/environment';
import { hasAccessToEntity } from '../../auth/auth';
import ThreadEvents from './thread.events';
import config from '../../config/environment';

// Model events to emit
var events = ['save', 'remove'];
Expand All @@ -21,21 +19,22 @@ export function register(spark) {
}
}

function createListener(namespace, event, spark) {
function createListener(modelName, event, spark) {
return function (doc) {
// console.log(`Firing ${namespace}:entity:${doc.entityId}:${event}`);
hasAccessToEntity(spark.userId, [
config.accessTypes.READ.value,
config.accessTypes.WRITE.value,
config.accessTypes.ADMIN.value
], doc.entityId, [
// console.log(`Firing ${modelName}:entity:${doc.entityId}:${event}`);
hasAccessToEntity(
spark.userId,
[config.accessTypes.READ.value, config.accessTypes.WRITE.value, config.accessTypes.ADMIN.value],
doc.entityId,
[
// TODO Rename inviteStatusTypes (do not use invite)
config.inviteStatusTypes.ACCEPTED.value
])
config.inviteStatusTypes.ACCEPTED.value,
]
)
.then(hasAccess => {
if (hasAccess) {
// spark.emit(`${namespace}:${event}`, doc);
spark.emit(`${namespace}:entity:${doc.entityId}:${event}`, doc);
// spark.emit(`${modelName}:${event}`, doc);
spark.emit(`${modelName}:entity:${doc.entityId}:${event}`, doc);
}
})
.catch(err => {
Expand Down

0 comments on commit 87d8cad

Please sign in to comment.