浏览代码

Fix #76: /users/ alias for profile pages

George S. Baugh 5 年之前
父节点
当前提交
9b2f74121e
共有 2 个文件被更改,包括 28 次插入5 次删除
  1. 22 3
      lib/Trog/Routes/HTML.pm
  2. 6 2
      www/templates/posts.tx

+ 22 - 3
lib/Trog/Routes/HTML.pm

@@ -148,6 +148,11 @@ our %routes = (
         callback => \&Trog::Routes::HTML::avatars,
         data     => { tag => ['about'] },
     },
+    '/users/(.*)' => {
+        method => 'GET',
+        callback => \&Trog::Routes::HTML::users,
+        captures => ['username'],
+    },
 );
 
 # Build aliases for /posts and /post with extra data
@@ -544,6 +549,14 @@ sub avatars ($query, $render_cb) {
     return [200,["Content-type: text/css\n"],[$content]];
 }
 
+sub users ($query, $render_cb) {
+    my (undef,$posts) = _post_helper({ limit => 10000 }, ['about'], $query->{acls});
+    my @user = grep { $_->{user} eq $query->{username} } @$posts;
+    $query->{id} = $user[0]->{id};
+    $query->{user_obj} = $user[0];
+    return posts($query,$render_cb);
+}
+
 =head2 posts
 
 Display multi or single posts, supports RSS and pagination.
@@ -553,9 +566,15 @@ Display multi or single posts, supports RSS and pagination.
 sub posts ($query, $render_cb) {
     my $tags = _coerce_array($query->{tag});
 
-    #TODO If we have a direct ID query, we should show unlisted videos as well as public ones IFF they have a valid campaign ID attached to query
-    push(@{$query->{acls}}, 'public', 'unlisted');
-    my ($pages,$posts) = _post_helper($query, $tags, $query->{acls});
+    push(@{$query->{acls}}, 'public');
+    push(@{$query->{acls}}, 'unlisted') if $query->{id};
+    my ($pages,$posts);
+    if ($query->{user_obj}) {
+        #Optimize the /users/* route
+        $posts = [$query->{user_obj}];
+    } else {
+        ($pages,$posts) = _post_helper($query, $tags, $query->{acls});
+    }
 
     #OK, so if we have a user as the ID we found, go grab the rest of their posts
     if ($query->{id} && @$posts && grep { $_ eq 'about'} @{$posts->[0]->{tags}} ) {

+ 6 - 2
www/templates/posts.tx

@@ -16,7 +16,11 @@
     : next;
     : }
     :if ( $tiled ) {
+        : if ($post.is_profile) {
+        <a href="/users/<: $post.user :>" class="tile">
+        : } else {
         <a href="<: $route :>/<: $post.id :>" class="tile">
+        : }
         : if ($post.is_video) {
             <video class="responsive" controls poster="<: $post.preview :>" >
                 <source src="<: $post.href :>" type="<: $post.content_type :>" />
@@ -74,14 +78,14 @@
             : if ( $post.is_image ) {
                 <div style="background-image:url(<: $post.href :>);" class="responsive preview"></div>
             : }
-            : if ( $post.wallpaper ) {
+            : if ( $post.is_profile ) {
                 <div style="background-image:url(<: $post.wallpaper :>);" class="banner">
                     <div style="background-image:url(<: $post.preview :>);" class="circle portrait"></div>
             : }
             : if( $post.data ) {
                 <div id="postData"><: $post.data | mark_raw :></div>
             : }
-            : if( $post.wallpaper ) {
+            : if( $post.is_profile ) {
                 </div>
             : }
             : if ( $can_edit ) {