コード例 #1
0
ファイル: YamsterCache.cs プロジェクト: octogonz/yamster
        internal void ProcessDbMessageState(DbMessageState record)
        {
            var eventCollector = new YamsterModelEventCollector();

            ProcessDbMessageState(record, eventCollector);
            eventCollector.FireEvents();
        }
コード例 #2
0
        internal void MarkAsDeleted()
        {
            if (this.Deleted)
            {
                return;
            }

            var yamsterCoreDb = this.YamsterCache.AppContext.YamsterCoreDb;

            yamsterCoreDb.Mapper.ExecuteNonQuery(@"
UPDATE " + yamsterCoreDb.MessageStates.TableName + @"
SET [Deleted] = 1
WHERE [MessageId] = ?",
                                                 this.messageId
                                                 );

            // This is correct, but not very elegant
            var newState = new DbMessageState();

            newState.CopyFrom(this.dbMessageState);
            newState.Deleted = true;
            this.YamsterCache.ProcessDbMessageState(newState);

            Debug.Assert(this.Deleted);
        }
コード例 #3
0
ファイル: YamsterDbRecords.cs プロジェクト: octogonz/yamster
 public void CopyFrom(DbMessageState source)
 {
     base.CopyFrom(source);
     this.MessageId = source.MessageId;
     this.Read      = source.Read;
     this.Starred   = source.Starred;
     this.Deleted   = source.Deleted;
 }
コード例 #4
0
ファイル: YamsterCache.cs プロジェクト: octogonz/yamster
        void ProcessDbMessageState(DbMessageState record, YamsterModelEventCollector eventCollector)
        {
            // Does the message exist yet?
            var message = this.GetMessageById(record.MessageId, nullIfMissing: true);

            if (message != null)
            {
                message.SetDbMessageState(record, eventCollector);
            }
            else
            {
                // Stash the message state and deal with it later
                this.unresolvedMessageStatesById[record.MessageId] = record;
            }
        }
コード例 #5
0
        internal YamsterMessage(long messageId, YamsterCache yamsterCache)
            : base(yamsterCache)
        {
            this.messageId = messageId;
            this.dbMessage = new DbMessage()
            {
                MessageId    = messageId,
                ChangeNumber = 0
            };

            this.dbMessageState = new DbMessageState()
            {
                MessageId    = messageId,
                ChangeNumber = 0
            };
        }
コード例 #6
0
        internal void SetDbMessageState(DbMessageState newValue, YamsterModelEventCollector eventCollector)
        {
            if (newValue == null)
            {
                throw new ArgumentNullException("DbMessageState");
            }
            if (newValue.MessageId != messageId)
            {
                throw new ArgumentException("Cannot change ID");
            }

            bool oldRead    = this.Read;
            bool oldDeleted = this.Deleted;

            if (newValue.Deleted != oldDeleted)
            {
                // Remove and re-add the message so it moves to the appropriate
                // collection (YamsterThread.Messages or DeletedMessages).
                // (Note that NotifyMessageReadChanged() assumes that the message
                // is in the right collection.)
                this.Thread.RemoveMessage(this, eventCollector);

                this.dbMessageState = newValue;

                this.Thread.AddMessage(this, eventCollector);

                this.cachedPreviewText = null;
            }
            else
            {
                this.dbMessageState = newValue;

                if (this.Read != oldRead && !this.Deleted)
                {
                    this.Thread.NotifyMessageReadChanged(this.Read, eventCollector);
                }
            }

            UpdateLoadedStatus();

            eventCollector.NotifyAfterUpdate(this);
        }
コード例 #7
0
        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();
            }
        }
コード例 #8
0
ファイル: YamsterCoreDb.cs プロジェクト: octogonz/yamster
        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);
        }