public static void AddNewMessages( ISyncContext context, JanusMessageInfo[] messages, JanusRatingInfo[] rates, JanusModerateInfo[] moderatorials, int selfID) { AddNewMessages( context, messages, rates, moderatorials, null, selfID); }
private static void AddNewRates( IDataContext db, JanusRatingInfo[] rates, Action<int, int> progressHandler) { var processed = 0; foreach (var rate in rates) { // Могут встречаться случаи (+/- && :) && оценка). +, - и :) // выражаются отрицательными константами. Так же может приходить // команда на удаление всего рейтинга. var localRate = rate; var rateType = (MessageRates) rate.rate; if (rateType == MessageRates.DeleteRate) { db .Rates(r => r.MessageID == localRate.messageId && r.UserID == localRate.userId) .Delete(); continue; } var q = db .Rates( r => r.MessageID == localRate.messageId && r.UserID == localRate.userId); if (rateType == MessageRates.Agree || rateType == MessageRates.DisAgree) q = q.Where( r => r.RateType == MessageRates.Agree || r.RateType == MessageRates.DisAgree); else if (rateType <= 0) q = q.Where(r => r.RateType == rateType); else q = q.Where(r => r.RateType > MessageRates.DisAgree); var oldRate = q .Select(r => (MessageRates?)r.RateType) .SingleOrDefault(); var newRate = rate; if (!oldRate.HasValue) db .Rates() .Value(_ => _.MessageID, newRate.messageId) .Value(_ => _.TopicID, newRate.topicId) .Value(_ => _.UserID, newRate.userId) .Value(_ => _.RateType, rateType) .Value(_ => _.Multiplier, newRate.userRating) .Value(_ => _.Date, newRate.rateDate) .Insert(); else db .Rates( r => r.MessageID == newRate.messageId && r.UserID == newRate.userId && r.RateType == oldRate) .Set(_ => _.TopicID, () => newRate.topicId) .Set(_ => _.RateType, () => rateType) .Set(_ => _.Multiplier, () => newRate.userRating) .Set(_ => _.Date, () => newRate.rateDate) .Update(); processed++; if (progressHandler != null) progressHandler(rates.Length, processed); } }
public static void AddNewMessages( ISyncContext context, JanusMessageInfo[] messages, JanusRatingInfo[] rates, JanusModerateInfo[] moderatorials, Action<IDataContext> afterProcessInTxHandler, int selfID) { if (messages == null) throw new ArgumentNullException("messages"); if (rates == null) throw new ArgumentNullException("rates"); if (messages.Length == 0 && rates.Length == 0) // Nothing to do return; context.LogInfo(Resources.ProcessMessages); var msgIds = EmptyArray<int>.Value; // Затычка. Блокируем интерфейс на время обработки сообщений. using (context.GetRequiredService<IUIShell>().FreezeUI(context)) using (context.GetRequiredService<IJanusDatabaseManager>().GetLock().GetWriterLock()) { var dbMgr = context.GetRequiredService<IJanusDatabaseManager>(); using (var db = dbMgr.CreateDBContext()) using (var tx = db.BeginTransaction()) { var tids = new HashSet<int>(); var pgSvc = context.GetService<ISyncProgressVisualizer>(); var topicIds = EmptyArray<int>.Value; if (messages.Length > 0) AddMessages( context, db, messages, selfID, pgSvc != null ? (total, current) => { pgSvc.ReportProgress(total, current); pgSvc.SetProgressText( current.GetDeclension( Resources.NewMsgProcessingProgressText1, Resources.NewMsgProcessingProgressText2, Resources.NewMsgProcessingProgressText5) .FormatStr(current)); } : (Action<int, int>) null, out topicIds, out msgIds); foreach (var msg in messages) tids.Add(msg.topicId != 0 ? msg.topicId : msg.messageId); foreach (var id in topicIds) tids.Add(id); AddNewRates( db, rates, pgSvc != null ? (total, current) => { pgSvc.ReportProgress(total, current); pgSvc.SetProgressText( current.GetDeclension( Resources.NewRatesProcessingProgress1, Resources.NewRatesProcessingProgress2, Resources.NewRatesProcessingProgress5) .FormatStr(current)); } : (Action<int, int>) null); foreach (var rate in rates) tids.Add(rate.topicId != 0 ? rate.topicId : rate.messageId); AddModeratorials(db, moderatorials); // Вариант с получением топиков с сервера foreach (var tid in moderatorials.Select( mod => mod.topicId == 0 ? mod.messageId : mod.topicId)) tids.Add(tid); context.GetRequiredService<IRsdnForumService>().UpdateForumAggregates(context, db, tids); if (afterProcessInTxHandler != null) afterProcessInTxHandler(db); tx.Commit(); GC.KeepAlive(db); } } AddBrokenTopicRequests(context, messages); var searchSvc = context.GetRequiredService<ISearchService>(); var addedCount = searchSvc.AddMessagesToIndex( messages.Select( svcMsg => new MessageSearchInfo( svcMsg.messageId, svcMsg.messageDate, svcMsg.subject, svcMsg.message, svcMsg.forumId, svcMsg.userId, svcMsg.userNick))); context.StatisticsContainer.AddValue(JanusATInfo.IndexedMessagesStats, addedCount); context.LogInfo( Resources.DownloadTopicsStat .FormatStr( msgIds.Length, msgIds.Length.GetDeclension( Resources.Messages1, Resources.Messages2, Resources.Messages5))); context.StatisticsContainer.AddValue(JanusATInfo.MessagesStats, messages.Length); context.StatisticsContainer.AddValue(JanusATInfo.RatesStats, rates.Length); context.StatisticsContainer.AddValue(JanusATInfo.ModeratorialsStats, moderatorials.Length); }