Esempio n. 1
0
        public HttpResponseMessage CreateStatus([FromBody] Status status)
        {
            if (status != null)
            {
                var account    = db.ACCOUNTs.SingleOrDefault(x => x.UserName == status.UserName);
                var conference = db.CONFERENCEs.SingleOrDefault(x => x.CONFERENCE_ID == status.CONFERENCE_ID);
                if (account == null)
                {
                    return(ResponseFail(StringResource.Account_does_not_exist));
                }
                else if (conference == null)
                {
                    return(ResponseFail(StringResource.Conference_do_not_exist));
                }
                else
                {
                    var messageFeed = new MESSAGE_FEED();
                    var result      = db.MESSAGE_FEED.ToList();
                    if (result.Count > 0)
                    {
                        messageFeed.MESSAGE_FEED_ID = result.Last().MESSAGE_FEED_ID + 1;
                    }
                    else
                    {
                        messageFeed.MESSAGE_FEED_ID = 0;
                    }
                    messageFeed.UserName = status.UserName;
                    messageFeed.REPLYING_TO_MESSAGE_FEED_ID = status.REPLYING_TO_MESSAGE_FEED_ID >= 0 ? status.REPLYING_TO_MESSAGE_FEED_ID : null;
                    messageFeed.FROM_DATE                 = DateTime.Now;
                    messageFeed.CURRENT_LAST_NAME         = account.CURRENT_LAST_NAME;
                    messageFeed.CURRENT_FIRST_NAME        = account.CURRENT_FIRST_NAME;
                    messageFeed.CURRENT_MIDDLE_NAME       = account.CURRENT_MIDDLE_NAME;
                    messageFeed.CONFERENCE_ID             = status.CONFERENCE_ID;
                    messageFeed.CONFERENCE_NAME           = conference.CONFERENCE_NAME;
                    messageFeed.CONFERENCE_NAME_EN        = conference.CONFERENCE_NAME_EN;
                    messageFeed.PUBLIC_OR_PRIVATE_MESSAGE = "PUBLIC";
                    messageFeed.MESSAGE_CONTENT           = status.MESSAGE_CONTENT;
                    messageFeed.ATTACHED_PHOTO_FILENAME   = status.ATTACHED_PHOTO_FILENAME;
                    messageFeed.ATTACHED_PHOTO_NOTE       = status.ATTACHED_PHOTO_NOTE;
                    messageFeed.ATTACHED_PHOTO_LONGITUDE  = status.ATTACHED_PHOTO_LONGITUDE;
                    messageFeed.ATTACHED_PHOTO_LATITUDE   = status.ATTACHED_PHOTO_LATITUDE;
                    db.MESSAGE_FEED.Add(messageFeed);
                    db.SaveChanges();

                    status.CURRENT_LAST_NAME   = account.CURRENT_LAST_NAME;
                    status.CURRENT_FIRST_NAME  = account.CURRENT_FIRST_NAME;
                    status.CURRENT_MIDDLE_NAME = account.CURRENT_MIDDLE_NAME;
                    status.MESSAGE_FEED_ID     = messageFeed.MESSAGE_FEED_ID;
                    status.Image = account.Image;
                    status.Time  = DateTimeFormater.GetTimeAgo(messageFeed.FROM_DATE.Value);

                    return(ResponseSuccess(StringResource.Success, status));
                }
            }
            else
            {
                return(ResponseFail(StringResource.Data_not_received));
            }
        }
