Skip to content

Commit fd81c8f

Browse files
committed
added support for item added
1 parent 8696f78 commit fd81c8f

File tree

4 files changed

+88
-93
lines changed

4 files changed

+88
-93
lines changed

Emby.Webhooks/Configuration/PluginConfiguration.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ public PluginConfiguration()
1818
public class Hook
1919
{
2020
public string URL { get; set; }
21+
2122
public bool onPlay { get; set; }
2223
public bool onPause { get; set; }
2324
public bool onStop { get; set; }
2425
public bool onResume { get; set; }
26+
public bool onItemAdded { get; set; }
27+
2528
public bool withMovies { get; set; }
2629
public bool withEpisodes { get; set; }
2730
public bool withSongs { get; set; }
28-
public bool withNotifications { get; set; }
31+
2932
}
3033
}
3134
}

Emby.Webhooks/Configuration/config.html

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ <h3>Events</h3>
3737
<span>Resume</span>
3838
</label>
3939
<label class="checkboxContainer" style="float:left; width:auto; padding-right:10px">
40-
<input is="emby-checkbox" type="checkbox" class="chkNotifications" />
41-
<span>Notifications</span>
42-
</label>
40+
<input is="emby-checkbox" type="checkbox" class="chkOnItemAdded" />
41+
<span>Item Added</span>
42+
</label>
43+
44+
4345
</div>
4446
<div>
4547
<h3>Media Types</h3>
@@ -49,22 +51,19 @@ <h3>Media Types</h3>
4951
</label>
5052
<label class="checkboxContainer" style="float:left; width:auto; padding-right:10px">
5153
<input is="emby-checkbox" type="checkbox" class="chkEpisodes" />
52-
<span>Episodes</span>
54+
<span>TV Shows</span>
5355
</label>
5456
<label class="checkboxContainer" style="float:left; width:auto; padding-right:10px">
5557
<input is="emby-checkbox" type="checkbox" class="chkSongs" />
56-
<span>Songs</span>
58+
<span>Music</span>
5759
</label>
5860
</div>
59-
<div><h3>Notifications</h3>
60-
<label class="checkboxContainer" style="float:left; width:auto; padding-right:10px">
61-
<input is="emby-checkbox" type="checkbox" class="chkNotifications" />
62-
<span>Notifications</span>
63-
</label>
64-
</div>
61+
62+
6563

66-
</div>
67-
<button title="Delete" class="btnDeleteDevice paper-icon-button-light removeHook" type="button" is="paper-icon-button-light"><i class="md-icon">delete</i></button>
64+
65+
</div>
66+
<button title="Delete" class="btnDeleteDevice paper-icon-button-light removeHook" type="button" is="paper-icon-button-light"><i class="md-icon">delete</i></button>
6867

6968
</div>
7069
</div>
@@ -216,7 +215,7 @@ <h1 style="display:inline-block;vertical-align:middle;">Webhooks</h1>
216215
withMovies: $(this).find('.chkMovies').first().checked(),
217216
withEpisodes: $(this).find('.chkEpisodes').first().checked(),
218217
withSongs: $(this).find('.chkSongs').first().checked(),
219-
withNotifications: $(this).find('.chkNotifications').first().checked()
218+
onItemAdded: $(this).find('.chkOnItemAdded').first().checked()
220219

221220
}
222221
);
@@ -259,7 +258,7 @@ <h1 style="display:inline-block;vertical-align:middle;">Webhooks</h1>
259258
$(a).find('.chkMovies').first().checked(config.Hooks[i].withMovies || false);
260259
$(a).find('.chkEpisodes').first().checked(config.Hooks[i].withEpisodes || false);
261260
$(a).find('.chkSongs').first().checked(config.Hooks[i].withSongs || false);
262-
$(a).find('.chkNotifications').first().checked(config.Hooks[i].withNotifications || false);
261+
$(a).find('.chkOnItemAdded').first().checked(config.Hooks[i].onItemAdded || false);
263262

264263

265264

Emby.Webhooks/Webhooks.cs

Lines changed: 69 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@
1414
using MediaBrowser.Common.Net;
1515
using System.Net.Http;
1616
using MediaBrowser.Controller.Notifications;
17-
using MediaBrowser.Controller.Entities;
1817
using System.Threading;
18+
using MediaBrowser.Controller.Entities;
1919

