/// <summary> /// Twitter Profile creation on Buzz /// </summary> /// <param name="siteName"></param> /// <returns></returns> private BaseResult CreateUpdateProfileOnBuzz(string siteName) { BuzzTwitterProfile twitterProfile = null; var isProfileCreated = string.Empty; BuzzClient client; try { client = new BuzzClient(); twitterProfile = new BuzzTwitterProfile(); ISite site; try { site = InputContext.TheSiteList.GetSite(siteName); if (site != null) { twitterProfile.SiteURL = site.SiteName; } else { return new Error { Type = "SITEINVALIDACTION", ErrorMessage = String.Format("Site, '{0}' doesn't exist", siteName) }; } } catch (Exception exc) { InputContext.Diagnostics.WriteExceptionToLog(exc); return new Error { Type = "SITEINVALIDACTION", ErrorMessage = String.Format("Site, '{0}' doesn't exist", siteName) }; } if (string.IsNullOrEmpty(_profileId) || string.IsNullOrEmpty(_title) || string.IsNullOrEmpty(_commentForumURI) || twitterUserScreenNameList.Count < 1) { return new Error { Type = "TWITTERPROFILEMANDATORYFIELDSMISSING", ErrorMessage = "Please fill in the mandatory fields for creating/updating a profile" }; } twitterProfile.ProfileId = _profileId; twitterProfile.Title = _title; twitterProfile.SearchKeywords = _searchterms.Split(',').Where(x => x != " " && !string.IsNullOrEmpty(x)).Distinct().Select(p => p.Trim()).ToList(); twitterProfile.ProfileCountEnabled = _isCountsEnabled; twitterProfile.ProfileKeywordCountEnabled = _keywordCountsEnabled; twitterProfile.ModerationEnabled = _isModerationEnabled; twitterProfile.TrustedUsersEnabled = _isTrustedUsersEnabled; twitterProfile.Active = _isActive; var invalidTwitterName = string.Empty; List<string> twitterUserIds = AddValidUserToTheList(twitterUserScreenNameList, site, out invalidTwitterName); if (twitterUserIds != null && twitterUserIds.Count > 0) { twitterProfile.Users = twitterUserIds; } else { twitterProfile.Users = twitterUserScreenNameList; if (false == string.IsNullOrEmpty(_pageAction) && _pageAction.ToUpper().Equals("UPDATEPROFILE")) { CreateTwitterProfileXML(twitterProfile, _commentForumURI, true); return new Error { Type = "TWITTERRETRIEVEUSERINVALIDACTIONONUPDATE", ErrorMessage = "Error while retrieving the twitter user, '" + invalidTwitterName + "'. Check if the twitter screen name entered is valid on update" }; } else { CreateTwitterProfileXML(twitterProfile, _commentForumURI, false); return new Error { Type = "TWITTERRETRIEVEUSERINVALIDACTIONONCREATION", ErrorMessage = "Error while retrieving the twitter user, '" + invalidTwitterName + "'. Check if the twitter screen name entered is valid" }; } } isProfileCreated = client.CreateUpdateProfile(twitterProfile); twitterProfile.Users = twitterUserScreenNameList; //Filling the xml with the screennames entered } catch (Exception ex) { twitterProfile.Users = twitterUserScreenNameList; CreateTwitterProfileXML(twitterProfile, _commentForumURI, false); InputContext.Diagnostics.WriteExceptionToLog(ex); return new Error { Type = "TWITTERPROFILECREATIONINVALIDACTION", ErrorMessage = String.Format("Twitter Profile, '{0}' creation failed", isProfileCreated) }; } if (isProfileCreated.Equals("OK")) { //Create and map commentforum Comments commentObj = new Comments(dnaDiagnostic,readerCreator, AppContext.DnaCacheManager, InputContext.TheSiteList); CommentForum commentForum = new CommentForum(); commentForum.isContactForm = false; commentForum.SiteName = siteName; if (false == string.IsNullOrEmpty(_commentForumURI)) commentForum.ParentUri = _commentForumURI; else commentForum.ParentUri = string.Empty; commentForum.Id = _profileId; commentForum.Title = _title; try { ISite site = InputContext.TheSiteList.GetSite(siteName); BaseResult result; if ((string.IsNullOrEmpty(_pageAction)) || (false == _pageAction.ToLower().Equals("updateprofile"))) { result = CreateCommentForum(siteName, commentObj, commentForum, site); } else { result = UpdateCommentForum(siteName, commentObj, site, commentForum); } if (!result.IsError()) { ((Result)result).Message += " Your profile has been created, now please contact the DNA team to activate it."; } CreateTwitterProfileXML(twitterProfile, commentForum.ParentUri, true); return result; } catch (Exception e) { CreateTwitterProfileXML(twitterProfile, commentForum.ParentUri, true); InputContext.Diagnostics.WriteExceptionToLog(e); return new Error { Type = "SITEINVALIDACTION", ErrorMessage = String.Format("Site, '{0}' doesn't exist", siteName) }; } } else { return new Error { Type = "TWITTERPROFILECREATIONINVALIDACTION", ErrorMessage = String.Format("Twitter Profile, '{0}' creation failed", isProfileCreated) }; } }
/// <summary> /// Get specific profile details from Buzz /// </summary> /// <param name="twitterProfileId"></param> /// <returns></returns> private BaseResult GetProfileFromBuzz(string twitterProfileId) { BuzzClient client; BuzzTwitterProfile twitterProfile = null; CommentForum commentForum = null; Comments commentObj = new Comments(dnaDiagnostic, readerCreator, AppContext.DnaCacheManager, InputContext.TheSiteList); try { client = new BuzzClient(); twitterProfile = new BuzzTwitterProfile(); commentForum = new CommentForum(); //Get the twitter profile from Buzz twitterProfile = client.GetProfile(twitterProfileId); if (twitterProfile != null) { var twitterUserIds = string.Empty; if (twitterProfile.Users.Count > 0) { twitterUserIds = string.Join(",", twitterProfile.Users.ToArray()); twitterProfile.Users = GetTwitterScreenNamesFromDNA(twitterUserIds); } try { ISite site = InputContext.TheSiteList.GetSite(_siteName); if (site != null) { commentForum = commentObj.GetCommentForumByUid(twitterProfile.ProfileId, site); if (commentForum != null) { _commentForumURI = commentForum.ParentUri; } } } catch (Exception ex) { InputContext.Diagnostics.WriteExceptionToLog(ex); } //seperate method to fill up the twitter profile xml CreateTwitterProfileXML(twitterProfile, _commentForumURI, true); string[] str1 = InputContext.CurrentDnaRequest.UrlReferrer.AbsoluteUri.Split('?').ToArray(); var commentforumlistURI = string.Empty; if (commentForum != null && (false == String.IsNullOrEmpty(commentForum.SiteName)) ) { commentforumlistURI = str1[0].Replace("moderation", commentForum.SiteName); commentforumlistURI = commentforumlistURI.Replace("twitterprofilelist", "commentforumlist?dnahostpageurl=" + commentForum.ParentUri.Trim()); return new Result("TwitterProfileRetrieved", String.Format("Twitter profile, '{0}' retrieved successfully.", twitterProfileId), commentforumlistURI); } return new Result("TwitterProfileRetrieved", String.Format("Twitter profile, '{0} retrieved successfully but comment forum hasn't been created yet.", twitterProfileId)); } else { return new Error { Type = "GETTWITTERPROFILEINVALIDACTION", ErrorMessage = "Twitter Profile retrieval from Buzz failed" }; } } catch (Exception ex) { InputContext.Diagnostics.WriteExceptionToLog(ex); return new Error { Type = "GETTWITTERPROFILEINVALIDACTION", ErrorMessage = "Twitter Profile retrieval failed" }; } }
/// <summary> /// Method to create the fill up the twitter profile with values /// </summary> /// <param name="twitterProfile"></param> /// <param name="commentForumParentURI"></param> /// <param name="isExists"></param> private void CreateTwitterProfileXML(BuzzTwitterProfile twitterProfile, string commentForumParentURI, bool isExists) { string str = StringUtils.SerializeToXmlReturnAsString(twitterProfile); var actualXml = str.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", ""); actualXml = actualXml.Replace("xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"BBC.Dna.SocialAPI\"", "").Trim(); actualXml = actualXml.Replace("xmlns:d2p1=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\"", "").Trim(); actualXml = actualXml.Replace("d2p1:string", "item"); actualXml = actualXml.Replace("</profile>", "<commentforumparenturi>" + commentForumParentURI + "</commentforumparenturi></profile>"); //As the values are persisted even if there is a failure, we need to introduce another node to see if the //profile exists or a new profile. This is used in making the fields readonly in xslt if (isExists) { actualXml = actualXml.Replace("</profile>", "<exists> </exists></profile>"); } //Making all the XML Nodes uppercase actualXml = StringUtils.ConvertXmlTagsToUppercase(actualXml); XmlDocument doc = new XmlDocument(); doc.LoadXml(actualXml); XmlNode appendNode = doc.DocumentElement; ImportAndAppend(appendNode, ""); }
private static BuzzTwitterProfile CreateNewProfile(string profileID) { BuzzTwitterProfile profile = new BuzzTwitterProfile(); if (profileID.Length == 0) { profile.ProfileId = Guid.NewGuid().ToString(); } else { profile.ProfileId = profileID; } return profile; }