Exemple #1
0
        private ActivityPostAttachment _DeserializeVideoPostAttachmentData(ActivityPost post, JSON_OBJECT jsonAttachment)
        {
            if (jsonAttachment == null || jsonAttachment.Count == 0)
            {
                return(null);
            }

            ActivityPostAttachment attachment = _DeserializeGenericPostAttachmentData(post, jsonAttachment);

            attachment.Type = ActivityPostAttachmentType.Video;

            var jsonMediaArray = jsonAttachment["media"] as JSON_ARRAY;

            if (jsonMediaArray != null && jsonMediaArray.Count > 0)
            {
                var jsonStreamMediaObject = jsonMediaArray[0] as JSON_OBJECT;
                Uri previewImageUri       = _SafeGetUri(jsonStreamMediaObject, "src");

                attachment.VideoPreviewImage = new FacebookImage(_service, previewImageUri);
                attachment.VideoSource       = _SafeGetUri(jsonStreamMediaObject, "href");
                // Not using this one because of a bug in Adobe's player when loading in an external browser...
                //XElement videoElement = streamElement.Element("video");
                //if (videoElement != null)
                //{
                //    attachment.VideoSource = _SafeGetUri(videoElement, "source_url");
                //}
            }

            return(attachment);
        }
Exemple #2
0
        private ActivityPostAttachment _DeserializePhotoPostAttachmentData(ActivityPost post, JSON_OBJECT jsonAttachment)
        {
            if (jsonAttachment == null || jsonAttachment.Count == 0)
            {
                return(null);
            }

            ActivityPostAttachment attachment = _DeserializeGenericPostAttachmentData(post, jsonAttachment);

            attachment.Type = ActivityPostAttachmentType.Photos;

            var photosEnum = from JSON_OBJECT jsonMediaObject in jsonAttachment["media"] as JSON_ARRAY
                             let photoElement = jsonMediaObject["photo"] as JSON_OBJECT
                                                where photoElement != null
                                                let link = _SafeGetUri(jsonMediaObject, "href")
                                                           select new FacebookPhoto(
                _service,
                _SafeGetId(photoElement, "aid"),
                _SafeGetId(photoElement, "pid"),
                _SafeGetUri(jsonMediaObject, "src"))
            {
                Link    = link,
                OwnerId = _SafeGetId(photoElement, "owner"),
            };

            attachment.Photos = FacebookPhotoCollection.CreateStaticCollection(photosEnum);

            return(attachment);
        }
Exemple #3
0
        public string[] GetWordsFromPost(ActivityPost post)
        {
            if (post == null)
            {
                return(null);
            }

            return(GetWords(post.Message, (post.Actor != null) ? post.Actor.Name : ""));
        }
Exemple #4
0
        public List <ActivityComment> GetComments(ActivityPost post)
        {
            Assert.IsNotNull(post);

            var commentMap = new METHOD_MAP
            {
                { "method", "stream.getComments" },
                { "post_id", post.PostId.ToString() },
            };

            string response = Utility.FailableFunction(10, () => _SendRequest(commentMap));

            return(_jsonSerializer.DeserializeCommentsDataList(post, response));
        }
Exemple #5
0
        public FacebookObjectId AddComment(ActivityPost post, string comment)
        {
            var commentMap = new METHOD_MAP
            {
                { "method", "stream.addComment" },
                { "post_id", post.PostId.ToString() },
                { "comment", comment },
            };

            string result = Utility.FailableFunction(() => _SendRequest(commentMap));

            // retrieve the new comment Id.
            return(_jsonSerializer.DeserializeAddCommentResponse(result));
        }
Exemple #6
0
        private ActivityPostAttachment _DeserializeGenericPostAttachmentData(ActivityPost post, JSON_OBJECT jsonAttachment)
        {
            Assert.IsNotNull(post);
            Uri iconUri = _SafeGetUri(jsonAttachment, "icon");

            return(new ActivityPostAttachment(post)
            {
                Caption = _SafeGetString(jsonAttachment, "caption"),
                Link = _SafeGetUri(jsonAttachment, "href"),
                Name = _SafeGetString(jsonAttachment, "name"),
                Description = _SafeGetString(jsonAttachment, "description"),
                Properties = _SafeGetJson(jsonAttachment, "properties"),
                Icon = new FacebookImage(_service, iconUri),
            });
        }