Esempio n. 2
0
 public bool updateMessageFeed(MESSAGE_FEED messageFeed)
 {
     try
     {
         var result = db.MESSAGE_FEED.SingleOrDefault(x => x.MESSAGE_FEED_ID == messageFeed.MESSAGE_FEED_ID);
         result.DELETED_UserName = messageFeed.DELETED_UserName;
         result.DELETED_DATETIME = messageFeed.DELETED_DATETIME;
         result.DELETED          = messageFeed.DELETED;
         db.SaveChanges();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Esempio n. 3
0
 public bool addNewFeed(MESSAGE_FEED messageFeed)
 {
     try
     {
         var result = db.MESSAGE_FEED.ToList();
         if (result.Count > 0)
         {
             messageFeed.MESSAGE_FEED_ID = result.Last().MESSAGE_FEED_ID + 1;
         }
         else
         {
             messageFeed.MESSAGE_FEED_ID = 0;
         }
         db.MESSAGE_FEED.Add(messageFeed);
         db.SaveChanges();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Esempio n. 4
0
        public HttpResponseMessage SendMessageGroup([FromBody] ChatGroup chat)
        {
            var accountSend = db.ACCOUNTs.SingleOrDefault(x => x.UserName == chat.data.UserName);
            var group       = db.MESSAGING_GROUP.SingleOrDefault(x => x.MESSAGING_GROUP_ID == chat.data.MESSAGING_GROUP_ID);
            var conference  = db.CONFERENCEs.SingleOrDefault(x => x.CONFERENCE_ID == chat.data.CONFERENCE_ID);

            if (accountSend == null)
            {
                return(ResponseFail(StringResource.Account_does_not_exist));
            }
            else if (group == null)
            {
                return(ResponseFail(StringResource.Message_group_do_not_exist));
            }
            else if (conference == null)
            {
                return(ResponseFail(StringResource.Conference_do_not_exist));
            }
            else
            {
                var listMember = db.ACCOUNT_MESSAGING_GROUP_MEMBERSHIP.Where(x => x.MESSAGING_GROUP_ID == chat.data.MESSAGING_GROUP_ID &&
                                                                             (x.GROUP_ASSIGNED_OR_GROUP_JOIN_REQUEST_OR_GROUP_JOIN_REQUEST_APPROVED == "GROUP ASSIGNED" || x.GROUP_ASSIGNED_OR_GROUP_JOIN_REQUEST_OR_GROUP_JOIN_REQUEST_APPROVED == "GROUP JOIN REQUEST APPROVED") &&
                                                                             (x.DELETED == false || x.DELETED == null)).ToList();

                var adminGroup = new ACCOUNT_MESSAGING_GROUP_MEMBERSHIP();
                adminGroup.UserName = group.CREATED_UserName;
                listMember.Add(adminGroup);

                /*Thêm vào CSDL*/
                var messageFeed = new MESSAGE_FEED();
                messageFeed.FROM_DATE                           = DateTime.Now;
                messageFeed.UserName                            = chat.data.UserName;
                messageFeed.CURRENT_FIRST_NAME                  = accountSend.CURRENT_FIRST_NAME;
                messageFeed.CURRENT_MIDDLE_NAME                 = accountSend.CURRENT_MIDDLE_NAME;
                messageFeed.CURRENT_LAST_NAME                   = accountSend.CURRENT_LAST_NAME;
                messageFeed.CONFERENCE_ID                       = chat.data.CONFERENCE_ID;
                messageFeed.CONFERENCE_NAME                     = conference.CONFERENCE_NAME;
                messageFeed.CONFERENCE_NAME_EN                  = conference.CONFERENCE_NAME_EN;
                messageFeed.PUBLIC_OR_PRIVATE_MESSAGE           = "PRIVATE";
                messageFeed.MESSAGE_CONTENT                     = chat.Body;
                messageFeed.RECIPIENT_MESSAGING_GROUP_ID_1      = group.MESSAGING_GROUP_ID;
                messageFeed.RECIPIENT_MESSAGING_GROUP_NAME_1    = group.MESSAGING_GROUP_NAME;
                messageFeed.RECIPIENT_MESSAGING_GROUP_NAME_EN_1 = group.MESSAGING_GROUP_NAME;
                new NewFeedModel().addNewFeed(messageFeed);

                /*Tạo dữ liệu trả về*/
                var msg = new MessageResponse();
                msg.MESSAGE_FEED_ID                     = messageFeed.MESSAGE_FEED_ID;
                msg.MESSAGE_CONTENT                     = messageFeed.MESSAGE_CONTENT;
                msg.IsMoreThanOneDay                    = false;
                msg.IsToDay                             = DateTimeFormater.CheckIsToday(messageFeed.FROM_DATE.Value);
                msg.TimeFormat                          = DateTimeFormater.GetTimeAgo(messageFeed.FROM_DATE.Value);
                msg.Time                                = messageFeed.FROM_DATE.Value;
                msg.UserName                            = messageFeed.UserName;
                msg.PERSON_ID_SEND                      = accountSend.PERSON_ID;
                msg.ORGANIZATION_SEND                   = accountSend.CURRENT_HOME_ORGANIZATION_NAME;
                msg.ORGANIZATION_EN_SEND                = accountSend.CURRENT_HOME_ORGANIZATION_NAME_EN;
                msg.Image                               = accountSend.Image;
                msg.FULL_NAME_PERSON_SEND               = Utils.GetFullName(messageFeed.CURRENT_FIRST_NAME, messageFeed.CURRENT_MIDDLE_NAME, messageFeed.CURRENT_LAST_NAME);
                msg.RECIPIENT_MESSAGING_GROUP_ID_1      = messageFeed.RECIPIENT_MESSAGING_GROUP_ID_1;
                msg.RECIPIENT_MESSAGING_GROUP_NAME_1    = messageFeed.RECIPIENT_MESSAGING_GROUP_NAME_1;
                msg.RECIPIENT_MESSAGING_GROUP_NAME_EN_1 = messageFeed.RECIPIENT_MESSAGING_GROUP_NAME_EN_1;
                msg.IS_MESSAGE_GROUP                    = true;

                foreach (ACCOUNT_MESSAGING_GROUP_MEMBERSHIP item in listMember)
                {
                    var memberRecive = db.ACCOUNT_DEVICE_RELATIONSHIP.SingleOrDefault(x => x.UserName == item.UserName);
                    if (memberRecive != null)
                    {
                        var deviceId = memberRecive.DEVICE_TOKEN;
                        if (deviceId != null)
                        {
                            var result  = "-1";
                            var webAddr = "https://fcm.googleapis.com/fcm/send";

                            var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
                            httpWebRequest.ContentType = "application/json";
                            httpWebRequest.Headers.Add("Authorization:key=" + StringResource.Server_fcm_key);
                            httpWebRequest.Method = "POST";

                            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                            {
                                var notification = new Notification();
                                notification.body     = chat.Body;
                                notification.title    = msg.FULL_NAME_PERSON_SEND;
                                notification.sound    = chat.Sound;
                                notification.priority = chat.Priority;

                                JsonData data = new JsonData();
                                data.Data = JsonConvert.SerializeObject(msg);

                                var json = new DataJson();
                                json.notification = notification;
                                json.data         = data;
                                json.to           = deviceId;

                                streamWriter.Write(JsonConvert.SerializeObject(json));
                                streamWriter.Flush();

                                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                                {
                                    result = streamReader.ReadToEnd();
                                }
                            }
                        }
                    }
                }

                return(ResponseSuccess(StringResource.Success, msg));
            }
        }
Esempio n. 5
0
        public HttpResponseMessage SendMessage([FromBody] Chat chat)
        {
            if (chat != null)
            {
                try
                {
                    var accountSend   = db.ACCOUNTs.SingleOrDefault(x => x.UserName == chat.data.UserName);
                    var accountRecive = db.ACCOUNTs.SingleOrDefault(x => x.UserName == chat.data.RECIPIENT_UserName_1);
                    var conference    = db.CONFERENCEs.SingleOrDefault(x => x.CONFERENCE_ID == chat.data.CONFERENCE_ID);
                    var manage        = new ManageDeviceModel().GetDeviceUserName(chat.data.RECIPIENT_UserName_1);
                    if (accountSend == null)
                    {
                        return(ResponseFail(StringResource.Account_does_not_exist));
                    }
                    else if (accountRecive == null)
                    {
                        return(ResponseFail(StringResource.Account_does_not_exist));
                    }
                    else if (conference == null)
                    {
                        return(ResponseFail(StringResource.Conference_do_not_exist));
                    }
                    else if (manage == null)
                    {
                        return(ResponseFail(StringResource.Account_has_not_been_logged));
                    }
                    else if (String.IsNullOrEmpty(manage.DEVICE_TOKEN))
                    {
                        return(ResponseFail(StringResource.Account_has_not_been_logged));
                    }
                    else
                    {
                        string deviceId = manage.DEVICE_TOKEN;
                        var    result   = "-1";
                        var    webAddr  = "https://fcm.googleapis.com/fcm/send";

                        var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
                        httpWebRequest.ContentType = "application/json";
                        httpWebRequest.Headers.Add("Authorization:key=" + StringResource.Server_fcm_key);
                        httpWebRequest.Method = "POST";
                        /*Thêm vào CSDL*/
                        var messageFeed = new MESSAGE_FEED();
                        messageFeed.FROM_DATE                       = DateTime.Now;
                        messageFeed.UserName                        = chat.data.UserName;
                        messageFeed.CURRENT_FIRST_NAME              = accountSend.CURRENT_FIRST_NAME;
                        messageFeed.CURRENT_MIDDLE_NAME             = accountSend.CURRENT_MIDDLE_NAME;
                        messageFeed.CURRENT_LAST_NAME               = accountSend.CURRENT_LAST_NAME;
                        messageFeed.CONFERENCE_ID                   = chat.data.CONFERENCE_ID;
                        messageFeed.CONFERENCE_NAME                 = conference.CONFERENCE_NAME;
                        messageFeed.CONFERENCE_NAME_EN              = conference.CONFERENCE_NAME_EN;
                        messageFeed.PUBLIC_OR_PRIVATE_MESSAGE       = "PRIVATE";
                        messageFeed.MESSAGE_CONTENT                 = chat.Body;
                        messageFeed.RECIPIENT_UserName_1            = chat.data.RECIPIENT_UserName_1;
                        messageFeed.RECIPIENT_CURRENT_LAST_NAME_1   = accountRecive.CURRENT_LAST_NAME;
                        messageFeed.RECIPIENT_CURRENT_MIDDLE_NAME_1 = accountRecive.CURRENT_MIDDLE_NAME;
                        messageFeed.RECIPIENT_CURRENT_FIRST_NAME_1  = accountRecive.CURRENT_FIRST_NAME;
                        new NewFeedModel().addNewFeed(messageFeed);

                        /*Tạo dữ liệu trả về*/
                        var msg = new MessageResponse();
                        msg.MESSAGE_FEED_ID         = messageFeed.MESSAGE_FEED_ID;
                        msg.MESSAGE_CONTENT         = messageFeed.MESSAGE_CONTENT;
                        msg.IsMoreThanOneDay        = false;
                        msg.IsToDay                 = DateTimeFormater.CheckIsToday(messageFeed.FROM_DATE.Value);
                        msg.TimeFormat              = DateTimeFormater.GetTimeAgo(messageFeed.FROM_DATE.Value);
                        msg.Time                    = messageFeed.FROM_DATE.Value;
                        msg.UserName                = messageFeed.UserName;
                        msg.PERSON_ID_SEND          = accountSend.PERSON_ID;
                        msg.ORGANIZATION_SEND       = accountSend.CURRENT_HOME_ORGANIZATION_NAME;
                        msg.ORGANIZATION_EN_SEND    = accountSend.CURRENT_HOME_ORGANIZATION_NAME_EN;
                        msg.Image                   = accountSend.Image;
                        msg.FULL_NAME_PERSON_SEND   = Utils.GetFullName(messageFeed.CURRENT_FIRST_NAME, messageFeed.CURRENT_MIDDLE_NAME, messageFeed.CURRENT_LAST_NAME);
                        msg.RECIPIENT_UserName_1    = messageFeed.RECIPIENT_UserName_1;
                        msg.FULL_NAME_PERSON_RECIVE = Utils.GetFullName(messageFeed.RECIPIENT_CURRENT_FIRST_NAME_1, messageFeed.RECIPIENT_CURRENT_MIDDLE_NAME_1, messageFeed.RECIPIENT_CURRENT_LAST_NAME_1);
                        msg.IS_MESSAGE              = true;

                        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                        {
                            var notification = new Notification();
                            notification.body     = chat.Body;
                            notification.title    = msg.FULL_NAME_PERSON_SEND;
                            notification.sound    = chat.Sound;
                            notification.priority = chat.Priority;

                            JsonData data = new JsonData();
                            data.Data = JsonConvert.SerializeObject(msg);

                            var json = new DataJson();
                            json.notification = notification;
                            json.data         = data;
                            json.to           = deviceId;

                            streamWriter.Write(JsonConvert.SerializeObject(json));
                            streamWriter.Flush();

                            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                            {
                                result = streamReader.ReadToEnd();
                            }

                            return(ResponseSuccess(StringResource.Success, msg));
                        }
                    }
                }
                catch (Exception)
                {
                    return(ResponseFail(StringResource.Sorry_an_error_has_occurred));
                }
            }
            else
            {
                return(ResponseFail(StringResource.Data_not_received));
            }
        }