Skip to content

Commit

Permalink
* Save chat history with UTC dates
Browse files Browse the repository at this point in the history
* Fixed the history search to return only chat sessions you participated in
  • Loading branch information
hasankhan committed Aug 19, 2013
1 parent a47c068 commit 352eb43
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 43 deletions.
18 changes: 11 additions & 7 deletions Squiggle.Client/Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ void LogHistory(EventType eventType, IBuddy sender, string data = null)
{
if (!sessionLogged)
LogSessionStart();
manager.AddSessionEvent(session.Id, DateTime.Now, eventType, new Guid(sender.Id), sender.DisplayName, buddies.Select(b => new Guid(b.Id)), data);
manager.AddSessionEvent(session.Id, eventType, new Guid(sender.Id), sender.DisplayName, buddies.Select(b => new Guid(b.Id)), data);
});
}

Expand All @@ -257,12 +257,16 @@ void LogSessionStart()
ContactName = primaryBuddy.DisplayName,
Start = DateTime.Now
};
manager.AddSession(newSession, Buddies.Select(b => new Participant()
{
Id = Guid.NewGuid(),
ContactId = new Guid(b.Id),
ContactName = b.DisplayName,
}).ToList());

var participants = Buddies.Append(self)
.Select(b => new Participant()
{
Id = Guid.NewGuid(),
ContactId = new Guid(b.Id),
ContactName = b.DisplayName,
}).ToList();

manager.AddSession(newSession, participants);
});
}

Expand Down
2 changes: 1 addition & 1 deletion Squiggle.Client/ChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void LogStatus(IBuddy buddy)
ExceptionMonster.EatTheException(() =>
{
var manager = new HistoryManager();
manager.AddStatusUpdate(DateTime.Now, new Guid(buddy.Id), buddy.DisplayName, (int)buddy.Status);
manager.AddStatusUpdate(new Guid(buddy.Id), buddy.DisplayName, (int)buddy.Status);
}, "logging history.");
}

