public void UpdateChannel(int channelId, MongoChannel channel) { var channels = _db.GetCollection <MongoChannel>("channels"); MongoChannel mongoChannel = channels.Find(c => c.ChannelId == channelId).First(); mongoChannel.ParticipantsCounts = mongoChannel.ParticipantsCounts.Concat(channel.ParticipantsCounts).ToList(); var filter = Builders <MongoChannel> .Filter.Eq("channelId", channelId); channels.UpdateOne(filter, Builders <MongoChannel> .Update.Set("title", channel.Title)); channels.UpdateOne(filter, Builders <MongoChannel> .Update.Set("username", channel.Username)); channels.UpdateOne(filter, Builders <MongoChannel> .Update.Set("about", channel.About)); channels.UpdateOne(filter, Builders <MongoChannel> .Update.Set("participantsCount", mongoChannel.ParticipantsCounts)); }
private async Task <bool> AddChannelHelper(string username, bool active) { // get channel info TLInterfacePackage tl = GetAvailableTLInterface(); Channel channel = await tl.Interface.GetChannelAsync(username); tl.LastCallTime = DateTime.Now; tl.IsAvailable = true; if (channel != null) { tl = GetAvailableTLInterface(); ChannelFull channelFull = await tl.Interface.GetFullChannelAsync(channel.ChannelId, channel.AccessHash.Value); tl.LastCallTime = DateTime.Now; tl.IsAvailable = true; if (!db.IsChannelAdded(channel.ChannelId)) { // refresh channel on duplicate username if (db.IsChannelAdded(username)) { MongoChannel channelToRefresh = db.GetChannel(username); await RefreshChannelHelper(channelToRefresh.ChannelId, channelToRefresh.AccessHash.Value); } // add channel db.AddChannel(new MongoChannel { ChannelId = channel.ChannelId, AccessHash = channel.AccessHash, Title = channel.Title, Username = channel.Username, About = channelFull.About, ParticipantsCounts = new List <CountTime>() { new CountTime(channelFull.ParticipantsCount, DateTime.Now) }, IsActive = active }); Log(string.Format("Channel '{0}' (@{1}) with {2} members has been added.", channel.Title, channel.Username, channelFull.ParticipantsCount)); return(true); } else { // refresh channel if it already exists await RefreshChannelHelper(channel.ChannelId, channel.AccessHash.Value); } } return(false); }
private JobResult ProcessDeactivateCommand(string[] args) { int count = 0; bool stopped = false; foreach (string username in args) { if (_stop) { stopped = true; break; } if (!username.StartsWith("-")) { MongoChannel channel = db.GetChannel(username); if (channel != null) { if (channel.IsActive) { db.UpdateChannelActivationStatus(channel.ChannelId, false); count++; Log(string.Format("Channel '{0}' (@{1}) with {2} members has been deactivated.", channel.Title, channel.Username, channel.ParticipantsCounts.Last().Count)); } } } } if (stopped) { return(new JobResult(JobStatus.Stopped)); } string message = ""; if (count == 0) { message = "No channel has been deactivated."; } else if (count == 1) { message = "1 channel has been deactivated."; } else { message = string.Format("{0} channels has been deactivated.", count); } return(new JobResult(message, JobStatus.Finished)); }
private async Task <JobResult> ProcessRefreshCommand(string[] args) { int count = 0; bool stopped = false; foreach (string username in args) { if (_stop) { stopped = true; break; } if (!username.StartsWith("-")) { MongoChannel mongoChannel = db.GetChannel(username); if (mongoChannel != null) { if (await RefreshChannelHelper(mongoChannel.ChannelId, mongoChannel.AccessHash.Value)) { count++; } } } } if (stopped) { return(new JobResult(JobStatus.Stopped)); } string message = ""; if (count == 0) { message = "No channel has been refreshed."; } else if (count == 1) { message = "1 channel has been refreshed."; } else { message = string.Format("{0} channels has been refreshed.", count); } return(new JobResult(message, JobStatus.Finished)); }
public void AddChannel(MongoChannel channel) { var channels = _db.GetCollection <MongoChannel>("channels"); channels.InsertOne(channel); }
private async Task <JobResult> ProcessGetFeedCommand(string[] args) { int totalMessagesCount = 0; int from = DateTime.Now.ToIntSeconds(); int to = DateTime.Now.ToIntSeconds(); bool stopped = false; for (int i = 0; i < args.Length; i++) { if (args[i].ToLower() == "-from") { try { from = DateTime.Parse(args[i + 1]).ToIntSeconds(); } catch (Exception) { return(new JobResult("Invalid date after '-from'", JobStatus.Finished)); } } if (args[i].ToLower() == "-to") { try { to = DateTime.Parse(args[i + 1]).ToIntSeconds(); } catch (Exception) { return(new JobResult("Invalid date after '-to'", JobStatus.Finished)); } } } List <int> channelsIDs = db.GetActiveChannelsIDs(); foreach (int channelId in channelsIDs) { if (_stop) { stopped = true; break; } MongoChannel channel = db.GetChannel(channelId); if (channel.CoveredTimePeriods == null) { channel.CoveredTimePeriods = new TimePeriods(); } int currentChannelMessagesCount = 0; bool skipChannel = false; int time = to; int lastMessageTime = 0; if (channel.CoveredTimePeriods.ContainsLarger(time)) { lastMessageTime = time; } while (true) { if (_stop) { stopped = true; break; } if (time < from) { break; } time = channel.CoveredTimePeriods.LargestNonIncludedTimeFromPeriod(from, time); if (time < from || skipChannel) { break; } int addedMessagesCount = 0; List <Message> messages = new List <Message>(); TLInterfacePackage tl = new TLInterfacePackage(); tl = GetAvailableTLInterface(); try { messages = await tl.Interface.GetChannelMessagesAsync(channel.ChannelId, channel.AccessHash.Value, null, time, null); tl.LastCallTime = DateTime.Now; tl.IsAvailable = true; } catch (InvalidOperationException e) { Log("InvalidOperationException: " + e.Message); return(new JobResult(JobStatus.Stopped)); } if (messages.Count == 0) { break; } foreach (Message item in messages) { if (!db.IsMessageAdded(item.SourceChannel.ChannelId, item.MessageId)) { // extract usernames from message text foreach (string username in ExtractUsernamesHelper(item.Text)) { if (!db.IsChannelAdded(username)) { if (!db.IsUnresolvedUsernameAdded(new MongoUsername { Username = username })) { db.AddUnresolvedUsername(new MongoUsername { Username = username }); } } } // add message source channel if it's not added yet. if (!db.IsChannelAdded(item.SourceChannel.ChannelId)) { if (item.SourceChannel.AccessHash.HasValue) { await AddChannelHelper(item.SourceChannel.ChannelId, item.SourceChannel.AccessHash.Value, false); } } // add message to database MongoMessage messageToAdd = new MongoMessage { ChannelId = item.SourceChannel.ChannelId, MessageId = item.MessageId, Text = item.Text, Date = item.OriginalDate ?? item.Date, Views = new List <CountTime>() { item.Views } }; if (item.MediaContent != null) { if (string.IsNullOrEmpty(messageToAdd.Text)) { messageToAdd.Text = item.MediaContent.Caption; } messageToAdd.MediaType = item.MediaContent.Type; messageToAdd.MediaSize = item.MediaContent.Size; messageToAdd.FileName = item.MediaContent.FileName; } db.AddMessage(messageToAdd); addedMessagesCount++; } else if (messages.Count == 1) { skipChannel = true; } } if (lastMessageTime == 0) { lastMessageTime = messages.Max(m => m.Date); } time = messages.Min(m => m.Date); channel.CoveredTimePeriods.AddPeriod(new TimePeriod(time, lastMessageTime)); db.UpdateChannelCoveredTimePeriods(channel.ChannelId, channel.CoveredTimePeriods); currentChannelMessagesCount += addedMessagesCount; Log(string.Format("{0} new messages has been added from '{1}' (@{2}).", addedMessagesCount, channel.Title, channel.Username)); lastMessageTime = time; } totalMessagesCount += currentChannelMessagesCount; Log(string.Format("{0} total new messages has been added from '{1}' (@{2}).", currentChannelMessagesCount, channel.Title, channel.Username)); } if (stopped) { return(new JobResult(JobStatus.Stopped)); } string message = ""; if (totalMessagesCount == 0) { message = "No new message has been added."; } else if (totalMessagesCount == 1) { message = "1 new message has been added."; } else { message = string.Format("{0} new messages has been added.", totalMessagesCount); } return(new JobResult(message, JobStatus.Finished)); }