/// <summary> /// retreives an updated channellist from the mednanet API asyncronously /// once the await has completed DiscordVisualHandler will asyncronously update the channels UI /// </summary> private async void GetChannelsAsync() { // allow only one poll of this type at a time if (ChannelsIsPolling == true) { return; } ChannelsIsPolling = true; try { // get channels from MednaNet API Channels = (await Client.Channels.GetChannels()).ToList(); // update visual handler once await has completed ChannelsIsPolling = false; // update channel list foreach (var c in Channels.OrderBy(a => a.discordId).ToList()) { DVH.channels.UpdateChannel(c.id, c.channelName); } // add invisible channel with ID 0 (for initial display) DVH.channels.UpdateChannel(0, "initChannel"); // Update the UI DVH.UpdateChannelButtons(); // make sure at least one channel is selected DVH.CheckChannelSelection(); } catch (Exception ex) { APIDisconnected(ex); return; } }
/// <summary> /// keeps the sqlite log DB in sync with the local in-memory object /// </summary> private async Task SyncMessageListAsync() { // allow only one poll of this type at a time if (MessageDBSync == true) { return; } MessageDBSync = true; try { // get channels from MednaNet API Channels = (await Client.Channels.GetChannels()).ToList(); // update visual handler once await has completed MessageDBSync = false; // update channel list foreach (var c in Channels.OrderBy(a => a.discordId).ToList()) { DVH.channels.UpdateChannel(c.id, c.channelName); } // Update the UI DVH.UpdateChannelButtons(); // make sure at least one channel is selected DVH.CheckChannelSelection(); } catch (Exception ex) { APIDisconnected(ex); return; } }
public async void DoPoll() { AbortThread = true; isPolling = true; Timer.Stop(); if (isConnected == false) { return; } /* Set Username */ // if the specified local username differs from API, set it if (Username != CurrentInstall.username) { CurrentInstall.username = Username; try { await Client.Install.UpdateUsername(CurrentInstall); } catch (Exception ex) { APIDisconnected(ex); return; } MednaNetSettings.SetUsername(Username); } /* Update Channels */ try { Channels = await Client.Channels.GetChannels(); } catch (Exception ex) { APIDisconnected(ex); return; } // update channel list foreach (var c in Channels.OrderBy(a => a.discordId)) { DVH.channels.UpdateChannel(c.id, c.channelName); } // Update the UI DVH.UpdateChannelButtons(); // make sure at least one channel is selected DVH.CheckChannelSelection(); /* Update Users */ // query API try { Users = await Client.Users.GetAllUsers(); } catch (Exception ex) { APIDisconnected(ex); return; } // update user list foreach (var u in Users) { if (u.discordId == null) // && u.isOnline == false) { //continue; } ClientType ct = ClientType.discord; if (u.discordId == null) { ct = ClientType.medlaunch; } DVH.users.UpdateUser(u.id, u.username, ct, true); } // Update the UI DVH.UpdateUsers(); /* Update messages */ List <Messages> foundMessages = new List <Messages>(); // loop through each channel foreach (var c in Channels) { // get the last received message from this channel //var lookup = DiscordMessages.GetLastMessageId(c.id); int lookup = 0; var lookupA = AllMessages.Where(a => a.channel == c.id).OrderBy(b => b.id).LastOrDefault(); if (lookupA != null) { lookup = lookupA.id; } if (lookup == 0) { // no message found - get all messages using the timeframe specified try { foundMessages.AddRange((await Client.Channels.GetChannelMessagesFrom(c.id, DateTime.Now.AddMinutes(MessageHistoryInMinutes))).ToList()); } catch (Exception ex) { APIDisconnected(ex); return; } } else { // result found - get messages from the API after this message ID try { foundMessages.AddRange((await Client.Channels.GetChannelMessagesAfterMessageId(c.id, lookup)).ToList()); } catch (Exception ex) { APIDisconnected(ex); return; } } // get the last messageId for this channel already posted to the screen int lastWritten = 0; if (LastChannelMessages.ContainsKey(c.id)) { lastWritten = LastChannelMessages[c.id]; } // get the last messageId received on this channel int lastId = lookup; //var look2 = DiscordMessages.GetLastMessageId(c.id); int look2 = 0; var look2a = AllMessages.Where(a => a.channel == c.id).OrderBy(b => b.id).LastOrDefault(); if (look2a != null) { look2 = look2a.id; } if (look2 > lookup) { lastId = look2; } // update the dictionary LastChannelMessages[c.id] = lastId; } // add foundMessages to the database //DiscordMessages.SaveToDatabase(foundMessages); // add foundMessages to AllMessages AllMessages.AddRange(foundMessages); AllMessages.Distinct(); // now post new messages based on the currently selected channel foreach (var c in Channels) { int last = LastChannelMessages[c.id]; //var newMessages = DiscordMessages.GetAllMessages().Where(a => a.MessageId > last).OrderBy(b => b.MessageId).ToList(); var newMessages = AllMessages.Where(a => a.id > last).OrderBy(b => b.id).ToList().OrderBy(d => d.id).ToList(); foreach (var me in newMessages) { // create discordmessage format DiscordMessage d = new DiscordMessage(); d.channel = me.channel; d.code = me.code; d.message = me.message; d.messageId = me.id; //d.name = me.name; d.postedOn = me.postedOn; // write the message to the relevant local channel MednaNetAPI.Instance.DVH.PostMessage(d); //DVH.PostMessage(me, c.id); } } AbortThread = false; isPolling = false; Timer.Start(); /* * foreach (var c in Channels) * { * // get last message ID recorded for this channel * var lookup = MessageLog.GetLastChannelMessage(c.id); * * if (lookup == null) * { * // no messages found locally for this channel, get all messages from the last day * try * { * var messages = await Client.Channels.GetChannelMessagesFrom(c.id, DateTime.Now.AddMinutes(-600)); * * // add to new messages * foreach (var m in messages.OrderBy(a => a.id)) * { * if (m.channel == 1) * { * * } * * // check that is isnt one that we have posted ourself * var allZeros = from a in MednaNetAPI.Instance.MessageArchive * where a.APIMessage.channel == c.id && a.APIMessage.id == 0 && a.APIMessage.message == m.message && a.HasBeenParsed == true * select a; * * if (allZeros.Count() > 0) * { * // message found, do nothing * } * else * { * MessageLog.AddMessage(m); * } * } * } * catch (Exception ex) { APIDisconnected(ex); return; } * * * } * else * { * // query the API for newer messages * try * { * var messages = await Client.Channels.GetChannelMessagesAfterMessageId(c.id, lookup.id); * // add to new messages * foreach (var m in messages) * { * MessageLog.AddMessage(m); * } * } * catch (Exception ex) { APIDisconnected(ex); return; } * } * } * * // process new messages * MessageLog.PostNewMessages(); * AbortThread = false; * isPolling = false; * Timer.Start(); * */ }