Exemplo n.º 1
0
        void ProcessDbGroup(DbGroup record)
        {
            var          eventCollector = new YamsterModelEventCollector();
            YamsterGroup group          = this.FetchGroupById(record.GroupId, eventCollector);

            group.SetDbGroup(record, eventCollector);
            eventCollector.FireEvents();
        }
Exemplo n.º 2
0
        public YamsterGroup AddGroupToYamster(long groupId, JsonSearchedGroup searchedGroup = null)
        {
            DbGroup group = yamsterCoreDb.Groups.Query("WHERE GroupId = " + groupId)
                            .FirstOrDefault();
            DbGroupState groupState = yamsterCoreDb.GroupStates.Query("WHERE GroupId = " + groupId)
                                      .FirstOrDefault();

            using (var transaction = yamsterCoreDb.BeginTransaction())
            {
                if (group == null)
                {
                    if (searchedGroup == null)
                    {
                        group = new DbGroup()
                        {
                            GroupId   = groupId,
                            GroupName = "(Unsynced Group #" + groupId + ")"
                        };
                    }
                    else
                    {
                        group = new DbGroup()
                        {
                            GroupId          = groupId,
                            GroupName        = searchedGroup.FullName ?? "???",
                            GroupDescription = searchedGroup.Description ?? "",
                            WebUrl           = searchedGroup.WebUrl ?? "",
                            MugshotUrl       = searchedGroup.Photo ?? ""
                        };
                    }
                    yamsterCoreDb.Groups.InsertRecord(group);
                }

                if (groupState == null)
                {
                    groupState = new DbGroupState()
                    {
                        GroupId       = groupId,
                        ShowInYamster = true,
                        ShouldSync    = true,
                        TrackRead     = true
                    };
                }
                groupState.ShowInYamster = true;
                yamsterCoreDb.GroupStates.InsertRecord(groupState, SQLiteConflictResolution.Replace);

                transaction.Commit();
            }

            // Force an update
            this.ProcessDbGroup(group);
            this.ProcessDbGroupState(groupState);

            return(this.GetGroupById(group.GroupId));
        }
Exemplo n.º 3
0
 internal void SetDbGroup(DbGroup newValue, YamsterModelEventCollector eventCollector)
 {
     if (newValue == null)
     {
         throw new ArgumentNullException("DbGroup");
     }
     if (newValue.GroupId != groupId)
     {
         throw new ArgumentException("Cannot change ID");
     }
     this.dbGroup = newValue;
     UpdateLoadedStatus();
     eventCollector.NotifyAfterUpdate(this);
 }
Exemplo n.º 4
0
        internal YamsterGroup(long groupId, YamsterCache yamsterCache)
            : base(yamsterCache)
        {
            this.groupId = groupId;

            this.dbGroup = new DbGroup()
            {
                GroupId      = groupId,
                GroupName    = "(Group #" + groupId + ")",
                ChangeNumber = 0
            };
            this.dbGroupState = new DbGroupState()
            {
                GroupId      = groupId,
                ChangeNumber = 0
            };
        }
Exemplo n.º 5
0
        void UpdateGroup(DbArchiveRecord archiveGroupRef)
        {
            JsonGroupReference groupRef = SQLiteJsonConverter.LoadFromJson <JsonGroupReference>(archiveGroupRef.Json);

            bool incompleteRecord = false;

            DbGroup coreGroup = new DbGroup();

            coreGroup.LastFetchedUtc   = archiveGroupRef.LastFetchedUtc;
            coreGroup.GroupId          = groupRef.Id;
            coreGroup.GroupName        = groupRef.FullName ?? "";
            coreGroup.GroupDescription = groupRef.Description ?? "";

            if (!Enum.TryParse <DbGroupPrivacy>(groupRef.Privacy, true, out coreGroup.Privacy))
            {
                if (!string.IsNullOrEmpty(groupRef.Privacy))
                {
                    throw new YamsterProtocolException(string.Format("Unsupported group privacy \"{0}\"", coreGroup.Privacy));
                }
                coreGroup.Privacy = DbGroupPrivacy.Unknown;
                incompleteRecord  = true;
            }

            coreGroup.WebUrl     = groupRef.WebUrl ?? "";
            coreGroup.MugshotUrl = groupRef.MugshotUrl ?? "";

            // If the record is incomplete, don't overwrite an existing record that might have complete data
            // TODO: this merging should be more fine-grained
            Groups.InsertRecord(coreGroup, incompleteRecord ? SQLiteConflictResolution.Ignore : SQLiteConflictResolution.Replace);

            DbGroupState groupState = new DbGroupState()
            {
                GroupId = groupRef.Id
            };

            GroupStates.InsertRecord(groupState, SQLiteConflictResolution.Ignore);
        }
