Beispiel #1
0
        public int SyncToDatabase(SyncToDatabaseOp Op, CurrentUser currentUser, long UserId)
        {
            logger.Debug("SyncFollowersToDatabase(Op: {0}, CurrentUser: {1}, UserId: {2})", (object) Op, (object) currentUser.Id, (object) UserId);

            FriendsClient friends = new FriendsClient(currentUser.Id);
            WebClientResult<FriendsList> FriendsList;

            if (Op == SyncToDatabaseOp.GetFollowers)
                FriendsList = friends.FollowerIds(UserId.ToString(), null, null);
            else
                FriendsList = friends.FriendIds(UserId.ToString(), null, null);

            using (var db = new ToketeeData().Context)
            {

                // TODO Handle deletes

                // Get the current known followers list from the DB
                // TODO find a way to create an IQueryable when specific columns are returned
                IQueryable<TwitterFriends> qFriendsInDb;
                if (Op == SyncToDatabaseOp.GetFollowers)
                {
                    qFriendsInDb = from f in db.TwitterFriends
                                   where f.FriendId == UserId
                                   select f;
                }
                else
                {
                    qFriendsInDb = from f in db.TwitterFriends
                                   where f.OwnerId == UserId
                                   select f;
                }

                logger.Trace("Known friends in DB for {0} / Count: {1}", UserId.ToString(), qFriendsInDb.Count().ToString());

                var qtest = qFriendsInDb.Where(x => x.FriendId == 13348).Count();
                // Determine which followers are not in the friend tracking table

                //ownerid

                IEnumerable<long> NewFollowers;
                IEnumerable<TwitterFriends> DeletedFollowers;
                if (Op == SyncToDatabaseOp.GetFollowers)
                {
                    // Followers are identified by the OwnerId having a FriendId of the method's UserId
                    NewFollowers = from f in FriendsList.Object
                                   where !(from a in qFriendsInDb
                                           select a.OwnerId).Contains(f)
                                   select f;

                    var tempFriendsList = FriendsList.Object.ToList();
                    DeletedFollowers = from f in qFriendsInDb
                                       where !tempFriendsList.Contains(f.OwnerId)
                                       select f;

                    //DeletedFollowers = from f in qFriendsInDb
                    //                   where !(from a in FriendsList.Object
                    //                           select a).Contains(f.OwnerId)
                    //                   select f;
                }
                else
                {
                    // Friends are identified by the FriendId having the OwnerId of the method's UserId
                    NewFollowers = from f in FriendsList.Object
                                   where !(from a in qFriendsInDb
                                           select a.FriendId).Contains(f)
                                   select f;

                    var tempFriendsList = FriendsList.Object.ToList();
                    DeletedFollowers = from f in qFriendsInDb
                                       where !tempFriendsList.Contains(f.FriendId)
                                       select f;

                    //DeletedFollowers = from f in qFriendsInDb
                    //                   where !(from a in FriendsList.Object
                    //                           select a).Contains(f.FriendId)
                    //                   select f;
                }

                logger.Trace("New followers: {0} / Unfollows: {1}", NewFollowers.Count(), DeletedFollowers.Count());

                var NewFollowersList = new List<long>();
                foreach (long Id in NewFollowers)
                {
                    var tFriends = new TwitterFriends()
                    {
                        OwnerId = (Op == SyncToDatabaseOp.GetFollowers) ? Id : UserId,
                        FriendId = (Op == SyncToDatabaseOp.GetFollowers) ? UserId : Id
                    };
                    db.TwitterFriends.Add(tFriends);
                    NewFollowersList.Add(Id);
                }

                var userOps = new UserOps(currentUser.Id);
                Users users = userOps.Lookup(NewFollowersList);

                string s = string.Join(",", FriendsList.Object.ToArray());
                var qExistingUsers = db.TwitterUser.SqlQuery("select * from TwitterUser where Id in (" + s + ");");

                // for each User in Users, find the corresponding TwitterUser to update

                // Filter out the users that do exist so we can insert the new ones

                var qDontExist = from u in users
                                 where !(from a in qExistingUsers
                                         select a.Id.ToString()).Contains(u.Id)
                                 select u;

                logger.Trace("Users to be inserted: {0}", qDontExist.Count().ToString());

                foreach (TwitterAPI.User u in qDontExist)
                {
                    if (qExistingUsers.Where(x => x.Id.ToString() == u.Id).Count() > 0)
                        logger.Error("User already exists, but shouldn't! {0}", u.Id);

                }

                TwitterUser tUser;

                foreach (TwitterAPI.User u in qDontExist.Distinct())
                {
                    tUser = new TwitterUser();
                    UserOps.UserObjToTwitterUser(u, tUser);
                    db.TwitterUser.Add(tUser);
                    logger.Trace("Inserted TwitterUser: {0}", tUser.Id);
                }

                foreach (TwitterUser tu in qExistingUsers)
                {
                    try
                    {
                        var q2 = (from u in users
                                  where u.Id == tu.Id.ToString()
                                  select u).SingleOrDefault();
                        if (q2 != null) UserOps.UserObjToTwitterUser(q2, tu);
                        logger.Trace("Updated TwitterUser: {0}", tu.Id);
                    }
                    catch (InvalidOperationException e)
                    {
                        string sError = String.Format("UserObjToTwitterUser unable to match existing user TwitterUser {0} to friends list user", tu.Id);
                        logger.ErrorException(sError, e);
                    }
                }

                // Process unfollows

                int iFriendshipsDeleted = 0;
                foreach (TwitterFriends tf in DeletedFollowers)
                {
                    logger.Trace("Deleting friendship: Owner {0} Friend {1}", tf.OwnerId, tf.FriendId);
                    db.TwitterFriends.Remove(tf);
                    iFriendshipsDeleted++;
                }
                if (iFriendshipsDeleted > 0) logger.Debug("Friendships deleted: {0}", iFriendshipsDeleted);
                return db.SaveChanges();
            }
        }
Beispiel #2
0
        public Users Lookup(List<long> UserIdList)
        {
            // Lookup function is limited to 100 id's at a time
            List<string> Lookups = new List<string>();

            for (int i = 0; i < UserIdList.Count(); i += 100)
            {
                var q = (from u in UserIdList
                         select u).Skip(i).Take(100);
                Lookups.Add(string.Join(",", q.ToArray()));
            }

            var user = new UserClient(CurrentUserId);
            var results = new List<WebClientResult<Users>>();
            var list = new Users();
            foreach (string IdSet in Lookups)
            {
                WebClientResult<Users> usersListReturn = user.Lookup(IdSet, null);

                foreach (var u in usersListReturn.Object)
                {
                    list.Add(u);
                }
            }

            return list;
        }