예제 #1
0
        public static bool UserLikes(this Facebook facebook, string userId, string objectId)
        {
            AssertRequireAccessToken(facebook);

            if (userId.Equals("me()", StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException("User ID cannot be me().", userId);
            }

            var result = facebook.Query(
                string.Format("SELECT user_id FROM like WHERE user_id={0} AND object_id={1}",
                              userId, objectId));

            var jsonObj = FacebookUtils.FromJson(result);

            if (jsonObj.Count == 1)
            {
                var users = (IDictionary <string, object>)jsonObj["object"];
                if (users.Count == 1)
                {
                    if (users["user_id"].ToString().Equals(userId, StringComparison.OrdinalIgnoreCase))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
예제 #2
0
        /// <summary>
        /// Writes a wall post to the given profile's wall.
        /// </summary>
        /// <param name="facebook"></param>
        /// <param name="message">The message to put on the wall.</param>
        /// <param name="parameters">Optional parameters for the message.</param>
        /// <param name="profileId">The profile where the message is goin to be put.</param>
        /// <returns>Result of the operation.</returns>
        /// <remarks>
        /// Default to writing to the authenticated user's wall if no
        /// profile_id is specified.
        ///
        /// attachment adds a structured attachment to the status message being
        /// posted to the Wall. It should be a dictionary of the form:
        ///
        ///     {"name": "Link name"
        ///      "link": "http://www.example.com/",
        ///      "caption": "{*actor*} posted a new review",
        ///      "description": "This is a longer description of the attachment",
        ///      "picture": "http://www.example.com/thumbnail.jpg"}
        ///
        /// Incase profileId is null, it will set to me
        /// </remarks>
        public static string PostToWall(this Facebook facebook, string message, IDictionary <string, string> parameters,
                                        string profileId)
        {
            IDictionary <string, string> pars = parameters != null
                                                  ? new Dictionary <string, string>(parameters)
                                                  : new Dictionary <string, string>();

            pars.Add("message", message);

            var result = facebook.PutObject(profileId ?? "me", "feed", pars);

            var jsonObj = FacebookUtils.FromJson(result);

            return(jsonObj["id"].ToString());
        }
예제 #3
0
        public static string CreateEvent(this Facebook facebook, Event @event, IDictionary <string, string> parameters)
        {
            IDictionary <string, string> pars = parameters != null
                                                  ? new Dictionary <string, string>(parameters)
                                                  : new Dictionary <string, string>();

            pars.Add("name", @event.Name);
            pars.Add("location", @event.Location);
            pars.Add("start_time", @event.StartTime);
            pars.Add("end_time", @event.EndTime);

            var result = facebook.Post("/events", pars);

            return(FacebookUtils.FromJson(result)["id"].ToString());
        }
예제 #4
0
        public static string[] UserLikes(this Facebook facebook, string userId, string[] objectIds)
        {
            AssertRequireAccessToken(facebook);

            if (objectIds == null)
            {
                throw new ArgumentNullException("userIds");
            }

            if (userId.Equals("me()", StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException("User ID cannot be me()", "userId");
            }

            if (objectIds.Length == 0)
            {
                return new string[] { }
            }
            ;                            // return empty array of string. no need to query facebook.

            var query = new StringBuilder();

            query.AppendFormat("SELECT object_id FROM like WHERE user_id={0} and (", userId);

            var i = 0;

            foreach (var objectId in objectIds)
            {
                if (i != 0)
                {
                    query.Append(" OR ");
                }

                if (objectId.Equals("me()", StringComparison.OrdinalIgnoreCase))
                {
                    throw new ArgumentException("Object ID cannot contain me()", "objectIds");
                }

                query.AppendFormat("object_id={0}", objectId);

                ++i;
            }

            query.Append(")");

            var result = facebook.Query(query.ToString());

            // json returned by fb in not a valid json, so we need to do some ugly hack before can use FacebookUtils.FromJson
            if (result.StartsWith("["))
            {
                result = result.Substring(1, result.Length - 2);

                var objects = result.Split(',');

                var likedObjects = new List <string>(objectIds.Length);

                foreach (var obj in objects)
                {
                    var jsonObj = FacebookUtils.FromJson(obj);
                    likedObjects.Add(jsonObj["object_id"].ToString());
                }

                return(likedObjects.ToArray());
            }

            return(new string[] { });
        }
예제 #5
0
        /// <summary>
        /// Writes the given comment on the given post.
        /// </summary>
        /// <param name="facebook">The facebook.</param>
        /// <param name="objectId">Id of the object.</param>
        /// <param name="parameters">The parameters.</param>
        /// <returns>Returns the id of the newly posted comment.</returns>
        public static string PostComment(this Facebook facebook, string objectId, IDictionary <string, string> parameters)
        {
            var result = facebook.PutObject(objectId, "comments", parameters);

            return(FacebookUtils.FromJson(result)["id"].ToString());
        }
예제 #6
0
 /// <summary>
 /// Converts Json string to ExpandoObject
 /// </summary>
 /// <param name="json">The json string.</param>
 /// <returns>Expando Object</returns>
 public static ExpandoObject ToExpandoObject(this string json)
 {
     return(FacebookUtils.ToExpandoObject(FacebookUtils.FromJson(json, false)));
 }