/// <summary> /// Synchronise moderators list changes with the server /// </summary> private void SyncModerators() { Thread t = new Thread(() => { try { string encodedForumName = FolderCollection.EncodeForumName(Name); foreach (string part in AddedModerators) { string url = string.Format("moderator/{0}/{1}/modadd", encodedForumName, part); HttpWebRequest getUrl = APIRequest.Get(url, APIRequest.APIFormat.XML); string responseString = APIRequest.ReadResponseString(getUrl); if (responseString == "Success") { LogFile.WriteLine("Moderator {0} successfully added to {1}", part, Name); } } foreach (string part in RemovedModerators) { string url = string.Format("moderator/{0}/{1}/modrem", encodedForumName, part); HttpWebRequest getUrl = APIRequest.Get(url, APIRequest.APIFormat.XML); string responseString = APIRequest.ReadResponseString(getUrl); if (responseString == "Success") { LogFile.WriteLine("Moderator {0} successfully removed from {1}", part, Name); } } // Clear out the current moderators lists to force a refresh from the server AddedMods = string.Empty; RemovedMods = string.Empty; Mods = string.Empty; lock (CIX.DBLock) { CIX.DB.Update(this); } // Notify interested parties that the moderators list has changed CIX.DirectoryCollection.NotifyModeratorsUpdated(this); } catch (Exception e) { CIX.ReportServerExceptions("DirForum.SyncModerators", e); } }); t.Start(); }
/// <summary> /// Refresh this topic from the server /// </summary> internal void InternalRefresh() { if (CIX.Online & !_isFolderRefreshing) { _isFolderRefreshing = true; CIX.FolderCollection.NotifyTopicUpdateStarted(this); try { // When refreshing, get the last 30 days worth unless the topic is empty in which // case we get everything. The assumption is that if any changes occur with the // read/unread status on the server then 30 days back is enough to sync those // with the local database. DateTime latestReply = new DateTime(1900, 1, 1); if (Messages.Count > 0) { latestReply = DateTime.Now.Subtract(new TimeSpan(30, 0, 0, 0)); } // First clear the flag so we don't over-refresh RefreshRequired = false; Folder forum = ParentFolder; int countOfNewMessages = 0; string urlFormat = string.Format("forums/{0}/{1}/allmessages", FolderCollection.EncodeForumName(forum.Name), Name); HttpWebRequest request = APIRequest.GetWithQuery(urlFormat, APIRequest.APIFormat.XML, "maxresults=5000&since=" + latestReply.ToString("yyyy-MM-dd HH:mm:ss")); using (Stream objStream = APIRequest.ReadResponse(request)) { if (objStream != null) { countOfNewMessages = FolderCollection.AddMessages(objStream, ref latestReply, false, false); } } if (countOfNewMessages > 0) { LogFile.WriteLine("{0}/{1} refreshed with {2} new messages", forum.Name, Name, countOfNewMessages); } } catch (Exception e) { CIX.ReportServerExceptions("Folder.Refresh", this, e); } CIX.FolderCollection.NotifyTopicUpdateCompleted(this); _isFolderRefreshing = false; } }
/// <summary> /// Refresh the list of moderators /// </summary> private void RefreshModerators() { Thread t = new Thread(() => { try { List <string> moderators = new List <string>(); LogFile.WriteLine("Updating list of moderators for {0}", Name); string urlFormat = string.Format("forums/{0}/moderators", FolderCollection.EncodeForumName(Name)); HttpWebRequest wrGeturl = APIRequest.GetWithQuery(urlFormat, APIRequest.APIFormat.XML, "maxresults=10000"); Stream objStream = APIRequest.ReadResponse(wrGeturl); if (objStream != null) { using (XmlReader reader = XmlReader.Create(objStream)) { XmlSerializer serializer = new XmlSerializer(typeof(ForumMods)); ForumMods allModerators = (ForumMods)serializer.Deserialize(reader); moderators.AddRange(allModerators.Mods.Select(mod => mod.Name)); Mods = string.Join(",", moderators); LogFile.WriteLine("List of moderators for {0} updated", Name); lock (CIX.DBLock) { CIX.DB.Update(this); } } } CIX.DirectoryCollection.NotifyModeratorsUpdated(this); } catch (Exception e) { CIX.ReportServerExceptions("DirForum.Moderators", e); } isModeratorsRefreshing = 0; }); t.Start(); }
/// <summary> /// Join the specified forum. /// </summary> private void InternalJoin() { try { LogFile.WriteLine("Joining forum {0}", Name); HttpWebRequest request = APIRequest.GetWithQuery("forums/" + FolderCollection.EncodeForumName(Name) + "/join", APIRequest.APIFormat.XML, "mark=true"); string responseString = APIRequest.ReadResponseString(request); if (responseString == "Success") { LogFile.WriteLine("Successfully joined forum {0}", Name); Folder folder = CIX.FolderCollection.Get(-1, Name); if (folder == null) { folder = new Folder { Name = Name, Flags = FolderFlags.Recent, ParentID = -1 }; CIX.FolderCollection.Add(folder); } folder.Flags &= ~FolderFlags.Resigned; lock (CIX.DBLock) { CIX.DB.Update(folder); } CIX.DirectoryCollection.NotifyForumJoined(folder); CIX.FolderCollection.Refresh(false); } JoinPending = false; lock (CIX.DBLock) { CIX.DB.Update(this); } } catch (Exception e) { CIX.ReportServerExceptions("DirForum.Join", e); } }
/// <summary> /// Refresh the details of the forum, including title and description. /// </summary> /// <param name="forumName">The forum name</param> public void RefreshForum(string forumName) { Thread t = new Thread(() => { DirForum forum = null; try { string encodedForumName = FolderCollection.EncodeForumName(forumName); LogFile.WriteLine("Updating directory for {0}", forumName); HttpWebRequest wrGeturl = APIRequest.Get("forums/" + encodedForumName + "/details", APIRequest.APIFormat.XML); Stream objStream = APIRequest.ReadResponse(wrGeturl); if (objStream != null) { using (XmlReader reader = XmlReader.Create(objStream)) { XmlSerializer serializer = new XmlSerializer(typeof(ForumDetails)); ForumDetails forumDetails = (ForumDetails)serializer.Deserialize(reader); bool isNewForum = false; forum = ForumByName(forumDetails.Name); if (forum == null) { forum = new DirForum(); isNewForum = true; } forum.Name = forumDetails.Name; forum.Title = forumDetails.Title; forum.Desc = forumDetails.Description; forum.Cat = forumDetails.Category; forum.Sub = forumDetails.SubCategory; forum.Recent = forumDetails.Recent; forum.Type = forumDetails.Type; lock (CIX.DBLock) { if (isNewForum) { CIX.DB.Insert(forum); _allForums[forum.ID] = forum; } else { CIX.DB.Update(forum); } } LogFile.WriteLine("Directory for {0} updated", forum.Name); } } } catch (Exception e) { CIX.ReportServerExceptions("DirectoryCollection.RefreshForum", e); } NotifyForumUpdated(forum); }); t.Start(); }
/// <summary> /// Withdraw this message from the server. /// </summary> private void WithdrawMessage() { try { LogFile.WriteLine("Withdrawing message {0}", RemoteID); Folder topic = Topic; Folder forum = topic.ParentFolder; string urlFormat = string.Format("forums/{0}/{1}/{2}/withdraw", FolderCollection.EncodeForumName(forum.Name), topic.Name, RemoteID); HttpWebRequest request = APIRequest.Get(urlFormat, APIRequest.APIFormat.XML); // Don't try and withdraw again because this was an error. WithdrawPending = false; lock (CIX.DBLock) { CIX.DB.Update(this); } string responseString = APIRequest.ReadResponseString(request); if (responseString == "Success") { LogFile.WriteLine("Successfully withdrawn message {0}", RemoteID); // Replace the message text with the one that we'd get from the server // and save us a round-trip. Body = IsMine ? Resources.WithdrawnByAuthor : Resources.WithdrawnByModerator; lock (CIX.DBLock) { CIX.DB.Update(this); } CIX.FolderCollection.NotifyMessageChanged(this); } else { LogFile.WriteLine("Error withdrawing message. Response is: {1}", forum.Name, responseString); } } catch (Exception e) { CIX.ReportServerExceptions("CIXMessage.WithdrawMessage", e); } }
/// <summary> /// Add or remove the star from this message on the server. /// </summary> private void StarMessage() { Folder folder = Topic; if (Starred) { StarAdd addStar = new StarAdd { Forum = Topic.ParentFolder.Name, Topic = folder.Name, MsgID = RemoteID.ToString(CultureInfo.InvariantCulture) }; LogFile.WriteLine("Adding star to message {0} in {1}/{2}", addStar.MsgID, addStar.Forum, addStar.Topic); try { HttpWebRequest postUrl = APIRequest.Post("starred/add", APIRequest.APIFormat.XML, addStar); string responseString = APIRequest.ReadResponseString(postUrl); if (responseString.Contains("Success")) { LogFile.WriteLine("Star successfully added"); } else { LogFile.WriteLine("Failed to add star to message {0} : {1}", RemoteID, responseString); } } catch (Exception e) { CIX.ReportServerExceptions("CIXMessage.StarMessage", e); } StarPending = false; lock (CIX.DBLock) { CIX.DB.Update(this); } } else { string forumName = FolderCollection.EncodeForumName(Topic.ParentFolder.Name); string starUrl = string.Format("starred/{0}/{1}/{2}/rem", forumName, Topic.Name, RemoteID); LogFile.WriteLine("Removing star from message {0} in {1}/{2}", RemoteID, forumName, Topic.Name); try { HttpWebRequest getRequest = APIRequest.Get(starUrl, APIRequest.APIFormat.XML); string responseString = APIRequest.ReadResponseString(getRequest); if (responseString.Contains("Success")) { LogFile.WriteLine("Star successfully removed"); } else { LogFile.WriteLine("Failed to remove star for message {0} : {1}", RemoteID, responseString); } } catch (Exception e) { CIX.ReportServerExceptions("CIXMessage.StarMessage", e); } if (StarPending) { StarPending = false; lock (CIX.DBLock) { CIX.DB.Update(this); } } } }
/// <summary> /// Resign this folder on the server. /// </summary> private void ResignFolder() { if (!CIX.Online) { ResignPending = true; lock (CIX.DBLock) { CIX.DB.Update(this); } return; } try { string url; if (IsRootFolder) { LogFile.WriteLine("Resigning forum {0}", Name); url = "forums/" + FolderCollection.EncodeForumName(Name) + "/resign"; } else { Folder forum = ParentFolder; LogFile.WriteLine("Resigning topic {0}/{1}", forum.Name, Name); url = "forums/" + FolderCollection.EncodeForumName(forum.Name) + "/" + FolderCollection.EncodeForumName(Name) + "/resigntopic"; } HttpWebRequest request = APIRequest.Get(url, APIRequest.APIFormat.XML); string responseString = APIRequest.ReadResponseString(request); if (responseString == "Success") { LogFile.WriteLine("Successfully resigned from {0}", Name); CIX.FolderCollection.NotifyFolderUpdated(this); } else { LogFile.WriteLine("Error resigning {0}. Response is: {1}", Name, responseString); } if (DeletePending) { DeletePending = false; Delete(false); return; } // Whatever happens, clear the pending action so we don't keep trying to // resign the forum repeatedly. ResignPending = false; Flags |= FolderFlags.Resigned; lock (CIX.DBLock) { CIX.DB.Update(this); } } catch (Exception e) { CIX.ReportServerExceptions("Folder.ResignFolder", this, e); } }