Code coverage report for api/activity/Activities.js

Statements: 79.65% (137 / 172)      Branches: 73.97% (108 / 146)      Functions: 84% (21 / 25)      Lines: 79.65% (137 / 172)      Ignored: none     

All files » api/activity/ » Activities.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 3911 1 1 1 1 1 1 1 1   1 4 4   4   1                         1                         1 81 81   81 31       81 81     1                                       2                 7                 115 55     60 44 44 44 2 2   42 38 38       20 15 15 15 4 4 11 1 1                     143   143 72     143     106   106 106           4 2                           1020     51     51       51         51 51 51 37               35 35 29   6                                       160 9     151 22     129 42     87         4     4       4               4       4       4 1   4 4 4 4     4 4 3       3 3   3                 4         4       4                 4 4 4 4                     4 3 1   3 3 3 3 3   3     1 1 1 1       1               2     2   2 2 1 1 1 1 1   1     1 1 1 1 1   1           1           36   36 36 32 32   4     36 36         1  
var Confidential = require("./../confidentiality_encryption/Confidential").Confidential;
var DataList = require("./../data_list/DataList").DataList;
var Activity = require("./Activity").Activity;
var Connection = require("./../corpus/Connection").Connection;
var Comments = require("./../comment/Comments").Comments;
var DocumentCollection = require("./../datum/DocumentCollection").DocumentCollection;
var Database = require("./../corpus/Database").Database;
var ContextualizableObject = require("./../locales/ContextualizableObject").ContextualizableObject;
var Q = require("q");
 
var ActivityCollection = function ActivityCollection() {
  Eif (!this._fieldDBtype) {
    this._fieldDBtype = "ActivityCollection";
  }
  DocumentCollection.apply(this, arguments);
};
ActivityCollection.prototype = Object.create(DocumentCollection.prototype, /** @lends ActivityCollection.prototype */ {
  constructor: {
    value: ActivityCollection
  },
  primaryKey: {
    value: "whenWhatWho"
  },
  INTERNAL_MODELS: {
    value: {
      item: Activity
    }
  }
});
exports.ActivityCollection = ActivityCollection;
 
 
/**
 * @class
 
 * @name  Activities
 * @description The Activities is a minimal customization of the DataList
 * to add an internal model of Activity.
 *
 * @extends DataList
 * @constructs
 */
var Activities = function Activities(options) {
  Eif (!this._fieldDBtype) {
    this._fieldDBtype = "Activities";
  }
  if (options && options.port) {
    options = {
      connection: options
    };
  }
  this.debug("Constructing Activities ", options);
  DataList.apply(this, arguments);
};
 
