Skip to content

Commit 9af0970

Browse files
committed
Custom badges
1 parent 0867274 commit 9af0970

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

src/Server/Database/Badges.purs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import Type.Proxy (Proxy(..))
88

99
type Badges =
1010
( idColumn Int (PrimaryKey /\ Identity)
11-
, kindBadge
12-
, descriptionString
11+
, kindInt
12+
, descriptionBadge
1313
)
1414

1515
type BadgesTable = Table "badges" Badges

src/Server/Im/Database.purs

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ userFields =
6767
/\ _description
6868
/\ (select _name # from countries # wher (_id .=. u ... _country) # orderBy _id # limit (Proxy _ 1) # as _country)
6969
/\ (select (array_agg _feature # as _privileges) # from privileges # wher (_quantity .<=. k ... _current_karma) # orderBy _privileges # limit (Proxy _ 1))
70-
/\ (select (array_agg _kind # as _badges) # from (((badges # as b) `join` (badges_users # as bu)) # on (b ... _id .=. bu ... _badge .&&. bu ... _receiver .=. u ... _id)) # orderBy _badges # limit (Proxy _ 1))
70+
/\ (select (array_agg _description # as _badges) # from (((badges # as b) `join` (badges_users # as bu)) # on (b ... _id .=. bu ... _badge .&&. bu ... _receiver .=. u ... _id)) # orderBy _badges # limit (Proxy _ 1))
7171
/\ (select (array_agg (l ... _name # orderBy (l ... _id)) # as _tags) # from (((tags # as l) `join` (tags_users # as tu)) # on (l ... _id .=. tu ... _tag .&&. tu ... _creator .=. u ... _id)) # orderBy _tags # limit (Proxy _ 1))
7272
/\ (k ... _current_karma # as _karma)
7373
/\ (_position # as _karmaPosition)
@@ -148,7 +148,7 @@ presentUserContactFields =
148148
, description
149149
, (SELECT name FROM countries WHERE id = u.country) country
150150
, (SELECT ARRAY_AGG(l.name ORDER BY name) FROM languages l JOIN languages_users lu ON l.id = lu.language AND lu.speaker = u.id) languages
151-
, (SELECT ARRAY_AGG(kind) FROM badges b JOIN badges_users bu ON b.id = bu.badge AND bu.receiver = u.id) badges
151+
, (SELECT ARRAY_AGG(description) FROM badges b JOIN badges_users bu ON b.id = bu.badge AND bu.receiver = u.id) badges
152152
, (SELECT ARRAY_AGG(t.name ORDER BY t.id) FROM tags t JOIN tags_users tu ON t.id = tu.tag AND tu.creator = u.id) tags
153153
, k.current_karma karma
154154
, position "karmaPosition"

src/Server/Profile/Database.purs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ presentProfile loggedUserId = map SU.fromJust <<< SD.single $ select profilePres
4242
/\ (select (array_agg _feature # as _privileges) # from privileges # wher (_quantity .<=. k ... _current_karma) # orderBy _privileges # limit (Proxy _ 1))
4343
/\ (select (array_agg (l ... _id) # as _languages) # from (((languages # as l) `join` (languages_users # as lu)) # on (l ... _id .=. lu ... _language .&&. lu ... _speaker .=. u ... _id)) # orderBy _languages # limit (Proxy _ 1))
4444
/\ (select (array_agg (l ... _name # orderBy (l ... _id)) # as _tags) # from (((tags # as l) `join` (tags_users # as tu)) # on (l ... _id .=. tu ... _tag .&&. tu ... _creator .=. u ... _id)) # orderBy _tags # limit (Proxy _ 1))
45-
/\ (select (array_agg _kind # as _badges) # from (((badges # as b) `join` (badges_users # as bu)) # on (b ... _id .=. bu ... _badge .&&. bu ... _receiver .=. u ... _id)) # orderBy _badges # limit (Proxy _ 1))
45+
/\ (select (array_agg _description # as _badges) # from (((badges # as b) `join` (badges_users # as bu)) # on (b ... _id .=. bu ... _badge .&&. bu ... _receiver .=. u ... _id)) # orderBy _badges # limit (Proxy _ 1))
4646
/\ (k ... _current_karma # as _karma)
4747
/\ (_position # as _karmaPosition)
4848
source = join (users # as u) (karma_leaderboard # as k) # on (u ... _id .=. k ... _ranker)

src/Shared/Badge.purs

+25-9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Simple.JSON (class ReadForeign, class WriteForeign)
2020
data Badge
2121
= Admin
2222
| Contributor
23+
| Custom String
2324

2425
derive instance Eq Badge
2526

@@ -35,43 +36,53 @@ instance EncodeJson Badge where
3536

3637
instance Bounded Badge where
3738
bottom = Admin
38-
top = Contributor
39+
top = Custom ""
3940

4041
instance BoundedEnum Badge where
4142
cardinality = Cardinality 1
4243

4344
fromEnum = case _ of
4445
Admin0
4546
Contributor100
47+
Custom _ → 200
4648

4749
toEnum = case _ of
4850
0Just Admin
4951
100Just Contributor
52+
200Just $ Custom ""
5053
_ → Nothing
5154

5255
instance Enum Badge where
5356
succ = case _ of
5457
AdminJust Contributor
55-
ContributorNothing
58+
ContributorJust $ Custom ""
59+
Custom _ → Nothing
5660

5761
pred = case _ of
5862
AdminNothing
5963
ContributorJust Admin
64+
Custom _ → Just Contributor
6065

6166
instance Show Badge where
62-
show = DSG.genericShow
67+
show = case _ of
68+
Admin"Admin"
69+
Contributor"Contributor"
70+
Custom c → c
71+
72+
unshow :: String -> Badge
73+
unshow = case _ of
74+
"Admin"Admin
75+
"Contributor"Contributor
76+
c → Custom c
6377

6478
instance ReadForeign Badge where
65-
readImpl f = SU.fromJust <<< DE.toEnum <$> F.readInt f
79+
readImpl f = unshow <$> F.readString f
6680

6781
instance WriteForeign Badge where
68-
writeImpl = F.unsafeToForeign <<< DE.fromEnum
69-
70-
instance ToValue Badge where
71-
toValue = F.unsafeToForeign <<< DE.fromEnum
82+
writeImpl = F.unsafeToForeign <<< show
7283

7384
instance FromValue Badge where
74-
fromValue v = map (SU.fromJust <<< DE.toEnum) (DL.fromValue v Either String Int)
85+
fromValue v = map unshow (DL.fromValue v Either String String)
7586

7687
type BadgeUser =
7788
{ badge Badge
@@ -91,4 +102,9 @@ badgeFor b = case b of
91102
, text: "Contributor"
92103
, description: "This user has contributed to MeroChat"
93104
}
105+
Custom c →
106+
{ badge: b
107+
, text: c
108+
, description: "Special user badge: " <> c
109+
}
94110

0 commit comments

Comments
 (0)