void WriteMetaPropertiesToDb(JsonMessageEnvelope envelope) { var originalProperties = yamsterCoreDb.Properties; if (envelope.Meta.CurrentUserId != originalProperties.CurrentUserId) { yamsterCoreDb.UpdateProperties(properties => { properties.CurrentUserId = envelope.Meta.CurrentUserId; }); } var anyMessage = envelope.Messages.FirstOrDefault(); if (anyMessage != null) { if (anyMessage.NetworkId != originalProperties.CurrentNetworkId) { yamsterCoreDb.UpdateProperties(properties => { properties.CurrentNetworkId = anyMessage.NetworkId; }); } } }
void WriteToDatabase(YamsterCoreDb yamsterCoreDb) { using (var transaction = yamsterCoreDb.BeginTransaction()) { yamsterCoreDb.DeleteEverything(markArchiveDbInactive: true); yamsterCoreDb.UpdateProperties(row => { row.CurrentNetworkId = this.networkId; }); foreach (var user in this.usersById.Values) { // NOTE: For now, deleted users are always included because they // are heavily referenced //if (this.IncludeDeletedObjects || !this.deletedUsers.Contains(user.UserId)) yamsterCoreDb.Users.InsertRecord(user); } foreach (var group in this.groupsById.Values) { if (this.IncludeDeletedObjects || !this.deletedGroups.Contains(group.GroupId)) { yamsterCoreDb.Groups.InsertRecord(group); DbGroupState groupState = new DbGroupState() { GroupId = group.GroupId }; groupState.ShowInYamster = true; yamsterCoreDb.GroupStates.InsertRecord(groupState); } } foreach (var conversation in this.conversationsById.Values) { if (this.IncludeDeletedObjects || this.notDeletedConversations.Contains(conversation.ConversationId)) { yamsterCoreDb.Conversations.InsertRecord(conversation); } } foreach (var message in this.messagesById.Values) { bool messageIsDeleted = this.deletedMessages.Contains(message.MessageId); if (this.IncludeDeletedObjects || !messageIsDeleted) { yamsterCoreDb.Messages.InsertRecord(message); DbMessageState messageState = new DbMessageState() { MessageId = message.MessageId }; messageState.Deleted = messageIsDeleted; yamsterCoreDb.MessageStates.InsertRecord(messageState); // Ensure that every message has a corresponding DbThreadState for its thread DbThreadState threadState = new DbThreadState() { ThreadId = message.ThreadId }; yamsterCoreDb.ThreadStates.InsertRecord(threadState, SQLiteConflictResolution.Ignore); } } transaction.Commit(); } }