public BotMessage ToBotMessage(RemoteIDEModel model)
        {
            var message = new BotMessage()
            {
                Text = $"{model.IDEUserIdentity.DisplayName} is doing some coding!"
            };

            var attachment = new SlackAttachment()
            {
                ThumbUrl   = model.IDEUserIdentity.ImageUrl,
                AuthorName = model.IDEUserIdentity.DisplayName,
                AuthorLink = model.IDEUserIdentity.Id,
                AuthorIcon = model.IDEUserIdentity.ImageUrl,
                ColorHex   = new System.Windows.Media.ColorConverter().ConvertToString(model.IDEUserIdentity.GetUserColour()),
                Fallback   = model.OpenFiles.Count == 0 ?
                             $"{ model.IDEUserIdentity.DisplayName} has no files open." :
                             $"{model.IDEUserIdentity.DisplayName} has {model.OpenFiles.Count} files open in {model.OpenFiles.FirstOrDefault()?.RepoUrl}"
            };

            foreach (var field in GetSlackFields(model).OrderBy(f => f.Title.Count(c => c == '.')).ThenBy(f => f.Title))
            {
                attachment.Fields.Add(field);
            }

            message.Attachments.Add(attachment);

            return(message);
        }
Beispiel #2
0
 public void OnRemoteModelReceived(RemoteIDEModel remoteModel)
 {
     TeamCodingPackage.Current.IDEWrapper.InvokeAsync(() =>
     {
         CachedOpenFiles = null;
         if (remoteModel == null)
         {
             ClearRemoteModels();
             RemoteModelReceived?.Invoke(this, EventArgs.Empty);
         }
         else if (remoteModel.Id == LocalIDEModel.Id.Value && !TeamCodingPackage.Current.Settings.UserSettings.ShowSelf)
         {
             // If the remote model is the same as the local model, then remove it if it's there already
             if (RemoteModels.ContainsKey(remoteModel.Id))
             {
                 RemoteModels.Remove(remoteModel.Id);
                 RemoteModelReceived?.Invoke(this, EventArgs.Empty);
             }
         }
         else if (remoteModel.OpenFiles.Count == 0)
         {
             if (RemoteModels.ContainsKey(remoteModel.Id))
             {
                 RemoteModels.Remove(remoteModel.Id);
                 RemoteModelReceived?.Invoke(this, EventArgs.Empty);
             }
         }
         else
         {
             RemoteModels[remoteModel.Id] = remoteModel;
             RemoteModelReceived?.Invoke(this, EventArgs.Empty);
         }
     });
 }
 protected override void SendModel(RemoteIDEModel remoteModel)
 {
     using (var ms = new MemoryStream())
     {
         ProtoBuf.Serializer.Serialize(ms, remoteModel);
         TeamCodingPackage.Current.Redis.PublishAsync(RedisRemoteModelPersister.ModelPersisterChannel, ms.ToArray()).HandleException();
     }
 }
Beispiel #4
0
 protected override void SendModel(RemoteIDEModel model)
 {
     if (PersistenceFolderPath != null && Directory.Exists(PersistenceFolderPath))
     {
         // Delete any temporary persistence files
         foreach (var file in Directory.EnumerateFiles(PersistenceFolderPath, PersistenceFileSearchFormat))
         {
             if (File.Exists(file) && file != PersistenceFilePath)
             {
                 File.Delete(file);
             }
         }
     }
     base.SendModel(model);
 }
