示例#1
0
文件: Group.cs 项目: nrag/yapper
        internal static bool AddGroupMember(User caller, Group group, User member)
        {
            if (group.Owner.Id != caller.Id)
            {
                return false;
            }

            // Create and open the connection in a using block. This
            // ensures that all resources will be closed and disposed
            // when the code exits.
            using (SqlConnection connection = new SqlConnection(Globals.SqlConnectionString))
            {
                connection.Open();
                using (SqlTransaction sqlTransaction = connection.BeginTransaction())
                {
                    // Open the connection in a try/catch block.
                    // Create and execute the DataReader, writing the result
                    // set to the console window.
                    try
                    {
                        using (SqlCommand command = new SqlCommand(Group.InsertGroupMemberCommand, connection, sqlTransaction))
                        {
                            command.Parameters.AddWithValue("@groupId", group.Id);
                            command.Parameters.AddWithValue("@userId", member.Id);

                            command.ExecuteNonQuery();

                            // Remove the user from cache so that
                            // the correct set of group will be loaded
                            // next time
                            UserService.Instance.RemoveUserFromCache(member);
                            UserService.Instance.RemoveUserFromCache(group);

                            sqlTransaction.Commit();

                            return true;
                        }
                    }
                    catch (Exception)
                    {
                        sqlTransaction.Rollback();
                    }
                    finally
                    {
                        connection.Close();
                    }

                    return false;
                }
            }
        }
示例#2
0
        public DAL.Group CreateGroup(DAL.Group newGroup)
        {
            DAL.User sender = this.GetAuthenticatedUser();

            if (newGroup.Members == null)
            {
                throw new Exception("Invalid member list");
            }

            Dictionary <int, DAL.User> uniqueUsers = new Dictionary <int, DAL.User>();

            for (int i = 0; i < newGroup.Members.Count; i++)
            {
                if (string.Compare(DAL.UserService.Instance.GetUserFromId(newGroup.Members[i].Id).PhoneNumber, newGroup.Members[i].PhoneNumber, StringComparison.OrdinalIgnoreCase) != 0)
                {
                    throw new Exception("Invalid user specified");
                }

                uniqueUsers.Add(newGroup.Members[i].Id, newGroup.Members[i]);
            }

            if (!uniqueUsers.ContainsKey(sender.Id))
            {
                uniqueUsers.Add(sender.Id, sender);
            }

            DAL.Group createdGroup = DAL.Group.CreateGroup(sender, newGroup.Name, uniqueUsers.Values.ToList());

            MessageLayer.Message newGroupMessage = new MessageLayer.Message()
            {
                SenderId = sender.Id, RecipientId = createdGroup.Id, MessageFlags = MessageLayer.MessageFlags.GroupCreatedMessage, TextMessage = sender.Name + " has created the group"
            };

            MessageStore.MessageStore.Instance.SaveMessage(sender, newGroupMessage);

            if (newGroupMessage.RecipientId != sender.Id)
            {
                this.SendPushNotifications(
                    DAL.Subscription.GetSubscriptions(newGroupMessage.RecipientId, sender),
                    newGroupMessage.MessageId,
                    newGroupMessage.ConversationId,
                    sender,
                    newGroupMessage.PostDateTimeUtcTicks,
                    newGroupMessage.TextMessage,
                    createdGroup,
                    newGroupMessage.IsEncrypted);
            }
            return(createdGroup);
        }
示例#3
0
 public PushNotification(
     List<Tuple<User, string>> urls,
     Guid messageId,
     Guid conversationId,
     User sender,
     long postDate,
     string message,
     Group group)
 {
     this.Urls = urls;
     this.MessageId = messageId;
     this.ConversationId = conversationId;
     this.Sender = sender;
     this.PostDate = postDate;
     this.MessageId = messageId;
     this.Message = message;
     this.Group = group;
 }
示例#4
0
 /// <summary>
 /// Method to post push notifications for windows phones
 /// </summary>
 /// <param name="urls"></param>
 /// <param name="messageId"></param>
 /// <param name="conversationId"></param>
 /// <param name="senderId"></param>
 /// <param name="postDate"></param>
 /// <param name="message"></param>
 /// <param name="sender"></param>
 private void SendPushNotifications(
     List <Tuple <DAL.User, string> > urls,
     Guid messageId,
     Guid conversationId,
     DAL.User sender,
     long postDate,
     string message,
     DAL.Group group,
     bool isEncrypted)
 {
     PushNotificationQueue.Instance.SchedulePush(
         new PushNotification(
             urls,
             messageId,
             conversationId,
             sender,
             postDate,
             isEncrypted ? "Message content is encrypted! Please open Yapper to view this message." : message,
             group));
 }
示例#5
0
        public bool AddUserToGroup(int groupId, string userPhone)
        {
            DAL.User sender = this.GetAuthenticatedUser();

            DAL.Group group = (DAL.Group)DAL.UserService.Instance.GetUserFromId(groupId);

            DAL.User member = DAL.UserService.Instance.GetUserFromPhone(userPhone);

            bool success = DAL.Group.AddGroupMember(sender, group, member);

            if (!success)
            {
                return(false);
            }

            // Read the group again
            group = (DAL.Group)DAL.UserService.Instance.GetUserFromId(groupId);

            MessageLayer.Message newGroupMessage = new MessageLayer.Message()
            {
                SenderId = sender.Id, RecipientId = group.Id, MessageFlags = MessageLayer.MessageFlags.GroupJoinMessage, TextMessage = sender.Name + " has added " + member.Name + " to the group"
            };

            MessageStore.MessageStore.Instance.SaveMessage(sender, newGroupMessage);

            if (newGroupMessage.RecipientId != sender.Id)
            {
                this.SendPushNotifications(
                    DAL.Subscription.GetSubscriptions(newGroupMessage.RecipientId, sender),
                    newGroupMessage.MessageId,
                    newGroupMessage.ConversationId,
                    sender,
                    newGroupMessage.PostDateTimeUtcTicks,
                    newGroupMessage.TextMessage,
                    group,
                    newGroupMessage.IsEncrypted);
            }

            return(success);
        }
示例#6
0
        private static User CreateUserFromRow(DataRow dataRow)
        {
            if ((UserType)dataRow[4] == UserType.User)
            {
                DateTime LastSyncTime = DateTime.MinValue;

                if (!DBNull.Value.Equals(dataRow[5]))
                {
                    LastSyncTime = (DateTime)dataRow[5];
                }

                byte[] publicKey = null;

                if (!DBNull.Value.Equals(dataRow[6]))
                {
                    publicKey = (byte[])dataRow[6];
                }

                return new User(
                    (int)dataRow[0],
                    (string)dataRow[1],
                    (string)dataRow[2],
                    (string)dataRow[3],
                    LastSyncTime,
                    publicKey,
                    dataRow[8] == DBNull.Value ? string.Empty : (string)dataRow[8],
                    dataRow[8] == DBNull.Value ? 0 : (long)dataRow[9]);
            }
            else
            {
                int groupOwnerId = (int)dataRow[7];
                User groupOwner = UserService.Instance.GetUserFromId(groupOwnerId);
                Group group = new Group((int)dataRow[0], (string)dataRow[2], groupOwner);

                return group;
            }
        }