Remove stale nicks that are no longer present on other side after restart
This commit is contained in:
parent
2460ca844a
commit
eac717044d
30
Session.hs
30
Session.hs
|
@ -13,6 +13,17 @@ import qualified Network.Protocol.XMPP as XMPP
|
||||||
import qualified Config
|
import qualified Config
|
||||||
import Util
|
import Util
|
||||||
|
|
||||||
|
cleanOld :: Config.Config -> XMPP.JID -> XMPP.XMPP ()
|
||||||
|
cleanOld config source = do
|
||||||
|
old <- liftIO $ DB.query (Config.db config)
|
||||||
|
(s"SELECT target_muc, target_nick, source_nick FROM sessions WHERE version < ? AND source_muc = ?")
|
||||||
|
(Config.dbVersion config, bareTxt source)
|
||||||
|
forM_ old $ \(muc, nick, source_nick) ->
|
||||||
|
let Just target = XMPP.parseJID $ muc ++ s"/" ++ nick in
|
||||||
|
sendPresence config ((XMPP.emptyPresence XMPP.PresenceUnavailable) {
|
||||||
|
XMPP.presenceFrom = XMPP.parseJID $ bareTxt source ++ s"/" ++ source_nick
|
||||||
|
}) target
|
||||||
|
|
||||||
mkSession :: (MonadIO m) =>
|
mkSession :: (MonadIO m) =>
|
||||||
Config.Config
|
Config.Config
|
||||||
-> XMPP.PresenceType
|
-> XMPP.PresenceType
|
||||||
|
@ -26,13 +37,16 @@ mkSession config typ source target
|
||||||
(sourceMuc, sourceNick, targetMuc, targetNick)
|
(sourceMuc, sourceNick, targetMuc, targetNick)
|
||||||
| otherwise =
|
| otherwise =
|
||||||
liftIO $ DB.execute (Config.db config)
|
liftIO $ DB.execute (Config.db config)
|
||||||
(s"INSERT INTO sessions VALUES (?,?,?,?,?)")
|
(s"INSERT INTO sessions VALUES (?,?,?,?,?) ON CONFLICT(source_muc,source_nick,target_muc,target_nick) DO UPDATE SET version=?")
|
||||||
(sourceMuc, sourceNick, targetMuc, targetNick, Config.dbVersion config)
|
(
|
||||||
|
sourceMuc, sourceNick, targetMuc, targetNick,
|
||||||
|
Config.dbVersion config, Config.dbVersion config
|
||||||
|
)
|
||||||
where
|
where
|
||||||
sourceMuc = bareTxt <$> source
|
sourceMuc = fromMaybe mempty (bareTxt <$> source)
|
||||||
sourceNick = XMPP.strResource <$> (XMPP.jidResource =<< source)
|
sourceNick = fromMaybe mempty (XMPP.strResource <$> (XMPP.jidResource =<< source))
|
||||||
targetMuc = bareTxt target
|
targetMuc = bareTxt target
|
||||||
targetNick = XMPP.strResource <$> XMPP.jidResource target
|
targetNick = fromMaybe mempty (XMPP.strResource <$> XMPP.jidResource target)
|
||||||
|
|
||||||
maybeAddNick :: XMPP.JID -> Text -> XMPP.JID
|
maybeAddNick :: XMPP.JID -> Text -> XMPP.JID
|
||||||
maybeAddNick jid@XMPP.JID { XMPP.jidResource = Just _ } _ = jid
|
maybeAddNick jid@XMPP.JID { XMPP.jidResource = Just _ } _ = jid
|
||||||
|
@ -94,7 +108,8 @@ sendGroupChat config message@XMPP.Message {
|
||||||
XMPP.messagePayloads = payloads
|
XMPP.messagePayloads = payloads
|
||||||
} target = do
|
} target = do
|
||||||
nickSwaps1 <- liftIO $ DB.query (Config.db config) (s"SELECT target_nick, source_nick FROM sessions WHERE source_muc=?") (DB.Only $ bareTxt target)
|
nickSwaps1 <- liftIO $ DB.query (Config.db config) (s"SELECT target_nick, source_nick FROM sessions WHERE source_muc=?") (DB.Only $ bareTxt target)
|
||||||
nickSwaps2 <- liftIO $ DB.query (Config.db config) (s"SELECT source_nick, target_nick FROM sessions WHERE target_muc=? AND source_muc IS NOT NULL") (DB.Only $ bareTxt target)
|
nickSwaps2 <- liftIO $ DB.query (Config.db config) (s"SELECT source_nick, target_nick FROM sessions WHERE target_muc=? AND source_muc <> ''") (DB.Only $ bareTxt target)
|
||||||
|
let nickSwap = replaceWords (nickSwaps1 ++ nickSwaps2)
|
||||||
XMPP.putStanza $ message {
|
XMPP.putStanza $ message {
|
||||||
XMPP.messageFrom = Just (proxyJid config from),
|
XMPP.messageFrom = Just (proxyJid config from),
|
||||||
XMPP.messageTo = Just target,
|
XMPP.messageTo = Just target,
|
||||||
|
@ -103,8 +118,7 @@ sendGroupChat config message@XMPP.Message {
|
||||||
[body] ->
|
[body] ->
|
||||||
body { XML.elementNodes = [
|
body { XML.elementNodes = [
|
||||||
XML.NodeContent $ XML.ContentText $
|
XML.NodeContent $ XML.ContentText $
|
||||||
replaceWords (nickSwaps1 ++ nickSwaps2)
|
nickSwap (mconcat (XML.elementText body))
|
||||||
(mconcat (XML.elementText body))
|
|
||||||
]}
|
]}
|
||||||
_ -> el
|
_ -> el
|
||||||
) payloads
|
) payloads
|
||||||
|
|
|
@ -40,7 +40,8 @@ handlePresence config presence@XMPP.Presence {
|
||||||
| bareTxt to /= bareTxt (Config.bridgeJid config) =
|
| bareTxt to /= bareTxt (Config.bridgeJid config) =
|
||||||
-- This is to one of our ghosts, so just ignore it
|
-- This is to one of our ghosts, so just ignore it
|
||||||
return ()
|
return ()
|
||||||
| hasMucCode 110 presence = return () -- ignore self presence
|
| hasMucCode 110 presence = -- done joining room, clean up old data
|
||||||
|
Session.cleanOld config from
|
||||||
| Just resource <- XMPP.jidResource from,
|
| Just resource <- XMPP.jidResource from,
|
||||||
not (s"[x]" `T.isInfixOf` XMPP.strResource resource) = forM_ (targets config from) $
|
not (s"[x]" `T.isInfixOf` XMPP.strResource resource) = forM_ (targets config from) $
|
||||||
Session.sendPresence config presence
|
Session.sendPresence config presence
|
||||||
|
|
Loading…
Reference in New Issue
Block a user