Skip to content

Commit

Permalink
Simplify suggestion indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
easafe committed Feb 11, 2025
1 parent 105e03e commit 838f185
Show file tree
Hide file tree
Showing 23 changed files with 83 additions and 111 deletions.
18 changes: 9 additions & 9 deletions src/Client/Im/Chat.purs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ beforeSendMessage content model = case content of
where
--the user messaged could be in the contacts and suggestions
-- in either case, check if the chat history has not already been fetched
shouldFetchHistory /\ updatedModel = case model.chatting, model.suggesting of
Nothing, (Just index)
shouldFetchHistory /\ updatedModel = case model.chatting of
Nothing
let
user = model.suggestions !@ index
user = model.suggestions !@ model.suggesting
maybeIndex = DA.findIndex ((_ == user.id) <<< _.id <<< _.user) model.contacts
updatedContacts
| DM.isJust maybeIndex = model.contacts
Expand All @@ -122,11 +122,11 @@ beforeSendMessage content model = case content of
Tuple (updatedContacts !@ SU.fromJust updatedChatting).shouldFetchChatHistory model
{ chatting = updatedChatting
, contacts = updatedContacts
, suggestions = SU.fromJust $ DA.deleteAt index model.suggestions
, suggestions = SU.fromJust $ DA.deleteAt model.suggesting model.suggestions
}
_, _Tuple false model
_ → Tuple false model

nextEffects ct = [ fetchHistory, nextSendMessage ct ]
nextEffects ct = [ nextSendMessage ct, fetchHistory ]

fetchHistory = pure <<< Just <<< SpecialRequest $ FetchHistory shouldFetchHistory
nextSendMessage ct = do
Expand Down Expand Up @@ -529,9 +529,9 @@ editMessage message id model =
where
setIt = EC.liftEffect do
input ← chatInput model.chatting
CCD.setValue input (spy "msg" message)
CCD.setValue (spy "inp" input) message

deleteMessage Int WebSocket -> ImModel NoMessages
deleteMessage Int WebSocket ImModel NoMessages
deleteMessage id webSocket model =
model
{ toggleContextMenu = HideContextMenu
Expand All @@ -540,6 +540,6 @@ deleteMessage id webSocket model =
chatting = SU.fromJust model.chatting

deleteIt = do
EC.liftEffect <<< CIW.sendPayload webSocket $ DeletedMessage { id, userId : (model.contacts !@ chatting).user.id }
EC.liftEffect <<< CIW.sendPayload webSocket $ DeletedMessage { id, userId: (model.contacts !@ chatting).user.id }
pure Nothing

14 changes: 6 additions & 8 deletions src/Client/Im/History.purs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ import Shared.Unsafe as SU
import Web.DOM.Element as WDE

checkFetchHistory ImModel MoreMessages
checkFetchHistory model@{ freeToFetchChatHistory }
| freeToFetchChatHistory = model /\ [ Just <<< SpecialRequest <<< FetchHistory <$> getScrollTop ]
checkFetchHistory model =
model /\ if model.freeToFetchChatHistory then [ Just <<< SpecialRequest <<< FetchHistory <$> getScrollTop ] else []

where
getScrollTop = liftEffect do
element ← CCD.unsafeGetElementById MessageHistory
(_ < 1.0) <$> WDE.scrollTop element

| otherwise = F.noMessages model
where
getScrollTop = liftEffect do
element ← CCD.unsafeGetElementById MessageHistory
(_ < 1.0) <$> WDE.scrollTop element

--to avoid issues with older missed unread messages just get the whole chat history on first load
fetchHistory Boolean ImModel MoreMessages
Expand Down
15 changes: 6 additions & 9 deletions src/Client/Im/Suggestion.purs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ nextSuggestion model =
else
model
{ freeToFetchSuggestions = true
, suggesting = Just next
, suggesting = next
, chatting = Nothing
, bugging = Nothing
} /\ [ bugUser model ]
where
next = DM.maybe 0 (_ + 1) model.suggesting
next = model.suggesting + 1

-- | Display previous suggestion card
previousSuggestion ImModel MoreMessages
Expand All @@ -42,12 +42,12 @@ previousSuggestion model =
else
model
{ freeToFetchSuggestions = true
, suggesting = Just previous
, suggesting = previous
, chatting = Nothing
, bugging = Nothing
} /\ [ bugUser model ]
where
previous = DM.maybe 0 (_ - 1) model.suggesting
previous = model.suggesting - 1

-- | When moving suggestion cards, diplay a special card n% of the time
bugUser ImModel Aff (Maybe ImMessage)
Expand Down Expand Up @@ -97,7 +97,7 @@ displayMoreSuggestions suggestions model =
}
where
suggestionsSize = DA.length suggestions
suggesting = Just $ if suggestionsSize <= 1 then 0 else 1
suggesting = if suggestionsSize <= 1 then 0 else 1

