2014-04-25 20:15:50 +00:00
using MediaBrowser.Controller ;
2013-09-26 21:20:26 +00:00
using MediaBrowser.Controller.Notifications ;
2013-07-06 21:23:32 +00:00
using MediaBrowser.Model.Logging ;
using MediaBrowser.Model.Notifications ;
2014-04-25 20:15:50 +00:00
using MediaBrowser.Server.Implementations.Persistence ;
2013-07-06 21:23:32 +00:00
using System ;
using System.Collections.Generic ;
using System.Data ;
2014-04-25 20:15:50 +00:00
using System.IO ;
2013-07-06 21:23:32 +00:00
using System.Linq ;
using System.Threading ;
using System.Threading.Tasks ;
2014-04-25 20:15:50 +00:00
namespace MediaBrowser.Server.Implementations.Notifications
2013-07-06 21:23:32 +00:00
{
2016-02-14 05:32:19 +00:00
public class SqliteNotificationsRepository : BaseSqliteRepository , INotificationsRepository
2013-07-06 21:23:32 +00:00
{
2016-06-11 15:55:05 +00:00
public SqliteNotificationsRepository ( ILogManager logManager , IServerApplicationPaths appPaths , IDbConnector dbConnector ) : base ( logManager , dbConnector )
{
DbFilePath = Path . Combine ( appPaths . DataPath , "notifications.db" ) ;
}
2013-07-06 21:23:32 +00:00
public event EventHandler < NotificationUpdateEventArgs > NotificationAdded ;
public event EventHandler < NotificationReadEventArgs > NotificationsMarkedRead ;
public event EventHandler < NotificationUpdateEventArgs > NotificationUpdated ;
2016-06-11 15:55:05 +00:00
public async Task Initialize ( )
2016-02-14 05:32:19 +00:00
{
2016-06-11 15:55:05 +00:00
using ( var connection = await CreateConnection ( ) . ConfigureAwait ( false ) )
{
string [ ] queries = {
2013-07-06 21:23:32 +00:00
"create table if not exists Notifications (Id GUID NOT NULL, UserId GUID NOT NULL, Date DATETIME NOT NULL, Name TEXT NOT NULL, Description TEXT, Url TEXT, Level TEXT NOT NULL, IsRead BOOLEAN NOT NULL, Category TEXT NOT NULL, RelatedId TEXT, PRIMARY KEY (Id, UserId))" ,
2016-05-16 01:22:22 +00:00
"create index if not exists idx_Notifications1 on Notifications(Id)" ,
2016-06-11 20:12:01 +00:00
"create index if not exists idx_Notifications2 on Notifications(UserId)"
2013-07-06 21:23:32 +00:00
} ;
2016-06-11 15:55:05 +00:00
connection . RunQueries ( queries , Logger ) ;
}
2013-07-06 21:23:32 +00:00
}
/// <summary>
/// Gets the notifications.
/// </summary>
/// <param name="query">The query.</param>
/// <returns>NotificationResult.</returns>
public NotificationResult GetNotifications ( NotificationQuery query )
{
var result = new NotificationResult ( ) ;
2016-06-11 15:55:05 +00:00
using ( var connection = CreateConnection ( true ) . Result )
2013-07-06 21:23:32 +00:00
{
2016-06-11 15:55:05 +00:00
using ( var cmd = connection . CreateCommand ( ) )
2013-07-07 05:41:28 +00:00
{
2016-06-11 15:55:05 +00:00
var clauses = new List < string > ( ) ;
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
if ( query . IsRead . HasValue )
{
clauses . Add ( "IsRead=@IsRead" ) ;
cmd . Parameters . Add ( cmd , "@IsRead" , DbType . Boolean ) . Value = query . IsRead . Value ;
}
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
clauses . Add ( "UserId=@UserId" ) ;
cmd . Parameters . Add ( cmd , "@UserId" , DbType . Guid ) . Value = new Guid ( query . UserId ) ;
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
var whereClause = " where " + string . Join ( " And " , clauses . ToArray ( ) ) ;
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
cmd . CommandText = string . Format ( "select count(Id) from Notifications{0};select Id,UserId,Date,Name,Description,Url,Level,IsRead,Category,RelatedId from Notifications{0} order by IsRead asc, Date desc" , whereClause ) ;
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
using ( var reader = cmd . ExecuteReader ( CommandBehavior . SequentialAccess ) )
2013-07-06 21:23:32 +00:00
{
2016-06-11 15:55:05 +00:00
if ( reader . Read ( ) )
2013-07-06 21:23:32 +00:00
{
2016-06-11 15:55:05 +00:00
result . TotalRecordCount = reader . GetInt32 ( 0 ) ;
2013-07-06 21:23:32 +00:00
}
2016-06-11 15:55:05 +00:00
if ( reader . NextResult ( ) )
2013-07-06 21:23:32 +00:00
{
2016-06-11 15:55:05 +00:00
var notifications = GetNotifications ( reader ) ;
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
if ( query . StartIndex . HasValue )
{
notifications = notifications . Skip ( query . StartIndex . Value ) ;
}
if ( query . Limit . HasValue )
{
notifications = notifications . Take ( query . Limit . Value ) ;
}
result . Notifications = notifications . ToArray ( ) ;
}
2013-07-06 21:23:32 +00:00
}
2016-06-11 15:55:05 +00:00
return result ;
}
2013-07-06 21:23:32 +00:00
}
}
2014-04-25 20:15:50 +00:00
public NotificationsSummary GetNotificationsSummary ( string userId )
2013-07-06 21:23:32 +00:00
{
var result = new NotificationsSummary ( ) ;
2016-06-11 15:55:05 +00:00
using ( var connection = CreateConnection ( true ) . Result )
2013-07-06 21:23:32 +00:00
{
2016-06-11 15:55:05 +00:00
using ( var cmd = connection . CreateCommand ( ) )
2013-07-06 21:23:32 +00:00
{
2016-06-11 15:55:05 +00:00
cmd . CommandText = "select Level from Notifications where UserId=@UserId and IsRead=@IsRead" ;
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
cmd . Parameters . Add ( cmd , "@UserId" , DbType . Guid ) . Value = new Guid ( userId ) ;
cmd . Parameters . Add ( cmd , "@IsRead" , DbType . Boolean ) . Value = false ;
using ( var reader = cmd . ExecuteReader ( CommandBehavior . SequentialAccess ) )
2013-07-06 21:23:32 +00:00
{
2016-06-11 15:55:05 +00:00
var levels = new List < NotificationLevel > ( ) ;
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
while ( reader . Read ( ) )
{
levels . Add ( GetLevel ( reader , 0 ) ) ;
}
2013-07-07 05:41:28 +00:00
2016-06-11 15:55:05 +00:00
result . UnreadCount = levels . Count ;
if ( levels . Count > 0 )
{
result . MaxUnreadNotificationLevel = levels . Max ( ) ;
}
2013-07-06 21:23:32 +00:00
}
2016-06-11 15:55:05 +00:00
return result ;
}
2013-07-06 21:23:32 +00:00
}
}
/// <summary>
/// Gets the notifications.
/// </summary>
/// <param name="reader">The reader.</param>
/// <returns>IEnumerable{Notification}.</returns>
private IEnumerable < Notification > GetNotifications ( IDataReader reader )
{
2016-06-11 15:55:05 +00:00
var list = new List < Notification > ( ) ;
2013-07-06 21:23:32 +00:00
while ( reader . Read ( ) )
{
2016-06-11 15:55:05 +00:00
list . Add ( GetNotification ( reader ) ) ;
2013-07-06 21:23:32 +00:00
}
2016-06-11 15:55:05 +00:00
return list ;
2013-07-06 21:23:32 +00:00
}
private Notification GetNotification ( IDataReader reader )
{
var notification = new Notification
{
2014-04-25 20:15:50 +00:00
Id = reader . GetGuid ( 0 ) . ToString ( "N" ) ,
UserId = reader . GetGuid ( 1 ) . ToString ( "N" ) ,
2013-07-06 21:23:32 +00:00
Date = reader . GetDateTime ( 2 ) . ToUniversalTime ( ) ,
Name = reader . GetString ( 3 )
} ;
if ( ! reader . IsDBNull ( 4 ) )
{
notification . Description = reader . GetString ( 4 ) ;
}
if ( ! reader . IsDBNull ( 5 ) )
{
notification . Url = reader . GetString ( 5 ) ;
}
notification . Level = GetLevel ( reader , 6 ) ;
notification . IsRead = reader . GetBoolean ( 7 ) ;
return notification ;
}
/// <summary>
/// Gets the level.
/// </summary>
/// <param name="reader">The reader.</param>
/// <param name="index">The index.</param>
/// <returns>NotificationLevel.</returns>
private NotificationLevel GetLevel ( IDataReader reader , int index )
{
NotificationLevel level ;
var val = reader . GetString ( index ) ;
Enum . TryParse ( val , true , out level ) ;
return level ;
}
/// <summary>
/// Adds the notification.
/// </summary>
/// <param name="notification">The notification.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
public async Task AddNotification ( Notification notification , CancellationToken cancellationToken )
{
await ReplaceNotification ( notification , cancellationToken ) . ConfigureAwait ( false ) ;
if ( NotificationAdded ! = null )
{
try
{
NotificationAdded ( this , new NotificationUpdateEventArgs
{
Notification = notification
} ) ;
}
catch ( Exception ex )
{
2016-02-14 05:32:19 +00:00
Logger . ErrorException ( "Error in NotificationAdded event handler" , ex ) ;
2013-07-06 21:23:32 +00:00
}
}
}
/// <summary>
/// Replaces the notification.
/// </summary>
/// <param name="notification">The notification.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
private async Task ReplaceNotification ( Notification notification , CancellationToken cancellationToken )
{
2014-04-25 20:15:50 +00:00
if ( string . IsNullOrEmpty ( notification . Id ) )
2013-07-06 21:23:32 +00:00
{
2014-04-25 20:15:50 +00:00
notification . Id = Guid . NewGuid ( ) . ToString ( "N" ) ;
2013-07-06 21:23:32 +00:00
}
2014-04-25 20:15:50 +00:00
if ( string . IsNullOrEmpty ( notification . UserId ) )
2013-07-06 21:23:32 +00:00
{
throw new ArgumentException ( "The notification must have a user id" ) ;
}
cancellationToken . ThrowIfCancellationRequested ( ) ;
2016-06-11 15:55:05 +00:00
using ( var connection = await CreateConnection ( ) . ConfigureAwait ( false ) )
2013-07-06 21:23:32 +00:00
{
2016-06-11 15:55:05 +00:00
using ( var replaceNotificationCommand = connection . CreateCommand ( ) )
{
replaceNotificationCommand . CommandText = "replace into Notifications (Id, UserId, Date, Name, Description, Url, Level, IsRead, Category, RelatedId) values (@Id, @UserId, @Date, @Name, @Description, @Url, @Level, @IsRead, @Category, @RelatedId)" ;
replaceNotificationCommand . Parameters . Add ( replaceNotificationCommand , "@Id" ) ;
replaceNotificationCommand . Parameters . Add ( replaceNotificationCommand , "@UserId" ) ;
replaceNotificationCommand . Parameters . Add ( replaceNotificationCommand , "@Date" ) ;
replaceNotificationCommand . Parameters . Add ( replaceNotificationCommand , "@Name" ) ;
replaceNotificationCommand . Parameters . Add ( replaceNotificationCommand , "@Description" ) ;
replaceNotificationCommand . Parameters . Add ( replaceNotificationCommand , "@Url" ) ;
replaceNotificationCommand . Parameters . Add ( replaceNotificationCommand , "@Level" ) ;
replaceNotificationCommand . Parameters . Add ( replaceNotificationCommand , "@IsRead" ) ;
replaceNotificationCommand . Parameters . Add ( replaceNotificationCommand , "@Category" ) ;
replaceNotificationCommand . Parameters . Add ( replaceNotificationCommand , "@RelatedId" ) ;
IDbTransaction transaction = null ;
try
{
transaction = connection . BeginTransaction ( ) ;
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
replaceNotificationCommand . GetParameter ( 0 ) . Value = new Guid ( notification . Id ) ;
replaceNotificationCommand . GetParameter ( 1 ) . Value = new Guid ( notification . UserId ) ;
replaceNotificationCommand . GetParameter ( 2 ) . Value = notification . Date . ToUniversalTime ( ) ;
replaceNotificationCommand . GetParameter ( 3 ) . Value = notification . Name ;
replaceNotificationCommand . GetParameter ( 4 ) . Value = notification . Description ;
replaceNotificationCommand . GetParameter ( 5 ) . Value = notification . Url ;
replaceNotificationCommand . GetParameter ( 6 ) . Value = notification . Level . ToString ( ) ;
replaceNotificationCommand . GetParameter ( 7 ) . Value = notification . IsRead ;
replaceNotificationCommand . GetParameter ( 8 ) . Value = string . Empty ;
replaceNotificationCommand . GetParameter ( 9 ) . Value = string . Empty ;
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
replaceNotificationCommand . Transaction = transaction ;
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
replaceNotificationCommand . ExecuteNonQuery ( ) ;
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
transaction . Commit ( ) ;
}
catch ( OperationCanceledException )
{
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
throw ;
}
catch ( Exception e )
{
Logger . ErrorException ( "Failed to save notification:" , e ) ;
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
throw ;
}
finally
{
if ( transaction ! = null )
{
transaction . Dispose ( ) ;
}
}
2013-07-06 21:23:32 +00:00
}
}
}
/// <summary>
/// Marks the read.
/// </summary>
/// <param name="notificationIdList">The notification id list.</param>
/// <param name="userId">The user id.</param>
/// <param name="isRead">if set to <c>true</c> [is read].</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
2014-04-25 20:15:50 +00:00
public async Task MarkRead ( IEnumerable < string > notificationIdList , string userId , bool isRead , CancellationToken cancellationToken )
2013-07-06 21:23:32 +00:00
{
2014-04-25 20:15:50 +00:00
var list = notificationIdList . ToList ( ) ;
var idArray = list . Select ( i = > new Guid ( i ) ) . ToArray ( ) ;
2013-07-06 21:23:32 +00:00
await MarkReadInternal ( idArray , userId , isRead , cancellationToken ) . ConfigureAwait ( false ) ;
if ( NotificationsMarkedRead ! = null )
{
try
{
NotificationsMarkedRead ( this , new NotificationReadEventArgs
{
2014-04-25 20:15:50 +00:00
IdList = list . ToArray ( ) ,
2013-07-06 21:23:32 +00:00
IsRead = isRead ,
UserId = userId
} ) ;
}
catch ( Exception ex )
{
2016-02-14 05:32:19 +00:00
Logger . ErrorException ( "Error in NotificationsMarkedRead event handler" , ex ) ;
2013-07-06 21:23:32 +00:00
}
}
}
2014-10-04 18:05:24 +00:00
public async Task MarkAllRead ( string userId , bool isRead , CancellationToken cancellationToken )
{
cancellationToken . ThrowIfCancellationRequested ( ) ;
2016-06-11 15:55:05 +00:00
using ( var connection = await CreateConnection ( ) . ConfigureAwait ( false ) )
{
using ( var markAllReadCommand = connection . CreateCommand ( ) )
{
markAllReadCommand . CommandText = "update Notifications set IsRead=@IsRead where UserId=@UserId" ;
2014-10-04 18:05:24 +00:00
2016-06-11 15:55:05 +00:00
markAllReadCommand . Parameters . Add ( markAllReadCommand , "@UserId" ) ;
markAllReadCommand . Parameters . Add ( markAllReadCommand , "@IsRead" ) ;
2014-10-04 18:05:24 +00:00
2016-06-11 15:55:05 +00:00
IDbTransaction transaction = null ;
2014-10-04 18:05:24 +00:00
2016-06-11 15:55:05 +00:00
try
{
cancellationToken . ThrowIfCancellationRequested ( ) ;
2014-10-04 18:05:24 +00:00
2016-06-11 15:55:05 +00:00
transaction = connection . BeginTransaction ( ) ;
2014-10-04 18:05:24 +00:00
2016-06-11 15:55:05 +00:00
markAllReadCommand . GetParameter ( 0 ) . Value = new Guid ( userId ) ;
markAllReadCommand . GetParameter ( 1 ) . Value = isRead ;
2014-10-04 18:05:24 +00:00
2016-06-11 15:55:05 +00:00
markAllReadCommand . ExecuteNonQuery ( ) ;
2014-10-04 18:05:24 +00:00
2016-06-11 15:55:05 +00:00
transaction . Commit ( ) ;
}
catch ( OperationCanceledException )
{
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
2014-10-04 18:05:24 +00:00
2016-06-11 15:55:05 +00:00
throw ;
}
catch ( Exception e )
{
Logger . ErrorException ( "Failed to save notification:" , e ) ;
2014-10-04 18:05:24 +00:00
2016-06-11 15:55:05 +00:00
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
2014-10-04 18:05:24 +00:00
2016-06-11 15:55:05 +00:00
throw ;
}
finally
{
if ( transaction ! = null )
{
transaction . Dispose ( ) ;
}
}
}
2014-10-04 18:05:24 +00:00
}
}
2014-04-25 20:15:50 +00:00
private async Task MarkReadInternal ( IEnumerable < Guid > notificationIdList , string userId , bool isRead , CancellationToken cancellationToken )
2013-07-06 21:23:32 +00:00
{
cancellationToken . ThrowIfCancellationRequested ( ) ;
2016-06-11 15:55:05 +00:00
using ( var connection = await CreateConnection ( ) . ConfigureAwait ( false ) )
2013-07-06 21:23:32 +00:00
{
2016-06-11 15:55:05 +00:00
using ( var markReadCommand = connection . CreateCommand ( ) )
{
markReadCommand . CommandText = "update Notifications set IsRead=@IsRead where Id=@Id and UserId=@UserId" ;
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
markReadCommand . Parameters . Add ( markReadCommand , "@UserId" ) ;
markReadCommand . Parameters . Add ( markReadCommand , "@IsRead" ) ;
markReadCommand . Parameters . Add ( markReadCommand , "@Id" ) ;
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
IDbTransaction transaction = null ;
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
try
{
cancellationToken . ThrowIfCancellationRequested ( ) ;
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
transaction = connection . BeginTransaction ( ) ;
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
markReadCommand . GetParameter ( 0 ) . Value = new Guid ( userId ) ;
markReadCommand . GetParameter ( 1 ) . Value = isRead ;
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
foreach ( var id in notificationIdList )
{
markReadCommand . GetParameter ( 2 ) . Value = id ;
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
markReadCommand . Transaction = transaction ;
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
markReadCommand . ExecuteNonQuery ( ) ;
}
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
transaction . Commit ( ) ;
}
catch ( OperationCanceledException )
{
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
2013-07-06 21:23:32 +00:00
2016-06-11 15:55:05 +00:00
throw ;
}
catch ( Exception e )
{
Logger . ErrorException ( "Failed to save notification:" , e ) ;
2016-02-14 05:32:19 +00:00
2016-06-11 15:55:05 +00:00
if ( transaction ! = null )
{
transaction . Rollback ( ) ;
}
2016-02-14 05:32:19 +00:00
2016-06-11 15:55:05 +00:00
throw ;
}
finally
{
if ( transaction ! = null )
{
transaction . Dispose ( ) ;
}
}
}
2013-07-06 21:23:32 +00:00
}
}
}
}