Activities.prototype = Object.create(DataList.prototype, /** @lends Activities.prototype */ {
  constructor: {
    value: Activities
  },
 
  INTERNAL_MODELS: {
    value: {
      connection: Connection,
      comments: Comments,
      docs: ActivityCollection,
      title: ContextualizableObject,
      description: ContextualizableObject,
      database: Database,
      confidential: Confidential,
      item: Activity
    }
  },
 
  api: {
    get: function() {
      return "activities";
    },
    set: function() {
      // no op
    }
  },
 
  primaryKey: {
    get: function() {
      return "whenWhatWho";
    },
    set: function() {
      // cant change the primary key of the activity feed.
    }
  },
 
  teamOrPersonal: {
    get: function() {
      if (this._teamOrPersonal) {
        return this._teamOrPersonal;
      }
 
      if (this.parent && this.parent.fieldDBtype) {
        var type = this.parent.fieldDBtype || "";
        type = type.toLowerCase();
        if (type.indexOf("user") > -1) {
          this._teamOrPersonal = "personal";
          return this._teamOrPersonal;
        }
        if (type.indexOf("corpus") > -1) {
          this._teamOrPersonal = "team";
          return this._teamOrPersonal;
        }
      }
 
      if (this._connection && typeof this._connection.guessDbType === "function") {
        var guessedType = this._connection.guessDbType(this._connection.dbname);
        this.debug(" acitivyt feed guessed the db type " + guessedType);
        if (guessedType === "user_activity_feed") {
          this._teamOrPersonal = "personal";
          return this._teamOrPersonal;
        } else if (guessedType === "corpus_activity_feed") {
          this._teamOrPersonal = "team";
          return this._teamOrPersonal;
        }
      }
    },
    set: function() {
      // no op
    }
  },
 
  connection: {
    get: function() {
      this.debug("getting connection");
 
      if (this.parent && this.parent.connection && typeof this.parent.connection.toJSON === "function") {
        this.connection = this.parent.connection.toJSON();
      }
 
      return this._connection;
    },
    set: function(value) {
      this.ensureSetViaAppropriateType("connection", value);
 
      this.title = this._connection.title || "Activity feed";
      this.description = this._connection.description || "";
    }
  },
 
  url: {
    get: function() {
      if (this.connection && this.connection.corpusUrl) {
        return this.connection.corpusUrl;
      }
    },
    set: function(value) {
      if (this.connection) {
        this.connection.corpusUrl = value;
      } else {
        this.warn("not setting hte url", value);
      }
    }
  },
 
  parent: {
    get: function() {
      return this._parent;
    },
    set: function(value) {
      Iif (value === this._parent) {
        return;
      }
      Iif (this.dbname && this.teamOrPersonal === "personal" && this.dbname !== value.username + "-activity_feed") {
        this.bug("Cannot change the " + this.dbname + " activity feed to " + value.username + "-activity_feed");
        return;
      }
      Iif (this.dbname && this.teamOrPersonal === "team" && this.dbname !== value.dbname + "-activity_feed") {
        this.bug("Cannot change the " + this.dbname + " activity feed to " + value.dbname + "-activity_feed");
        return;
      }
 
      Eif (value) {
        this._parent = value;
        if (value && value.set && value.get && this.teamOrPersonal === "team") {
          this._database = value;
        }
      }
    }
  },
 
  confidential: {
    get: function() {
      this.debug("getting confidential");
      if (this.parent && this.parent.confidential) {
        return this.parent.confidential;
      } else {
        this.warn("Activites can only be viewed in masked form in this app. The parent of the activity feed is not defined.");
      }
    },
    set: function() {
      // cant set confidential on activity feeds, it must come from the parent. This means activities cannot be demasked unless in the context of the orignal corpus or user
    }
  },
 
  database: {
    get: function() {
      this.debug("getting database");
      return this._database;
    },
    set: function(value) {
      this.ensureSetViaAppropriateType("database", value);
    }
  },
 
  dbname: {
    get: function() {
      if (this.parent && this.teamOrPersonal === "personal") {
        return this.parent.username + "-activity_feed";
      }
 
      if (this.parent && this.teamOrPersonal === "team") {
        return this.parent.dbname + "-activity_feed";
      }
 
      if (this._connection && this._connection.dbname) {
        return this._connection.dbname;
      }
 
      Iif (this._dbname) {
        return this._dbname;
      }
    },
    set: function(value) {
      Iif (value === this._dbname) {
        return;
      }
      Iif (this.parent && this.teamOrPersonal === "personal" && value !== this.parent.dbname + "-activity_feed") {
        this.bug("Cannot change the " + this.parent.dbname + "-activity_feed" + " to " + value);
        return;
      }
      Iif (value) {
        this._dbname = value;
      }
    }
  },
 
  add: {
    value: function(activity, optionalUserWhoSaved) {
      Iif (activity.url && activity.url !== this.url) {
        this.bug("Cannot add " + activity.url + " activity to " + this.url + " server, please report this. ");
        return;
      }
      Iif (activity.dbname && activity.dbname !== this.dbname) {
        this.bug("Cannot add " + activity.dbname + " activity to " + this.dbname + " activity feed, please report this. ");
        return;
      }
      if (!activity.parent && this.parent) {
        activity.parent = this.parent;
      }
      activity.dbname = this.dbname;
      activity.fieldDBtype = "Activity";
      this.debug("adding activity", activity);
      activity.timestamp = Date.now();
 
 
      Eif (!activity.user) {
        if (!optionalUserWhoSaved) {
          optionalUserWhoSaved = {
            name: "",
            username: "unknown"
          };
          try {
            Iif (this.corpus && this.corpus.connectionInfo && this.corpus.connectionInfo.userCtx) {
              optionalUserWhoSaved.username = this.corpus.connectionInfo.userCtx.name;
            } else Iif (this.application && this.application.user && this.application.user.username) {
              optionalUserWhoSaved.username = optionalUserWhoSaved.username || this.application.user.username;
              optionalUserWhoSaved.gravatar = optionalUserWhoSaved.gravatar || this.application.user.gravatar;
            }
          } catch (e) {
            this.warn("Can't get the corpus connection info to guess who saved this.", e);
          }
        }
        // optionalUserWhoSaved._name = optionalUserWhoSaved.name || optionalUserWhoSaved.username || optionalUserWhoSaved.browserVersion;
        Iif (typeof optionalUserWhoSaved.toJSON === "function") {
          var asJson = optionalUserWhoSaved.toJSON();
          asJson.name = optionalUserWhoSaved.name;
          optionalUserWhoSaved = asJson;
        } else {
          optionalUserWhoSaved.name = optionalUserWhoSaved.name;
        }
        // optionalUserWhoSaved.browser = browser;
 
        activity.user = {
          username: optionalUserWhoSaved.username,
          name: optionalUserWhoSaved.name,
          lastname: optionalUserWhoSaved.lastname,
          firstname: optionalUserWhoSaved.firstname,
          gravatar: optionalUserWhoSaved.gravatar
        };
      }
 
      activity.parent = this;
      var addedActivity;
      try {
        addedActivity = DataList.prototype.add.apply(this, [activity]);
      } catch (e) {
        this.warn("Error adding this activity", e);
        var message = e ? e.message : " Error adding this activity.";
        if (e) {
          console.error(e.stack);
        }
        this.warn(message);
        activity.errorMessage = message;
      }
 
      if (addedActivity) {
        if (this._database) {
          addedActivity.corpus = this._database;
        }
        Eif (!addedActivity.rev) {
          addedActivity.debug("This activity " + addedActivity.directobject + " has no evidence of having been saved before, makeing its fossil empty to trigger save.");
          addedActivity.unsaved = true;
          addedActivity.fossil = {};
          addedActivity.debug("This activity", addedActivity);
        }
        return addedActivity;
      }
 
      Eif (!addedActivity) {
        this.incompleteActivitesStockPile = this.incompleteActivitesStockPile || [];
        delete activity.parent;
        this.incompleteActivitesStockPile.push({
          activity: activity,
          errorMessage: activity.errorMessage || "Activity was not added."
        });
        return undefined;
      }
 
    }
  },
 
  save: {
    value: function() {
      var deferred = Q.defer(),
        self = this;
 
      this.whenReady = deferred.promise;
 
      this.saving = true;
      if (!this._docs || this._docs.length === 0) {
        this.warn("Save was unncessary, the activity feed was empty...");
        Q.nextTick(function() {
          self.saving = false;
          deferred.resolve(self);
          return self;
        });
        return deferred.promise;
      }
 
      try {
        this._docs.save(null, null, this.url).done(function() {
          self.saving = false;
          deferred.resolve(self);
          return self;
        });
        this.warn("Requested save of activity feed", this.whenReady);
      } catch (e) {
        console.log("problem saving activity feed");
        console.error(e);
      }
 
      return deferred.promise;
    }
  },
 
  toJSON: {
    value: function(includeEvenEmptyAttributes, removeEmptyAttributes) {
      this.debug("Customizing toJSON ", includeEvenEmptyAttributes, removeEmptyAttributes);
 
      var json;
      if (this.connection && typeof this.connection.toJSON === "function") {
        json = this.connection.toJSON.apply(this.connection, arguments);
        json.dbname = this.dbname;
      } else {
        json = new Connection().toJSON(includeEvenEmptyAttributes, removeEmptyAttributes);
      }
 
      this.debug(json);
      return json;
    }
  }
 
});
exports.Activities = Activities;