2017-10-04 13:14:44 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using DML.Application.Classes;
|
2017-10-04 15:01:54 +02:00
|
|
|
|
using DML.Client;
|
2017-10-04 13:14:44 +02:00
|
|
|
|
using SweetLib.Utils;
|
2018-04-23 21:40:17 +02:00
|
|
|
|
using SweetLib.Utils.Extensions;
|
2017-10-04 13:14:44 +02:00
|
|
|
|
using static SweetLib.Utils.Logger.Logger;
|
|
|
|
|
|
|
|
|
|
namespace DML.AppCore.Classes
|
|
|
|
|
{
|
|
|
|
|
public class Job
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
public ulong GuildId { get; set; }
|
|
|
|
|
public ulong ChannelId { get; set; }
|
|
|
|
|
public double KnownTimestamp { get; set; } = 0;
|
|
|
|
|
private double StopTimestamp { get; set; } = 0;
|
|
|
|
|
private bool IsValid { get; set; } = true;
|
|
|
|
|
|
|
|
|
|
internal void Store()
|
|
|
|
|
{
|
|
|
|
|
Debug("Storing job to database...");
|
|
|
|
|
Trace("Getting jobs collection...");
|
|
|
|
|
var jobDb = Core.Database.GetCollection<Job>("jobs");
|
|
|
|
|
|
|
|
|
|
Trace("Adding new value...");
|
|
|
|
|
|
|
|
|
|
if (jobDb.Find(x => x.ChannelId == ChannelId && x.GuildId == GuildId).Any())
|
|
|
|
|
{
|
|
|
|
|
jobDb.Update(this);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
jobDb.Insert(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Delete()
|
|
|
|
|
{
|
|
|
|
|
Debug("Deleting job from database...");
|
|
|
|
|
Trace("Getting jobs collection...");
|
|
|
|
|
var jobDb = Core.Database.GetCollection<Job>("jobs");
|
|
|
|
|
|
|
|
|
|
Trace("Deleting value...");
|
|
|
|
|
jobDb.Delete(Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private SocketGuild FindServerById(ulong id)
|
|
|
|
|
{
|
|
|
|
|
Trace($"Trying to find server by Id: {id}");
|
2017-10-04 15:01:54 +02:00
|
|
|
|
return (from s in DMLClient.Client.Guilds where s.Id == id select s).FirstOrDefault();
|
2017-10-04 13:14:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private SocketTextChannel FindChannelById(SocketGuild server, ulong id)
|
|
|
|
|
{
|
|
|
|
|
Trace($"Trying to find channel in {server} by id: {id}");
|
|
|
|
|
return (from c in server.TextChannels where c.Id == id select c).FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-02 19:57:10 +02:00
|
|
|
|
internal async Task<bool> Scan()
|
2017-10-04 13:14:44 +02:00
|
|
|
|
{
|
|
|
|
|
Debug($"Starting scan of guild {GuildId} channel {ChannelId}...");
|
2017-10-04 15:01:54 +02:00
|
|
|
|
var result = new List<IMessage>();
|
2017-10-04 13:14:44 +02:00
|
|
|
|
|
|
|
|
|
var limit = 100;
|
|
|
|
|
var lastId = ulong.MaxValue;
|
|
|
|
|
var isFirst = true;
|
|
|
|
|
var finished = false;
|
2018-05-02 19:57:10 +02:00
|
|
|
|
var scanStartTimeStamp = DateTime.UtcNow;
|
2017-10-04 13:14:44 +02:00
|
|
|
|
|
|
|
|
|
var guild = FindServerById(GuildId);
|
|
|
|
|
var channel = FindChannelById(guild, ChannelId);
|
|
|
|
|
|
2017-10-04 15:01:54 +02:00
|
|
|
|
Debug("Checking channel access");
|
2018-01-22 20:08:08 +01:00
|
|
|
|
if (channel.GetUser(channel.Guild.CurrentUser.Id) == null)
|
2017-10-04 15:01:54 +02:00
|
|
|
|
{
|
|
|
|
|
Info("Skipping channel without access");
|
2018-05-02 19:57:10 +02:00
|
|
|
|
return true;
|
2017-10-04 15:01:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 13:14:44 +02:00
|
|
|
|
if (Math.Abs(StopTimestamp) < 0.4)
|
|
|
|
|
StopTimestamp = KnownTimestamp;
|
|
|
|
|
Trace("Initialized scanning parameters.");
|
|
|
|
|
|
|
|
|
|
while (!finished)
|
|
|
|
|
{
|
|
|
|
|
Trace("Entering scanning loop...");
|
2017-10-04 15:01:54 +02:00
|
|
|
|
var messages = new List<IMessage>();
|
2017-10-04 13:14:44 +02:00
|
|
|
|
|
|
|
|
|
Trace($"Downloading next {limit} messages...");
|
|
|
|
|
if (isFirst)
|
|
|
|
|
{
|
2017-10-04 15:01:54 +02:00
|
|
|
|
var realMessages = await channel.GetMessagesAsync(limit).ToArray();
|
|
|
|
|
|
2018-02-01 21:17:12 +01:00
|
|
|
|
messages.AddRange(realMessages.SelectMany(realMessageArray => realMessageArray));
|
2017-10-04 13:14:44 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-10-04 15:01:54 +02:00
|
|
|
|
var realMessages = await channel.GetMessagesAsync(lastId, Direction.Before, limit).ToArray();
|
|
|
|
|
|
2018-02-01 21:17:12 +01:00
|
|
|
|
messages.AddRange(realMessages.SelectMany(realMessageArray => realMessageArray));
|
2017-10-04 13:14:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 21:17:12 +01:00
|
|
|
|
Trace($"Downloaded {messages.Count} messages.");
|
2017-10-04 13:14:44 +02:00
|
|
|
|
isFirst = false;
|
|
|
|
|
|
|
|
|
|
foreach (var m in messages)
|
|
|
|
|
{
|
|
|
|
|
if (!IsValid)
|
2018-05-02 19:57:10 +02:00
|
|
|
|
return false;
|
2017-10-04 13:14:44 +02:00
|
|
|
|
|
2018-01-22 20:08:08 +01:00
|
|
|
|
Core.Scheduler.MessagesScanned++;
|
|
|
|
|
|
2017-10-04 13:14:44 +02:00
|
|
|
|
Debug($"Processing message {m.Id}");
|
|
|
|
|
if (m.Id < lastId)
|
|
|
|
|
{
|
|
|
|
|
Trace($"Updating lastId ({lastId}) to {m.Id}");
|
|
|
|
|
lastId = m.Id;
|
|
|
|
|
}
|
2018-05-02 19:57:10 +02:00
|
|
|
|
|
2018-04-23 21:40:17 +02:00
|
|
|
|
if (m.CreatedAt.UtcDateTime.ToUnixTimeStamp() <= StopTimestamp)
|
2017-10-04 13:14:44 +02:00
|
|
|
|
{
|
|
|
|
|
Debug("Found a message with a known timestamp...Stopping scan.");
|
|
|
|
|
finished = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Trace($"Message {m.Id} has {m.Attachments.Count} attachments.");
|
|
|
|
|
if (m.Attachments.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
result.Add(m);
|
2017-10-12 21:05:06 +02:00
|
|
|
|
Core.Scheduler.TotalAttachments += (ulong)m.Attachments.Count;
|
2017-10-04 13:14:44 +02:00
|
|
|
|
Trace($"Added message {m.Id}");
|
|
|
|
|
}
|
|
|
|
|
Debug($"Finished message {m.Id}");
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 15:01:54 +02:00
|
|
|
|
finished = finished || messages.Count < limit;
|
2017-10-04 13:14:44 +02:00
|
|
|
|
}
|
2018-02-01 21:17:12 +01:00
|
|
|
|
|
2017-10-04 13:14:44 +02:00
|
|
|
|
Trace($"Downloaded all messages for guild {GuildId} channel {ChannelId}.");
|
|
|
|
|
|
|
|
|
|
Trace("Sorting messages...");
|
|
|
|
|
result.Sort((a, b) => DateTime.Compare(a.CreatedAt.UtcDateTime, b.CreatedAt.UtcDateTime));
|
|
|
|
|
|
2018-02-01 21:17:12 +01:00
|
|
|
|
foreach (var r in result)
|
|
|
|
|
{
|
|
|
|
|
foreach (var a in r.Attachments)
|
|
|
|
|
{
|
|
|
|
|
var mediaData = new MediaData
|
|
|
|
|
{
|
|
|
|
|
Id = a.Id,
|
|
|
|
|
GuildId = (r.Channel as SocketTextChannel)?.Guild?.Id ?? 0,
|
|
|
|
|
ChannelId = r.Channel.Id,
|
|
|
|
|
DownloadSource = a.Url,
|
|
|
|
|
Filename = a.Filename,
|
2018-04-23 21:59:29 +02:00
|
|
|
|
TimeStamp = r.CreatedAt.UtcDateTime.ToUnixTimeStamp(),
|
2018-02-01 21:36:16 +01:00
|
|
|
|
FileSize = a.Size
|
2018-02-01 21:17:12 +01:00
|
|
|
|
};
|
|
|
|
|
mediaData.Store();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 13:14:44 +02:00
|
|
|
|
if (result.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
Trace("Updating StopTimestamp for next scan...");
|
2018-04-23 21:40:17 +02:00
|
|
|
|
StopTimestamp = result[result.Count - 1].CreatedAt.UtcDateTime.ToUnixTimeStamp();
|
2018-02-05 20:33:40 +01:00
|
|
|
|
KnownTimestamp = StopTimestamp;
|
|
|
|
|
Store();
|
2018-05-02 19:57:10 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// if we found any messages we remember the timestamp of starting so we don´t have to scan all past messages....
|
|
|
|
|
StopTimestamp = scanStartTimeStamp.ToUnixTimeStamp();
|
|
|
|
|
KnownTimestamp = StopTimestamp;
|
|
|
|
|
Store();
|
|
|
|
|
|
|
|
|
|
var realLastMessage = await channel.GetMessagesAsync(1).ToArray();
|
|
|
|
|
return scanStartTimeStamp > (realLastMessage.SelectMany(realLastMessageArray => realLastMessageArray)
|
|
|
|
|
.FirstOrDefault()?.CreatedAt.UtcDateTime ??
|
|
|
|
|
scanStartTimeStamp);
|
2017-10-04 13:14:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Debug($"Fisnished scan of guild {GuildId} channel {ChannelId}.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
|
|
|
|
IsValid = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IEnumerable<Job> RestoreJobs()
|
|
|
|
|
{
|
|
|
|
|
Debug("Restoring jobs...");
|
|
|
|
|
Trace("Getting jobs collection...");
|
|
|
|
|
var jobDb = Core.Database.GetCollection<Job>("jobs");
|
|
|
|
|
|
|
|
|
|
Trace("Creating new empty job list");
|
|
|
|
|
return jobDb.FindAll();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|