Beispiel #5
0
        public void UpdateModel(RemoteIDEModel remoteModel)
        {
            using (var ms = new MemoryStream())
            {
                ProtoBuf.Serializer.Serialize(ms, remoteModel);
                LastSqlWriteTime = DateTime.UtcNow;

                using (var connection = GetConnection)
                {
                    connection?.ExecuteWithLogging(MergeQuery, new QueryData()
                    {
                        Id = remoteModel.Id, Model = ms.ToArray(), LastUpdated = LastSqlWriteTime
                    });
                }
            }
        }
 protected override void SendModel(RemoteIDEModel remoteModel)
 {
     if (!string.IsNullOrEmpty(PersistenceFolderPath))
     {
         try
         {
             using (var f = File.Create(PersistenceFilePath))
             {
                 ProtoBuf.Serializer.Serialize(f, remoteModel);
                 LastFileWriteTime = DateTime.UtcNow;
             }
         }
         catch (IOException ex)
         {
             TeamCodingPackage.Current.Logger.WriteError("Failed to create persistence file.");
             TeamCodingPackage.Current.Logger.WriteError(ex);
         }
     }
 }
Beispiel #7
0
        public void SendModel(RemoteIDEModel model)
        {
            if (Socket != null)
            {
                using (var ms = new MemoryStream())
                {
                    ProtoBuf.Serializer.Serialize(ms, model);

                    var buffer = ASBuffer.New((int)ms.Length + sizeof(short));
                    ASBuffer.ClearBuffer(buffer);
                    // TODO: Remove AwesomeSocket and roll my own or find another because of crap like this
                    ASBuffer.Add(buffer, BitConverter.GetBytes((short)ms.Length).Concat(ms.ToArray()).ToArray());
                    ASBuffer.FinalizeBuffer(buffer);
                    try
                    {
                        Socket.SendMessage(buffer);
                    }
                    catch (Exception ex)
                    {
                        TeamCodingPackage.Current.Logger.WriteError(ex);
                    }
                }
            }
        }
Beispiel #8
0
 protected override void SendModel(RemoteIDEModel remoteModel)
 {
     TeamCodingPackage.Current.Slack.PublishAsync(TeamCodingPackage.Current.ObjectSlackMessageConverter.ToBotMessage(remoteModel)).HandleException();
 }
