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.
Model
To add a hook that responds to an event you should first add a class that inherits from Hook
.
Your class should also implement the IHook<TEvent>
interface for the event you want to handle.
A good convention to follow is placing your class in the Models
folder.
In the example below we add a hook that listens to the AfterInsertComment
event and trashes the inserted 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 Weavy.Models {
[Serializable]
[Guid("CF2303B5-A8B1-4F91-8029-9F464338A8DC")]
[Plugin(Description = "A hook that automatically trashes comments with certain keywords.")]
public class CommentFilterHook : Hook, IHook<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>
public void Handle(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;
}
}
}
}
}
}
Class attributes
As shown above, hooks must be decorated with the [Serializable]
and [Guid]
attributes.
To further customize your hook you can also decorate it with the[Plugin]
attribute. Some of the properties you can set are:
Icon
– name of an icon to use when displaying the daemon.Color
– the color to use for the icon.Name
– display name for the content item, e.g. “Comment filter”.Description
– a description to use for the daemon, 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 a 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.
IValidatableObject
and add your own validation logic.
Configuration
If a hook requires some kind of configuration settings, like the Keywords
property in our CommentFilterHook
,
you can navigate to /manage/hooks/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX where the last segment should be replaced with the Guid of your Hook,
in this case CF2303B5-A8B1-4F91-8029-9F464338A8DC for the CommentFilterHook
.