Beispiel #1
0
 public Forums()
 {
     this.InitializeComponent();
     APILibrary = new Library_APICall();
     mlibrary = new methodLibrary();
     rforumposts = new RootObjectForumPosts();
     rforumtopics = new RootObjectForumTopics();
     rforumdata = new RootObjectForumsData();
     remails = new RootObjectEmails();
     remaildata = new RootEmailData();
     ftopic = new ObservableCollection<ForumsTopic>();
     etopic = new ObservableCollection<EmailTopics>();
     DisplayInformation.AutoRotationPreferences = DisplayOrientations.Portrait;
     this.NavigationCacheMode = NavigationCacheMode.Required;
     //rforumdatatopic = new ForumsTopic();
     //this.NavigationCacheMode = NavigationCacheMode.Required;
     teamid = 0;
 }
Beispiel #2
0
        public static async Task<string> fetchForumTopics()
        {
            string hreftopics = "https://api.teamsnap.com/v3/forum_topics/search?team_id=" + teamid.ToString();
            string hrefposts = "https://api.teamsnap.com/v3/forum_posts/search?team_id=" + teamid.ToString();
            string access_token = (string)ApplicationData.Current.LocalSettings.Values["Tokens"];
            string topics_resp = await APILibrary.apiCall(access_token, hreftopics);
            if (topics_resp == "fail")
            {
                return "networkProblem";
            }
            string posts_resp = await APILibrary.apiCall(access_token, hrefposts);
            if (posts_resp == "fail")
            {
                return "networkProblem";
            }
            rforumposts = forum_posts.forumpostsDataDeserializer(posts_resp);
            rforumtopics = forum_topics.forumtopicsDataDeserializer(topics_resp);
            Dictionary<string, int> rforumposts_ht = new Dictionary<string,int>();
            Dictionary<string,int> rforumtopics_ht = new Dictionary<string, int>();
            mlibrary.writeFile("forum_topics", topics_resp);
            mlibrary.writeFile("forum_posts", posts_resp);
            rforumdata.topics = new List<ForumsTopic>();
            try
            {
                int len = 0;
                while (len < rforumtopics.collection.items.Count)
                {
                    ForumsTopic rforumdatatopic = new ForumsTopic();
                    for (int i = 0; i < rforumtopics.collection.items[len].data.Count; i++)
                    {
                        rforumtopics_ht.Add(rforumtopics.collection.items[len].data[i].name, i);
                    }
                    rforumdatatopic.forum_topic_id = (long)rforumtopics.collection.items[len].data[(int)rforumtopics_ht["id"]].value;
                    rforumdatatopic.title = (string)rforumtopics.collection.items[len].data[(int)rforumtopics_ht["title"]].value;
                    rforumdatatopic.post = new List<ForumsPost>();
                    int postslen = 0;
                    while (postslen < rforumposts.collection.items.Count)
                    {

                        for (int i = 0; i < rforumposts.collection.items[postslen].data.Count; i++)
                        {
                            rforumposts_ht.Add(rforumposts.collection.items[postslen].data[i].name, i);
                        }
                        if (postslen < 1)
                        {
                            rforumdatatopic.started_by = (string)rforumposts.collection.items[postslen].data[(int)rforumposts_ht["poster_name"]].value;
                        }
                        if (rforumdatatopic.forum_topic_id == (long)rforumposts.collection.items[postslen].data[(int)rforumposts_ht["forum_topic_id"]].value)
                        {
                            ForumsPost rforumdataposts = new ForumsPost();
                            rforumdataposts.forum_post_id = rforumposts.collection.items[postslen].data[(int)rforumposts_ht["id"]].value == null ? 0 : (long)rforumposts.collection.items[postslen].data[(int)rforumposts_ht["id"]].value;
                            rforumdataposts.member_id = rforumposts.collection.items[postslen].data[(int)rforumposts_ht["member_id"]].value == null ? 0 : (long)rforumposts.collection.items[postslen].data[(int)rforumposts_ht["member_id"]].value;
                            rforumdataposts.message = methodLibrary.StripTagsRegexCompiled((string)rforumposts.collection.items[postslen].data[(int)rforumposts_ht["message"]].value);
                            rforumdataposts.poster_name = (string)rforumposts.collection.items[postslen].data[(int)rforumposts_ht["poster_name"]].value;
                            DateTime updated_at = rforumposts.collection.items[postslen].data[(int)rforumposts_ht["updated_at"]].value == null ? DateTime.MinValue : (DateTime)rforumposts.collection.items[postslen].data[(int)rforumposts_ht["updated_at"]].value;
                            updated_at = updated_at.ToLocalTime();
                            rforumdatatopic.last_updated_at = updated_at == null ? DateTime.MinValue : (DateTime)updated_at;
                            rforumdataposts.updated_at = String.Format("{0:ddd, MMM d, hh:mm tt}", updated_at);
                            rforumdatatopic.last_updated_at_string = rforumdataposts.updated_at;
                            rforumdatatopic.post.Add(rforumdataposts);
                        }
                        if (rforumdatatopic.post.Count == 1)
                        {
                            rforumdatatopic.replies = "No replies";
                        }
                        else if (rforumdatatopic.post.Count == 2)
                        {
                            rforumdatatopic.replies = (rforumdatatopic.post.Count - 1).ToString() + " reply";
                        }
                        else
                        {
                            rforumdatatopic.replies = (rforumdatatopic.post.Count - 1).ToString() + " replies";
                        }

                        rforumposts_ht.Clear();
                        postslen++;
                    }

                    rforumdata.topics.Add(rforumdatatopic);
                    rforumtopics_ht.Clear();
                    len++;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception returned : " + ex);
                return "noData";
            }

            return "allGood";
        }
        private async void PostTopic_Click(object sender, RoutedEventArgs e)
        {
            RootObjectForumTopics rftopics = new RootObjectForumTopics();
            CollectionForumTopicsTemplate rfttemplate = new CollectionForumTopicsTemplate();
            RootObjectUsers ruser = new RootObjectUsers();
            RootObjectForumPosts rfposts = new RootObjectForumPosts();
            CollectionForumPostsTemplate rfptemplate = new CollectionForumPostsTemplate();
            if (Subject.Text == "" || Message.Text == "")
            {

            }
            else
            {
                try
                {
                    /// filling out the template for Forum Topics
                    string forumtopic = await mlibrary.readFile("forum_topics");
                    rftopics = forum_topics.forumtopicsDataDeserializer(forumtopic);
                    rfttemplate = rftopics.collection.template;
                    Dictionary<string, int> rfttemplate_ht = new Dictionary<string, int>();
                    for (int i = 0; i < rfttemplate.data.Count; i++)
                    {
                        rfttemplate_ht.Add(rfttemplate.data[i].name, i);
                    }
                    rfttemplate.data[(int)rfttemplate_ht["title"]].value = Subject.Text;
                    if (broadcast_to_team.IsChecked == true)
                    {
                        rfttemplate.data[(int)rfttemplate_ht["is_announcement"]].value = true;
                    }
                    else
                    {
                        rfttemplate.data[(int)rfttemplate_ht["is_announcement"]].value = false;
                    }
                    rfttemplate.data[(int)rfttemplate_ht["team_id"]].value = teamid.ToString();
                    rfttemplate.data[(int)rfttemplate_ht["type"]].value = "forum_topic";
                    string rftresponse = forum_topics.forumTopicsTemplateSerializer(rfttemplate);
                    rftresponse = "{\"template\":" + rftresponse + "}";
                    Debug.WriteLine(rftresponse);
                    // make POST to forum_topic
                    await APILibrary.apiPOST((string)ApplicationData.Current.LocalSettings.Values["Tokens"], rftresponse, rftopics.collection.href);

                    // read POST response to fetch message id.
                    string postresp = await mlibrary.readFile("POSTresponse");
                    rftopics = forum_topics.forumtopicsDataDeserializer(postresp);

                    string userclass = await mlibrary.readSerializedUserClass();
                    Debug.WriteLine(userclass);
                    ruser = Users.usersDataDeserializer(userclass);
                    Debug.WriteLine(ruser.items[0].id);
                    /// filling out the template for forum posts
                    string forumposts = await mlibrary.readFile("forum_posts");
                    rfposts = forum_posts.forumpostsDataDeserializer(forumposts);
                    rfptemplate = rfposts.collection.template;
                    Dictionary<string, int> rfptemplate_ht = new Dictionary<string, int>();
                    for (int i = 0; i < rfptemplate.data.Count; i++)
                    {
                        rfptemplate_ht.Add(rfptemplate.data[i].name, i);
                    }
                    long teammember_id = 0;
                    for (int i = 0; i < ruser.teams.Count; i++)
                    {
                        if (ruser.teams[i].id == (long)ApplicationData.Current.LocalSettings.Values["currentTeamID"])
                        {
                            teammember_id = ruser.teams[i].loggedInUsersmemberID;
                        }
                    }
                    rfptemplate.data[(int)rfptemplate_ht["forum_topic_id"]].value = rftopics.collection.items[0].data[0].value;
                    rfptemplate.data[(int)rfptemplate_ht["member_id"]].value = teammember_id;
                    rfptemplate.data[(int)rfptemplate_ht["message"]].value = Message.Text;
                    rfptemplate.data[(int)rfptemplate_ht["broadcast_to_team"]].value = rfttemplate.data[(int)rfttemplate_ht["is_announcement"]].value;
                    rfptemplate.data[(int)rfptemplate_ht["type"]].value = "forum_post";

                    string rfpresponse = forum_posts.forumpostsTemplateSerializer(rfptemplate);
                    rfpresponse = "{\"template\":" + rfpresponse + "}";
                    string remove = "{\"name\":\"division_member_id\",\"value\":null},";
                    int index = rfpresponse.IndexOf(remove);
                    rfpresponse = (index < 0) ? rfpresponse : rfpresponse.Remove(index, remove.Length);

                    Debug.WriteLine(rfpresponse);
                    await APILibrary.apiPOST((string)ApplicationData.Current.LocalSettings.Values["Tokens"], rfpresponse, rfposts.collection.href);
                    Frame rootFrame = Window.Current.Content as Frame;
                    rootFrame.Navigate(typeof(Forums), teamid);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Exception returned in AddNewForumTopic.xaml.cs : " + ex);
                    Frame rootFrame = Window.Current.Content as Frame;
                    rootFrame.Navigate(typeof(Forums), teamid);
                }
            }
        }