Esempio n. 1
0
        public static IEnumerable<TBUserNote> GetUserNotes(IWebAgent webAgent, string subName)
        {
            var request = webAgent.CreateGet(String.Format(ToolBoxUserNotesWiki, subName));
            var reqResponse = webAgent.ExecuteRequest(request);
            var response = JObject.Parse(reqResponse["data"]["content_md"].Value<string>());

            int version = response["ver"].Value<int>();
            string[] mods = response["constants"]["users"].Values<string>().ToArray();

            string[] warnings = response["constants"]["warnings"].Values<string>().ToArray();

            if (version < 6) throw new ToolBoxUserNotesException("Unsupported ToolBox version");

            try
            {
                var data = Convert.FromBase64String(response["blob"].Value<string>());

                string uncompressed;
                using (System.IO.MemoryStream compressedStream = new System.IO.MemoryStream(data))
                {
                    compressedStream.ReadByte();
                    compressedStream.ReadByte(); //skips first to bytes to fix zlib block size
                    using (DeflateStream blobStream = new DeflateStream(compressedStream, CompressionMode.Decompress))
                    {
                        using (var decompressedReader = new System.IO.StreamReader(blobStream))
                        {
                            uncompressed = decompressedReader.ReadToEnd();
                        }

                    }
                }

                JObject users = JObject.Parse(uncompressed);

                List<TBUserNote> toReturn = new List<TBUserNote>();
                foreach (KeyValuePair<string, JToken> user in users)
                {
                    var x = user.Value;
                    foreach (JToken note in x["ns"].Children())
                    {

                        TBUserNote uNote = new TBUserNote();
                        uNote.AppliesToUsername = user.Key;
                        uNote.SubName = subName;
                        uNote.SubmitterIndex = note["m"].Value<int>();
                        uNote.Submitter = mods[uNote.SubmitterIndex];
                        uNote.NoteTypeIndex = note["w"].Value<int>();
                        uNote.NoteType = warnings[uNote.NoteTypeIndex];
                        uNote.Message = note["n"].Value<string>();
                        uNote.Timestamp = UnixTimeStamp.UnixTimeStampToDateTime(note["t"].Value<long>());
                        uNote.Url = UnsquashLink(subName, note["l"].ValueOrDefault<string>());

                        toReturn.Add(uNote);
                    }
                }
                return toReturn;
            }
            catch (Exception e)
            {
                throw new ToolBoxUserNotesException("An error occured while processing Usernotes wiki. See inner exception for details", e);
            }
        }
        public static async Task <IEnumerable <TBUserNote> > GetUserNotesAsync(IWebAgent webAgent, string subName)
        {
            var reqResponse = await webAgent.Get(string.Format(ToolBoxUserNotesWiki, subName)).ConfigureAwait(false);

            var response = JObject.Parse(reqResponse["data"]["content_md"].Value <string>());
            int version  = response["ver"].Value <int>();

            if (version < 6)
            {
                throw new ToolBoxUserNotesException("Unsupported ToolBox version");
            }

            string[] mods = response["constants"]["users"].Values <string>().ToArray();

            string[] warnings = response["constants"]["warnings"].Values <string>().ToArray();

            try
            {
                var data = Convert.FromBase64String(response["blob"].Value <string>());

                string uncompressed;
                using (System.IO.MemoryStream compressedStream = new System.IO.MemoryStream(data))
                {
                    compressedStream.ReadByte();
                    compressedStream.ReadByte(); //skips first to bytes to fix zlib block size
                    using (DeflateStream blobStream = new DeflateStream(compressedStream, CompressionMode.Decompress))
                    {
                        using (var decompressedReader = new System.IO.StreamReader(blobStream))
                        {
                            uncompressed = decompressedReader.ReadToEnd();
                        }
                    }
                }

                JObject users = JObject.Parse(uncompressed);

                List <TBUserNote> toReturn = new List <TBUserNote>();
                foreach (KeyValuePair <string, JToken> user in users)
                {
                    var x = user.Value;
                    foreach (JToken note in x["ns"].Children())
                    {
                        TBUserNote uNote = new TBUserNote()
                        {
                            AppliesToUsername = user.Key,
                            SubName           = subName,
                            SubmitterIndex    = note["m"].Value <int>(),
                            Submitter         = mods[note["m"].Value <int>()],
                            NoteTypeIndex     = note["w"].Value <int>(),
                            NoteType          = warnings[note["w"].Value <int>()],
                            Message           = note["n"].Value <string>(),
                            Timestamp         = UnixTimeStamp.UnixTimeStampToDateTime(note["t"].Value <long>()),
                            Url = UnsquashLink(subName, note["l"].ValueOrDefault <string>())
                        };
                        toReturn.Add(uNote);
                    }
                }
                return(toReturn);
            }
            catch (Exception e)
            {
                throw new ToolBoxUserNotesException("An error occured while processing Usernotes wiki. See inner exception for details", e);
            }
        }