Exemple #7
0
        private ActivityPostAttachment _DeserializeLinkPostAttachmentData(ActivityPost post, JSON_OBJECT jsonAttachment)
        {
            if (jsonAttachment == null || jsonAttachment.Count == 0)
            {
                return(null);
            }

            ActivityPostAttachment attachment = _DeserializeGenericPostAttachmentData(post, jsonAttachment);

            attachment.Type = ActivityPostAttachmentType.Links;

            var linksEnum = from JSON_OBJECT jsonMediaObject in jsonAttachment.Get <JSON_ARRAY>("media")
                            let srcUri = _SafeGetUri(jsonMediaObject, "src")
                                         let hrefUri = _SafeGetUri(jsonMediaObject, "href")
                                                       where srcUri != null && hrefUri != null
                                                       select new FacebookImageLink
            {
                Image = new FacebookImage(_service, srcUri),
                Link  = hrefUri,
            };

            attachment.Links = new FacebookCollection <FacebookImageLink>(linksEnum);
            return(attachment);
        }
Exemple #8
0
 public ActivityPostAttachment(ActivityPost post)
 {
     Verify.IsNotNull(post, "post");
     Post = post;
 }
Exemple #9
0
        public object[] GetRelevantFacebookObjects(object o)
        {
            List <object> objects = new List <object>();

            FacebookContact contact = o as FacebookContact;

            if (contact != null)
            {
                foreach (var post in _service.RawNewsFeed)
                {
                    if (post.ActorUserId == contact.UserId)
                    {
                        objects.Add(post);
                    }

                    foreach (var comment in post.Comments)
                    {
                        if (comment.FromUserId == contact.UserId)
                        {
                            objects.Add(comment);
                        }
                    }
                }

                foreach (var album in _service.RawPhotoAlbums)
                {
                    if (album.OwnerId == contact.UserId)
                    {
                        objects.Add(album);
                    }

                    foreach (var photo in album.Photos)
                    {
                        if (photo.OwnerId == contact.UserId)
                        {
                            objects.Add(photo);
                            continue;
                        }

                        foreach (var tag in photo.Tags)
                        {
                            if (tag.Contact != null && tag.Contact.UserId == contact.UserId)
                            {
                                objects.Add(photo);
                                goto next;
                            }
                        }

                        next :;
                    }
                }
            }

            FacebookPhoto p = o as FacebookPhoto;

            if (p != null)
            {
                objects.Add(p.Album);

                if (p.Album.Owner != null)
                {
                    objects.Add(p.Album.Owner);
                }

                foreach (var tag in p.Tags)
                {
                    if (tag.Contact != null && !objects.Contains(tag.Contact))
                    {
                        objects.Add(tag.Contact);
                    }
                }
            }

            FacebookPhotoAlbum a = o as FacebookPhotoAlbum;

            if (a != null)
            {
                if (a.Owner != null)
                {
                    objects.Add(a.Owner);
                }

                foreach (var photo in a.Photos)
                {
                    objects.Add(photo);

                    foreach (var tag in photo.Tags)
                    {
                        if (tag.Contact != null && !objects.Contains(tag.Contact))
                        {
                            objects.Add(tag.Contact);
                        }
                    }
                }
            }

            ActivityPost activityPost = o as ActivityPost;

            if (activityPost != null)
            {
                if (activityPost.Actor != null)
                {
                    objects.Add(activityPost.Actor);
                }

                if (activityPost.Target != null)
                {
                    objects.Add(activityPost.Target);
                }

                foreach (var comment in activityPost.Comments)
                {
                    objects.Add(comment);
                }
            }

            ActivityComment activityComment = o as ActivityComment;

            if (activityComment != null)
            {
                if (activityComment.FromUser != null)
                {
                    objects.Add(activityComment.FromUser);
                }
            }

            objects.Sort(new SearchResultsComparer());
            return(objects.ToArray());
        }
