Skip to content

Commit a2f2265

Browse files
authored
Merge pull request #816 from tsoding/611
(#611) Keep track of how many times a video was suggested
2 parents 8a1e4dd + 1431f84 commit a2f2265

File tree

5 files changed

+59
-7
lines changed

5 files changed

+59
-7
lines changed

src/Bot.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,11 @@ builtinCommands =
505505
( "Asciify Twitch, BTTV or FFZ emote"
506506
, $githubLinkLocationStr
507507
, nonEmptyRoles asciifyReaction))
508+
, ( "fridaycount"
509+
, mkBuiltinCommand
510+
( "Check how many times a particular video was submitted"
511+
, $githubLinkLocationStr
512+
, nonEmptyRoles fridayCountCommand))
508513
]
509514

510515
nextStreamCommand :: Reaction Message a

src/Bot/Friday.hs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module Bot.Friday
1212
, videoQueueCommand
1313
, startRefreshFridayGistTimer
1414
, ytLinkId
15+
, fridayCountCommand
1516
) where
1617

1718
import Bot.GitHub
@@ -98,15 +99,16 @@ instance IsEntity FridayState where
9899
containsYtLink :: T.Text -> Bool
99100
containsYtLink = isJust . ytLinkId
100101

102+
ytLinkRegexp :: T.Text
103+
ytLinkRegexp =
104+
"https?:\\/\\/(www\\.)?youtu(be\\.com\\/watch\\?v=|\\.be\\/)([a-zA-Z0-9_-]+)"
105+
101106
ytLinkId :: T.Text -> Maybe T.Text
102107
ytLinkId text =
103108
(\case
104109
[_, _, ytId] -> return ytId
105110
_ -> Nothing) =<<
106-
rightToMaybe
107-
(regexParseArgs
108-
"https?:\\/\\/(www\\.)?youtu(be\\.com\\/watch\\?v=|\\.be\\/)([a-zA-Z0-9_-]+)"
109-
text)
111+
rightToMaybe (regexParseArgs ytLinkRegexp text)
110112

111113
fridayCommand :: Reaction Message T.Text
112114
fridayCommand =
@@ -116,13 +118,31 @@ fridayCommand =
116118
liftR
117119
(\msg -> do
118120
markGistUnfresh
119-
createEntity Proxy .
121+
void $
122+
createEntity Proxy .
120123
FridayVideo
121124
(messageContent msg)
122125
(senderName $ messageSender msg)
123126
Nothing =<<
124-
now) $
125-
cmapR (const "Added to the suggestions") $ Reaction replyMessage
127+
now
128+
return $ messageContent msg) $
129+
cmapR ytLinkId $
130+
maybeReaction
131+
(Reaction $
132+
const $
133+
logMsg
134+
[qms|Could not check friday video
135+
duplicates because there is no YouTube
136+
id in the message.|]) $
137+
liftR
138+
(\ytId ->
139+
selectEntities (Proxy :: Proxy FridayVideo) $
140+
Filter (PropertyTextLike "name" ("%" <> ytId <> "%")) All) $
141+
cmapR
142+
(\dups ->
143+
[qms|Added to the suggested video.
144+
This video was suggested {length dups} times|]) $
145+
Reaction replyMessage
126146

127147
unwatchedVideos :: Effect [Entity FridayVideo]
128148
unwatchedVideos =
@@ -302,3 +322,14 @@ videoQueueCommand =
302322
liftR updateEntityById $
303323
cmapR (const "Freshness invalidated 👌") $ Reaction replyMessage)
304324
]
325+
326+
fridayCountCommand :: Reaction Message T.Text
327+
fridayCountCommand =
328+
cmapR ytLinkId $
329+
replyOnNothing "Please submit a YouTube link" $
330+
liftR
331+
(\ytId ->
332+
selectEntities (Proxy :: Proxy FridayVideo) $
333+
Filter (PropertyTextLike "name" ("%" <> ytId <> "%")) All) $
334+
cmapR (\dups -> [qms|This video was suggested {length dups} times|]) $
335+
Reaction replyMessage

src/Effect.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ data Condition
4848
| PropertyGreater T.Text
4949
Property
5050
| PropertyMissing T.Text
51+
| PropertyTextLike T.Text
52+
T.Text
5153
| ConditionAnd [Condition]
5254
deriving (Show)
5355

src/Sqlite/Compiler.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ compileCondition _ (E.ConditionAnd _) = undefined
4545
compileCondition _ (E.PropertyGreater _ _) = undefined
4646
-- TODO(#756): E.PropertyMissing is not interpreted in Sqlite.Compiler.compileCondition
4747
compileCondition _ (E.PropertyMissing _) = undefined
48+
compileCondition _ (E.PropertyTextLike _ _) = undefined
4849

4950
-- TODO(#253): compileCteChain doesn't optimize common patterns like Sqlite.EntityPersistence.selectEntityIds
5051
compileCteChain :: T.Text -> E.Selector -> (Int, NamedQuery)

src/Sqlite/EntityPersistence.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,19 @@ selectEntityIds conn name (SortBy propertyName1 Asc (Filter (PropertyGreater pro
427427
]
428428
where
429429
propertyName = propertyName1
430+
selectEntityIds conn name (Filter (PropertyTextLike propertyName propertyPattern) All) =
431+
map fromOnly <$>
432+
queryNamed
433+
conn
434+
[r|select entityId from EntityProperty
435+
where entityName = :entityName and
436+
propertyName = :propertyName and
437+
propertyText like :propertyPattern
438+
group by entityId; |]
439+
[ ":entityName" := name
440+
, ":propertyName" := propertyName
441+
, ":propertyPattern" := propertyPattern
442+
]
430443
selectEntityIds _ _ selector =
431444
error ("Unsupported selector combination " ++ show selector)
432445

0 commit comments

Comments
 (0)