-
Notifications
You must be signed in to change notification settings - Fork 0
/
Retwis.scala
154 lines (129 loc) · 4.14 KB
/
Retwis.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.retwis.model
import _root_.redis.clients.jedis._
import scala.collection.JavaConversions._
import compat.Platform
// Provide some static methods and access to JedisPool
object Retwis {
val pool = new JedisPool(new JedisPoolConfig(), "localhost");
//create a string showing the time elapsed
def strElapsed(time: Long): String = {
val elapsedSeconds = (Platform.currentTime - time) / 1000
if(elapsedSeconds < 60) return elapsedSeconds + " seconds"
if(elapsedSeconds < 3600) {
val m = elapsedSeconds / 60
return m + " minute"
}
if(elapsedSeconds < 3600*24) {
val h = elapsedSeconds / 3600
return h + " hour"
}
val d = elapsedSeconds / (3600*24)
return d + " day"
}
/*
// Some of this will go in view snippets
function showPost($id) {
$r = redisLink();
$postdata = $r->get("post:$id");
if (!$postdata) return false;
$aux = explode("|",$postdata);
$id = $aux[0];
$time = $aux[1];
$username = $r->get("uid:$id:username");
$post = join(array_splice($aux,2,count($aux)-2),"|");
$elapsed = strElapsed($time);
$userlink = "<a class=\"username\" href=\"profile.php?u=".urlencode($username)."\">".utf8entities($username)."</a>";
echo('<div class="post">'.$userlink.' '.utf8entities($post)."<br>");
echo('<i>posted '.$elapsed.' ago via web</i></div>');
return true;
}
/*
addPostsToTimeline
Merge $userid's posts with existing set of posts and sort chronologically (by PostID)
*/
function addPostsToTimeline($userid, $timeline, $start, $count) {
$r = redisLink();
$posts = $r->lrange("uid:$userid:posts",$start,$start+$count);
foreach($posts as $p) {
$timeline[] = $p;
}
rsort($timeline, SORT_NUMERIC);
return($timeline);
}
/*
getRelevantPosts
Returns array of user's and followee's posts, sorted chronologically.
*/
function getRelevantPosts($userid,$start,$count) {
$r = redisLink();
$followees = $r->smembers("uid:"."$userid".":following"); //get userids of all followees
$posts = $r->lrange("uid:$userid:posts",$start,$start+$count);
foreach($followees as $f) {
$posts = addPostsToTimeline($f, $posts, $start, $count);
}
return $posts;
}
/*
showUserPosts
Show only the user's posts if $includeFollowees if false.
Otherwise, show user's and followees' posts.
*/
function showUserPosts($userid,$start,$count,$includeFollowees) {
$r = redisLink();
$key = ($userid == -1) ? "global:timeline" : "uid:$userid:posts";
if($includeFollowees)
$posts = getRelevantPosts($userid,$start,$count);
else
$posts = $r->lrange($key,$start,$start+$count);
$c = 0;
foreach($posts as $p) {
if (showPost($p)) $c++;
if ($c == $count) break;
}
return count($posts) == $count+1;
}
/*
showUserPostsWithPagination
Shows own and followee's posts, formatted and paginated.
$username - User's names
$userid - User's id
$start - Where to start from
$count - How many to show per page
$includeFollowees - include user's followees?
*/
function showUserPostsWithPagination($username,$userid,$start,$count,$includeFollowees) {
global $_SERVER;
$thispage = $_SERVER['PHP_SELF'];
$navlink = "";
$next = $start+10;
$prev = $start-10;
$nextlink = $prevlink = false;
if ($prev < 0) $prev = 0;
$u = $username ? "&u=".urlencode($username) : "";
if (showUserPosts($userid,$start,$count,$includeFollowees))
$nextlink = "<a href=\"$thispage?start=$next".$u."\">Older posts »</a>";
if ($start > 0) {
$prevlink = "<a href=\"$thispage?start=$prev".$u."\">« Newer posts</a>".($nextlink ? " | " : "");
}
if ($nextlink || $prevlink)
echo("<div class=\"rightlink\">$prevlink $nextlink</div>");
}
function showLastUsers() {
$r = redisLink();
$users = $r->sort("global:users", array('GET' => 'uid:*:username',
'DESC' => 0,
'LIMIT' => array(0 => '0',
1 => '10')));
echo("<div>");
foreach($users as $u) {
echo("<a class=\"username\" href=\"profile.php?u=".urlencode($u)."\">".utf8entities($u)."</a> ");
}
echo("</div><br>");
}
function getAllUsers() {
$r = redisLink();
$allusers = $r->sort("global:users", array('GET' => 'uid:*:username'));
return $allusers;
}
*/
}