2020
namespace Emby.Webhooks
2121
{
2222

23-
public class Webhooks : IServerEntryPoint, INotificationService
23+
public class Webhooks : IServerEntryPoint
2424
{
2525
private readonly ISessionManager _sessionManager;
2626
private readonly IUserDataManager _userDataManager;
2727
private readonly ILogger _logger;
2828
private readonly IJsonSerializer _jsonSerializer;
29+
private readonly ILibraryManager _libraryManager;
2930

3031
private List<PauseControl> pauseControl = new List<PauseControl>();
3132
public class PauseControl
@@ -52,10 +53,10 @@ public string Name
5253
get{ return "Webhooks";}
5354
}
5455

55-
public Webhooks(ISessionManager sessionManager, IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IUserDataManager userDataManager)
56+
public Webhooks(ISessionManager sessionManager, IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IUserDataManager userDataManager, ILibraryManager libraryManager)
5657
{
5758
_logger = logManager.GetLogger(Plugin.Instance.Name);
58-
59+
_libraryManager = libraryManager;
5960
_sessionManager = sessionManager;
6061
_userDataManager = userDataManager;
6162
_jsonSerializer = jsonSerializer;
@@ -69,65 +70,66 @@ public void Dispose()
6970
_sessionManager.PlaybackStart -= PlaybackStart;
7071
_sessionManager.PlaybackStopped -= PlaybackStopped;
7172
_sessionManager.PlaybackProgress -= PlaybackProgress;
73+
74+
_libraryManager.ItemAdded -= ItemAdded;
7275
}
7376

74-
7577
public void Run()
7678
{
7779
_sessionManager.PlaybackStart += PlaybackStart;
7880
_sessionManager.PlaybackStopped += PlaybackStopped;
7981
_sessionManager.PlaybackProgress += PlaybackProgress;
82+
83+
_libraryManager.ItemAdded += ItemAdded;
8084
}
8185

86+
private void ItemAdded(object sender, ItemChangeEventArgs e) {
87+
var iType = _libraryManager.GetContentType(e.Item);
88+
89+
var hooks = hooksByType(iType).Where(i => i.onItemAdded);
90+
91+
if (hooks.Count() > 0)
92+
{
93+
var jsonString = buildJson(e.Item, "media.added");
94+
SendHooks(hooks, jsonString);
95+
}
96+
}
8297
private void PlaybackProgress(object sender, PlaybackProgressEventArgs e)
8398
{
84-
if (e.IsPaused & getPauseControl(e.DeviceId).wasPaused == false) {
99+
var iType = _libraryManager.GetContentType(e.Item);
100+
101+
if (e.IsPaused & getPauseControl(e.DeviceId).wasPaused == false)
102+
{
85103
//Paused Event
86104
getPauseControl(e.DeviceId).wasPaused = true;
87105

88-
var hooks = Plugin.Instance.Configuration.Hooks.Where
89-
(i => i.onPause & (
90-
(i.withMovies & e.MediaInfo.Type == "Movie")
91-
|| (i.withEpisodes & e.MediaInfo.Type == "Episode")
92-
|| (i.withSongs & e.MediaInfo.Type == "Song")
93-
)
94-
);
95-
106+
var hooks = hooksByType(iType).Where(i => i.onPause);
96107

97108
var jsonString = buildJson(e, "media.pause");
98109

99110
SendHooks(hooks, jsonString);
100-
}else if (e.IsPaused == false & getPauseControl(e.DeviceId).wasPaused)
111+
}
112+
else if (e.IsPaused == false & getPauseControl(e.DeviceId).wasPaused)
101113
{
102114
getPauseControl(e.DeviceId).wasPaused = false;
103115

104-
var hooks = Plugin.Instance.Configuration.Hooks.Where
105-
(i => i.onResume & (
106-
(i.withMovies & e.MediaInfo.Type == "Movie")
107-
|| (i.withEpisodes & e.MediaInfo.Type == "Episode")
108-
|| (i.withSongs & e.MediaInfo.Type == "Song")
109-
)
110-
);
116+
var hooks = hooksByType(iType).Where(i => i.onResume);
111117
var jsonString = buildJson(e, "media.resume");
112118

113119
SendHooks(hooks, jsonString);
114120
}
115121

116122
}
117-
118123
private void PlaybackStart(object sender, PlaybackProgressEventArgs e)
119124
{
120125
getPauseControl(e.DeviceId).wasPaused = false;
121126

122-
var jsonString = buildJson(e, "media.play");
127+
var iType = _libraryManager.GetContentType(e.Item);
128+
123129
//get all configured hooks for onPlay
124-
var hooks = Plugin.Instance.Configuration.Hooks.Where
125-
(i => i.onPlay & (
126-
(i.withMovies & e.MediaInfo.Type == "Movie")
127-
|| (i.withEpisodes & e.MediaInfo.Type == "Episode")
128-
|| (i.withSongs & e.MediaInfo.Type == "Song")
129-
)
130-
);
130+
var hooks = hooksByType(iType).Where(i => i.onPlay);
131+
132+
var jsonString = buildJson(e, "media.play");
131133

132134
SendHooks(hooks, jsonString);
133135
}
@@ -142,7 +144,7 @@ private void PlaybackStopped(object sender, PlaybackProgressEventArgs e)
142144
(i => i.onStop & (
143145
(i.withMovies & e.MediaInfo.Type == "Movie")
144146
|| (i.withEpisodes & e.MediaInfo.Type == "Episode")
145-
|| (i.withSongs & e.MediaInfo.Type == "Song")
147+
|| (i.withSongs & e.MediaInfo.Type == "Audio")
146148
)
147149
);
148150
if (hooks.Count() > 0)
@@ -151,19 +153,28 @@ private void PlaybackStopped(object sender, PlaybackProgressEventArgs e)
151153
var jsonString = buildJson(e, "media.stop");
152154
SendHooks(hooks, jsonString);
153155
}
154-
}
156+
}
155157