Expand Down
3 changes: 1 addition & 2 deletions Squiggle.History/DAL/HistoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void AddSessionEvent(Guid sessionId, DateTime stamp, EventType type, Guid
var session = context.Sessions.FirstOrDefault(s => s.Id == sessionId);
if (session != null)
{
session.End = DateTime.Now;
session.End = session.End < stamp ? stamp : session.End;
var evnt = new Event()
{
Id = Guid.NewGuid(),
Expand All @@ -37,7 +37,6 @@ public void AddSessionEvent(Guid sessionId, DateTime stamp, EventType type, Guid
public IEnumerable<Session> GetSessions(SessionCriteria criteria)
{
string text = criteria.Text ?? String.Empty;
string participant = criteria.Participant.HasValue ? criteria.Participant.Value.ToString("N") : String.Empty;

var result = (from session in context.Sessions.Include("Participants")
where (criteria.SessionId == null || session.Id == criteria.SessionId.Value) &&
Expand Down
8 changes: 4 additions & 4 deletions Squiggle.History/HistoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ namespace Squiggle.History
{
public class HistoryManager
{
public void AddSessionEvent(Guid sessionId, DateTime stamp, EventType type, Guid senderId, string senderName, IEnumerable<Guid> recipients, string data)
public void AddSessionEvent(Guid sessionId, EventType type, Guid senderId, string senderName, IEnumerable<Guid> recipients, string data)
{
using (var repository = new HistoryRepository())
{
repository.AddSessionEvent(sessionId, stamp, type, senderId, senderName, recipients, data);
repository.AddSessionEvent(sessionId, DateTime.UtcNow, type, senderId, senderName, recipients, data);
if (type == EventType.Joined)
{
var participant = new Participant()
Expand All @@ -27,10 +27,10 @@ public void AddSessionEvent(Guid sessionId, DateTime stamp, EventType type, Guid
}
}

public void AddStatusUpdate(DateTime stamp, Guid contactId, string contactName, int status)
public void AddStatusUpdate(Guid contactId, string contactName, int status)
{
using (var repository = new HistoryRepository())
repository.AddStatusUpdate(stamp, contactId, contactName, status);
repository.AddStatusUpdate(DateTime.UtcNow, contactId, contactName, status);
}

public IEnumerable<Session> GetSessions(SessionCriteria criteria)
Expand Down
37 changes: 21 additions & 16 deletions Squiggle.UI/Controls/ChatHistoryViewer.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows.Input;
using Squiggle.History;
using Squiggle.History.DAL;
using Squiggle.History.DAL.Entities;
using Squiggle.Plugins;
using Squiggle.UI.Helpers;
using Squiggle.UI.Resources;
Expand Down Expand Up @@ -62,18 +63,14 @@ void SearchSessions(DateTime? from, DateTime? to, string message)
{
var historyManager = new HistoryManager();
var sessions = historyManager.GetSessions(new SessionCriteria()
{
From = from,
To = to,
Text = message.Length == 0 ? null : message,
Participant = new Guid(SquiggleContext.ChatClient.CurrentUser.Id)
}).Select(session => new Result()
{
Id = session.Id,
Start = session.Start,
End = session.End,
Participants = String.Join(", ", session.Participants.Select(p => p.ContactName).ToArray())
}).ToList();
{
From = from.HasValue ? from.Value.ToUniversalTime() : from,
To = to.HasValue ? to.Value.ToUniversalTime() : to,
Text = message.Length == 0 ? null : message,
Participant = new Guid(SquiggleContext.ChatClient.CurrentUser.Id)
})
.Select(session => new Result(session))
.ToList();

Dispatcher.Invoke(() =>
{
Expand Down Expand Up @@ -135,10 +132,18 @@ private void results_ContextMenuOpening(object sender, ContextMenuEventArgs e)

class Result
{
public Guid Id { get; set; }
public DateTime Start { get; set; }
public DateTime? End { get; set; }
public string Participants { get; set; }
public Guid Id { get; private set; }
public DateTime Start { get; private set; }
public DateTime? End { get; private set; }
public string Participants { get; private set; }

public Result(Session session)
{
Id = session.Id;
Start = session.Start.ToLocalTime();
End = session.End.HasValue ? session.End.Value.ToLocalTime() : session.End;
Participants = String.Join(", ", session.Participants.Select(p => p.ContactName).ToArray());
}
}

private void UserControl_GotFocus(object sender, RoutedEventArgs e)
Expand Down
29 changes: 17 additions & 12 deletions Squiggle.UI/Controls/StatusHistoryViewer.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Squiggle.Core.Presence;
using Squiggle.History;
using Squiggle.History.DAL;
using Squiggle.History.DAL.Entities;
using Squiggle.UI.Resources;
using Squiggle.Utilities;
using Squiggle.Utilities.Threading;
Expand Down Expand Up @@ -45,15 +46,12 @@ void SearchUpdates(DateTime? from, DateTime? to)
{
var historyManager = new HistoryManager();
var updates = historyManager.GetStatusUpdates(new StatusCriteria()
{
From = from,
To = to,
}).Select(update => new Result()
{
Time = update.Stamp,
Name = update.ContactName,
Status = (UserStatus)update.StatusCode
}).ToList();
{
From = from.HasValue ? from.Value.ToUniversalTime() : from,
To = to.HasValue ? to.Value.ToUniversalTime() : to,
})
.Select(update => new Result(update))
.ToList();

Dispatcher.Invoke(() =>
{
Expand Down Expand Up @@ -96,9 +94,16 @@ void AsyncInvoke(Action action, Action onComplete)

class Result
{
public DateTime Time { get; set; }
public string Name { get; set; }
public UserStatus Status { get; set; }
public DateTime Time { get; private set; }
public string Name { get; private set; }
public UserStatus Status { get; private set; }

public Result(StatusUpdate update)
{
Time = update.Stamp.ToLocalTime();
Name = update.ContactName;
Status = (UserStatus)update.StatusCode;
}
}
}
}
11 changes: 10 additions & 1 deletion Squiggle.UI/Windows/ConversationViewer.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,16 @@ public ConversationViewer(Guid sessionId): this()
this.SessionId = sessionId;
var historyManager = new HistoryManager();
Session session = historyManager.GetSession(sessionId);
messages.ItemsSource = session.Events.OrderBy(e=>e.Stamp).ToList();
messages.ItemsSource = session.Events
.OrderBy(e => e.Stamp)
.Select(e => new
{
Stamp = e.Stamp.ToLocalTime(),
e.SenderName,
e.Type,
e.Data
})
.ToList();
}

private void StickyWindow_KeyDown(object sender, KeyEventArgs e)
Expand Down
5 changes: 5 additions & 0 deletions Squiggle.Utilities/LinqExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
action(item);
}

public static IEnumerable<T> Append<T>(this IEnumerable<T> items, T item)
{
return items.Concat(Enumerable.Repeat(item, 1));
}

public static string ToTraceString<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> items)
{
string output = String.Join(", ", items.Select(i => i.Key + " = " + i.Value).ToArray());
Expand Down

0 comments on commit 352eb43

Please sign in to comment.