]> Sergey Matveev's repositories - public-inbox.git/commitdiff
inbox: streamline ->nntp_url
authorEric Wong <e@80x24.org>
Thu, 16 Sep 2021 00:26:51 +0000 (00:26 +0000)
committerEric Wong <e@80x24.org>
Thu, 16 Sep 2021 04:29:11 +0000 (04:29 +0000)
We no longer waste a precious hash slot for a per-Inbox
{nntpserver} if it's only configured globally for all inboxes.

lib/PublicInbox/Config.pm
lib/PublicInbox/Inbox.pm
lib/PublicInbox/WwwText.pm
t/config.t

index ee5322fe142a1507cd1c11a0d9db23a9549ad5d2..5e7a9f2b6d8d55e61fce036ca101bbda10c9f761 100644 (file)
@@ -424,7 +424,7 @@ sub _fill_ibx {
        my ($self, $name) = @_;
        my $pfx = "publicinbox.$name";
        my $ibx = {};
-       for my $k (qw(watch nntpserver)) {
+       for my $k (qw(watch)) {
                my $v = $self->{"$pfx.$k"};
                $ibx->{$k} = $v if defined $v;
        }
@@ -451,7 +451,7 @@ sub _fill_ibx {
        # TODO: more arrays, we should support multi-value for
        # more things to encourage decentralization
        for my $k (qw(address altid nntpmirror coderepo hide listid url
-                       infourl watchheader)) {
+                       infourl watchheader nntpserver)) {
                my $v = $self->{"$pfx.$k"} // next;
                $ibx->{$k} = _array($v);
        }
index b0bb9dcc288daa328cfd44da340d4ae74a274a2b..f234b96f782f66f76d11df40f83e2ede5710c54b 100644 (file)
@@ -109,8 +109,6 @@ sub new {
        } else {
                delete $opts->{feedmax};
        }
-       $opts->{nntpserver} ||= $pi_cfg->{'publicinbox.nntpserver'};
-
        # allow any combination of multi-line or comma-delimited hide entries
        my $hide = {};
        if (defined(my $h = $opts->{hide})) {
@@ -261,22 +259,21 @@ sub base_url {
 }
 
 sub nntp_url {
-       my ($self) = @_;
+       my ($self, $ctx) = @_;
        $self->{-nntp_url} ||= do {
                # no checking for nntp_usable here, we can point entirely
                # to non-local servers or users run by a different user
-               my $ns = $self->{nntpserver};
+               my $ns = $self->{nntpserver} //
+                      $ctx->{www}->{pi_cfg}->get_all('publicinbox.nntpserver');
                my $group = $self->{newsgroup};
                my @urls;
                if ($ns && $group) {
-                       $ns = [ $ns ] if ref($ns) ne 'ARRAY';
                        @urls = map {
                                my $u = m!\Anntps?://! ? $_ : "nntp://$_";
                                $u .= '/' if $u !~ m!/\z!;
                                $u.$group;
                        } @$ns;
                }
-
                my $mirrors = $self->{nntpmirror};
                if ($mirrors) {
                        my @m;
index fabe39f6573077366a9b6d533639f403a7330fba..177d55e46aa1b6a2d269e29b512a9bc06306b0e4 100644 (file)
@@ -210,7 +210,7 @@ EOF
                defined(my $v = $ibx->{$k}) or next;
                $$txt .= "\t$k = $v\n";
        }
-       $$txt .= "\tnntpmirror = $_\n" for (@{$ibx->nntp_url});
+       $$txt .= "\tnntpmirror = $_\n" for (@{$ibx->nntp_url($ctx)});
        _coderepo_config($ctx, $txt);
        1;
 }
@@ -343,7 +343,7 @@ EOM
 Example config snippet for mirrors: $cfg_link
 EOF
        if ($ibx->can('nntp_url')) {
-               my $nntp = $ibx->nntp_url;
+               my $nntp = $ibx->nntp_url($ctx);
                if (scalar @$nntp) {
                        $$txt .= "\n";
                        $$txt .= @$nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
index 877e5d5d45f7fbb1a9edfbe49c5b8fc98f17682a..2b02f06372e21e059cc943d5a3808568c5733171 100644 (file)
@@ -43,7 +43,6 @@ my ($tmpdir, $for_destroy) = tmpdir();
                -primary_address => 'meta@public-inbox.org',
                'name' => 'meta',
                -httpbackend_limiter => undef,
-               nntpserver => undef,
        }, "lookup matches expected output");
 
        is($cfg->lookup('blah@example.com'), undef,
@@ -60,7 +59,6 @@ my ($tmpdir, $for_destroy) = tmpdir();
                'name' => 'test',
                'url' => [ 'http://example.com/test' ],
                -httpbackend_limiter => undef,
-               nntpserver => undef,
        }, "lookup matches expected output for test");
 }
 
@@ -99,20 +97,27 @@ EOF
        my $str = <<EOF;
 $pfx.address=test\@example.com
 $pfx.inboxdir=/path/to/non/existent
+$pfx.newsgroup=inbox.test
 publicinbox.nntpserver=news.example.com
 EOF
        my $cfg = PublicInbox::Config->new(\$str);
        my $ibx = $cfg->lookup_name('test');
-       is($ibx->{nntpserver}, 'news.example.com', 'global NNTP server');
+       is_deeply($ibx->nntp_url({ www => { pi_cfg => $cfg }}),
+               [ 'nntp://news.example.com/inbox.test' ],
+               'nntp_url uses global NNTP server');
 
        $str = <<EOF;
 $pfx.address=test\@example.com
 $pfx.inboxdir=/path/to/non/existent
+$pfx.newsgroup=inbox.test
 $pfx.nntpserver=news.alt.example.com
+publicinbox.nntpserver=news.example.com
 EOF
        $cfg = PublicInbox::Config->new(\$str);
        $ibx = $cfg->lookup_name('test');
-       is($ibx->{nntpserver}, 'news.alt.example.com','per-inbox NNTP server');
+       is_deeply($ibx->nntp_url({ www => { pi_cfg => $cfg }}),
+               [ 'nntp://news.alt.example.com/inbox.test' ],
+               'nntp_url uses per-inbox NNTP server');
 }
 
 # no obfuscate domains