1
0

2 Commits df2a1f6ab8 ... e9a042995b

Autor SHA1 Nachricht Datum
  Andrew e9a042995b penis man vor 1 Woche
  Andrew 1b0bcb4206 idk vor 3 Wochen

+ 21 - 5
app/Controllers/AccountController.php

@@ -14,6 +14,7 @@ use app\Types\DatabaseObjects\Follow;
 use app\Types\DatabaseObjects\Link;
 use app\Types\DatabaseObjects\Session;
 use app\Types\LinkEnum;
+use Exception;
 use Hajeebtok\Types\Exceptions\SecurityFaultException;
 use Mimey\MimeTypes;
 use Pecee\SimpleRouter\SimpleRouter;
@@ -52,6 +53,7 @@ class AccountController implements IRouteController
     public static function getAccount($id): string
     {
         if($id === "myself") $id = get_token_id(request());
+        Logger::Debug($id);
         Logger::Debug("Getting account id ($id).");
 
         $account = new Account(id: $id);
@@ -60,8 +62,14 @@ class AccountController implements IRouteController
         $followers = count(new Follow(followee_id: $id)->LoadMany());
         $following = count(new Follow(follower_id: $id)->LoadMany());
 
-        $links = new Link(account_id: $id)->LoadMany();
+        try {
+            $links = new Link(account_id: $id)->LoadMany();
+        } catch (Exception $e) {
+            $links = [];
+        }
+
 
+        CORSHelper();
         return api_json([
             "id" => $id,
             "username" => $account->username,
@@ -88,6 +96,8 @@ class AccountController implements IRouteController
         $account = new Account(id: $id);
         $data = Hajeebtok::$Database->Query("SELECT * FROM videos WHERE author_id = :author_id", ["author_id" => $account->id]);
         if (empty($data)) throw new VideoNotFoundException(0, 404);
+
+        CORSHelper();
         return api_json($data);
     }
 
@@ -97,10 +107,14 @@ class AccountController implements IRouteController
         if($id === "myself") $id = get_token_id(request());
 
         if($signed_in) {
-            $account = new Account(id: $id);
-            $account->Load();
-
-            $picture_path = APP_ROOT . "/usercontent/pictures/$account->picture_hash.png";
+            try {
+                $account = new Account(id: $id);
+                $account->Load();
+
+                $picture_path = APP_ROOT . "/usercontent/pictures/$account->picture_hash.png";
+            } catch (Exception $e) {
+                $picture_path = APP_ROOT . "/usercontent/pictures/not_found.png";
+            }
         } else {
             // this is hardcoded because i dont care
             $picture_path = APP_ROOT . "/usercontent/pictures/premium_" . rand(1, 57) . ".png";
@@ -340,6 +354,8 @@ class AccountController implements IRouteController
             SimpleRouter::post("/addLink", [AccountController::class, "addLink"]);
             SimpleRouter::options("/update", "CORSHelper");
             SimpleRouter::options("/token", "CORSHelper");
+            SimpleRouter::options("/{id}/get", "CORSHelper");
+            SimpleRouter::options("/{id}/videos", "CORSHelper");
         });
     }
 }

+ 1 - 0
app/Controllers/ErrorController.php

