Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ Posts also have sequencial IDs, generated by incrementing the following key:
INCR next_post_id => 134

Every post is stored into an hash named `post:134` with the following
fields:

user_id
time
body
pre

Comments
---

Comments also have sequencial IDs, generated by incrementing the following key:

INCR next_comment_id => 123

Every comment is stored into an hash named `comment:123` with the following
fields:

user_id
Expand Down
28 changes: 28 additions & 0 deletions comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?
include("retwis.php");

if (!isLoggedIn()) {
header("Location:".index.php);
exit;
}

foreach ($_POST as $key => $value){
if (empty($value)) {
$refferer = $_SERVER['HTTP_REFERER'];
header("Location:".$refferer);
exit;
}
$cid = $key;
$comment = $value;
}

$r = redisLink();
$commentid = $r->incr("next_comment_id");
$comment = str_replace("\n"," ",$comment);
$r->hmset("comment:$commentid","user_id",$User['id'],"time",time(),"body",$comment);
$r->lpush("comments:$cid", $commentid);

$refferer = $_SERVER['HTTP_REFERER'];
header("Location:".$refferer);
?>

27 changes: 27 additions & 0 deletions followers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?
include("retwis.php");
if (!isLoggedIn()) {
header("Location: index.php");
exit;
}
include("header.php");
echo("<h2>My Followers</h2>");
echo("<i>They are:</i><br>");
$r = redisLink();
$userid = $User['id'];
$followers = $r->zrange("followers:$userid",0,-1);
echo("<table>");
echo("<tr>");
$i = 0;
foreach($followers as $followerID) {
if ($i % 8 == 0) {
echo("</tr>");
echo("<tr>");
}
$followerName = $r->hget("user:$followerID","username");
echo("<td><a class=\"username\" href=\"profile.php?u=".urlencode($followerName)."\">".utf8entities($followerName)."</a></td>");
$i ++;
}
echo("</table>");
include("footer.php")
?>
27 changes: 27 additions & 0 deletions following.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?
include("retwis.php");
if (!isLoggedIn()) {
header("Location: index.php");
exit;
}
include("header.php");
echo("<h2>My Following</h2>");
echo("<i>They are</i><br>");
$r = redisLink();
$userid = $User['id'];
$following = $r->zrange("following:$userid",0,-1);
echo("<table>");
echo("<tr>");
$i = 0;
foreach($following as $followingID) {
if ($i % 8 == 0) {
echo("</tr>");
echo("<tr>");
}
$followingName = $r->hget("user:$followingID","username");
echo("<td><a class=\"username\" href=\"profile.php?u=".urlencode($followingName)."\">".utf8entities($followingName)."</a></td>");
$i ++;
}
echo("</table>");
include("footer.php")
?>
4 changes: 2 additions & 2 deletions home.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
</table>
</form>
<div id="homeinfobox">
<?=$r->zcard("followers:".$User['id'])?> followers<br>
<?=$r->zcard("following:".$User['id'])?> following<br>
<a href="followers.php" style="text-decoration: none;"><?=$r->zcard("followers:".$User['id'])?> followers</a><br>
<a href="following.php" style="text-decoration: none;"><?=$r->zcard("following:".$User['id'])?> following</a><br>
</div>
</div>
<?
Expand Down
6 changes: 4 additions & 2 deletions post.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
$r = redisLink();
$postid = $r->incr("next_post_id");
$status = str_replace("\n"," ",gt("status"));
$r->hmset("post:$postid","user_id",$User['id'],"time",time(),"body",$status);
$r->hmset("post:$postid","user_id",$User['id'],"time",time(),"body",$status,"ref",-1);
$followers = $r->zrange("followers:".$User['id'],0,-1);
$followers[] = $User['id']; /* Add the post to our own posts too */
$userID = $User['id'];
$followers[] = $userID; /* Add the post to our own posts too */

$r->lpush("selfPosts:$userID",$postid);
foreach($followers as $fid) {
$r->lpush("posts:$fid",$postid);
}
Expand Down
2 changes: 1 addition & 1 deletion profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
?>
<?
$start = gt("start") === false ? 0 : intval(gt("start"));
showUserPostsWithPagination(gt("u"),$userid,$start,10);
showUserPostsWithPagination(gt("u"),$userid,$start,10,"SELF");
include("footer.php")
?>
35 changes: 35 additions & 0 deletions repost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?
include("retwis.php");

if (!isLoggedIn()) {
header("Location:".index.php);
exit;
}

foreach ($_POST as $key => $value){
if (empty($value)) {
$value = "REPOST";
}
$cid = $key;
$status = $value;
}
$r = redisLink();
$postid = $r->incr("next_post_id");
$r->hmset("post:$postid","user_id",$User['id'],"time",time(),"body",$status,"ref",$cid);
$followers = $r->zrange("followers:".$User['id'],0,-1);
$userID = $User['id'];
$followers[] = $userID; /* Add the post to our own posts too */

