示例#1
0
 /**
  * @brief send a new notification to the given user
  * @param $appid app which sends the notification
  * @param $class id relating to a template in the app's info.xml
  * @param $uid receiving user
  * @param $params keys and values for placeholders in the template and href/img
  * @return id of the inserted notification, null if unsuccessful
  */
 public static function sendUserNotification($appid, $class, $uid, $params = array())
 {
     try {
         $classId = self::getClassId($appid, $class);
         if ($classId === false) {
             throw new Exception("Notification template {$appid}/{$class} not found");
         }
         if (self::isBlacklisted($uid, $classId)) {
             return null;
         }
         OCP\DB::beginTransaction();
         if (!isset(self::$notifyStmt)) {
             self::$notifyStmt = OCP\DB::prepare("INSERT INTO *PREFIX*notifications (class, uid, moment) VALUES (?, ?, NOW())");
         }
         OC_Hook::emit("notify", "pre_sendUserNotification", array("classId" => $classId, "uid" => $uid, "params" => $params));
         self::$notifyStmt->execute(array($classId, $uid));
         $id = OCP\DB::insertid("*PREFIX*notifications");
         if (count($params)) {
             if (!isset(self::$paramStmt)) {
                 self::$paramStmt = OCP\DB::prepare("INSERT INTO *PREFIX*notification_params (nid, key, value) VALUES ({$id}, ?, ?)");
             }
             foreach ($params as $key => $value) {
                 self::$paramStmt->execute(array($key, $value));
                 OCP\DB::insertid("*PREFIX*notification_params");
             }
         }
         OCP\DB::commit();
         OC_Hook::emit("notify", "post_sendUserNotification", array("id" => $id, "classId" => $classId, "uid" => $uid, "params" => $params));
         return (int) $id;
     } catch (Exception $e) {
         OCP\Util::writeLog("notify", "Could not send notification: " . $e->getMessage(), OCP\Util::ERROR);
         throw $e;
         /* TODO: good exception handling and throwing, e.g.:
          * - throw exception when there are errors
          * - return false (or null?) when the app/class is blacklisted
          */
     }
 }