瀏覽代碼

Update Webfinger util, add avatar entity. Fixes #1629

Daniel Supernault 1 年之前
父節點
當前提交
bca2484994
共有 1 個文件被更改,包括 54 次插入37 次删除
  1. 54 37
      app/Util/Webfinger/Webfinger.php

+ 54 - 37
app/Util/Webfinger/Webfinger.php

@@ -4,43 +4,60 @@ namespace App\Util\Webfinger;
 
 class Webfinger
 {
-	protected $user;
-	protected $subject;
-	protected $aliases;
-	protected $links;
+    protected $user;
+    protected $subject;
+    protected $aliases;
+    protected $links;
 
-	public function __construct($user)
-	{
-		$this->subject = 'acct:'.$user->username.'@'.parse_url(config('app.url'), PHP_URL_HOST);
-		$this->aliases = [
-			$user->url(),
-			$user->permalink(),
-		];
-		$this->links = [
-			[
-				'rel'  => 'http://webfinger.net/rel/profile-page',
-				'type' => 'text/html',
-				'href' => $user->url(),
-			],
-			[
-				'rel'  => 'http://schemas.google.com/g/2010#updates-from',
-				'type' => 'application/atom+xml',
-				'href' => $user->permalink('.atom'),
-			],
-			[
-				'rel'  => 'self',
-				'type' => 'application/activity+json',
-				'href' => $user->permalink(),
-			],
-		];
-	}
+    public function __construct($user)
+    {
+        $avatar = $user ? $user->avatarUrl() : url('/storage/avatars/default.jpg');
+        $avatarPath = parse_url($avatar, PHP_URL_PATH);
+        $extension = pathinfo($avatarPath, PATHINFO_EXTENSION);
+        $mimeTypes = [
+            'jpg' => 'image/jpeg',
+            'jpeg' => 'image/jpeg',
+            'png' => 'image/png',
+            'gif' => 'image/gif',
+            'svg' => 'image/svg',
+        ];
+        $avatarType = $mimeTypes[$extension] ?? 'application/octet-stream';
 
-	public function generate()
-	{
-		return [
-			'subject' => $this->subject,
-			'aliases' => $this->aliases,
-			'links'   => $this->links,
-		];
-	}
+        $this->subject = 'acct:'.$user->username.'@'.parse_url(config('app.url'), PHP_URL_HOST);
+        $this->aliases = [
+            $user->url(),
+            $user->permalink(),
+        ];
+        $this->links = [
+            [
+                'rel'  => 'http://webfinger.net/rel/profile-page',
+                'type' => 'text/html',
+                'href' => $user->url(),
+            ],
+            [
+                'rel'  => 'http://schemas.google.com/g/2010#updates-from',
+                'type' => 'application/atom+xml',
+                'href' => $user->permalink('.atom'),
+            ],
+            [
+                'rel'  => 'self',
+                'type' => 'application/activity+json',
+                'href' => $user->permalink(),
+            ],
+            [
+                'rel' => 'http://webfinger.net/rel/avatar',
+                'type' => $avatarType,
+                'href' => $avatar,
+            ],
+        ];
+    }
+
+    public function generate()
+    {
+        return [
+            'subject' => $this->subject,
+            'aliases' => $this->aliases,
+            'links'   => $this->links,
+        ];
+    }
 }