@@ -94,6 +94,7 @@ class ErrorController implements IRouteController
     }
     public static function ApiError($code)
     {
+        CORSHelper();
         return api_json([
             "error" => $code,
             "message" => self::$codeLookup[$code],

+ 25 - 2
app/Controllers/VideoController.php

@@ -6,6 +6,7 @@ use app\Exceptions\AccountNotFoundException;
 use app\Exceptions\CommentNotFoundException;
 use app\Exceptions\VideoNotFoundException;
 use app\Hajeebtok;
+use app\Types\DatabaseObjects\View;
 use Pecee\SimpleRouter\SimpleRouter;
 use Mimey\MimeTypes;
 use app\Interfaces\IRouteController;
@@ -56,7 +57,9 @@ class VideoController implements IRouteController
 		$author_information = new Account($video_information->author_id);
 		$author_information->Load();
 
+        CORSHelper();
 		return api_json([
+            "id" => $video_information->id,
 			"title" => $video_information->title,
 			"description" => $video_information->description,
 			"likes" => $video_information->likes,
@@ -83,12 +86,13 @@ class VideoController implements IRouteController
         $data = [];
         foreach($videos as $vid) {
             $data[] = [
+                "id" => $vid["id"],
                 "title" => $vid["title"],
                 "description" => $vid["description"],
                 "likes" => $vid["likes"],
                 "dislikes" => $vid["dislikes"],
-                "comments" => $vid["comments"],
-                "shares" => $vid["shares"],
+                "comments" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM comments WHERE video_id = :id", ["id" => $vid["id"]]),
+                "shares" =>  Hajeebtok::$Database->Single("SELECT COUNT(*) FROM messages INNER JOIN videos ON messages.video_id = videos.id WHERE videos.id = :id", ["id" => $vid["id"]]),
                 "author" => [
                     "id" => $vid["author_id"],
                     "pictureHash" => $accounts[$vid["author_id"] - 1]["picture_hash"], // kinda scuffed fix
@@ -97,6 +101,7 @@ class VideoController implements IRouteController
             ];
         }
 
+        CORSHelper();
         return api_json($data);
     }
 
@@ -120,6 +125,8 @@ class VideoController implements IRouteController
         $mime_types = new MimeTypes();
         if(!file_exists($video_path)) throw new VideoNotFoundException($id, 404);
         if(!file_exists($frame_path)) {
+            Logger::Debug("Generating thumbnail for video $id");
+
             $ffmpeg = FFMpeg\FFMpeg::create();
             $video = $ffmpeg->open($video_path);
             $frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds($seconds));
@@ -135,6 +142,20 @@ class VideoController implements IRouteController
         return file_get_contents($frame_path);
     }
 
+    public static function getFeed(): string
+    {
+        $signed_in = signed_in(request());
+        if(!$signed_in) throw new AccountNotFoundException(0, 404);
+        $id = get_token_id(request());
+        $view = new View(account_id: $id);
+        $view->LoadMany();
+
+
+
+        CORSHelper();
+        return api_json([]);
+    }
+
 	public static function RegisterRoutes(): void
 	{
 		SimpleRouter::group([
@@ -148,6 +169,8 @@ class VideoController implements IRouteController
             SimpleRouter::options("/{id}", "CORSHelper");
             SimpleRouter::options("/{id}/info", "CORSHelper");
             SimpleRouter::options("/{id}/thumbnail", "CORSHelper");
+            SimpleRouter::options("/upload", "CORSHelper");
+            SimpleRouter::options("/search", "CORSHelper");
 		});
 	}
 }

+ 16 - 0
app/Exceptions/ViewNotFoundException.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace app\Exceptions;
+
+use app\Logger;
+use \Exception;
+use \Throwable;
+
+class ViewNotFoundException extends Exception
+{
+	public function __construct(int $id, int $code = 0, ?Throwable $previous = null)
+	{
+		Logger::Error("Couldn't find view id ($id)");
+		parent::__construct("Couldn't find view id ($id)", $code, $previous);
+	}
+}

+ 6 - 5
app/Helpers.php

@@ -107,7 +107,6 @@ function signed_in(Request $request): bool
         $authorizationHeader = urldecode($_COOKIE["token"]);
     }
 
-    Logger::debug("Authorization header: $authorizationHeader");
     $token = explode(" ", $authorizationHeader)[1];
     $tokenData = Hajeebtok::$Database->Row("SELECT * FROM sessions WHERE token = :token", ["token" => $token]);
     return !empty($tokenData);
@@ -123,12 +122,14 @@ function get_token_id(Request $request): int
 
     $token = explode(" ", $authorizationHeader)[1];
     $tokenData = Hajeebtok::$Database->Row("SELECT * FROM sessions WHERE token = :token", ["token" => $token]);
-    if(!empty($tokenData)) return $tokenData["account_id"];
-    return 0;
+    if(empty($tokenData)) return 0;
+    return $tokenData["account_id"];
 }
 
-function CORSHelper() {
+function CORSHelper(): void
+{
     $response = response();
-    $response->header("Access-Control-Allow-Origin: *");
+    $response->header("Access-Control-Allow-Origin: http://localhost:5173");
     $response->header("Access-Control-Allow-Headers: Authorization, Content-Type");
+    $response->header("Access-Control-Allow-Credentials: true");
 }

+ 1 - 0
app/Types/DatabaseObjects/Link.php

@@ -6,6 +6,7 @@ use app\Hajeebtok;
 use app\Interfaces\IDatabaseObject;
 use app\Logger;
 use app\Types\LinkEnum;
+use app\Exceptions\LinkNotFoundException;
 use Hajeebtok\Types\Exceptions\SecurityFaultException;
 
 class Link implements IDatabaseObject

+ 87 - 0
app/Types/DatabaseObjects/View.php

@@ -0,0 +1,87 @@
+<?php
+
+namespace app\Types\DatabaseObjects;
+
+use app\Exceptions\ViewNotFoundException;
+use app\Hajeebtok;
+use app\Interfaces\IDatabaseObject;
+use app\Logger;
+use app\Types\LinkEnum;
+use app\Exceptions\LinkNotFoundException;
+use Hajeebtok\Types\Exceptions\SecurityFaultException;
+
+class View implements IDatabaseObject
+{
+
+	public private(set) ?int $video_id;
+    public private(set) ?int $account_id;
+
+	public function __construct(?int $video_id = null, ?int $account_id = null) {
+        $this->video_id = $video_id;
+        $this->account_id = $account_id;
+	}
+
+	/**
+	 * Creates the table for the object type in the database.
+	 */
+	public static function CreateTable(): void {
+		throw new SecurityFaultException("Attempt to create table on view object.");
+	}
+
+	/**
+	 * Drops the table for the object type from the database.
+	 */
+	public static function DropTable(): void {
+		throw new SecurityFaultException("Attempt to drop table on view object.");
+	}
+
+	/**
+	 * Saves the object to the database.
+	 */
+	public function Save() {
+		Hajeebtok::$Database->Query("INSERT INTO views (account_id, video_id) VALUES (:account_id, :video_id)", [
+			"account_id" => $this->account_id,
+            "video_id" => $this->video_id
+		]);
+		Logger::Debug("Saved view.");
+	}
+
+	/**
+	 * Deletes the object from the database.
+	 */
+	public function Delete() {
+        // TODO: make this thing speficifc
+		//Hajeebtok::$Database->Query("DELETE FROM link WHERE account_id = :id", ["id" => $this->id]);
+	}
+
+    public function DeleteMany() {
+        if(!empty($this->account_id)) return Hajeebtok::$Database->Query("DELETE FROM views WHERE account_id = :account_id", ["account_id" => $this->account_id]);
+        if(!empty($this->video_id)) return Hajeebtok::$Database->Query("DELETE FROM views WHERE video_id = :video_id", ["video_id" => $this->video_id]);
+    }
+
+	/**
+	 * Loads the object from the database.
+	 */
+	public function Load() {
+        // not implemented
+	}
+
+    public function LoadMany(): array {
+
+        if(!empty($this->account_id)) {
+            $array =  ["account_id" => $this->account_id];
+            $query = "SELECT * FROM links WHERE account_id = :account_id";
+        } else if(!empty($this->video_id)) {
+            $array =  ["video_id" => $this->video_id];
+            $query = "SELECT * FROM links WHERE video_id = :video_id";
+        } else {
+            throw new ViewNotFoundException(0, 404);
+        }
+
+        $data = Hajeebtok::$Database->Query($query, $array);
+        if(empty($data)) throw new ViewNotFoundException(0, 404);
+
+        return $data;
+    }
+}
+