Ejemplo n.º 1
0
        public void UnlikeComment(FacebookComment comment)
        {
            dynamic parameters = new ExpandoObject();

            facebookClient.Delete("/" + comment.Id + "/likes", parameters);
            UpdateNewsFeed();
        }
Ejemplo n.º 2
0
        void backgroundWorkerNewsFeed_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                if (e != null)
                {
                    if (e.Cancel)
                    {
                        return;
                    }
                }
                dynamic parameters = new ExpandoObject();

                if (!IsInitialFetch && LastUpdate != null)
                {
                    // danach kamen keine Updates mehr durch :(
                    //  parameters.since = GetUnixTimestamp(LastUpdate).ToString();
                }

                string[] feeds = { "home", "feed" };

                foreach (string feedName in feeds)
                {
                    dynamic result = facebookClient.Get("/me/" + feedName, parameters); // Home

                    if (result != null)
                    {
                        foreach (dynamic post in result.data)
                        {
                            if (e != null)
                            {
                                if (e.Cancel)
                                {
                                    return;
                                }
                            }
                            FacebookItem item = FacebookItem.ConvertResponseToItem(post, this);
                            if (item == null)
                            {
                                AppController.Current.Logger.writeToLogfile("Null item in facebook retrieval");
                                continue;
                            }

                            FacebookItem ExistingItem = null;
                            try
                            {
                                switch (item.MessageType)
                                {
                                case FacebookItem.MessageTypes.Link:
                                    if (Links.Where(oldItem => oldItem.fullId == item.fullId).Count() > 0)
                                    {
                                        ExistingItem = Links.Where(oldItem => oldItem.fullId == item.fullId).First();
                                    }

                                    break;

                                case FacebookItem.MessageTypes.Video:
                                    if (Videos.Where(oldItem => oldItem.fullId == item.fullId).Count() > 0)
                                    {
                                        ExistingItem = Videos.Where(oldItem => oldItem.fullId == item.fullId).First();
                                    }

                                    break;

                                case FacebookItem.MessageTypes.Photo:
                                    if (Photos.Where(oldItem => oldItem.fullId == item.fullId).Count() > 0)
                                    {
                                        ExistingItem = Photos.Where(oldItem => oldItem.fullId == item.fullId).First();
                                    }

                                    break;

                                case FacebookItem.MessageTypes.Note:
                                    if (Notes.Where(oldItem => oldItem.fullId == item.fullId).Count() > 0)
                                    {
                                        ExistingItem = Notes.Where(oldItem => oldItem.fullId == item.fullId).First();
                                    }

                                    break;

                                case FacebookItem.MessageTypes.Event:
                                    if (Events.Where(oldItem => oldItem.fullId == item.fullId).Count() > 0)
                                    {
                                        ExistingItem = Events.Where(oldItem => oldItem.fullId == item.fullId).First();
                                    }

                                    break;

                                case FacebookItem.MessageTypes.CheckIn:
                                    if (CheckIns.Where(oldItem => oldItem.fullId == item.fullId).Count() > 0)
                                    {
                                        ExistingItem = CheckIns.Where(oldItem => oldItem.fullId == item.fullId).First();
                                    }

                                    break;

                                default:
                                    if (StatusMessages.Where(oldItem => oldItem.fullId == item.fullId).Count() > 0)
                                    {
                                        ExistingItem = StatusMessages.Where(oldItem => oldItem.fullId == item.fullId).First();
                                    }

                                    break;
                                }
                            }
                            catch
                            {
                                ExistingItem = null;
                            }
                            if (ExistingItem == null)
                            {
                                try
                                {
                                    dynamic comments = facebookClient.Get("/" + item.fullId + "/comments", parameters);
                                    item.Comments.Clear();
                                    if (comments != null)
                                    {
                                        if (comments.data != null)
                                        {
                                            AppController.Current.Logger.writeToLogfile("Facebook item has comments/data");
                                            foreach (dynamic fbcomment in comments.data)
                                            {
                                                AppController.Current.Logger.writeToLogfile("Reading comment");
                                                FacebookComment comment = new FacebookComment(fbcomment);
                                                comment.Account = item.Account;
                                                if (comment.User.Id == this.Id.ToString())
                                                {
                                                    item.isCommented = true;
                                                }
                                                item.Comments.Add(comment);
                                            }
                                        }
                                    }
                                }
                                catch { }
                                item.ReceivingAccount = this;
                                backgroundWorkerNewsFeed.ReportProgress(100, item);
                                continue;
                            }
                            else
                            {
                                if (item.LikesCount != ExistingItem.LikesCount)
                                {
                                    item.HasUpdatedLikes = true;
                                }

                                List <FacebookComment> updatedComments = new List <FacebookComment>();
                                foreach (FacebookComment comment in item.Comments)
                                {
                                    if (e != null)
                                    {
                                        if (e.Cancel)
                                        {
                                            return;
                                        }
                                    }
                                    if (ExistingItem.Comments.Where(c => c.Text == comment.Text && c.User.Id == comment.User.Id).Count() == 0)
                                    {
                                        updatedComments.Add(comment);
                                    }
                                }

                                if (updatedComments.Count > 0)
                                {
                                    item.HasUpdatedComments = true;
                                    item.Comments.Clear();
                                    foreach (FacebookComment comment in updatedComments)
                                    {
                                        item.Comments.Add(comment);
                                    }
                                }
                                if (item.HasUpdatedComments || item.HasUpdatedLikes)
                                {
                                    item.ReceivingAccount = this;
                                    backgroundWorkerNewsFeed.ReportProgress(100, item);
                                }
                                else
                                {
                                    item = null;
                                }
                            }
                        }
                    }
                }
            }
            catch (FacebookOAuthException oauthExp)
            {
                AppController.Current.Logger.writeToLogfile(oauthExp);
            }
            catch (Exception exp)
            {
                AppController.Current.Logger.writeToLogfile(exp);;
            }
        }
