public string SendMessage(long usid, long[] toArray, string subject, string txt) { aqufitEntities entities = new aqufitEntities(); if (string.IsNullOrEmpty(subject)) { subject = "(No Subject)"; } subject = Utils.Web.WebUtils.MakeWebSafeString(subject); txt = Utils.Web.WebUtils.MakeWebSafeString(txt); string shortTxt = Utils.Web.WebUtils.MakeWebSafeString( txt.Length > 128 ? txt.Substring(0, 128) + "..." : txt ); DateTime dt = DateTime.Now.ToUniversalTime(); UserSettings settings = entities.UserSettings.FirstOrDefault(us => us.Id == usid); Message message = new Message() { UserSetting = settings, PortalKey = settings.PortalKey, Status = 0, // TODO: make nice message status ( 0 = unread ) DateTime = dt, ParentKey = 0, // This is not a reply so set parent to 0 Subject = subject, Text = txt, LastText = shortTxt, LastDateTime = dt, LastUserKey = settings.Id, LastUserName = settings.UserName, SecondDateTime = dt, SecondText = shortTxt, SecondUserKey = settings.Id, SecondUserName = settings.UserName }; MessageRecipiant recipiant2 = new MessageRecipiant() { UserSettingsKey = settings.Id, Message = message }; message.MessageRecipiants.Add(recipiant2); entities.AddToMessages(message); IList<UserFriends> friendList = new List<UserFriends>(); UserSettings you = settings; foreach (long tid in toArray) { // First thing we want to do here is to varify that the people are really friends UserFriends friend = entities.UserFriends.FirstOrDefault(f => ((f.SrcUserSettingKey == you.Id && f.DestUserSettingKey == tid) || (f.SrcUserSettingKey == tid && f.DestUserSettingKey == you.Id)) && f.Relationship == (int)Affine.Utils.ConstsUtil.Relationships.FRIEND ); if (friend != null) { friendList.Add(friend); long toId = settings.Id == friend.DestUserSettingKey ? friend.SrcUserSettingKey : friend.DestUserSettingKey; MessageRecipiant recipiant = new MessageRecipiant() { UserSettingsKey = toId, Message = message }; message.MessageRecipiants.Add(recipiant); // create a notification Publish setting 3 so does not show in stream... User toUser = entities.UserSettings.OfType<User>().FirstOrDefault(us => us.Id == toId); Notification workoutNotification = new Notification() { PortalKey = toUser.PortalKey, UserSetting = toUser, Date = DateTime.Now.ToUniversalTime(), Title = settings.UserName +" has sent you a message.", Description = settings.UserName +" ("+ settings.UserFirstName + " " + settings.UserLastName + ") has sent you a message.", TimeStamp = DateTime.Now.ToUniversalTime(), NotificationType = (int)Affine.Utils.ConstsUtil.NotificationTypes.NEW_MESSAGE, PublishSettings = (int) Affine.Utils.ConstsUtil.PublishSettings.NO_STREAM, Message = message }; entities.AddToUserStreamSet(workoutNotification); } } entities.SaveChanges(); sendEmailAsync(message); return "{ 'status':'success' }"; }
public string saveReply(long usid, long mid, string txt) { // TODO: make sure that the person can reply to this message aqufitEntities entities = new aqufitEntities(); DateTime dt = DateTime.Now.ToUniversalTime(); // TODO: cache settings ? UserSettings settings = entities.UserSettings.FirstOrDefault(us => us.Id == usid ); txt = Utils.Web.WebUtils.MakeWebSafeString(txt); Message message = new Message() { UserSetting = settings, PortalKey = settings.PortalKey, Status = 0, // TODO: make nice message status ( 0 = unread ) DateTime = dt, ParentKey = mid, Text = txt }; // This is how we handle repys ... we want to be able to query the messages easy and know what has changed... so we store some // data about the last reply in the "source" message. That way we can update and show that there is a new message for people // but not have to search the entire message history to do this. Message reply = entities.Messages.Include("MessageRecipiants").FirstOrDefault(m => m.Id == mid); if (reply.LastUserKey != usid) { reply.SecondUserName = reply.LastUserName; reply.SecondUserKey = reply.LastUserKey; reply.SecondText = reply.LastText; reply.SecondDateTime = reply.LastDateTime; } reply.LastUserName = settings.UserName; reply.LastUserKey = usid; reply.LastText = txt.Length > 128 ? txt.Substring(0,128) + "..." : txt; reply.LastDateTime = dt; Message parent = entities.Messages.FirstOrDefault(m => m.Id == mid); foreach (MessageRecipiant mr in reply.MessageRecipiants) { if (mr.UserSettingsKey != settings.Id) // dont notify the person who just replied... { // mark the message as unread for all recipiants... But the person who wrote this. if (mr.UserSettingsKey != usid && mr.Status == 1) mr.Status = 0; UserSettings toUser = entities.UserSettings.FirstOrDefault(u => u.Id == mr.UserSettingsKey); Notification mailNotification = new Notification() { PortalKey = toUser.PortalKey, UserSetting = toUser, Date = DateTime.Now.ToUniversalTime(), Title = settings.UserName + " has replied you a message.", Description = settings.UserName + " (" + settings.UserFirstName + " " + settings.UserLastName + ") has replied to a message.", TimeStamp = DateTime.Now.ToUniversalTime(), NotificationType = (int)Affine.Utils.ConstsUtil.NotificationTypes.NEW_MESSAGE, PublishSettings = (int)Affine.Utils.ConstsUtil.PublishSettings.NO_STREAM, Message = parent }; entities.AddToUserStreamSet(mailNotification); } } entities.AddToMessages(message); try { entities.SaveChanges(); } catch (Exception ex) { throw new Exception(ex.InnerException.Message.Replace("'","")); } sendReplyEmailAsync(reply, settings); Message ret = entities.Messages.Where( m => m.ParentKey == mid ).OrderByDescending( m => m.Id ).FirstOrDefault(); string json = _jserializer.Serialize(_IStreamManager.MessageEntityToMessageData(ret)); return json; }