public static Notification Create(Core core, ApplicationEntry application, User actionBy, User receiver, ItemKey itemOwnerKey, ItemKey itemKey, string verb, string url, string action) { if (core == null) { throw new NullCoreException(); } int applicationId = 0; if (application != null) { // TODO: ensure only internals can call a null application applicationId = (int)application.Id; } RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); byte[] randomNumber = new byte[16]; rng.GetBytes(randomNumber); string rand = SessionState.HexRNG(randomNumber); string verificationString = SessionState.SessionMd5(rand + "bsseed" + DateTime.Now.Ticks.ToString() + core.Session.IPAddress.ToString()).ToLower(); InsertQuery iQuery = new InsertQuery("notifications"); iQuery.AddField("notification_primitive_id", receiver.Id); iQuery.AddField("notification_primitive_type_id", ItemKey.GetTypeId(core, typeof(User))); if (itemKey != null) { iQuery.AddField("notification_item_id", itemKey.Id); iQuery.AddField("notification_item_type_id", itemKey.TypeId); } if (itemOwnerKey != null) { iQuery.AddField("notification_item_owner_id", itemOwnerKey.Id); iQuery.AddField("notification_item_owner_type_id", itemOwnerKey.TypeId); } iQuery.AddField("notification_user_id", actionBy.Id); iQuery.AddField("notification_user_count", 1); iQuery.AddField("notification_verb", verb); iQuery.AddField("notification_action", action); iQuery.AddField("notification_url", url); iQuery.AddField("notification_time_ut", UnixTime.UnixTimeStamp()); iQuery.AddField("notification_read", false); iQuery.AddField("notification_seen", false); iQuery.AddField("notification_application", applicationId); iQuery.AddField("notification_verification_string", verificationString); long notificationId = core.Db.Query(iQuery); core.Db.BeginTransaction(); UpdateQuery query = new UpdateQuery(typeof(UserInfo)); query.AddField("user_unread_notifications", new QueryOperation("user_unread_notifications", QueryOperations.Addition, 1)); query.AddCondition("user_id", receiver.Id); core.Db.Query(query); Notification notification = new Notification(core, receiver, notificationId, string.Empty, string.Empty, UnixTime.UnixTimeStamp(), applicationId); // this is not elegant // TODO: write appropriate constructor notification.userId = actionBy.Id; notification.verb = verb; notification.action = action; notification.url = url; notification.itemKey = itemKey; notification.itemOwnerKey = itemOwnerKey; notification.verificationString = verificationString; return notification; }
public static Notification Create(Core core, ApplicationEntry application, User receiver, ItemKey itemKey, string subject, string body) { if (core == null) { throw new NullCoreException(); } int applicationId = 0; if (application != null) { // TODO: ensure only internals can call a null application applicationId = (int)application.Id; } InsertQuery iQuery = new InsertQuery("notifications"); iQuery.AddField("notification_primitive_id", receiver.Id); iQuery.AddField("notification_primitive_type_id", ItemKey.GetTypeId(core, typeof(User))); if (itemKey != null) { iQuery.AddField("notification_item_id", itemKey.Id); iQuery.AddField("notification_item_type_id", itemKey.TypeId); } iQuery.AddField("notification_title", subject); iQuery.AddField("notification_body", body); iQuery.AddField("notification_time_ut", UnixTime.UnixTimeStamp()); iQuery.AddField("notification_read", false); iQuery.AddField("notification_seen", false); iQuery.AddField("notification_application", applicationId); long notificationId = core.Db.Query(iQuery); UpdateQuery query = new UpdateQuery(typeof(UserInfo)); query.AddField("user_unread_notifications", new QueryOperation("user_unread_notifications", QueryOperations.Addition, 1)); query.AddCondition("user_id", receiver.Id); core.Db.Query(query); Notification notification = new Notification(core, receiver, notificationId, subject, body, UnixTime.UnixTimeStamp(), applicationId); return notification; }