Exemplo n.º 1
0
 public void addFriendRequest(long friend_id, FriendRelation fr)
 {
     lock (thisLock)
     {
         friend_map.Add(friend_id, fr);
     }
 }
Exemplo n.º 2
0
        public void AddFriend(string userId, List <string> friendIds)
        {
            //以一百个为单位保存
            int count = 0;

            foreach (var friendId in friendIds)
            {
                try
                {
                    FriendRelation addModel = new FriendRelation()
                    {
                        Id         = Guid.NewGuid(),
                        CreateTime = DateTime.Now,
                        UserId     = StringHelper.LimitLength(userId),
                        FriendId   = StringHelper.LimitLength(friendId),
                        IsFocus    = false
                    };
                    db.AddToFriendRelations(addModel);
                    count++;
                    if (count > 100)
                    {
                        db.SaveChanges();
                        count = 0;
                    }
                }
                catch
                {
                    throw new Exception("保存出错");
                }
                finally
                {
                    db.SaveChanges();
                }
            }
        }
 public FriendRelationMenuOptionItem(
     string menu_option_id,
     string link_val,
     string select_action,
     string display_text,
     FriendRelation fvr)
     : base(menu_option_id, link_val, select_action, display_text)
 {
     this.fr = fvr;
 }
Exemplo n.º 4
0
        public IActionResult AddToFriends(int id, string guesses)
        {
            if (GetCurrentUser() == null)
            {
                return(RedirectToAction("Login"));
            }

            var wrapperNames = guesses.Split(",");

            if (wrapperNames.Length > 100)
            {
                return(BadRequest("100 guesses maximum"));
            }

            var friend = Db.Users.Include(u => u.WrapperCollection).FirstOrDefault(x => x.Id == id);

            if (friend == null)
            {
                return(NotFound());
            }

            var rel = Db.FriendRelations.FirstOrDefault(r =>
                                                        r.FromId == GetCurrentUser().Id&& r.ToId == friend.Id);

            if (rel != null)
            {
                return(BadRequest("Already friends!"));
            }

            var cnt = 0;

            foreach (var item in friend.WrapperCollection)
            {
                if (wrapperNames.Contains(item.Name))
                {
                    cnt += 1;
                }
            }

            if (cnt * 2 <= friend.WrapperCollection.Count)
            {
                return(BadRequest("You don't know this user, go away!"));
            }

            var relation = new FriendRelation {
                FromId = GetCurrentUser().Id, ToId = id
            };

            Db.FriendRelations.Add(relation);
            Db.SaveChanges();

            return(RedirectToAction("ViewUser", new { id }));
        }
        public ActionResult AddFriend(string id)
        {
            // Check if two of users are friends, and if they'r not don't allow to add again
            if (CheckIfTwoUsersAreFriends(this.User.Identity.GetUserId(), id))
            {
                return RedirectToAction("Index", new { id = id });
            }

            var user = this.Data.Users
                .All()
                .FirstOrDefault(us => us.Id == id);

            var loggedUserId = this.User.Identity.GetUserId();
            var loggedUser = this.Data.Users
                .All()
                .FirstOrDefault(us => us.Id == loggedUserId);

            var friends = new FriendRelation()
            {
                AreFriends = true,
                FirstUser = user,
                SecondUser = loggedUser
            };

            // Check if users have been already friends, and if they were just manage fields through DeletableEntityRepository
            var allFriendsWithDeleted = this.Data.FriendRelations.AllWithDeleted().FirstOrDefault(fr =>
                                    (fr.FirstUser.Id == loggedUserId && fr.SecondUser.Id == user.Id) ||
                                    (fr.FirstUser.Id == user.Id && fr.SecondUser.Id == loggedUserId));

            if (allFriendsWithDeleted != null)
            {
                allFriendsWithDeleted.IsDeleted = false;
                allFriendsWithDeleted.AreFriends = true;
                allFriendsWithDeleted.DeletedOn = null;
            }
            else
            {
                this.Data.FriendRelations.Add(friends);
            }

            // Remove user from list of applying friends
            loggedUser.ApplyingFriends.Remove(user);
            this.Data.SaveChanges();

            return RedirectToAction("Index", new { id = loggedUserId });
        }
Exemplo n.º 6
0
 public void AddFriend(string userId, string friendId)
 {
     try
     {
         FriendRelation addModel = new FriendRelation()
         {
             Id         = Guid.NewGuid(),
             CreateTime = DateTime.Now,
             UserId     = StringHelper.LimitLength(userId),
             FriendId   = StringHelper.LimitLength(friendId),
             IsFocus    = false
         };
         db.AddToFriendRelations(addModel);
         db.SaveChanges();
     }
     catch
     {
         throw new Exception("数据保存出错");
     }
 }