lowQualityUsersBin = 5
lowQualityUsersIn = DA.length <<< DA.filter ((_ >= lowQualityUsersBin) <<< _.bin)
Expand Down Expand Up @@ -131,8 +131,5 @@ setBugging ∷ MeroChatCall → ImModel → NoMessages
setBugging mc model = F.noMessages model
{ bugging = Just mc
--offset index to account for non profile suggestion
, suggesting = case model.suggesting of
Just s | s > 0Just $ s - 1
Just s → Just s
NothingNothing
, suggesting = if model.suggesting > 0 then model.suggesting - 1 else model.suggesting
}
2 changes: 1 addition & 1 deletion src/Client/Im/WebSocket/Events.purs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ unsuggest userId model = model
i ← unsuggestedIndex
DA.deleteAt i model.suggestions
updatedSuggesting
| unsuggestedIndex /= Nothing && unsuggestedIndex < model.suggesting = (max 0 <<< (_ - 1)) <$> model.suggesting
| unsuggestedIndex /= Nothing && unsuggestedIndex < Just model.suggesting = max 0 (model.suggesting - 1)
| otherwise = model.suggesting

-- | Updated contacts if user is already there
Expand Down
8 changes: 4 additions & 4 deletions src/Server/Im/Database.purs
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,11 @@ canEditMessage loggedUserId messageId =
# from messages
# wher (_id .=. messageId .&&. _sender .=. loggedUserId)

deleteMessage r. Int Int Int -> BaseEffect { pool Pool | r } Unit
deleteMessage r. Int Int Int BaseEffect { pool Pool | r } Unit
deleteMessage loggedUserId userId messageId = SD.execute $
delete
# from messages
# wher (_id .=. messageId .&&. _sender .=. loggedUserId .&&. _recipient .=. userId)
delete
# from messages
# wher (_id .=. messageId .&&. _sender .=. loggedUserId .&&. _recipient .=. userId)

insertMessage r. Int Int String BaseEffect { pool Pool | r } Int
insertMessage loggedUserId recipient content = SD.withTransaction $ \connection → do
Expand Down
8 changes: 4 additions & 4 deletions src/Server/Im/Template.purs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ import Shared.Resource as SP

template Payload Effect String
template payload = do
let
unreadChats = SIU.countUnreadChats payload.user.id payload.contacts
suggestionsCount = DA.length payload.suggestions
lt ← EN.nowDateTime
F.preMount (QuerySelector $ show SE.Im)
{ view: \model → ST.templateWith $ defaultParameters
Expand All @@ -41,7 +38,7 @@ template payload = do
, temporaryId: 0
, typingIds: []
, modalsLoaded: []
, suggesting: if suggestionsCount == 0 then Nothing else if suggestionsCount == 1 then Just 0 else Just 1
, suggesting: if suggestionsCount == 1 then 0 else 1
, freeToFetchChatHistory: true
, suggestionsPage: 1
, errorMessage: ""
Expand Down Expand Up @@ -80,6 +77,9 @@ template payload = do
}
}
where
unreadChats = SIU.countUnreadChats payload.user.id payload.contacts
suggestionsCount = DA.length payload.suggestions

