| 1 | <?php |
| 2 | /************************************************************* |
| 3 | # 076 License |
| 4 | |
| 5 | Copyright (c) テクニカル諏訪子 |
| 6 | |
| 7 | Permission is hereby granted to any person obtaining a copy of the software |
| 8 | Little Beast (the "Software") to use, modify, merge, copy, publish, distribute, |
| 9 | sublicense, and/or sell copies of the Software, subject to the following conditions: |
| 10 | |
| 11 | 1. **Origin Attribution**: |
| 12 | - You must not misrepresent the origin of the Software; you must not claim |
| 13 | you created the original Software. |
| 14 | - If the Software is used in a product, you must either: |
| 15 | a. Provide clear attribution in the product's documentation, user interface, |
| 16 | or other visible areas, **OR** |
| 17 | b. Pay the original developers a fee they specify in writing. |
| 18 | 2. **Usage Restriction**: |
| 19 | - The Software, or any derivative works, dependencies, or libraries |
| 20 | incorporating it, must not be used for censorship or to suppress freedom of |
| 21 | speech, expression, or creativity. Prohibited uses include, but are not |
| 22 | limited to: |
| 23 | - Censorship of so-called "hate speech", visuals, non-mainstream opinions, |
| 24 | ideas, or objective reality. |
| 25 | - Tools or systems designed to restrict access to information or |
| 26 | artistic works. |
| 27 | 3. **Notice Preservation**: |
| 28 | - This license and the above copyright notice must remain intact in all copies |
| 29 | of the source code. |
| 30 | |
| 31 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 32 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 33 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 34 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 35 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 36 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 37 | */ |
| 38 | namespace Std\Lib\Fedi; |
| 39 | |
| 40 | /** |
| 41 | * ローカルの投稿の保存(data/ap/notes/{ID}.json) |
| 42 | */ |
| 43 | final class Notes { |
| 44 | public static function path(string $id): string { |
| 45 | return Fedi::path("notes/{$id}.json"); |
| 46 | } |
| 47 | |
| 48 | public static function get(string $id): ?array { |
| 49 | if (!preg_match('/^[0-9a-f]{20}$/', $id)) return null; |
| 50 | return Fedi::read(self::path($id), null); |
| 51 | } |
| 52 | |
| 53 | public static function save(array $note): bool { |
| 54 | return Fedi::write(self::path($note['id']), $note); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * 投稿をロックして変更する |
| 59 | * |
| 60 | * @return array|null 変更後の投稿、無ければnull |
| 61 | */ |
| 62 | public static function modify(string $id, callable $fn): ?array { |
| 63 | if (self::get($id) === null) return null; |
| 64 | return Fedi::update(self::path($id), fn($n) => $n ? $fn($n) : $n, null); |
| 65 | } |
| 66 | |
| 67 | public static function delete(string $id): void { |
| 68 | @unlink(self::path($id)); |
| 69 | } |
| 70 | |
| 71 | /** 投稿のURL(オブジェクトのID) */ |
| 72 | public static function url(array $note): string { |
| 73 | return Fedi::noteUrl($note['author'], $note['id']); |
| 74 | } |
| 75 | |
| 76 | /** 投稿のページ(HTML) */ |
| 77 | public static function htmlUrl(string $objectId): string { |
| 78 | return Fedi::base().'/microblog/note?id='.rawurlencode($objectId); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * ActivityPubのNote |
| 83 | */ |
| 84 | public static function toObject(array $note): array { |
| 85 | $url = self::url($note); |
| 86 | $obj = [ |
| 87 | '@context' => Fedi::CONTEXT, |
| 88 | 'id' => $url, |
| 89 | 'type' => 'Note', |
| 90 | 'attributedTo' => Fedi::actorUrl($note['author']), |
| 91 | 'content' => $note['content'], |
| 92 | 'source' => ['content' => $note['source'], 'mediaType' => 'text/markdown'], |
| 93 | 'to' => $note['to'], |
| 94 | 'cc' => $note['cc'], |
| 95 | 'published' => $note['published'], |
| 96 | 'url' => self::htmlUrl($url), |
| 97 | 'sensitive' => (bool)$note['sensitive'], |
| 98 | 'tag' => $note['tags'], |
| 99 | // 「path」は削除用の内部の項目なので外す |
| 100 | 'attachment' => array_map(fn($a) => array_diff_key($a, ['path' => 1]), $note['attachments']), |
| 101 | 'replies' => [ |
| 102 | 'id' => "{$url}/replies", |
| 103 | 'type' => 'Collection', |
| 104 | 'totalItems' => count($note['replies'] ?? []), |
| 105 | ], |
| 106 | 'likes' => ['id' => "{$url}/likes", 'type' => 'Collection', 'totalItems' => count($note['likes'] ?? [])], |
| 107 | 'shares' => ['id' => "{$url}/shares", 'type' => 'Collection', 'totalItems' => count($note['boosts'] ?? [])], |
| 108 | ]; |
| 109 | if (($note['summary'] ?? '') !== '') $obj['summary'] = $note['summary']; |
| 110 | if (!empty($note['inReplyTo'])) $obj['inReplyTo'] = $note['inReplyTo']; |
| 111 | if (!empty($note['updated'])) $obj['updated'] = $note['updated']; |
| 112 | return $obj; |
| 113 | } |
| 114 | |
| 115 | /** Createアクティビティ */ |
| 116 | public static function toCreate(array $note): array { |
| 117 | $obj = self::toObject($note); |
| 118 | unset($obj['@context']); |
| 119 | return [ |
| 120 | '@context' => Fedi::CONTEXT, |
| 121 | 'id' => self::url($note).'/activity', |
| 122 | 'type' => 'Create', |
| 123 | 'actor' => Fedi::actorUrl($note['author']), |
| 124 | 'published' => $note['published'], |
| 125 | 'to' => $note['to'], |
| 126 | 'cc' => $note['cc'], |
| 127 | 'object' => $obj, |
| 128 | ]; |
| 129 | } |
| 130 | } |