'use strict';
import { _ } from 'meteor/underscore';
import { check } from 'meteor/check';
import { Meteor } from 'meteor/meteor';
import { dbInstantMessage } from '../../db/dbInstantMessage';

Meteor.methods({
  instantMessageChat(message) {
    check(this.userId, String);
    check(message, String);
    const userId = this.userId;
    const username = Meteor.users.findOne(userId).username;
    dbInstantMessage.insert({
      type: '聊天發言',
      createdAt: new Date(),
      onlyForUsers: [],
      source: username,
      message: message
    });
  }
});

Meteor.publish('instantMessage', function() {
  let username = false;
  if (this.userId) {
    const user = Meteor.users.findOne(this.userId, {
      fields: {
        username: 1
      }
    });
    username = user.username;
  }
  const observer = dbInstantMessage
    .find({
      createdAt: {
        $gte: new Date( Date.now() - 60000 )
      }
    })
    .observeChanges({
      added: (id, fields) => {
        if (username && fields.onlyForUsers.length > 0 && _.contains(fields.onlyForUsers, username) === false) {
          return false;
        }
        this.added('instantMessage', id, fields);
      }
    });
  this.ready();
  this.onStop(() => {
    observer.stop();
  });
});

@@ -981,20 +981,8 @@
 if (
-username && 
 fiel
@@ -1010,16 +1010,31 @@
 h %3E 0 &&
+ ! (username &&
  _.conta
@@ -1071,18 +1071,9 @@
 ame)
- === false
+)
 ) %7B%0A

修正未登入者可以看到所有「只針對指定使用者的訊息」的bug。