Ejemplo n.º 3
0
        public static FacebookItem ConvertResponseToItem(dynamic post, AccountFacebook ReceivingAccount)
        {
            try
            {
                FacebookItem item = new FacebookItem();
                item.Account = ReceivingAccount;
                item.fullId  = post.id as string;
                string[] ids = item.fullId.Split(new Char[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
                if (ids.Length < 2)
                {
                    return(null);
                }
                try
                {
                    item.Id = Convert.ToDecimal(ids[1]);
                }
                catch
                {
                    return(null);
                }

                #region Messagetypes

                if (post.type != null)
                {
                    string postType = post.type;
                    switch (postType)
                    {
                    case "photo":
                        item.MessageType = MessageTypes.Photo;
                        break;

                    case "link":
                        item.MessageType = MessageTypes.Link;
                        break;

                    case "event":
                        item.MessageType = MessageTypes.Event;
                        break;

                    case "checkin":
                        item.MessageType = MessageTypes.CheckIn;
                        break;

                    case "status":
                        item.MessageType = MessageTypes.StatusMessage;
                        break;

                    case "video":
                        item.MessageType = MessageTypes.Video;
                        break;

                    case "note":
                        item.MessageType = MessageTypes.Note;
                        break;
                    }
                }

                #endregion

                item.To.FullName = null;

                if (post.to != null)
                {
                    if (post.to.data != null)
                    {
                        foreach (dynamic receiver in post.to.data)
                        {
                            item.To = FacebookUser.CreateFromDynamic(receiver, ReceivingAccount);
                            item.To.ReceivingAccount = ReceivingAccount;
                            // only one receiver for now - should be a collection later on of course
                            break;
                        }
                    }
                }


                DateTime createdDate;
                DateTime.TryParse(post.created_time as string, out createdDate);
                item.CreatedAt = createdDate;

                DateTime updatedDate;
                DateTime.TryParse(post.updated_time as string, out updatedDate);
                item.UpdatedAt = updatedDate;



                //item.User.Id = post.from.id;
                //item.User.FullName = post.from.name;
                item.User = FacebookUser.CreateFromDynamic(post.from, item.Account);
                if (item.User == null)
                {
                    AppController.Current.Logger.writeToLogfile("Null user on item retrieval");
                    return(null);
                }

                if (post.comments != null)
                {
                    AppController.Current.Logger.writeToLogfile("Facebook item has comments");
                    if (post.comments.data != null)
                    {
                        AppController.Current.Logger.writeToLogfile("Facebook item has comments/data");
                        foreach (dynamic fbcomment in post.comments.data)
                        {
                            AppController.Current.Logger.writeToLogfile("Reading comment");
                            FacebookComment comment = new FacebookComment(fbcomment);
                            comment.Account = item.Account;
                            if (comment.Id == ReceivingAccount.Id.ToString())
                            {
                                item.isCommented = true;
                            }
                            item.Comments.Add(comment);
                        }
                    }
                }

                if (post.application != null)
                {
                    item.Application.Name = post.application.name;
                    item.Application.Link = post.application.link;
                    item.Application.Id   = post.application.id;
                }


                if (post.likes != null)
                {
                    if (post.likes.count != null)
                    {
                        item.LikesCount = Convert.ToInt32(post.likes.count);
                    }
                    if (post.likes.data != null)
                    {
                        AppController.Current.Logger.writeToLogfile("Facebook item has likes/data");
                        foreach (dynamic fbLike in post.likes.data)
                        {
                            AppController.Current.Logger.writeToLogfile("Reading like");
                            FacebookUser user = FacebookUser.CreateFromDynamic(fbLike, item.Account);
                            user.ReceivingAccount = item.Account;

                            if (user != null)
                            {
                                item.Likes.Add(user);
                                if (user.FullName == item.Account.FullName)
                                {
                                    item.isLiked = true;
                                }
                            }
                        }
                    }
                }

                if (post.picture != null)
                {
                    FacebookPicture picture = new FacebookPicture();
                    picture.ThumbnailPath = post.picture;
                    picture.Link          = post.link;
                    picture.Caption       = post.caption;
                    item.Picture          = picture.ThumbnailPath;
                    item.PictureLink      = picture.Link;

                    item.Images.Add(picture);
                }

                item.Description = post.description;

                item.Message = post.message;
                item.Name    = post.name;
                item.Caption = post.caption;
                item.Link    = post.link;

                // ganz zum Schluss, das in der Textbox auf diese Aenderung getriggert wird
                item.Text = item.Message;

                return(item);
            }
            catch (Exception exp)
            {
                AppController.Current.Logger.writeToLogfile(exp);
                return(null);
            }
        }