Пример #1
0
        /// <summary> Get notes from the Paratext project folder. </summary>
        public string GetNotes(UserSecret userSecret, string projectId, int bookNum)
        {
            // TODO: should return some data structure instead of XML
            ScrText scrText = ScrTextCollection.FindById(GetParatextUsername(userSecret), projectId);

            if (scrText == null)
            {
                return(null);
            }

            CommentManager manager = CommentManager.Get(scrText);
            var            threads = manager.FindThreads((commentThread) => { return(commentThread.VerseRef.BookNum == bookNum); },
                                                         true);

            return(NotesFormatter.FormatNotes(threads));
        }
Пример #2
0
        /// <summary> Write up-to-date notes from the mongo database to the Paratext project folder </summary>
        public void PutNotes(UserSecret userSecret, string projectId, string notesText)
        {
            // TODO: should accept some data structure instead of XML
            string        username = GetParatextUsername(userSecret);
            List <string> users = new List <string>();
            int           nbrAddedComments = 0, nbrDeletedComments = 0, nbrUpdatedComments = 0;
            ScrText       scrText = ScrTextCollection.FindById(username, projectId);

            if (scrText == null)
            {
                throw new DataNotFoundException("Can't get access to cloned project.");
            }
            CommentManager manager = CommentManager.Get(scrText);
            var            ptUser  = new SFParatextUser(username);
            var            notes   = NotesFormatter.ParseNotes(notesText, ptUser);

            // Algorithm sourced from Paratext DataAccessServer
            foreach (var thread in notes)
            {
                CommentThread existingThread = manager.FindThread(thread[0].Thread);
                foreach (var comment in thread)
                {
                    var existingComment = existingThread?.Comments.FirstOrDefault(c => c.Id == comment.Id);
                    if (existingComment == null)
                    {
                        manager.AddComment(comment);
                        nbrAddedComments++;
                    }
                    else if (comment.Deleted)
                    {
                        existingComment.Deleted = true;
                        nbrDeletedComments++;
                    }
                    else
                    {
                        existingComment.ExternalUser   = comment.ExternalUser;
                        existingComment.Contents       = comment.Contents;
                        existingComment.VersionNumber += 1;
                        nbrUpdatedComments++;
                    }

                    if (!users.Contains(comment.User))
                    {
                        users.Add(comment.User);
                    }
                }
            }

            try
            {
                foreach (string user in users)
                {
                    manager.SaveUser(user, false);
                }
                _paratextDataHelper.CommitVersionedText(scrText, $"{nbrAddedComments} notes added and "
                                                        + $"{nbrDeletedComments + nbrUpdatedComments} notes updated or deleted in synchronize");
                _logger.LogInformation("{0} added {1} notes, updated {2} notes and deleted {3} notes", userSecret.Id,
                                       nbrAddedComments, nbrUpdatedComments, nbrDeletedComments);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Exception while updating notes: {0}", e.Message);
            }
        }