Exemple #10
0
        private ActivityPost _DeserializePost(JSON_OBJECT jsonPost)
        {
            var post = new ActivityPost(_service);

            JSON_OBJECT attachmentObject;

            if (jsonPost.TryGetTypedValue("attachment", out attachmentObject))
            {
                string postType = null;

                var jsonMediaArray = (JSON_ARRAY)_SafeGetValue(jsonPost, "attachment", "media");
                if (jsonMediaArray != null && jsonMediaArray.Count > 0)
                {
                    postType = _SafeGetString((JSON_OBJECT)jsonMediaArray[0], "type");
                }

                switch (postType)
                {
                case "photo":
                    post.Attachment = _DeserializePhotoPostAttachmentData(post, attachmentObject);
                    break;

                case "link":
                    post.Attachment = _DeserializeLinkPostAttachmentData(post, attachmentObject);
                    break;

                case "video":
                    post.Attachment = _DeserializeVideoPostAttachmentData(post, attachmentObject);
                    break;

                // We're not currently supporting music or flash.  Just treat it like a normal post...
                case "music":
                case "swf":

                case "":
                case null:
                    if (attachmentObject.Count != 0)
                    {
                        // We have attachment information but no rich stream-media associated with it.
                        ActivityPostAttachment attachment = _DeserializeGenericPostAttachmentData(post, attachmentObject);
                        if (!attachment.IsEmpty)
                        {
                            attachment.Type = ActivityPostAttachmentType.Simple;
                            post.Attachment = attachment;
                        }
                    }
                    break;

                default:
                    Assert.Fail("Unknown type:" + postType);
                    break;
                }
            }

            post.PostId = _SafeGetId(jsonPost, "post_id");
            if (!FacebookObjectId.IsValid(post.PostId))
            {
                // Massive Facebook failure.
                // This happens too frequently for the assert to be useful.
                // Assert.Fail();
                post.PostId = SafeGetUniqueId();
            }
            post.ActorUserId  = _SafeGetId(jsonPost, "actor_id");
            post.Created      = _SafeGetDateTime(jsonPost, "created_time") ?? _UnixEpochTime;
            post.Message      = _SafeGetString(jsonPost, "message");
            post.TargetUserId = _SafeGetId(jsonPost, "target_id");
            post.Updated      = _SafeGetDateTime(jsonPost, "updated_time") ?? _UnixEpochTime;

            JSON_OBJECT likesElement;

            if (jsonPost.TryGetTypedValue("likes", out likesElement))
            {
                post.CanLike    = _SafeGetBoolean(likesElement, "can_like") ?? false;
                post.HasLiked   = _SafeGetBoolean(likesElement, "user_likes") ?? false;
                post.LikedCount = _SafeGetInt32(likesElement, "count") ?? 0;
                post.LikeUri    = _SafeGetUri(likesElement, "likes", "href");
                //XElement friendsElement = likesElement.Element("friends");
                //XElement sampleElement = likesElement.Element("sample");
                //post.SetPeopleWhoLikeThisIds(
                //    Enumerable.Union(
                //        sampleElement == null
                //            ? new FacebookObjectId[0]
                //            : from uidElement in sampleElement.Elements("uid") select new FacebookObjectId(uidElement.Value),
                //        friendsElement == null
                //            ? new FacebookObjectId[0]
                //            : from uidElement in friendsElement.Elements("uid") select new FacebookObjectId(uidElement.Value)));
            }

            JSON_OBJECT jsonComments;

            jsonPost.TryGetTypedValue("comments", out jsonComments);

            post.CanComment        = _SafeGetBoolean(jsonComments, "can_post") ?? false;
            post.CanRemoveComments = _SafeGetBoolean(jsonComments, "can_remove") ?? false;
            post.CommentCount      = _SafeGetInt32(jsonComments, "count") ?? 0;

            if (jsonComments != null && post.CommentCount != 0)
            {
                JSON_ARRAY jsonCommentList;
                if (jsonComments.TryGetTypedValue("comment_list", out jsonCommentList))
                {
                    var commentNodes = from JSON_OBJECT jsonComment in jsonCommentList
                                       let comment = _DeserializeComment(jsonComment)
                                                     where (comment.Post = post) != null
                                                     select comment;

                    post.RawComments = new FBMergeableCollection <ActivityComment>(commentNodes);
                }
            }

            if (post.RawComments == null)
            {
                post.RawComments = new FBMergeableCollection <ActivityComment>();
            }

            // post.Comments = null;

            return(post);
        }
Exemple #11
0
        public List <ActivityComment> DeserializeCommentsDataList(ActivityPost post, string jsonInput)
        {
            JSON_ARRAY jsonComments = SafeParseArray(jsonInput);

            return(new List <ActivityComment>(from JSON_OBJECT jsonComment in jsonComments select _DeserializeComment(jsonComment)));
        }