javascript =
[ HE.script' [ HA.type' "text/javascript", HA.src $ SP.bundlePath Emoji Js ]
, HE.script' [ HA.type' "text/javascript", HA.src $ SP.bundlePath Im Js ]
Expand Down
8 changes: 4 additions & 4 deletions src/Shared/Avatar.purs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ avatarPath index = SP.resourcePath (Left name) Png
avatarForSender Maybe String String
avatarForSender = DM.fromMaybe defaultAvatar

avatarForRecipient Maybe Int Maybe String String
avatarForRecipient index = DM.fromMaybe (avatarPath <<< avatarIndex $ SU.fromJust index)
avatarForRecipient Int Maybe String String
avatarForRecipient index = DM.fromMaybe <<< avatarPath $ avatarIndex index

avatarIndex Int Int
avatarIndex index = mod index differentAvatarImages + 1

avatarColorClass Maybe Int String
avatarColorClass index = className <> show (mod (SU.fromJust index) totalColorClasses + 1)
avatarColorClass Int String
avatarColorClass index = className <> show (mod index totalColorClasses + 1)
where
className = " avatar-color-"
totalColorClasses = 4
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/Experiments/Doppelganger.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import Shared.Experiments.Types (ChatExperimentMessage, ChatExperimentModel)
view ChatExperimentModel Html ChatExperimentMessage
view model = HE.div (HA.class' "word-chain duller")
[ --HE.button (HA.class' "green-button") "Play!"
HE.text "Currently unavailable"
HE.text "Currently unavailable"
]
2 changes: 1 addition & 1 deletion src/Shared/Experiments/Impersonation.purs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ view model = HE.div (HA.class' "impersonation")
profiles s = HE.div (HA.class' { hidden: model.section /= s }) <<< DA.mapWithIndex toProfile
toProfile index p = HE.div [ HA.class' "contact", HA.onClick <<< ConfirmExperiment <<< Just <<< Impersonation $ Just p ]
[ HE.div (HA.class' "avatar-contact-list-div")
[ HE.img [ HA.title $ SU.fromJust p.avatar, HA.class' $ "avatar-contact-list" <> SA.avatarColorClass (Just index), HA.src $ SU.fromJust p.avatar ]
[ HE.img [ HA.title $ SU.fromJust p.avatar, HA.class' $ "avatar-contact-list" <> SA.avatarColorClass index, HA.src $ SU.fromJust p.avatar ]
]
, HE.div [ HA.class' "contact-profile", HA.title $ "Start Impersonation as " <> p.name ]
[ HE.span (HA.class' "contact-name") p.name
Expand Down
8 changes: 4 additions & 4 deletions src/Shared/Experiments/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,22 @@ instance BoundedEnum Experiment where
fromEnum = case _ of
Impersonation _ → 0
WordChain10
Doppelganger -> 20
Doppelganger 20
toEnum = case _ of
0Just (Impersonation Nothing)
10Just WordChain
20 -> Just Doppelganger
20 Just Doppelganger
_ → Nothing

instance Enum Experiment where
succ = case _ of
Impersonation _ → Just WordChain
WordChainJust Doppelganger
Doppelganger -> Nothing
Doppelganger Nothing
pred = case _ of
Impersonation _ → Nothing
WordChainJust (Impersonation Nothing)
Doppelganger -> Just WordChain
Doppelganger Just WordChain

derive instance Generic Experiment _

Expand Down
2 changes: 1 addition & 1 deletion src/Shared/Experiments/View.purs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ extra ∷ ChatExperimentModel → Experiment → Html ChatExperimentMessage
extra model = case _ of
Impersonation ip → SEI.view model
WordChainSEW.view model
Doppelganger -> SED.view model
Doppelganger SED.view model
6 changes: 2 additions & 4 deletions src/Shared/Im/Modals.purs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ modals model@{ erroredFields, toggleModal, chatting } =
where
tutorialSteps = toggleModal == Tutorial ChatSuggestions && DM.isNothing chatting || toggleModal == Tutorial Chatting

showAvatar ImModel Maybe Int Html ImMessage
showAvatar ImModel Int Html ImMessage
showAvatar model index = HE.lazy Nothing largeAvatar who
where
who = case model.chatting of
Just c → map _.user (model.contacts !! c)
Nothingcase model.suggesting of
Just s → model.suggestions !! s
NothingNothing
Nothing → model.suggestions !! model.suggesting
largeAvatar p =
HE.div (HA.class' "confirmation large") case p of
NothingHE.createEmptyElement "div"
Expand Down
4 changes: 2 additions & 2 deletions src/Shared/Im/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ type Im =
--the current logged in user
, userImUser
--indexes
, suggestingMaybe Int
, suggestingInt
, chattingMaybe Int
, smallScreenBoolean
, editingMaybe Int
Expand Down Expand Up @@ -202,7 +202,7 @@ data ShowUserMenuModal
| ShowSettings
| ShowKarmaPrivileges
| ShowHelp
| ShowAvatar (Maybe Int)
| ShowAvatar Int
| ShowBacker
| ShowFeedback
| ShowReport Int
Expand Down
8 changes: 3 additions & 5 deletions src/Shared/Im/View/ChatInput.purs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@ chatBarInput
model@
{ chatting
, contacts
, suggesting
, isWebSocketConnected
, messageEnter
, suggestions
, toggleChatModal
} = HE.fragment
[ emojiModal model
Expand All @@ -118,7 +116,7 @@ chatBarInput
]
, HE.div' [ HA.id $ show ChatInputPreview, HA.class' "chat-input-preview message-content" ]
]
, HE.div [ HA.class' { hidden: not available || toggleChatModal == ShowPreview || DM.isNothing chatting && DM.isNothing suggesting } ]
, HE.div [ HA.class' { hidden: not available || toggleChatModal == ShowPreview || DM.isNothing chatting && DA.null model.suggestions } ]
[ HE.div [ HA.class' "chat-input-options" ]
[ bold
, italic
Expand Down Expand Up @@ -155,7 +153,7 @@ chatBarInput
]
where
available = DM.fromMaybe true $ getContact ((_ /= Unavailable) <<< _.availability <<< _.user)
recipientName = DM.fromMaybe "" $ getContact (_.name <<< _.user) <|> getProperty suggesting suggestions _.name
recipientName = DM.fromMaybe "" $ getContact (_.name <<< _.user) <|> getProperty (Just model.suggesting) model.suggestions _.name

getContact a. (Contact a) Maybe a
getContact = getProperty chatting contacts
Expand Down Expand Up @@ -301,7 +299,7 @@ emojiModal { toggleChatModal } = HE.div [ HA.class' { "emoji-wrapper": true, hid
, HE.div_ $ map (HE.span_ <<< _.s) pairs
]

emojiClickEvent :: (Event -> ImMessage) -> NodeData ImMessage
emojiClickEvent (Event ImMessage) NodeData ImMessage
emojiClickEvent message = HA.createRawEvent "click" handler
where
handler event
Expand Down
5 changes: 2 additions & 3 deletions src/Shared/Im/View/ContactList.purs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ contactList

displayContactListEntry index { history, user, typing } =
let
justIndex = Just index
contact = user
numberUnreadMessages = countUnread history
lastHistoryEntry = SU.fromJust $ DA.last history
isContextMenuVisible = toggleContextMenu == ShowContactContextMenu user.id
avatarClasses
| DM.isNothing contact.avatar = "avatar-contact-list" <> SA.avatarColorClass justIndex
| DM.isNothing contact.avatar = "avatar-contact-list" <> SA.avatarColorClass index
| otherwise = "avatar-contact-list"

in
Expand All @@ -77,7 +76,7 @@ contactList
, HA.onClick $ ResumeChat user.id
]
[ HE.div [ HA.class' "avatar-contact-list-div", HA.title $ if contact.onlineStatus && onlineStatus then show contact.availability else "" ]
[ HE.img [ SA.async, SA.decoding "lazy", HA.class' avatarClasses, HA.src $ SA.avatarForRecipient justIndex contact.avatar ]
[ HE.img [ SA.async, SA.decoding "lazy", HA.class' avatarClasses, HA.src $ SA.avatarForRecipient index contact.avatar ]
, HE.div' [ HA.class' { "online-indicator": true, hidden: contact.availability /= Online || not contact.onlineStatus || not onlineStatus } ]
]
, HE.div [ HA.class' "contact-profile" ]
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/Im/View/NotificationMobile.purs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ unreadNotification { smallScreen, contacts, user: { id } } = HE.div [ HA.onClick
in
if DA.length all > 5 then DA.snoc (DA.take 5 all) $ HE.text "..." else all
unread index { history, user: { avatar } }
| DF.any (\{ status, sender } → status < Read && sender /= id) history = Just $ HE.img [ HA.class' $ "avatar-notification-mobile" <> SA.avatarColorClass (Just index), HA.src $ SA.avatarForRecipient (Just index) avatar ]
| DF.any (\{ status, sender } → status < Read && sender /= id) history = Just $ HE.img [ HA.class' $ "avatar-notification-mobile" <> SA.avatarColorClass index, HA.src $ SA.avatarForRecipient index avatar ]
| otherwise = Nothing
17 changes: 4 additions & 13 deletions src/Shared/Im/View/SuggestionCall.purs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,13 @@ suggestionCall { contacts, suggesting, chatting, suggestions, toggleModal }
Just { avatar, name } | not $ DA.null contacts → HE.div (HA.class' "side-suggestions-container")
[ HE.div [ HA.class' "side-suggestion" ]
[ HE.div [ HA.class' "avatar-contact-list-div faded", HA.onClick $ SpecialRequest PreviousSuggestion, HA.title "Move to this chat suggestion" ]
[ let
previousIndex = map (_ - 1) suggesting
in
SA.avatar [ HA.class' $ "avatar-contact-list" <> SA.avatarColorClass previousIndex, HA.src $ SA.avatarForRecipient previousIndex $ getAvatar previousIndex ]
[ SA.avatar [ HA.class' $ "avatar-contact-list" <> SA.avatarColorClass (suggesting - 1), HA.src $ SA.avatarForRecipient (suggesting - 1) $ getAvatar (suggesting - 1) ]
]
, HE.div [ HA.class' "avatar-contact-list-div margin-less-z", HA.onClick FocusCurrentSuggestion, HA.title "Move to this chat suggestion" ]
[ SA.avatar [ HA.class' $ avatarClasses avatar, HA.src $ SA.avatarForRecipient suggesting avatar ]
]
, HE.div [ HA.class' "avatar-contact-list-div margin-less faded", HA.onClick $ SpecialRequest NextSuggestion, HA.title "Move to this chat suggestion" ]
[ let
nextIndex = map (_ + 1) suggesting
in
SA.avatar [ HA.class' $ "avatar-contact-list" <> SA.avatarColorClass nextIndex, HA.src $ SA.avatarForRecipient nextIndex $ getAvatar nextIndex ]
[ SA.avatar [ HA.class' $ "avatar-contact-list" <> SA.avatarColorClass (suggesting + 1), HA.src $ SA.avatarForRecipient (suggesting + 1) $ getAvatar (suggesting + 1) ]
]
, HE.div [ HA.class' "contact-profile", HA.title "Your chat suggestions" ]
[ HE.span (HA.class' "contact-name") name
Expand All @@ -49,13 +43,10 @@ suggestionCall { contacts, suggesting, chatting, suggestions, toggleModal }
_ → HE.div' (HA.class' "side-suggestions-container")

where
suggs = do
index ← suggesting
suggestions !! index
suggs = suggestions !! suggesting

getAvatar index = do
i ← index
user ← suggestions !! i
user ← suggestions !! index
user.avatar

avatarClasses avatar
Expand Down
Loading

0 comments on commit 838f185

Please sign in to comment.