Beispiel #9
0
 protected abstract void SendModel(RemoteIDEModel remoteModel);
        public RemoteIDEModel ToIdeModel(BotMessage receivedMessage)
        {
            var result = new RemoteIDEModel();

            foreach (var slackField in receivedMessage.Attachments.SelectMany(a => a.Fields))
            {
                var propNames    = slackField.Title.Split('.');
                var lastPropName = propNames.Last();
                propNames = propNames.Take(propNames.Length - 1).ToArray();

                var propObj = (object)result;

                foreach (var propName in propNames)
                {
                    var trimmedPropName = propName;
                    int?arrayIndex      = null;

                    if (trimmedPropName.Contains('['))
                    {
                        arrayIndex      = int.Parse(trimmedPropName.Substring(trimmedPropName.IndexOf('[') + 1, trimmedPropName.IndexOf(']') - trimmedPropName.IndexOf('[') - 1));
                        trimmedPropName = trimmedPropName.Substring(0, trimmedPropName.IndexOf('['));
                    }

                    var prop  = propObj.GetType().GetProperty(trimmedPropName);
                    var field = propObj.GetType().GetField(trimmedPropName);
                    if (prop != null)
                    {
                        if (prop.GetValue(propObj) == null)
                        {
                            prop.SetValue(propObj, Activator.CreateInstance(prop.PropertyType));
                        }
                        propObj = prop.GetValue(propObj);
                    }
                    else if (field != null)
                    {
                        if (field.GetValue(propObj) == null)
                        {
                            field.SetValue(propObj, Activator.CreateInstance(field.FieldType));
                        }
                        propObj = field.GetValue(propObj);
                    }
                    else
                    {
                        throw new InvalidDataException($"Invalid property/field name found: {trimmedPropName} in sequence {slackField.Title}.");
                    }

                    if (arrayIndex != null)
                    {
                        var listObj = (System.Collections.IList)propObj;
                        // NOTE: This might need changing to be simiar to the Array handling below if we use this for objects with an array of a coplex type (not the final property)
                        while (listObj.Count < arrayIndex + 1)
                        {
                            listObj.Add(Activator.CreateInstance(listObj.GetType().GetGenericArguments()[0]));
                        }

                        propObj = listObj[arrayIndex.Value];
                    }
                }

                int?lastPropIndex = null;
                if (lastPropName.Contains('['))
                {
                    lastPropIndex = int.Parse(lastPropName.Substring(lastPropName.IndexOf('[') + 1, lastPropName.IndexOf(']') - lastPropName.IndexOf('[') - 1));
                    lastPropName  = lastPropName.Substring(0, lastPropName.IndexOf('['));

                    var lastProp  = propObj.GetType().GetProperty(lastPropName);
                    var lastField = propObj.GetType().GetField(lastPropName);
                    var lastObj   = propObj;

                    if (lastProp != null)
                    {
                        lastObj = lastProp.GetValue(propObj);
                        if (lastObj == null)
                        {
                            if (lastProp.PropertyType.IsSubclassOf(typeof(Array)))
                            {
                                lastObj = Activator.CreateInstance(lastProp.PropertyType, 0);
                            }
                            else
                            {
                                lastObj = Activator.CreateInstance(lastProp.PropertyType);
                            }
                        }
                    }
                    else if (lastField != null)
                    {
                        lastObj = lastField.GetValue(propObj);
                        if (lastObj == null)
                        {
                            if (lastProp.PropertyType.IsSubclassOf(typeof(Array)))
                            {
                                lastObj = Activator.CreateInstance(lastField.FieldType, 0);
                            }
                            else
                            {
                                lastObj = Activator.CreateInstance(lastField.FieldType);
                            }
                        }
                    }
                    else
                    {
                        throw new InvalidDataException($"Invalid property/field name found: {lastPropName} in sequence {slackField.Title}.");
                    }


                    var listObj  = (System.Collections.IList)lastObj;
                    var arrayObj = lastObj as Array;

                    if (arrayObj != null)
                    {
                        if (arrayObj.Length < lastPropIndex + 1)
                        {
                            var newArray = (Array)Activator.CreateInstance(arrayObj.GetType(), lastPropIndex + 1);
                            Array.Copy(arrayObj, newArray, arrayObj.Length);
                            arrayObj = newArray;
                        }

                        arrayObj.SetValue(Convert.ChangeType(slackField.Value, arrayObj.GetType().GetElementType()), lastPropIndex.Value);
                        lastProp.SetValue(propObj, arrayObj);
                    }
                    else if (listObj != null)
                    {
                        while (listObj.Count < lastPropIndex + 1)
                        {
                            listObj.Add(Activator.CreateInstance(listObj.GetType().GetGenericArguments()[0]));
                        }

                        listObj[lastPropIndex.Value] = Convert.ChangeType(slackField.Value, listObj.GetType().GetGenericArguments()[0]);
                        lastProp.SetValue(propObj, listObj);
                    }
                    else
                    {
                        throw new InvalidDataException($"Property/field had brackets [] but is not an IList or Array: {lastPropName} in sequence {slackField.Title}.");
                    }
                }
                else
                {
                    var lastProp  = propObj.GetType().GetProperty(lastPropName);
                    var lastField = propObj.GetType().GetField(lastPropName);
                    if (lastProp != null)
                    {
                        lastProp.SetValue(propObj, Convert.ChangeType(slackField.Value, lastProp.PropertyType));
                    }
                    else if (lastField != null)
                    {
                        lastField.SetValue(propObj, Convert.ChangeType(slackField.Value, lastField.FieldType));
                    }
                    else
                    {
                        throw new InvalidDataException($"Invalid property/field name found: {lastPropName} in sequence {slackField.Title}.");
                    }
                }
            }

            return(result);
        }
 protected override void SendModel(RemoteIDEModel remoteModel) => ConnectionWrapper.UpdateModel(remoteModel);
 protected override void SendModel(RemoteIDEModel remoteModel)
 {
     Client.SendModel(remoteModel);
 }