Exemplo n.º 1
0
 internal async Task <JToken> graphql_request(GraphQL query)
 {
     /*
      * Shorthand for `graphql_requests(query)[0]`
      * :raises: Exception if request failed
      */
     return((await this.graphql_requests(new[] { query }.ToList()))[0]);
 }
Exemplo n.º 2
0
        internal async Task <JToken> _forcedFetch(string mid)
        {
            var param = new Dictionary <string, object>()
            {
                { "thread_and_message_id", new Dictionary <string, object>()
                  {
                      { "thread_id", this.uid }, { "message_id", mid }
                  } }
            };

            return(await this.session.graphql_request(GraphQL.from_doc_id("1768656253222505", param)));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get thread list of your facebook account
        /// </summary>
        /// <param name="limit">Max.number of threads to retrieve. Capped at 20</param>
        /// <param name="thread_location">models.ThreadLocation: INBOX, PENDING, ARCHIVED or OTHER</param>
        /// <param name="before">A unix timestamp, indicating from which point to retrieve messages</param>
        public async Task <List <FB_Thread> > fetchThreadList(int limit = 20, string thread_location = ThreadLocation.INBOX, string before = null)
        {
            /*
             * Get thread list of your facebook account
             * :param limit: Max.number of threads to retrieve.Capped at 20
             * :param thread_location: models.ThreadLocation: INBOX, PENDING, ARCHIVED or OTHER
             * :param before: A timestamp (in milliseconds), indicating from which point to retrieve threads
             * :type limit: int
             * :return: `models.Thread` objects
             * :rtype: list
             * :raises: Exception if request failed
             */

            if (limit > 20 || limit < 1)
            {
                throw new FBchatUserError("`limit` should be between 1 and 20");
            }

            var dict = new Dictionary <string, object>()
            {
                { "limit", limit },
                { "tags", new string[] { thread_location } },
                { "before", before },
                { "includeDeliveryReceipts", true },
                { "includeSeqID", false }
            };

            var j = await this._session.graphql_request(GraphQL.from_doc_id(doc_id: "1349387578499440", param: dict));

            var rtn = new List <FB_Thread>();

            foreach (var node in j.get("viewer")?.get("message_threads")?.get("nodes"))
            {
                var _type = node.get("thread_type")?.Value <string>();
                if (_type == "GROUP")
                {
                    rtn.Add(FB_Group._from_graphql(_session, node));
                }
                else if (_type == "ONE_TO_ONE")
                {
                    rtn.Add(FB_User._from_thread_fetch(_session, node));
                }
                else if (_type == "MARKETPLACE")
                {
                    rtn.Add(FB_Marketplace._from_graphql(_session, node));
                }
                else
                {
                    throw new FBchatException(string.Format("Unknown thread type: {0}", _type));
                }
            }
            return(rtn);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Find and get a thread by its name
        /// </summary>
        /// <param name="name">Name of the thread</param>
        /// <param name="limit">The max. amount of threads to fetch</param>
        /// <returns>`FB_User`, `FB_Group` and `FB_Page` objects, ordered by relevance</returns>
        public async Task <List <FB_Thread> > searchThreads(string name, int limit = 1)
        {
            /*
             * Find and get a thread by its name
             * :param name: Name of the thread
             * :param limit: The max. amount of groups to fetch
             * : return: `User`, `Group` and `Page` objects, ordered by relevance
             * :rtype: list
             * :raises: FBchatException if request failed
             * */

            var param = new Dictionary <string, object>()
            {
                { "search", name }, { "limit", limit.ToString() }
            };
            var j = await this._session.graphql_request(GraphQL.from_query(GraphQL.SEARCH_THREAD, param));

            List <FB_Thread> rtn = new List <FB_Thread>();

            foreach (var node in j[name]?.get("threads")?.get("nodes"))
            {
                if (node.get("__typename").Value <string>().Equals("User"))
                {
                    rtn.Add(FB_User._from_graphql(_session, node));
                }
                else if (node.get("__typename").Value <string>().Equals("MessageThread"))
                {
                    // MessageThread => Group thread
                    rtn.Add(FB_Group._from_graphql(_session, node));
                }
                else if (node.get("__typename").Value <string>().Equals("Page"))
                {
                    rtn.Add(FB_Page._from_graphql(_session, node));
                }
                else if (node.get("__typename").Value <string>().Equals("Group"))
                {
                    // We don"t handle Facebook "Groups"
                    continue;
                }
                else
                {
                    Debug.WriteLine(string.Format("Unknown __typename: {0} in {1}", node.get("__typename").Value <string>(), node));
                }
            }

            return(rtn);
        }
Exemplo n.º 5
0
        internal async Task <List <JToken> > graphql_requests(List <GraphQL> queries)
        {
            /*
             * :param queries: Zero or more dictionaries
             * :type queries: dict
             * : raises: FBchatException if request failed
             * : return: A tuple containing json graphql queries
             * :rtype: tuple
             * */
            var data = new Dictionary <string, object>()
            {
                { "method", "GET" },
                { "response_format", "json" },
                { "queries", GraphQL.queries_to_json(queries) }
            };

            return((List <JToken>) await this._post("/api/graphqlbatch/", data, as_graphql : true));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Find and get page by its name
        /// </summary>
        /// <param name="name">Name of the page</param>
        /// <param name="limit">The max. amount of pages to fetch</param>
        /// <returns>`FB_Page` objects, ordered by relevance</returns>
        public async Task <List <FB_Page> > searchPages(string name, int limit = 1)
        {
            /*
             * Find and get page by its name
             * : param name: Name of the page
             * :return: `Page` objects, ordered by relevance
             * :rtype: list
             * :raises: FBchatException if request failed
             * */

            var param = new Dictionary <string, object>()
            {
                { "search", name }, { "limit", limit.ToString() }
            };
            var j = await this._session.graphql_request(GraphQL.from_query(GraphQL.SEARCH_PAGE, param));

            return(j[name]?.get("pages")?.get("nodes").Select(node => FB_Page._from_graphql(_session, node)).ToList());
        }
Exemplo n.º 7
0
        /// <summary>
        /// Find and get group thread by its name
        /// </summary>
        /// <param name="name">Name of the group</param>
        /// <param name="limit">The max. amount of groups to fetch</param>
        /// <returns>`FB_Group` objects, ordered by relevance</returns>
        public async Task <List <FB_Group> > searchGroups(string name, int limit = 1)
        {
            /*
             * Find and get group thread by its name
             * :param name: Name of the group thread
             * :param limit: The max. amount of groups to fetch
             * :return: `Group` objects, ordered by relevance
             * :rtype: list
             * :raises: FBchatException if request failed
             * */

            var param = new Dictionary <string, object>()
            {
                { "search", name }, { "limit", limit.ToString() }
            };
            var j = await this._session.graphql_request(GraphQL.from_query(GraphQL.SEARCH_GROUP, param));

            return(j.get("viewer")?.get("groups")?.get("nodes").Select(node => FB_Group._from_graphql(_session, node)).ToList());
        }
Exemplo n.º 8
0
        /// <summary>
        /// Find and get user by his/her name
        /// </summary>
        /// <param name="name">Name of the user</param>
        /// <param name="limit">The max. amount of users to fetch</param>
        /// <returns>`FB_User` objects, ordered by relevance</returns>
        public async Task <List <FB_User> > searchUsers(string name, int limit = 10)
        {
            /*
             * Find and get user by his/ her name
             * : param name: Name of the user
             * :param limit: The max. amount of users to fetch
             * : return: `User` objects, ordered by relevance
             * :rtype: list
             * :raises: FBchatException if request failed
             * */

            var param = new Dictionary <string, object>()
            {
                { "search", name }, { "limit", limit.ToString() }
            };
            var j = await this._session.graphql_request(GraphQL.from_query(GraphQL.SEARCH_USER, param));

            return(j[name]?.get("users")?.get("nodes").Select(node => FB_User._from_graphql(_session, node)).ToList());
        }
Exemplo n.º 9
0
        private async Task _usersApproval(List <string> user_ids, bool approve)
        {
            var uuser_ids = Utils.require_list <string>(user_ids).ToList();

            var data = new Dictionary <string, object>()
            {
                { "client_mutation_id", "0" },
                { "actor_id", this.session.user.uid },
                { "thread_fbid", this.uid },
                { "user_ids", user_ids },
                { "response", approve ? "ACCEPT" : "DENY" },
                { "surface", "ADMIN_MODEL_APPROVAL_CENTER" }
            };
            var j = await this.session.graphql_request(
                GraphQL.from_doc_id("1574519202665847", new Dictionary <string, object>()
            {
                { "data", data }
            })
                );
        }
Exemplo n.º 10
0
        private async Task <int> _fetch_mqtt_sequence_id()
        {
            // Get the sync sequence ID used for the /messenger_sync_create_queue call later.
            // This is the same request as fetch_thread_list, but with includeSeqID=true
            var j = await this._session.graphql_request(GraphQL.from_doc_id("1349387578499440", new Dictionary <string, object> {
                { "limit", 0 },
                { "tags", new string[] { ThreadLocation.INBOX } },
                { "before", null },
                { "includeDeliveryReceipts", false },
                { "includeSeqID", true },
            }));

            var sequence_id = j.get("viewer")?.get("message_threads")?.get("sync_sequence_id")?.Value <int>();

            if (sequence_id == null)
            {
                throw new FBchatException("Could not fetch sequence id");
            }
            return((int)sequence_id);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Get threads' info from IDs, unordered
        /// </summary>
        /// <param name="thread_ids">One or more thread ID(s) to query</param>
        /// <returns>A dictionary of FB_Thread objects, labeled by their ID</returns>
        public async Task <Dictionary <string, FB_Thread> > fetchThreadInfo(List <string> thread_ids)
        {
            /*
             * Get threads" info from IDs, unordered
             * ..warning::
             * Sends two requests if users or pages are present, to fetch all available info!
             * :param thread_ids: One or more thread ID(s) to query
             * :return: `models.Thread` objects, labeled by their ID
             * :rtype: dict
             * :raises: Exception if request failed
             */

            var queries = new List <GraphQL>();

            foreach (var thread_id in thread_ids)
            {
                queries.Add(GraphQL.from_doc_id(doc_id: "2147762685294928", param: new Dictionary <string, object>()
                {
                    { "id", thread_id },
                    { "message_limit", 0.ToString() },
                    { "load_messages", false.ToString() },
                    { "load_read_receipts", false.ToString() },
                    { "before", null }
                }));
            }

            var j = await this._session.graphql_requests(queries);

            foreach (var obj in j.Select((x, index) => new { entry = x, i = index }))
            {
                if (obj.entry.get("message_thread") == null)
                {
                    // If you don't have an existing thread with this person, attempt to retrieve user data anyways
                    j[obj.i]["message_thread"] = new JObject(
                        new JProperty("thread_key",
                                      new JObject(
                                          new JProperty("other_user_id", thread_ids[obj.i]))),
                        new JProperty("thread_type", "ONE_TO_ONE"));
                }
            }

            var pages_and_user_ids = j.Where(k => k.get("message_thread")?.get("thread_type")?.Value <string>()?.Equals("ONE_TO_ONE") ?? false)
                                     .Select(k => k.get("message_thread")?.get("thread_key")?.get("other_user_id")?.Value <string>());
            JObject pages_and_users = null;

            if (pages_and_user_ids.Count() != 0)
            {
                pages_and_users = await this._fetchInfo(pages_and_user_ids.ToList());
            }

            var rtn = new Dictionary <string, FB_Thread>();

            foreach (var obj in j.Select((x, index) => new { entry = x, i = index }))
            {
                var entry = obj.entry.get("message_thread");
                if (entry.get("thread_type")?.Value <string>()?.Equals("GROUP") ?? false)
                {
                    var _id = entry.get("thread_key")?.get("thread_fbid").Value <string>();
                    rtn[_id] = FB_Group._from_graphql(_session, entry);
                }
                if (entry.get("thread_type")?.Value <string>()?.Equals("MARKETPLACE") ?? false)
                {
                    var _id = entry.get("thread_key")?.get("thread_fbid").Value <string>();
                    rtn[_id] = FB_Marketplace._from_graphql(_session, entry);
                }
                else if (entry.get("thread_type")?.Value <string>()?.Equals("ONE_TO_ONE") ?? false)
                {
                    var _id = entry.get("thread_key")?.get("other_user_id")?.Value <string>();
                    if (pages_and_users[_id] == null)
                    {
                        throw new FBchatException(string.Format("Could not fetch thread {0}", _id));
                    }
                    foreach (var elem in pages_and_users[_id])
                    {
                        entry[((JProperty)elem).Name] = ((JProperty)elem).Value;
                    }
                    if (entry.get("first_name") != null)
                    {
                        rtn[_id] = FB_User._from_graphql(_session, entry);
                    }
                    else
                    {
                        rtn[_id] = FB_Page._from_graphql(_session, entry);
                    }
                }
                else
                {
                    throw new FBchatException(string.Format("{0} had an unknown thread type: {1}", thread_ids[obj.i], entry));
                }
            }

            return(rtn);
        }