Exemplo n.º 7
0
 public LinkedList<FriendRelation> getFriendRelationsFromDB(
     int status)
 {
     string sqlQuery =
      "SELECT row_id, id_a, id_b, status, datetime_created, datetime_accepted" +
      " FROM friends WHERE id_a = '" + us.user_profile.id + "' OR id_b ='" + us.user_profile.id + "'";
     if (status != -1)
         sqlQuery = sqlQuery + " AND status='" + status + "'";
     MySqlConnection conn = DBManager.getConnection();
     MySqlDataReader rdr = null;
     try
     {
         conn.Open();
         MySqlCommand cmd = new MySqlCommand(sqlQuery, conn);
         rdr = cmd.ExecuteReader();
         long row_id = -1;
         long id_a = -1;
         long id_b = -1;
         int status_of_relation;
         DateTime datetime_created;
         DateTime datetime_accepted = new DateTime();
         LinkedList<FriendRelation> friend_list = new LinkedList<FriendRelation>();
         String tmp_date = "";
         while (rdr.Read())
         {
             row_id = long.Parse((rdr[0]).ToString());
             id_a = long.Parse((rdr[1]).ToString());
             id_b = long.Parse((rdr[2]).ToString());
             status_of_relation = Int32.Parse(rdr[3].ToString());
             datetime_created = DateTime.Parse(rdr[4].ToString());
             tmp_date = rdr[5].ToString();
             if (tmp_date != null && !"".Equals(tmp_date))
                 datetime_accepted = DateTime.Parse(rdr[5].ToString());
             FriendRelation fr = new FriendRelation(
                 row_id,
                 id_a,
                 id_b,
                 status_of_relation,
                 datetime_created,
                 datetime_accepted);
             friend_list.AddLast(fr);
         }
         return friend_list;
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.StackTrace);
         return null;
     }
     finally
     {
         if (rdr != null)
             rdr.Close();
         conn.Close();
     }
 }
Exemplo n.º 8
0
        /*this stores the last request and is responsible for maintaining synchronisation between memory
        * and DB views.
        */
        public long addFriendRequest(
            long friend_id)
        {
            bool was_blocked_before = false;
            FriendRelation tmp_fr = null;
            if (user_list.ContainsKey(us.user_profile.id))
            {
                FriendMap fmap = user_list[us.user_profile.id];
                if (fmap.hasFriendRelation(friend_id))
                {
                    tmp_fr = fmap.getFriendRelation(friend_id);
                    if (tmp_fr.status == FriendRelation.STATUS_ACCEPTED)
                    {
                        return FRIEND_REQUEST_ALREADY_FRIENDS;//user already has friend relation for this friend.
                    }
                    else if (tmp_fr.status == FriendRelation.STATUS_PENDING)
                    {
                        return FRIEND_REQUEST_ALREADY_REQUESTED;
                    }
                    else if ((us.user_profile.id == tmp_fr.id_a && tmp_fr.status == FriendRelation.STATUS_BLOCKED_B)
                        || us.user_profile.id == tmp_fr.id_b && tmp_fr.status == FriendRelation.STATUS_BLOCKED_A)
                    {
                        return FRIEND_REQUEST_BLOCKED;
                    }
                    else if ((us.user_profile.id == tmp_fr.id_a && tmp_fr.status == FriendRelation.STATUS_BLOCKED_A)
                        || us.user_profile.id == tmp_fr.id_b && tmp_fr.status == FriendRelation.STATUS_BLOCKED_B)
                    {
                        was_blocked_before = true;
                    }
                }
            }
            DateTime datetime = DateTime.Now;
            MyFriendTask mft = new MyFriendTask(us, datetime, friend_id);
            FriendRelation fr = null;
            if (was_blocked_before)
            {
                mft.ApproveFriendRequestDBThreadTask();
                fr = tmp_fr;
                fr.setStatus(FriendRelation.STATUS_ACCEPTED);
                FriendMap fmap = user_list[us.user_profile.id];
                if (fmap != null && fmap.hasFriendRelation(friend_id))
                {
                        fmap.removeFriendRelation(friend_id);
                }
            }
            else
            {
                mft.AddFriendRequestDBThreadTask();
                fr = new FriendRelation(
                    -1,
                    us.user_profile.id,
                    friend_id,
                    FriendRelation.STATUS_PENDING,
                    datetime,
                    new DateTime());

            }

            if (!user_list.ContainsKey(us.user_profile.id))
            {
                user_list.Add(us.user_profile.id, new FriendMap(us));
            }

            FriendMap friend_map = user_list[us.user_profile.id];
            friend_map.addFriendRequest(friend_id, fr);//update status in memory.

            //if the friend entry does not exist, it means the friend is not online then we leave the in memory loading to happen only when user comes online (lazy loading).
            if (user_list.ContainsKey(friend_id))
            {
                //if was blocked before there is an entry before. so remove
                if (was_blocked_before)
                {
                    FriendMap tmp_fm = (user_list[friend_id]);
                    if (tmp_fm != null && tmp_fm.hasFriendRelation(us.user_profile.id))
                    {
                        tmp_fm.removeFriendRelation(us.user_profile.id);
                    }
                }
                (user_list[friend_id]).addFriendRequest(us.user_profile.id, fr);//update status in memory.
            }

            if (was_blocked_before)
                return FRIEND_REQUEST_BLOCKED_APPROVED;

            return 0;
        }
        public JsonResult CreateFriendRelation([DataSourceRequest] 
                                        DataSourceRequest request, FriendRelationGridViewModel friendRelation)
        {
            if (friendRelation == null || !ModelState.IsValid)
            {
                return Json(new[] { friendRelation }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
            }

            var newFriendRelation = new FriendRelation
            {
                AreFriends = true,
                FirstUserId = friendRelation.FirstUserId,
                SecondUserId = friendRelation.SecondUserId,
                CreatedOn = DateTime.Now
            };

            this.Data.FriendRelations.Add(newFriendRelation);
            this.Data.SaveChanges();
            friendRelation.Id = newFriendRelation.Id;
            friendRelation.CreatedOn = newFriendRelation.CreatedOn;

            return Json(new[] { friendRelation }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
        }