/** * Make post to user wall from $viewer to $target instance of iUser interface objects * @param iUser $target * @param iUser $viewer * @param int $delay * @return bool */ public function makePost(iUser $target, iUser $viewer, $delay = 60) { if ($target === null || $viewer === null) { return false; } $find = WallRecords::where('sender_id', '=', $viewer->id)->orderBy('updated_at', 'desc')->first(); if ($find !== null) { $lastPostTime = Date::convertToTimestamp($find->updated_at); if (time() - $lastPostTime < static::POST_GLOBAL_DELAY) { // past time was less then default delay return false; } } // save new post to db $record = new WallRecords(); $record->target_id = $target->id; $record->sender_id = $viewer->id; $record->message = $this->message; $record->save(); // add user notification if ($target->id !== $viewer->id) { $notify = new EntityAddNotification($target->id); $notify->add('profile/show/' . $target->id . '#wall-post-' . $record->id, EntityAddNotification::MSG_ADD_WALLPOST, ['snippet' => Text::snippet($this->message, 50)]); } // cleanup message $this->message = null; return true; }
/** * Delete user from database * @throws \Exception */ public function delete() { foreach ($this->users as $user) { /** @var iUser $user */ $uid = $user->getParam('id'); // delete wall records WallPost::where('target_id', '=', $uid)->orWhere('sender_id', '=', $uid)->delete(); // delete avatars File::remove('/upload/user/avatar/big/' . $uid . '.jpg'); File::remove('/upload/user/avatar/medium/' . $uid . '.jpg'); File::remove('/upload/user/avatar/small/' . $uid . '.jpg'); File::remove('/upload/user/avatar/original/' . $uid . '.jpg'); // delete user profile and auth data $user->getProfile()->delete(); // delete user provider data $user->getProviders()->delete(); // delete user object $user->delete(); } }
/** * Get wall post object * @return WallPost|null */ public function getWallPost() { return WallPost::where('id', '=', $this->post_id)->first(); }
/** * Add new post answer from AJAX post * @param int $postId * @return string * @throws ForbiddenException * @throws NativeException */ public function actionSendwallanswer($postId) { // not auth? what are you doing there? ;) if (!App::$User->isAuth()) { throw new ForbiddenException('Auth required'); } // no post id? wtf you doing man! if (!Obj::isLikeInt($postId) || $postId < 1) { throw new NativeException('Wrong input data'); } // get current(sender) user object $viewer = App::$User->identity(); // get message from post and validate minlength $message = $this->request->get('message'); $message = App::$Security->strip_tags($message); if (!Obj::isString($message) || Str::length($message) < 3) { throw new ForbiddenException('Wrong input data'); } // try to find this post $wallPost = WallPost::where('id', '=', $postId); if ($wallPost->count() < 1) { throw new NativeException('Wrong input data'); } $wallRow = $wallPost->first(); $target_id = $wallRow->target_id; // check if in blacklist if (!Blacklist::check($viewer->id, $target_id)) { throw new ForbiddenException('User is blocked!'); } // check delay between user last post and current $lastAnswer = WallAnswer::where('user_id', '=', App::$User->identity()->getId())->orderBy('created_at', 'DESC')->first(); if (null !== $lastAnswer && false !== $lastAnswer) { $now = time(); $answerTime = Date::convertToTimestamp($lastAnswer->created_at); $cfgs = \Apps\ActiveRecord\App::getConfigs('app', 'Profile'); // hmm, maybe past less then delay required? if ($now - (int) $cfgs['delayBetweenPost'] < $answerTime) { throw new ForbiddenException('Delay between answers not pass'); } } // make new row ;) $answers = new WallAnswer(); $answers->post_id = $postId; $answers->user_id = $viewer->id; $answers->message = $message; $answers->save(); // add notification for target user if ($viewer->id !== $target_id) { $notify = new EntityAddNotification($target_id); $notify->add('/profile/show/' . $target_id . '#wall-post-' . $wallRow->id, EntityAddNotification::MSG_ADD_WALLANSWER, ['snippet' => Text::snippet($message, 50), 'post' => $wallRow->message]); } // send "ok" response $this->setJsonHeader(); return json_encode(['status' => 1, 'message' => 'ok']); }