158+
public IEnumerable<PluginConfiguration.Hook> hooksByType(string type)
159+
{
160+
return Plugin.Instance.Configuration.Hooks.Where(h =>
161+
(h.withMovies && type == "movies") ||
162+
(h.withEpisodes && type == "tvshows") ||
163+
(h.withSongs && type == "music")
164+
);
165+
}
166+
156167
public void SendHooks(IEnumerable<PluginConfiguration.Hook> hooks, string jsonString)
157168
{
158169
foreach (var h in hooks)
159170
{
160171
//send payload
161-
SendHooks(h, jsonString);
172+
SendHook(h, jsonString);
162173

163174
}
164175
}
165-
166-
public async Task<bool> SendHooks (PluginConfiguration.Hook h, string jsonString)
176+
177+
public async Task<bool> SendHook (PluginConfiguration.Hook h, string jsonString)
167178
{
168179
_logger.Debug("Sending paylod to {0}", h.URL);
169180
_logger.Debug(jsonString);
@@ -177,12 +188,30 @@ public async Task<bool> SendHooks (PluginConfiguration.Hook h, string jsonString
177188
}
178189
return true;
179190
}
180-
191+
192+
public string buildJson(BaseItem i, string trigger)
193+
{
194+
195+
envelope j = new envelope()
196+
{
197+
@event = trigger,
198+
199+
Metadata = new Metadata()
200+
{
201+
type = _libraryManager.GetContentType(i),
202+
title = i.Name,
203+
grandparentTitle = i.Parent.Parent.Name,
204+
parentTitle = i.Parent.Name,
205+
guid = i.Id.ToString()
206+
}
207+
};
208+
return _jsonSerializer.SerializeToString(j);
209+
}
181210

182211
public string buildJson (PlaybackProgressEventArgs e, string trigger)
183212
{
184213
envelope j = new envelope() {
185-
_event = trigger,
214+
@event = trigger,
186215

187216
Account = new Account() { },
188217
Player = new Player() {
@@ -191,51 +220,15 @@ public string buildJson (PlaybackProgressEventArgs e, string trigger)
191220
},
192221
Metadata = new Metadata()
193222
{
194-
type = e.MediaInfo.Type,
195-
title = e.MediaInfo.Name,
223+
type = _libraryManager.GetContentType(e.Item),
224+
title = e.Item.Name,
196225
grandparentTitle = e.Item.Parent.Parent.Name,
197226
parentTitle = e.Item.Parent.Name,
198227
guid = e.Item.Id.ToString()
199228
}
200229
};
201230

202231
return _jsonSerializer.SerializeToString(j);
203-
204-
/*
205-
MemoryStream stream1 = new MemoryStream();
206-
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(envelope));
207-
ser.WriteObject(stream1, j);
208-
209-
stream1.Position = 0;
210-
StreamReader sr = new StreamReader(stream1);
211-
212-
213-
return (sr.ReadToEnd());
214-
*/
215-
}
216-
217-
public Task SendNotification(UserNotification request, CancellationToken cancellationToken)
218-
{
219-
return SendNotifcationHook(request);
220-
}
221-
222-
public async Task<bool> SendNotifcationHook(UserNotification request)
223-
{
224-
var hooks = Plugin.Instance.Configuration.Hooks.Where(i => i.withNotifications);
225-
var json = _jsonSerializer.SerializeToString(request);
226-
_logger.Debug(json);
227-
228-
foreach (var h in hooks)
229-
{
230-
await SendHooks(h, json);
231-
}
232-
return true;
233-
}
234-
235-
public bool IsEnabledForUser(User user)
236-
{
237-
238-
return user.Policy.IsAdministrator;
239232
}
240233
}
241234
}

Emby.Webhooks/envelope.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Emby.Webhooks
99
public class envelope
1010
{
1111

12-
public string _event { get; set; }
12+
public string @event { get; set; }
1313
public bool user { get; set; }
1414
public bool owner { get; set; }
1515
public Account Account { get; set; }

0 commit comments

Comments
 (0)