(function () { 'use strict'; var serviceId = 'myMessagesContext'; var mod = angular.module('myMessages'); mod.factory(serviceId, ['config', '$q', '$filter', '$http', myMessagesContext]); function myMessagesContext(config, $q, $filter, $http) { function handleSuccess(response) { return response.data; } function handleError(response) { if ( !angular.isObject(response.data) || !response.data.message ) { return $q.reject("An unknown error occurred."); } // Otherwise, use expected error message. return $q.reject(response.data.message); } return { getConversations: getConversations, getParticipantsForUser: getParticipantsForUser, getUnreadConversations: getUnreadConversations, getLatestConversation: getLatestConversation, getAllUserTagsForConversations: getAllUserTagsForConversations, addTagsToConversation: addTagsToConversation, removeTagsFromConversation: removeTagsFromConversation, setPinStatusForConversation: setPinStatusForConversation, setReadStatusForConversation: setReadStatusForConversation, addMessageToConversation: addMessageToConversation, createConversation: createConversation }; // gets conversations function getConversations() { var request = $http({ method: "get", url: config.myMessagesUrl + 'conversations' }); return request.then(handleSuccess, handleError); } // gets all of the participants for all of a users conversations function getParticipantsForUser() { var request = $http({ method: "get", url: config.myMessagesUrl + 'conversations/participants' }); return request.then(handleSuccess, handleError); } // gets all of the users unread conversations function getUnreadConversations() { var request = $http({ method: "get", url: config.myMessagesUrl + 'conversations/unread' }); return request.then(handleSuccess, handleError); } // gets the latest conversation for the user function getLatestConversation() { var request = $http({ method: "get", url: config.myMessagesUrl + 'conversations/latest' }); return request.then(handleSuccess, handleError); } // gets all the users tags for all of their conversations function getAllUserTagsForConversations() { var request = $http({ method: "get", url: config.myMessagesUrl + 'conversations/participant/tags' }); return request.then(handleSuccess, handleError); } // adds tags to participant model function addTagsToConversation(participant) { var request = $http({ method: "post", url: config.myMessagesUrl + 'conversations/participant/tag', data: participant }); return request.then(handleSuccess, handleError); } // removes tags from participant model function removeTagsFromConversation(participant) { var request = $http({ method: "delete", url: config.myMessagesUrl + 'conversations/participant/tag', data: participant, headers: { 'Content-Type': 'application/json' } }); return request.then(handleSuccess, handleError); } // set pin state for conversation for user function setPinStatusForConversation(id) { var request = $http({ method: "put", url: config.myMessagesUrl + 'conversations/' + id + '/participant/pin' }); return request.then(handleSuccess, handleError); } // marks read state for all messages in conversation for user function setReadStatusForConversation(conversation) { var request = $http({ method: "put", url: config.myMessagesUrl + 'conversations/messages/read', data: conversation }); return request.then(handleSuccess, handleError); } // adds message to conversation function addMessageToConversation(message) { var request = $http({ method: "post", url: config.myMessagesUrl + 'conversations/messages/reply', data: message }); return request.then(handleSuccess, handleError); } // creates a new conversation function createConversation(conversation) { var request = $http({ method: "post", url: config.myMessagesUrl + 'conversations', data: conversation }); return request.then(handleSuccess, handleError); } } })();