| 1 | # -*- mode: perl -*- | ||
| 2 | # Copyright (C) 2017–2021 Alex Schroeder <alex@gnu.org> | ||
| 3 | |||
| 4 | # This program is free software: you can redistribute it and/or modify it under | ||
| 5 | # the terms of the GNU Affero General Public License as published by the Free | ||
| 6 | # Software Foundation, either version 3 of the License, or (at your option) any | ||
| 7 | # later version. | ||
| 8 | # | ||
| 9 | # This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 10 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU Affero General Public License along | ||
| 15 | # with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | =encoding utf8 | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | App::Phoebe::Comments - add comment pages to Phoebe wiki | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Add a comment link to footers such that visitors can comment via Gemini. | ||
| 26 | Commenting requires the access token. | ||
| 27 | |||
| 28 | Comments are appended to a "comments page". For every page I<Foo> the comments | ||
| 29 | are found on I<Comments on Foo>. This prefix is fixed, currently. | ||
| 30 | |||
| 31 | On the comments page, each new comment starts with the character LEFT SPEECH | ||
| 32 | BUBBLE (🗨). This character is fixed, currently. | ||
| 33 | |||
| 34 | There is no configuration. Simply add it to your F<config> file: | ||
| 35 | |||
| 36 | use App::Phoebe::Comments; | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | package App::Phoebe::Comments; | ||
| 41 | use App::Phoebe qw(@footer @extensions $log with_lock port space host_regex space_regex | ||
| 42 | result valid_id valid_token wiki_dir write_page decode_query); | ||
| 43 | use Encode qw(decode_utf8); | ||
| 44 | use Modern::Perl; | ||
| 45 | use URI::Escape; | ||
| 46 | use File::Slurper qw(read_text); | ||
| 47 | use utf8; | ||
| 48 | |||
| 49 | push(@footer, \&add_comment_link_to_footer); | ||
| 50 | |||
| 51 | sub add_comment_link_to_footer { | ||
| 52 | my ($stream, $host, $space, $id, $revision, $scheme) = @_; | ||
| 53 | # only leave comments on current comment pages | ||
| 54 | return "" if $revision; | ||
| 55 | $space = "/" . uri_escape_utf8($space) if $space; | ||
| 56 | $space //= ""; | ||
| 57 | return "=> $space/page/" . uri_escape_utf8("Comments on $id") . " Comments" if $id !~ /^Comments on /; | ||
| 58 | return "=> $space/do/comment/" . uri_escape_utf8($id) . " Leave a short comment" if $scheme eq "gemini"; | ||
| 59 | } | ||
| 60 | |||
| 61 | push(@extensions, \&process_comment_requests); | ||
| 62 | |||
| 63 | sub process_comment_requests { | ||
| 64 | my ($stream, $url) = @_; | ||
| 65 | my $hosts = host_regex(); | ||
| 66 | my $spaces = space_regex(); | ||
| 67 | my $port = port($stream); | ||
| 68 | my ($host, $space, $id, $token, $query); | ||
| 69 | if ($url =~ m!^gemini://($hosts)(?::$port)?(?:/($spaces))?/do/comment/([^/#?]+)$!) { | ||
| 70 | result($stream, "10", "Access token"); | ||
| 71 | } elsif (($host, $space, $id, $token) = | ||
| 72 | $url =~ m!^gemini://($hosts)(?::$port)?(?:/($spaces))?/do/comment/([^/#?]+)\?([^#]+)!) { | ||
| 73 | if ($space) { | ||
| 74 | result($stream, "30", "gemini://$host:$port/$space/do/comment/$id/$token"); | ||
| 75 | } else { | ||
| 76 | result($stream, "30", "gemini://$host:$port/do/comment/$id/$token"); | ||
| 77 | } | ||
| 78 | } elsif ($url =~ m!^gemini://($hosts)(?::$port)?(?:/($spaces))?/do/comment/([^/#?]+)/([^/#?]+)$!) { | ||
| 79 | result($stream, "10", "Short comment"); | ||
| 80 | } elsif (($host, $space, $id, $token, $query) = $url =~ m!^gemini://($hosts)(?::$port)?(?:/($spaces))?/do/comment/([^/#?]+)/([^/#?]+)\?([^#]+)!) { | ||
| 81 | append_comment($stream, $host, space($host, $space), | ||
| 82 | decode_utf8(uri_unescape($id)), | ||
| 83 | decode_utf8(uri_unescape($token)), | ||
| 84 | decode_query($query)); | ||
| 85 | } else { | ||
| 86 | return 0; | ||
| 87 | } | ||
| 88 | return 1; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub append_comment { | ||
| 92 | my ($stream, $host, $space, $id, $token, $query) = @_; | ||
| 93 | return if not valid_id($stream, $host, $space, $id); | ||
| 94 | return if not valid_token($stream, $host, $space, $id, {token => $token}); | ||
| 95 | $log->info("Reading page $id and appending comment"); | ||
| 96 | my $dir = wiki_dir($host, $space); | ||
| 97 | my $file = "$dir/page/$id.gmi"; | ||
| 98 | my $text; | ||
| 99 | if (-e $file) { | ||
| 100 | $text = read_text($file) . "\n\n🗨 " . $query; | ||
| 101 | } else { | ||
| 102 | $text = "🗨 $query"; | ||
| 103 | } | ||
| 104 | # We don't need to close the stream because this is called via process_gemini | ||
| 105 | # which always closes the stream in the end. | ||
| 106 | with_lock($stream, $host, $space, sub { write_page($stream, $host, $space, $id, $text) } ); | ||
| 107 | } |