@@ -16,138 +16,81 @@ final class ActivityWriteApi(
16
16
import activities ._
17
17
import model ._
18
18
19
- def game (game : Game ): Funit = withColl { coll =>
20
- game.userIds
21
- .flatMap { userId =>
22
- for {
23
- pt <- game.perfType
24
- player <- game playerByUserId userId
25
- } yield for {
26
- a <- getOrCreate(userId)
27
- setGames = ! game.isCorrespondence ?? $doc(
28
- ActivityFields .games -> a.games.orDefault
29
- .add(pt, Score .make(game wonBy player.color, RatingProg make player))
30
- )
31
- setCorres = game.hasCorrespondenceClock ?? $doc(
32
- ActivityFields .corres -> a.corres.orDefault.add(GameId (game.id), moved = false , ended = true )
33
- )
34
- setters = setGames ++ setCorres
35
- _ <- (! setters.isEmpty) ?? coll.update.one($id(a.id), $set(setters), upsert = true ).void
36
- } yield ()
37
- }
38
- .sequenceFu
39
- .void
40
- }
41
-
42
- def forumPost (post : lila.forum.Post ): Funit = withColl { coll =>
19
+ def game (game : Game ): Funit =
20
+ (for {
21
+ userId <- game.userIds
22
+ pt <- game.perfType
23
+ player <- game playerByUserId userId
24
+ } yield update(userId) { a =>
25
+ val setGames = ! game.isCorrespondence ?? $doc(
26
+ ActivityFields .games -> a.games.orDefault
27
+ .add(pt, Score .make(game wonBy player.color, RatingProg make player))
28
+ )
29
+ val setCorres = game.hasCorrespondenceClock ?? $doc(
30
+ ActivityFields .corres -> a.corres.orDefault.add(GameId (game.id), moved = false , ended = true )
31
+ )
32
+ setGames ++ setCorres
33
+ }).sequenceFu.void
34
+
35
+ def forumPost (post : lila.forum.Post ): Funit =
43
36
post.userId.filter(User .lichessId != ) ?? { userId =>
44
- getOrCreate(userId) flatMap { a =>
45
- coll.update
46
- .one(
47
- $id(a.id),
48
- $set(ActivityFields .forumPosts -> (~ a.forumPosts + ForumPostId (post.id))),
49
- upsert = true
50
- )
51
- .void
37
+ update(userId) { a =>
38
+ $doc(ActivityFields .forumPosts -> (~ a.forumPosts + ForumPostId (post.id)))
52
39
}
53
40
}
54
- }
55
41
56
- def ublogPost (post : lila.ublog.UblogPost ): Funit = withColl { coll =>
57
- getOrCreate(post.created.by) flatMap { a =>
58
- coll.update
59
- .one(
60
- $id(a.id),
61
- $set(ActivityFields .ublogPosts -> (~ a.ublogPosts + UblogPostId (post.id.value))),
62
- upsert = true
63
- )
64
- .void
65
- }
42
+ def ublogPost (post : lila.ublog.UblogPost ): Funit = update(post.created.by) { a =>
43
+ $doc(ActivityFields .ublogPosts -> (~ a.ublogPosts + UblogPostId (post.id.value)))
66
44
}
67
45
68
- def puzzle (res : lila.puzzle.Puzzle .UserResult ): Funit = withColl { coll =>
69
- getOrCreate(res.userId) flatMap { a =>
70
- coll.update
71
- .one(
72
- $id(a.id),
73
- $set(ActivityFields .puzzles -> {
74
- ~ a.puzzles + Score .make(
75
- res = res.result.win.some,
76
- rp = RatingProg (Rating (res.rating._1), Rating (res.rating._2)).some
77
- )
78
- }),
79
- upsert = true
80
- )
81
- .void
82
- }
46
+ def puzzle (res : lila.puzzle.Puzzle .UserResult ): Funit = update(res.userId) { a =>
47
+ $doc(ActivityFields .puzzles -> {
48
+ ~ a.puzzles + Score .make(
49
+ res = res.result.win.some,
50
+ rp = RatingProg (Rating (res.rating._1), Rating (res.rating._2)).some
51
+ )
52
+ })
83
53
}
84
54
85
- def storm (userId : User .ID , score : Int ): Funit = withColl { coll =>
86
- getOrCreate(userId) flatMap { a =>
87
- coll.update
88
- .one(
89
- $id(a.id),
90
- $set(ActivityFields .storm -> { ~ a.storm + score }),
91
- upsert = true
92
- )
93
- .void
94
- }
55
+ def storm (userId : User .ID , score : Int ): Funit = update(userId) { a =>
56
+ $doc(ActivityFields .storm -> { ~ a.storm + score })
95
57
}
96
58
97
- def racer (userId : User .ID , score : Int ): Funit = withColl { coll =>
98
- getOrCreate(userId) flatMap { a =>
99
- coll.update
100
- .one(
101
- $id(a.id),
102
- $set(ActivityFields .racer -> { ~ a.racer + score }),
103
- upsert = true
104
- )
105
- .void
106
- }
59
+ def racer (userId : User .ID , score : Int ): Funit = update(userId) { a =>
60
+ $doc(ActivityFields .racer -> { ~ a.racer + score })
107
61
}
108
62
109
- def streak (userId : User .ID , score : Int ): Funit = withColl { coll =>
110
- getOrCreate(userId) flatMap { a =>
111
- coll.update
112
- .one(
113
- $id(a.id),
114
- $set(ActivityFields .streak -> { ~ a.streak + score }),
115
- upsert = true
116
- )
117
- .void
118
- }
63
+ def streak (userId : User .ID , score : Int ): Funit = update(userId) { a =>
64
+ $doc(ActivityFields .streak -> { ~ a.streak + score })
119
65
}
120
66
121
- def learn (userId : User .ID , stage : String ) =
122
- update(userId) { a =>
123
- a.copy(learn = Some (~ a.learn + Learn .Stage (stage))).some
124
- }
67
+ def learn (userId : User .ID , stage : String ) = update(userId) { a =>
68
+ $doc(ActivityFields .learn -> { ~ a.learn + Learn .Stage (stage) })
69
+ }
125
70
126
- def practice (prog : lila.practice.PracticeProgress .OnComplete ) =
127
- update(prog.userId) { a =>
128
- a.copy(practice = Some (~ a.practice + prog.studyId)).some
129
- }
71
+ def practice (prog : lila.practice.PracticeProgress .OnComplete ) = update(prog.userId) { a =>
72
+ $doc(ActivityFields .practice -> { ~ a.practice + prog.studyId })
73
+ }
130
74
131
75
def simul (simul : lila.simul.Simul ) =
132
- simulParticipant(simul, simul.hostId) >>
133
- simul.pairings.map(_.player.user).map { simulParticipant(simul, _) }.sequenceFu.void
134
-
135
- def corresMove (gameId : Game .ID , userId : User .ID ) =
136
- update(userId) { a =>
137
- a.copy(corres = Some ((~ a.corres).add(GameId (gameId), moved = true , ended = false ))).some
76
+ lila.common.Future .applySequentially(simul.hostId :: simul.pairings.map(_.player.user)) {
77
+ simulParticipant(simul, _)
138
78
}
139
79
140
- def plan (userId : User .ID , months : Int ) =
141
- update(userId) { a =>
142
- a.copy(patron = Some (Patron (months))).some
143
- }
80
+ def corresMove (gameId : Game .ID , userId : User .ID ) = update(userId) { a =>
81
+ $doc(ActivityFields .corres -> { (~ a.corres).add(GameId (gameId), moved = true , ended = false ) })
82
+ }
83
+
84
+ def plan (userId : User .ID , months : Int ) = update(userId) { a =>
85
+ $doc(ActivityFields .patron -> Patron (months))
86
+ }
144
87
145
88
def follow (from : User .ID , to : User .ID ) =
146
89
update(from) { a =>
147
- a.copy( follows = Some ( ~ a.follows addOut to)).some
90
+ $doc( ActivityFields . follows -> { ~ a.follows addOut to })
148
91
} >>
149
92
update(to) { a =>
150
- a.copy( follows = Some ( ~ a.follows addIn from)).some
93
+ $doc( ActivityFields . follows -> { ~ a.follows addIn from })
151
94
}
152
95
153
96
def unfollowAll (from : User , following : Set [User .ID ]) =
@@ -176,36 +119,47 @@ final class ActivityWriteApi(
176
119
studyApi byId id flatMap {
177
120
_.filter(_.isPublic) ?? { s =>
178
121
update(s.ownerId) { a =>
179
- a.copy( studies = Some ( ~ a.studies + s.id)).some
122
+ $doc( ActivityFields . studies -> { ~ a.studies + s.id })
180
123
}
181
124
}
182
125
}
183
126
184
127
def team (id : String , userId : User .ID ) =
185
128
update(userId) { a =>
186
- a.copy( teams = Some ( ~ a.teams + id)).some
129
+ $doc( ActivityFields . teams -> { ~ a.teams + id })
187
130
}
188
131
189
132
def streamStart (userId : User .ID ) =
190
- update(userId) { _.copy(stream = true ).some }
133
+ update(userId) { _ =>
134
+ $doc(ActivityFields .stream -> true )
135
+ }
191
136
192
137
def swiss (id : lila.swiss.Swiss .Id , ranking : lila.swiss.Ranking ) =
193
- ranking.map { case (userId, rank) =>
194
- update(userId) { a => a.copy(swisses = Some (~ a.swisses + SwissRank (id, rank))).some }
195
- }.sequenceFu
196
-
197
- private def simulParticipant (simul : lila.simul.Simul , userId : String ) =
198
- update(userId) { a =>
199
- a.copy(simuls = Some (~ a.simuls + SimulId (simul.id))).some
138
+ lila.common.Future .applySequentially(ranking.toList) { case (userId, rank) =>
139
+ update(userId) { a =>
140
+ $doc(ActivityFields .swisses -> { ~ a.swisses + SwissRank (id, rank) })
141
+ }
200
142
}
201
143
202
- private def get (userId : User .ID ) = withColl(_.byId[Activity , Id ](Id today userId))
203
- private def getOrCreate (userId : User .ID ) = get(userId) map { _ | Activity .make(userId) }
204
- private def save (activity : Activity ) = withColl(
205
- _.update.one($id(activity.id), activity, upsert = true ).void
206
- )
207
- private def update (userId : User .ID )(f : Activity => Option [Activity ]): Funit =
208
- getOrCreate(userId) flatMap { old =>
209
- f(old) ?? save
144
+ private def simulParticipant (simul : lila.simul.Simul , userId : User .ID ) = update(userId) { a =>
145
+ $doc(ActivityFields .simuls -> { ~ a.simuls + SimulId (simul.id) })
146
+ }
147
+
148
+ private def update (userId : User .ID )(makeSetters : Activity => Bdoc ): Funit =
149
+ withColl { coll =>
150
+ coll.byId[Activity , Id ](Id today userId).dmap { _ | Activity .make(userId) } flatMap { activity =>
151
+ val setters = makeSetters(activity)
152
+ ! setters.isEmpty ?? {
153
+ coll.update
154
+ .one($id(activity.id), $set(setters), upsert = true )
155
+ .flatMap { res =>
156
+ (res.upserted.nonEmpty ?? truncate(coll, activity.id.userId))
157
+ }
158
+ .void
159
+ }
160
+ }
210
161
}
162
+
163
+ private def truncate (coll : Coll , userId : User .ID ) = funit
164
+
211
165
}
0 commit comments