Skip to content

Commit

Permalink
create a single timeline entry per event
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed May 24, 2013
1 parent f2ac332 commit f51ed0c
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 35 deletions.
3 changes: 2 additions & 1 deletion bin/mongodb/play21.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ print('index forum post authors');
db.f_post.ensureIndex({userId:1})

print('create timeline_entry collection');
db.createCollection('timeline_entry');
db.createCollection("timeline_entry",{capped:true,size:50000000})
db.timeline_entry.ensureIndex({user:1, date: -1});
db.timeline_entry.ensureIndex({type:1, date: -1});

// print("Reset lobby_room");
// db.lobby_room.drop();
Expand Down
4 changes: 2 additions & 2 deletions modules/forum/src/main/PostApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import lila.common.paginator._
import lila.db.api._
import lila.db.Implicits._
import lila.db.paginator._
import lila.hub.actorApi.timeline.{ MakeEntry, ForumPost }
import lila.hub.actorApi.timeline.{ ShareEntry, ForumPost }
import lila.hub.ActorLazyRef
import lila.mod.ModlogApi
import lila.user.{ User, Context }
Expand Down Expand Up @@ -41,7 +41,7 @@ final class PostApi(
(indexer ! InsertPost(post)) >>
(env.recent.invalidate inject post) >>-
(ctx.userId ?? { userId
relationActor ! MakeEntry(
relationActor ! ShareEntry(
userId,
ForumPost(userId, categ.id, topic.slug, topic.name, lastPageOf(topic withPost post), post.number)
)
Expand Down
3 changes: 2 additions & 1 deletion modules/hub/src/main/actorApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ package timeline {
implicit val forumPostFormat = Json.format[ForumPost]
}

case class MakeEntry(user: String, data: Atom)
case class ShareEntry(user: String, data: Atom)
case class MakeEntry(users: List[String], data: Atom)
}

package game {
Expand Down
10 changes: 4 additions & 6 deletions modules/relation/src/main/RelationActor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import akka.pattern.{ ask, pipe }
import actorApi._
import lila.hub.actorApi.relation._
import lila.hub.actorApi.SendTos
import lila.hub.actorApi.timeline.MakeEntry
import lila.hub.actorApi.timeline.{ MakeEntry, ShareEntry }
import lila.hub.ActorLazyRef

private[relation] final class RelationActor(
Expand All @@ -18,11 +18,9 @@ private[relation] final class RelationActor(

def receive = {

case MakeEntry(userId, data) getFriendIds(userId) foreach { ids
ids foreach { id
timelinePush ! MakeEntry(id, data)
}
}
case ShareEntry(userId, data) getFriendIds(userId) map { ids
MakeEntry(ids.toList, data)
} pipeTo timelinePush.ref

// rarely called
// return a list of usernames, followers, following and online
Expand Down
6 changes: 3 additions & 3 deletions modules/relation/src/main/RelationApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package lila.relation
import lila.db.api._
import lila.db.Implicits._
import lila.game.GameRepo
import lila.hub.actorApi.timeline.{ MakeEntry, Follow FollowUser, FollowYou }
import lila.hub.actorApi.timeline.{ MakeEntry, ShareEntry, Follow FollowUser, FollowYou }
import lila.hub.ActorLazyRef
import lila.user.tube.userTube
import lila.user.{ User, UserRepo }
Expand Down Expand Up @@ -35,8 +35,8 @@ final class RelationApi(
case Some(Follow) fufail("Already following")
case _ RelationRepo.follow(u1, u2) >>
cached.invalidate(u1, u2) >>-
(timelinePush ! MakeEntry(u2, FollowYou(u1))) >>-
(relationActor ! MakeEntry(u1, FollowUser(u1, u2)))
(timelinePush ! MakeEntry(List(u2), FollowYou(u1))) >>-
(relationActor ! ShareEntry(u1, FollowUser(u1, u2)))
}

def block(u1: ID, u2: ID): Funit =
Expand Down
6 changes: 3 additions & 3 deletions modules/team/src/main/TeamApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.scala_tools.time.Imports._
import actorApi._
import lila.db.api._
import lila.hub.actorApi.forum.MakeTeam
import lila.hub.actorApi.timeline.{ MakeEntry, TeamJoin, TeamCreate }
import lila.hub.actorApi.timeline.{ ShareEntry, TeamJoin, TeamCreate }
import lila.hub.ActorLazyRef
import lila.user.tube.userTube
import lila.user.{ User, Context }
Expand Down Expand Up @@ -37,7 +37,7 @@ final class TeamApi(
(cached.teamIds remove me.id) >>-
(forum ! MakeTeam(team.id, team.name)) >>-
(indexer ! InsertTeam(team)) >>-
(relationActor ! MakeEntry(me.id, TeamCreate(me.id, team.id))) inject team
(relationActor ! ShareEntry(me.id, TeamCreate(me.id, team.id))) inject team
}

def update(team: Team, edit: TeamEdit, me: User): Funit = edit.trim |> { e
Expand Down Expand Up @@ -114,7 +114,7 @@ final class TeamApi(
MemberRepo.add(team.id, userId) >>
TeamRepo.incMembers(team.id, +1) >>
(cached.teamIds remove userId) >>-
(relationActor ! MakeEntry(userId, TeamJoin(userId, team.id)))
(relationActor ! ShareEntry(userId, TeamJoin(userId, team.id)))
}
}

Expand Down
11 changes: 4 additions & 7 deletions modules/timeline/src/main/Entry.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ import lila.hub.actorApi.timeline._
import lila.hub.actorApi.timeline.atomFormat._

case class Entry(
user: String,
users: List[String],
typ: String,
data: JsObject,
date: DateTime) {

import Entry._

def similarTo(other: Entry) =
(user == other.user) &&
(typ == other.typ) &&
(data == other.data)
def similarTo(other: Entry) = typ == other.typ && data == other.data

def decode: Option[Atom] = (typ match {
case "follow" Json.fromJson[Follow](data)
Expand All @@ -31,14 +28,14 @@ case class Entry(

object Entry {

private[timeline] def make(user: String, data: Atom): Option[Entry] = (data match {
private[timeline] def make(users: List[String], data: Atom): Option[Entry] = (data match {
case d: Follow "follow" -> Json.toJson(d)
case d: FollowYou "follow-you" -> Json.toJson(d)
case d: TeamJoin "team-join" -> Json.toJson(d)
case d: TeamCreate "team-create" -> Json.toJson(d)
case d: ForumPost "forum-post" -> Json.toJson(d)
}) match {
case (typ, json) json.asOpt[JsObject] map { new Entry(user, typ, _, DateTime.now) }
case (typ, json) json.asOpt[JsObject] map { new Entry(users, typ, _, DateTime.now) }
}

import lila.db.Tube
Expand Down
23 changes: 12 additions & 11 deletions modules/timeline/src/main/Push.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@ private[timeline] final class Push(
renderer: lila.hub.ActorLazyRef) extends Actor {

def receive = {
case MakeEntry(user, data) makeEntry(user, data) foreach { entry
lobbySocket.ref ! ReloadTimeline(user)
}
case MakeEntry(users, data) makeEntry(users, data) >>-
(users foreach { u
lobbySocket.ref ! ReloadTimeline(u)
})
}

private def makeEntry(user: String, data: Atom): Fu[Entry] =
Entry.make(user, data).fold(
private def makeEntry(users: List[String], data: Atom): Fu[Entry] =
Entry.make(users, data).fold(
fufail[Entry]("[timeline] invalid entry data " + data)
) { entry
$find(Json.obj("user" -> user, "date" -> $gt($date(DateTime.now - 1.hour)))) flatMap { entries
entries exists (_ similarTo entry) fold (
fufail[Entry]("[timeline] a similar entry already exists"),
$insert(entry) inject entry
)
$find(Json.obj("typ" -> entry.typ, "date" -> $gt($date(DateTime.now - 1.hour)))) flatMap { entries
entries exists (_ similarTo entry) fold (
fufail[Entry]("[timeline] a similar entry already exists"),
$insert(entry) inject entry
)
}
}
}
}
1 change: 0 additions & 1 deletion todo
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ takeback/enpassant glitch http:https://en.lichess.org/forum/lichess-feedback/i-found-a
show teams in user mini
badges for top players in ELO and number of games
use jquery.timeago everywhere
sort imports

DEPLOY p21
----------
Expand Down

0 comments on commit f51ed0c

Please sign in to comment.