(function () { 'use strict'; angular.module('streamApp').service('streamService', function (apiProxy, myTimeFilter,$q) { var uploading = []; var clearUploading = () => { }; var skip = -1; var done = false; //const re = /([0-9])+/; // changed indicates that the people, tagIds, or includeMe changed this.getStream = function (albumIds, peopleIds, currentData, changed, includeMe, day, month, useDay, chronological, year) { if (changed) skip = 0; if (!useDay) { day = null; month = null; } return apiProxy.GET({ url: 'api/drops', params: { albumIds: albumIds || [], peopleIds: peopleIds || [], skip: skip, includeMe: includeMe, day: day, month: month, chronological: chronological, year: year } }).then(function (data) { var dataArray = []; if (skip > 0) { dataArray = currentData ? currentData : []; } $.each(data.data.drops, function (i, drop) { dataArray.push(mapDrop(drop)); }); skip = data.data.skip; done = data.data.done; return dataArray; }); }; this.getMemory = function (id) { return apiProxy.GET({ url: 'api/drops/' + id, }).then(function (data) { return mapDrop(data.data); }); }; this.getTimeline = function (id, changed, currentData) { if (changed) skip = 0; return apiProxy.GET({ url: 'api/drops/timelines/' + id, params: { skip, ascending: true } }).then(function (data) { var dataArray = []; if (skip > 0) { dataArray = currentData ? currentData : []; } $.each(data.data.drops, function (i, drop) { dataArray.push(mapDrop(drop)); }); skip = data.data.Skip; done = data.data.done; return dataArray; }); }; this.getAlbum = function (id, changed, currentData) { if (changed) skip = 0; return apiProxy.GET({ url: 'api/drops/albums/' + id, params: { skip, ascending: false } }).then(function (data) { var dataArray = []; if (skip > 0) { dataArray = currentData ? currentData : []; } $.each(data.data.drops, function (i, drop) { dataArray.push(mapDrop(drop)); }); skip = data.data.Skip; done = data.data.done; return dataArray; }); }; function mapDrop(drop) { const uploadingdrop = uploading[0] === drop.dropId; const record = new Date(drop.orderBy).getTime(); const date = new Date(drop.date); return { content: drop.content.stuff, contentSplit: drop.content.splitStuff, drop: drop.dropId, date: myTimeFilter(date.getMonth() + 1) + '/' + myTimeFilter(date.getDate()) + '/' + date.getFullYear(), dateString: labelDate(date, drop.dateType), dateType: drop.dateType, year: date.getFullYear(), editable: drop.editable, hour: myTimeFilter(date.getHours()), minute: myTimeFilter(date.getMinutes()), imageCount: drop.images.length + drop.movies.length, prompt: drop.prompt ? { promptId: drop.prompt.promptId, question: drop.prompt.question } : null, record: record, createdAt: drop.createdAt, isTask: drop.isTask, complete: drop.completed, completedBy: drop.completedBy, createdBy: drop.createdBy, partial: uploadingdrop, images: drop.imageLinks ? drop.imageLinks.map(image => { return { link: image.link, id: image.id }; }) : [], movies: drop.movieLinks ? drop.movieLinks.map(movie => { return { id: movie.id, link: movie.link, thumbLink: movie.thumbLink }; }) : [], unCompletedTask: drop.isTask && !drop.completed, comments: drop.comments.map(x => { x.dateString = labelDate(new Date(x.date), 0); x.images = x.imageLinks ? x.imageLinks.map(image => { return { link: image.link, id: image.id }; }) : []; x.movies = x.movieLinks ? x.movieLinks.map(movie => { return { id: movie.id, link: movie.link, thumbLink: movie.thumbLink }; }) : []; return x; }), tags: drop.tags, hasAlbums: drop.hasAlbums }; } // This is a way to coordinate uploading message this.uploadPush = function (dropId) { uploading.push(dropId); }; this.uploadPop = function (dropId) { var indexOf = uploading.findIndex(x => x === dropId); if (indexOf >= 0) { uploading.splice(indexOf, 1); clearUploading(); clearUploading = () => { }; } }; this.isUploading = function (clear) { if (uploading.length) { clearUploading = clear; return uploading[0]; // only assume one at a time for now } return false; }; // end of uploading coordination section this.updateDropTags = function (dropId) { // gets the current groups for a drop return apiProxy.GET({ url: `api/drops/${dropId}/groups`, }).then(function (data) { return data; }); }; this.getFilter = function () { return apiProxy.GET({ url: 'api/streams/filters', }).then(function (data) { //skip = skip > 0 ? 0 : -1; //done = false; return data; }); }; // getAllTags - GET /groups this.updateTag = function (tagId) { reset(); return apiProxy.PUT({ url: 'api/streams/groups/' + tagId, }).then(function () { }); }; this.removeTag = function (tagId) { reset(); return apiProxy.DELETE({ url: 'api/streams/groups/' + tagId, }).then(function () { }); }; this.newTag = function (tagName, isTodo, getTags) { return apiProxy.POST({ url: 'api/groups', data: { name: tagName } }).then(function () { getTags(); }); }; this.addDrop = function (information, date, dateType, tagIds, promptId, timelineIds) { return apiProxy.POST({ url: 'api/drops', data: { information: information, date: date, dateType, hour: 0, minute: 0, tagIds: tagIds, timelineIds, promptId: promptId } }); }; this.updateDrop = function (information, date, dateType, tagIds, dropId, movies, images) { return apiProxy.PUT({ url: 'api/drops/' + dropId, data: { information: information, date: date, dateType, tagIds: tagIds, dropId: dropId, movies: movies, images: images } }); }; this.deleteDrop = function (dropId) { return apiProxy.DELETE({ url: 'api/drops/' + dropId, }); }; this.addPerson = function (id) { reset(); return apiProxy.PUT({ url: 'api/streams/people/' + id, }).then(function () { }); }; this.removePerson = function (id) { reset(); return apiProxy.DELETE({ url: 'api/streams/people/' + id, }).then(function () { }); }; this.clearTags = function () { reset(); return apiProxy.PUT({ url: 'api/streams/groups', }); }; this.checkForNotifications = function () { return apiProxy.GET({ url: 'api/notifications/', }); }; this.viewNotification = function (dropId) { return apiProxy.PUT({ url: 'api/notifications/' + dropId, }); }; this.removeAllNotification = function () { return apiProxy.DELETE({ url: 'api/notifications', }); }; this.addComment = function (comment, dropId) { return apiProxy.POST({ url: 'api/comments', data: { comment: comment, dropId: dropId } }); }; this.thank = function (dropId) { return apiProxy.POST({ url: 'api/comments/' + dropId + '/thanks', }).then(data => data.data); }; this.updateComment = function (commentId, comment) { return apiProxy.PUT({ url: 'api/comments/' + commentId, data: { comment: comment } }); }; this.removeComment = function (commentId) { return apiProxy.DELETE({ url: 'api/comments/' + commentId, }); }; var reset = function () { skip = 0; done = false; }; }); })();