public CommonResult Unfollow(string viewerId, string ownerId, string accessToken)
        {
            if (logService)
            {
                WriteLogToFile(viewerId + "/unfollow/" + ownerId + "?accessToken=" + accessToken);
            }
            try
            {
                ValidateSignature();
                ChatterSoapService soap = getChatterSoapService();
                var ssOwnerId           = getSalesforceUserId(soap, ownerId);
                var ssViewerId          = getSalesforceUserId(soap, viewerId);

                ChatterRestService rest  = getChatterRestService(accessToken);
                ChatterResponse    cresp = rest.Unfollow(ssViewerId, ssOwnerId);
                return(IsUserFollowing(viewerId, ownerId, accessToken));
            }
            catch (Exception ex)
            {
                HandleError(ex, url);
                HandleError(ex, accessToken);
                return(new CommonResult()
                {
                    Success = false, ErrorMessage = ex.Message
                });
            }
        }
        private string getSalesforceUserId(ChatterSoapService soap, string nodeId)
        {
            Object objUserId = HttpRuntime.Cache[nodeId];

            if (objUserId == null)
            {
                objUserId = soap.GetUserId(profilesService.GetEmployeeId(nodeId));
                HttpRuntime.Cache.Insert(nodeId, objUserId);
            }
            return(Convert.ToString(objUserId));
        }
        public static void CreateActivity(SqlString url, SqlString username, SqlString password, SqlString token, SqlDateTime createdDT, SqlBoolean externalMessage, SqlString employeeId, SqlString actUrl, SqlString actTitle, SqlString actBody)
        {
            IChatterSoapService service = new ChatterSoapService(url.Value);
            service.AllowUntrustedConnection();
            service.Login(username.Value, password.Value, token.Value);

            service.CreateProfileActivity(employeeId.IsNull ? null : employeeId.Value, actUrl.IsNull ? null : actUrl.Value, actTitle.IsNull ? null : actTitle.Value, actBody.IsNull ? null : actBody.Value, createdDT.Value);
            if (externalMessage.IsTrue)
            {
                service.CreateExternalMessage(actUrl.IsNull ? null : actUrl.Value, actTitle.IsNull ? null : actTitle.Value, actBody.IsNull ? null : actBody.Value, employeeId.IsNull ? null : employeeId.Value);
            }
        }
        private ChatterSoapService getChatterSoapService()
        {
            ChatterSoapService soap = (ChatterSoapService)HttpRuntime.Cache[url];

            if (soap == null)
            {
                lock (this)
                {
                    WriteLogToFile("Refreshing ChatterSoapService");
                    soap = new ChatterSoapService(url);
                    soap.Login(userName, password, token);
                    WriteLogToFile("Success in logging into ChatterSoapService");
                    HttpRuntime.Cache.Insert(url, soap);
                }
            }
            return(soap);
        }
        public Activity[] GetUserActivities(string userId, string mode, int count)
        {
            try {
                ValidateSignature();
                bool includeUserActivities = mode.Equals("all", StringComparison.InvariantCultureIgnoreCase);

                ChatterSoapService soap = getChatterSoapService();
                var        ssUserId     = getSalesforceUserId(soap, userId);
                Activity[] result       = soap.GetActivities(ssUserId, Int32.Parse(userId), includeUserActivities, count).ToArray();
                return(result);
            }
            catch (Exception e)
            {
                HandleError(e, url);
                return(null);
            }
        }
        public CommonResult IsUserFollowing(string viewerId, string ownerId, string accessToken)
        {
            try
            {
                ChatterSoapService soap = getChatterSoapService();
                var ssOwnerId           = getSalesforceUserId(soap, ownerId);
                var ssViewerId          = getSalesforceUserId(soap, viewerId);

                ChatterRestService rest  = getChatterRestService(accessToken);
                ChatterResponse    cresp = rest.GetFollowers(ssOwnerId);
                int total = cresp != null ? cresp.total : 0;

                while (cresp != null && cresp.followers != null)
                {
                    foreach (ChatterSubscription csub in cresp.followers)
                    {
                        if (csub.subscriber != null && csub.subscriber.id.StartsWith(ssViewerId))
                        {
                            return(new CommonResult()
                            {
                                Success = true, Following = true, Total = total, AccessToken = rest.GetAccessToken()
                            });
                        }
                    }
                    cresp = rest.GetNextPage(cresp);
                }

                return(new CommonResult()
                {
                    Success = true, Following = false, Total = total, AccessToken = rest.GetAccessToken()
                });
            }
            catch (Exception ex)
            {
                HandleError(ex, url);
                HandleError(ex, accessToken);
                return(new CommonResult()
                {
                    Success = false, ErrorMessage = ex.Message
                });
            }
        }
        public void GetActivities(Object stateInfo)
        {
            // this IS thread safe, but very hokey. Just don't want to pile up a bunch of threads in here
            if (fetchingActivities)
            {
                return;
            }
            fetchingActivities = true;
            try
            {
                Activity lastActivity = latestList.Count > 0 ? latestList[0] : null;

                ChatterSoapService soap          = getChatterSoapService();
                List <Activity>    newActivities = soap.GetProfileActivities(lastActivity, cacheCapacity);
                if (newActivities.Count > 0)
                {
                    WriteLogToFile("Adding " + newActivities.Count + " new activities to list");
                    lock (latestList)
                    {
                        latestList.AddRange(newActivities);
                        latestList.Sort(new ActivitiesComparer());
                        if (latestList.Count > cacheCapacity)
                        {
                            latestList.RemoveRange(cacheCapacity, latestList.Count - cacheCapacity);
                        }
                        latestList.Clump();
                    }
                    WriteLogToFile("List count is now " + latestList.Count);
                }
            }
            catch (Exception e)
            {
                HandleError(e, url);
            }
            finally
            {
                fetchingActivities = false;
            }
        }
        private string getSalesforceUserId(ChatterSoapService soap, string nodeId)
        {
            Object objUserId = HttpRuntime.Cache[nodeId];

            if (objUserId == null)
            {
                objUserId = soap.GetUserId(profilesService.GetEmployeeId(nodeId));
                HttpRuntime.Cache.Insert(nodeId, objUserId);
            }
            return Convert.ToString(objUserId);
        }
 private ChatterSoapService getChatterSoapService()
 {
     ChatterSoapService soap = (ChatterSoapService)HttpRuntime.Cache[url];
     if (soap == null)
     {
         lock (this)
         {
             WriteLogToFile("Refreshing ChatterSoapService");
             soap = new ChatterSoapService(url);
             soap.Login(userName, password, token);
             WriteLogToFile("Success in logging into ChatterSoapService");
             HttpRuntime.Cache.Insert(url, soap);
         }
     }
     return soap;
 }
        public CommonResult CreateGroup(Stream stream)
        {
            try {
                ValidateSignature();
                NameValueCollection p = parseParameters(stream);

                if (string.IsNullOrEmpty(p["name"]))
                {
                    return(new CommonResult()
                    {
                        Success = false, ErrorMessage = "Group name is required."
                    });
                }

                if (string.IsNullOrEmpty(p["ownerId"]))
                {
                    return(new CommonResult()
                    {
                        Success = false, ErrorMessage = "OwnerId is required."
                    });
                }

                string nodeId = p["ownerId"];
                string descr  = p["description"];
                if (string.IsNullOrEmpty(descr))
                {
                    descr = p["name"];
                }

                string employeeId = profilesService.GetEmployeeId(nodeId);

                ChatterSoapService soap    = getChatterSoapService();
                string             groupId = soap.CreateGroup(p["name"], descr, employeeId);

                string users = p["users"];
                if (!string.IsNullOrEmpty(users))
                {
                    string[]      personList   = users.Split(',');
                    List <string> employeeList = new List <string>();
                    foreach (string pId in personList)
                    {
                        try
                        {
                            string eId = profilesService.GetEmployeeId(pId);
                            employeeList.Add(eId);
                        }
                        catch (Exception ex)
                        {
                            //TODO: need to report it back to the server
                        }
                    }

                    if (employeeList.Count > 0)
                    {
                        soap.AddUsersToGroup(groupId, employeeList.ToArray <string>());
                    }
                }
                return(new CommonResult()
                {
                    Success = true, URL = url.Replace("/services", "/_ui/core/chatter/groups/GroupProfilePage?g=" + groupId)
                });
            }
            catch (Exception ex)
            {
                HandleError(ex, url);
                return(new CommonResult()
                {
                    Success = false, ErrorMessage = ex.Message
                });
            }
        }