$r->lpush("selfPosts:$userID",$postid);
foreach($followers as $fid) {
$r->lpush("posts:$fid",$postid);
}
# Push the post on the timeline, and trim the timeline to the
# newest 1000 elements.
$r->lpush("timeline",$postid);
$r->ltrim("timeline",0,1000);

$refferer = $_SERVER['HTTP_REFERER'];
header("Location:".$refferer);
?>

67 changes: 55 additions & 12 deletions retwis.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ function strElapsed($t) {
return "$d day".($d > 1 ? "s" : "");
}

function showComment($id) {
$r = redisLink();
$comments = $r->lrange("comments:".$id, 0, -1);
foreach($comments as $cid) {
$content = $r->hgetall("comment:".$cid);
$userid = $content['user_id'];
$username = $r->hget("user:$userid","username");
$elapsed = strElapsed($content['time']);
$userlink = "<a class=\"username\" href=\"profile.php?u=".urlencode($username)."\">".utf8entities($username)."</a>";
$comment = utf8entities($content['body']);
echo("&nbsp&nbsp&nbsp".$userlink.' '.utf8entities($content['body'])."<br/>");
echo('&nbsp&nbsp&nbsp<i>commented '.$elapsed.' ago via web</i><br/>');
}
}

function showPost($id) {
$r = redisLink();
$post = $r->hgetall("post:$id");
Expand All @@ -95,25 +110,53 @@ function showPost($id) {
$username = $r->hget("user:$userid","username");
$elapsed = strElapsed($post['time']);
$userlink = "<a class=\"username\" href=\"profile.php?u=".urlencode($username)."\">".utf8entities($username)."</a>";

echo('<div class="post">'.$userlink.' '.utf8entities($post['body'])."<br>");
echo('<i>posted '.$elapsed.' ago via web</i></div>');
$status = utf8entities($post['body']);
$ref = $post['ref'];

echo('<div class="post">'.$userlink.' '.$status);
while($ref != -1) {
$refpost = $r->hgetall("post:$ref");
if (empty($refpost)) break;
$refuserid = $refpost['user_id'];
$refusername = $r->hget("user:$refuserid","username");
$refelapsed = strElapsed($refpost['time']);
$refuserlink = "<a class=\"username\" href=\"profile.php?u=".urlencode($refusername)."\">".utf8entities($refusername)."</a>";
$refstatus = utf8entities($refpost['body']);
echo('&nbsp||&nbsp'.$refuserlink.' '.$refstatus);
$ref = $refpost['ref'];
}
echo('<br/>');
echo('<i>posted '.$elapsed.' ago via web</i>');
?>
<br/>
<form method="POST">
<table>
<tr><td><textarea cols="70" rows="2" name=<?echo "$id";?>></textarea></td></tr>
<tr><td align="right"><input type="submit" formaction="repost.php" value="repost">
<input type="submit" formaction="comment.php" value="comment"></td></tr>
</table>
</form>
<br/>
<?
showComment($id);
echo("</div>");
return true;
}

function showUserPosts($userid,$start,$count) {
function showUserPosts($userid,$start,$count,$means="ALL") {
$r = redisLink();
$key = ($userid == -1) ? "timeline" : "posts:$userid";
$posts = $r->lrange($key,$start,$start+$count);
$c = 0;
if($means != "ALL")
$key = "selfPosts:$userid";
else
$key = ($userid == -1) ? "timeline" : "posts:$userid";
$posts = $r->lrange($key,$start,$start+$count-1);
foreach($posts as $p) {
if (showPost($p)) $c++;
if ($c == $count) break;
showPost($p);
}
return count($posts) == $count+1;
return count($posts) == $count;
}

function showUserPostsWithPagination($username,$userid,$start,$count) {
function showUserPostsWithPagination($username,$userid,$start,$count,$means="ALL") {
global $_SERVER;
$thispage = $_SERVER['PHP_SELF'];

Expand All @@ -124,7 +167,7 @@ function showUserPostsWithPagination($username,$userid,$start,$count) {
if ($prev < 0) $prev = 0;

$u = $username ? "&u=".urlencode($username) : "";
if (showUserPosts($userid,$start,$count))
if (showUserPosts($userid,$start,$count,$means))
$nextlink = "<a href=\"$thispage?start=$next".$u."\">Older posts &raquo;</a>";
if ($start > 0) {
$prevlink = "<a href=\"$thispage?start=$prev".$u."\">&laquo; Newer posts</a>".($nextlink ? " | " : "");
Expand Down