baas.io Javascript SDK v0.9.0 src/group/group.js

Group

method
Baas.Group()

    Description

    A class to model a Baas group.
    Set the path in the options object.

    @constructor
    @param {object} options {client:client, data: {'key': 'value'}, path:'path'}

    Source

    Baas.Group = function(options) {
        this._path = options.path;
        this._list = [];
        this._client = options.client;
        this._data = options.data || {};
        this._data.type = "groups";
      }

      Description

      Inherit from Baas.Entity.
      Note: This only accounts for data on the group object itself.
      You need to use add and remove to manipulate group membership.

      Source

      Baas.Group.prototype = new Baas.Entity();

      fetch

      method
      Baas.Group.prototype.fetch()

        Description

        Fetches current group data, and members.

        @method fetch
        @public
        @param {function} callback
        @returns {function} callback(err, data)

        Source

        Baas.Group.prototype.fetch = function(callback) {
            var self = this;
            var groupEndpoint = 'groups/'+this._path;
            var memberEndpoint = 'groups/'+this._path+'/users';
        
            var groupOptions = {
              method:'GET',
              endpoint:groupEndpoint
            }
        
            var memberOptions = {
              method:'GET',
              endpoint:memberEndpoint
            }
        
            this._client.request(groupOptions, function(err, data){
              if(err) {
                if(self._client.logging) {
                  console.log('error getting group');
                }
                if(typeof(callback) === 'function') {
                  callback(err, data);
                }
              } else {
                if(data.entities) {
                  var groupData = data.entities[0];
                  self._data = groupData || {};
                  self._client.request(memberOptions, function(err, data) {
                    if(err && self._client.logging) {
                      console.log('error getting group users');
                    } else {
                      if(data.entities) {
                        var count = data.entities.length;
                        self._list = [];
                        for (var i = 0; i < count; i++) {
                          var uuid = data.entities[i].uuid;
                          if(uuid) {
                            var entityData = data.entities[i] || {};
                            var entityOptions = {
                              type: entityData.type,
                              client: self._client,
                              uuid:uuid,
                              data:entityData
                            };
                            var entity = new Baas.Entity(entityOptions);
                            self._list.push(entity);
                          }
        
                        }
                      }
                    }
                    if(typeof(callback) === 'function') {
                      callback(err, data, self._list);
                    }
                  });
                }
              }
            });
          }

        members

        method
        Baas.Group.prototype.members()

          Description

          Retrieves the members of a group.

          @method members
          @public
          @param {function} callback
          @return {function} callback(err, data);

          Source

          Baas.Group.prototype.members = function(callback) {
              if(typeof(callback) === 'function') {
                callback(null, this._list);
              }
            }

          add

          method
          Baas.Group.prototype.add()

            Description

            Adds a user to the group, and refreshes the group object.

            Options object: {user: user_entity}

            @method add
            @public
            @params {object} options
            @param {function} callback
            @return {function} callback(err, data)

            Source

            Baas.Group.prototype.add = function(options, callback) {
                var self = this;
                var options = {
                  method:"POST",
                  endpoint:"groups/"+this._path+"/users/"+options.user.get('username')
                }
            
                this._client.request(options, function(error, data){
                  if(error) {
                    if(typeof(callback) === 'function') {
                      callback(error, data, data.entities);
                    }
                  } else {
                    self.fetch(callback);
                  }
                });
              }

            remove

            method
            Baas.Group.prototype.remove()

              Description

              Removes a user from a group, and refreshes the group object.

              Options object: {user: user_entity}

              @method remove
              @public
              @params {object} options
              @param {function} callback
              @return {function} callback(err, data)

              Source

              Baas.Group.prototype.remove = function(options, callback) {
                  var self = this;
              
                  var options = {
                    method:"DELETE",
                    endpoint:"groups/"+this._path+"/users/"+options.user.get('username')
                  }
              
                  this._client.request(options, function(error, data){
                    if(error) {
                      if(typeof(callback) === 'function') {
                        callback(error, data);
                      }
                    } else {
                      self.fetch(callback);
                    }
                  });
                }

              feed

              method
              Baas.Group.prototype.feed()
              • @public:
              • @method: feed
              • @param: {function}callback
              • @returns: {callback} callback(err, data, activities)

              Description

              Gets feed for a group.

              Source

              Baas.Group.prototype.feed = function(callback) {
                  var self = this;
              
                  var endpoint = "groups/"+this._path+"/feed";
              
                  var options = {
                    method:"GET",
                    endpoint:endpoint
                  }
              
                  this._client.request(options, function(err, data){
                    if (err && self.logging) {
                      console.log('error trying to log user in');
                    }
                    if(typeof(callback) === 'function') {
                        callback(err, data, data.entities);
                    }
                  });
                }

              createGroupActivity

              method
              Baas.Group.prototype.createGroupActivity()
              • @public:
              • @method: createGroupActivity
              • @params: {object} options
              • @param: {function}callback
              • @returns: {callback} callback(err, entity)

              Description

              Creates activity and posts to group feed.

              options object: {user: user_entity, content: "activity content"}

              Source

              Baas.Group.prototype.createGroupActivity = function(options, callback){
                  var user = options.user;
                  var options = {
                    actor: {
                      "displayName":user.get("username"),
                      "uuid":user.get("uuid"),
                      "username":user.get("username"),
                      "email":user.get("email"),
                      "picture":user.get("picture"),
                      "image": {
                        "duration":0,
                        "height":80,
                        "url":user.get("picture"),
                        "width":80
                       },
                    },
                    "verb":"post",
                    "content":options.content };
              
                    options.type = 'groups/'+this._path+'/activities';
                    var options = {
                      client:this._client,
                      data:options
                    }
              
                    var entity = new Baas.Entity(options);
                    entity.save(function(err) {
                      if (typeof(callback) === 'function') {
                        callback(err, entity);
                      }
                    });
                }