Weavy Hooks

A hook is an extension that is triggered in response to one, or more, system events (see Weavy.Core.Events for a list of available events).

Hook class

To add a hook that responds to an event you should first add a class that inherits from Weavy.Core.Models.Hook. Your class should also implement the Weavy.Core.Models.IAsyncHook<TEvent> interface for the event you want to handle. In the example below we add a hook that trashes a comment if it contains certain keywords.

using NLog;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Weavy.Core.Events;
using Weavy.Core.Models;
using Weavy.Core.Services;

namespace Wvy.Models {

    [Serializable]
    [Guid("CF2303B5-A8B1-4F91-8029-9F464338A8DC")]
    [Plugin(Description = "A hook that automatically trashes comments with certain keywords.")]
    public class CommentFilterHook : Hook, IAsyncHook<AfterInsertComment> {

        private static readonly Logger _log = LogManager.GetCurrentClassLogger();

        [Required]
        [DataType("Tags")]
        [Display(Name = "Keywords", Description = "A list of keywords to look for.")]
        public List<string> Keywords { get; set; }

        /// <summary>
        /// Trash comment if it contains one of the specified keywords.
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public Task HandleAsync(AfterInsertComment e) {
            if (Keywords != null) {
                foreach (var keyword in Keywords) {
                    if (e.Inserted.Text.IndexOf(keyword, StringComparison.OrdinalIgnoreCase) >= 0) {
                        var trashed = CommentService.Trash(e.Inserted.Id);
                        _log.Warn($"Trashed comment '{trashed.Text}' with keyword '{keyword}'");
                        break;
                    }
                }
            }
            return Task.CompletedTask;
        }
    }
}

Class attributes

As shown above, hook classes in Weavy must be decorated with the [Serializable] and [Guid] attributes.

Make sure to give your class a unique Guid, otherwise it will not be recognized by Weavy.

To further customize your hook you can also decorate it with the [Plugin] attribute. This attribute has the following properties:

  • Icon - name of an icon to use when displaying the hook in the manage UI.
  • Color - color to use for the icon.
  • Name - display name for the hook
  • Description - a description to use for the hook, e.g “A hook that automatically trashes comments with certain keywords”.

Fields

Hook fields are pieces of information that can be added to a hook. This can be useful if your hook requires some kind of configuration or settings. Hook fields have a name and a type. In the example above, we added a field for storing the keywords not allowed in comments.

For a property to be considered an hook field, it must be declared as public read-write, i.e. the property has the public access modifier and have both a get and a set accessor.

Hook fields must also have one of the following supported types:

  • enum
  • byte
  • short
  • int
  • long
  • bool
  • double
  • float
  • string
  • Guid
  • DateTime
  • TimeSpan

Nullables and Lists of the above types are also supported, e.g. int?, List<string> and List<DateTime?>.

Field attributes

By decorating your fields with one, or more, of the following attributes you can customize how the field is displayed, edited and/or validated.

Datatype

By default, Weavy will look at the property type (in this case IList<string>) when deciding which editor to use for the field. By decorating a field with the [DataType] attribute you can specify an additional type to associate with the field. This value will then be used by the UI to determine which editor to use for the field.

[DataType("Tags")]
public List<string> Keywords { get; set; }

Here we specify that the Keywords field should use the Tags editor.

Display

The [Display] attribute lets you specify a name and description to use for the field.

[Display(Name = "Keywords", Description = "A list of keywords to look for.")]
public List<string> Keywords { get; set; }

Validation

By adding one, or more, [Validation] attributes to your fields you can control how they are validated.

[Required]
public List<string> Keywords { get; set; }

Here we specify that the Keywords property is required.

To control validation even more you can also let your class implement IValidatableObject and add your own custom validation logic.