Exemplo n.º 6
0
        void ReadCsvFiles()
        {
            using (var csvReader = new CsvReader(Path.Combine(this.FolderPath, "Networks.csv")))
            {
                int col_Id  = csvReader.GetColumnIndex("id");
                int col_Url = csvReader.GetColumnIndex("url");

                while (csvReader.ReadNextLine())
                {
                    csvReader.WrapExceptions(() =>
                    {
                        networkId  = long.Parse(csvReader[col_Id]);
                        networkUrl = csvReader[col_Url];
                    });
                }
            }

            using (var csvReader = new CsvReader(Path.Combine(this.FolderPath, "Groups.csv")))
            {
                int col_Id          = csvReader.GetColumnIndex("id");
                int col_Name        = csvReader.GetColumnIndex("name");
                int col_Description = csvReader.GetColumnIndex("description");
                int col_Private     = csvReader.GetColumnIndex("private");
                int col_Deleted     = csvReader.GetColumnIndex("deleted");

                while (csvReader.ReadNextLine())
                {
                    DbGroup group     = new DbGroup();
                    bool    isDeleted = false;
                    csvReader.WrapExceptions(() =>
                    {
                        group.GroupId          = long.Parse(csvReader[col_Id]);
                        group.GroupName        = csvReader[col_Name];
                        group.GroupDescription = csvReader[col_Description];

                        // TODO: How to recognize DbGroupPrivacy.Restricted?
                        bool isPrivate = bool.Parse(csvReader[col_Private]);
                        group.Privacy  = isPrivate ? DbGroupPrivacy.Private : DbGroupPrivacy.Public;

                        group.WebUrl = networkUrl + "/#/threads/inGroup?type=in_group&feedId=" + group.GroupId;

                        // TODO: How to obtain MugshotUrl?

                        isDeleted = bool.Parse(csvReader[col_Deleted]);
                    });

                    groupsById.Add(group.GroupId, group);

                    if (isDeleted)
                    {
                        deletedGroups.Add(group.GroupId);
                    }
                }
            }

            using (var csvReader = new CsvReader(Path.Combine(this.FolderPath, "Users.csv")))
            {
                int col_Id       = csvReader.GetColumnIndex("id");
                int col_Name     = csvReader.GetColumnIndex("name");
                int col_JobTitle = csvReader.GetColumnIndex("job_title");
                int col_Email    = csvReader.GetColumnIndex("email");
                int col_State    = csvReader.GetColumnIndex("state");

                while (csvReader.ReadNextLine())
                {
                    DbUser user      = new DbUser();
                    bool   isDeleted = false;
                    csvReader.WrapExceptions(() =>
                    {
                        user.UserId = long.Parse(csvReader[col_Id]);
                        // TODO: Is there a way to obtain the correct alias?
                        user.Alias    = csvReader[col_Email].Replace('@', '_');
                        user.Email    = csvReader[col_Email];
                        user.FullName = csvReader[col_Name];
                        user.JobTitle = csvReader[col_JobTitle];

                        // TODO: We need the alias to calculate user.WebUrl

                        // TODO: user.MugshotUrl

                        isDeleted = csvReader[col_State].Trim().ToUpper() == "SOFT_DELETE";
                    });

                    usersById.Add(user.UserId, user);

                    if (isDeleted)
                    {
                        deletedUsers.Add(user.UserId);
                    }
                }
            }

            using (var csvReader = new CsvReader(Path.Combine(this.FolderPath, "Messages.csv")))
            {
                int col_Id             = csvReader.GetColumnIndex("id");
                int col_GroupId        = csvReader.GetColumnIndex("group_id");
                int col_ThreadId       = csvReader.GetColumnIndex("thread_id");
                int col_ConversationId = csvReader.GetColumnIndex("conversation_id");

                int col_InPrivateGroup        = csvReader.GetColumnIndex("in_private_group");
                int col_InPrivateConversation = csvReader.GetColumnIndex("in_private_conversation");
                int col_Participants          = csvReader.GetColumnIndex("participants");

                int col_CreatedAt   = csvReader.GetColumnIndex("created_at");
                int col_SenderId    = csvReader.GetColumnIndex("sender_id");
                int col_RepliedToId = csvReader.GetColumnIndex("replied_to_id");
                int col_Body        = csvReader.GetColumnIndex("body");
                int col_DeletedAt   = csvReader.GetColumnIndex("deleted_at");

                while (csvReader.ReadNextLine())
                {
                    DbMessage message   = new DbMessage();
                    bool      isDeleted = false;
                    csvReader.WrapExceptions(() =>
                    {
                        message.MessageId = long.Parse(csvReader[col_Id]);
                        message.GroupId   = ParseLongWithDefault(csvReader[col_GroupId], YamsterGroup.AllCompanyGroupId);
                        message.ThreadId  = long.Parse(csvReader[col_ThreadId]);

                        bool inPrivateGroup        = bool.Parse(csvReader[col_InPrivateGroup]);
                        bool inPrivateConversation = bool.Parse(csvReader[col_InPrivateConversation]);

                        if (inPrivateConversation)
                        {
                            // This is apparently broken
                            // long conversationId = ParseLongWithDefault(csvReader[col_ConversationId], 0);
                            long conversationId = message.ThreadId;

                            DbConversation conversation;
                            if (!conversationsById.TryGetValue(conversationId, out conversation))
                            {
                                conversation = new DbConversation();
                                conversation.ConversationId = conversationId;
                                ParseParticipants(conversation.ParticipantUserIds, csvReader[col_Participants]);
                                conversationsById.Add(conversationId, conversation);
                            }

                            message.ConversationId = conversationId;
                            message.GroupId        = YamsterGroup.ConversationsGroupId;
                        }

                        message.CreatedDate        = DateTime.Parse(csvReader[col_CreatedAt]);
                        message.SenderUserId       = long.Parse(csvReader[col_SenderId]);
                        message.MessageIdRepliedTo = ParseLongWithDefault(csvReader[col_RepliedToId], 0);

                        // TODO: Likes?

                        // TODO: Parse message.NotifiedUserIds from "CC" line
                        message.Body   = csvReader[col_Body];
                        message.WebUrl = networkUrl + "/messages/" + message.MessageId;

                        // TODO: Attachments?

                        message.MessageType = DbMessageType.Update;

                        isDeleted = !string.IsNullOrWhiteSpace(csvReader[col_DeletedAt]);
                    });

                    messagesById.Add(message.MessageId, message);

                    if (isDeleted)
                    {
                        deletedMessages.Add(message.MessageId);
                    }
                    else
                    {
                        if (message.ConversationId != 0)
                        {
                            notDeletedConversations.Add(message.ConversationId);
                        }
                    }
                }
            }
        }