public void InsertArchiveMessage(JsonMessage message, DateTime lastFetchedUtc) { long groupId; if (message.IsDirectMessage) { groupId = YamsterGroup.ConversationsGroupId; } else { groupId = message.GroupId ?? YamsterGroup.AllCompanyGroupId; } DbArchiveMessageRecord archiveRecord = new DbArchiveMessageRecord(); archiveRecord.Id = message.Id; archiveRecord.ThreadId = message.ThreadId; archiveRecord.GroupId = groupId; archiveRecord.LastFetchedUtc = lastFetchedUtc; archiveRecord.Json = message.RawJson; ArchiveMessages.InsertRecord(archiveRecord, SQLiteConflictResolution.Replace); if (MessageInserted != null) { MessageInserted(this, new ArchiveInsertEventArgs <DbArchiveMessageRecord>(archiveRecord)); } }
void UpdateMessage(DbArchiveMessageRecord archiveMessage) { JsonMessage message = SQLiteJsonConverter.LoadFromJson <JsonMessage>(archiveMessage.Json); DbMessage coreMessage = new DbMessage(); coreMessage.LastFetchedUtc = archiveMessage.LastFetchedUtc; coreMessage.MessageId = message.Id; coreMessage.GroupId = archiveMessage.GroupId; coreMessage.ThreadId = message.ThreadId; coreMessage.ConversationId = message.ConversationId; coreMessage.CreatedDate = message.Created; coreMessage.SenderUserId = message.SenderId; coreMessage.MessageIdRepliedTo = message.RepliedToId ?? 0; coreMessage.LikingUserIds.AssignFrom(message.Likes.Users.Select(x => x.UserId)); foreach (var likingUser in message.Likes.Users) { // We don't get a proper UserReference for liking users, but we do get // some basic information. Write this to the Users table *only* if there // is not already some real data there. DbUser coreUser = new DbUser(); coreUser.LastFetchedUtc = archiveMessage.LastFetchedUtc; coreUser.UserId = likingUser.UserId; coreUser.FullName = likingUser.FullName ?? ""; coreUser.JobTitle = ""; coreUser.WebUrl = ""; // we could infer this from likingUser.Alias coreUser.MugshotUrl = ""; // Ignore = only write if there isn't already an existing record Users.InsertRecord(coreUser, SQLiteConflictResolution.Ignore); } coreMessage.LikesCount = message.Likes.Count; coreMessage.NotifiedUserIds.AssignFrom(message.NotifiedUserIds ?? new long[0]); coreMessage.Body = message.Body.Plain ?? ""; coreMessage.WebUrl = message.Permalink ?? ""; var firstImageAttachment = message.Attachments.Where(x => x.AttachmentType == "image").FirstOrDefault(); if (firstImageAttachment != null) { coreMessage.AttachmentFilename = firstImageAttachment.Name; coreMessage.AttachmentWebUrl = firstImageAttachment.WebUrl; coreMessage.AttachmentScaledUrlTemplate = firstImageAttachment.ScaledUrlTemplate; coreMessage.AttachmentWidth = firstImageAttachment.Width ?? 0; coreMessage.AttachmentHeight = firstImageAttachment.Height ?? 0; } if (!Enum.TryParse(message.MessageType, true, out coreMessage.MessageType)) { coreMessage.MessageType = DbMessageType.Unknown; } Messages.InsertRecord(coreMessage, SQLiteConflictResolution.Replace); DbMessageState messageState = new DbMessageState() { MessageId = archiveMessage.Id }; MessageStates.InsertRecord(messageState, SQLiteConflictResolution.Ignore); // Ensure that every message has a corresponding DbThreadState for its thread DbThreadState threadState = new DbThreadState() { ThreadId = coreMessage.ThreadId }; ThreadStates.InsertRecord(threadState, SQLiteConflictResolution.Ignore); }