Add toggle for desktop notifications

This commit is contained in:
Tulir Asokan 2020-04-16 19:42:55 +03:00
parent 815190be14
commit bc80ff3a56
3 changed files with 62 additions and 36 deletions

View File

@ -49,6 +49,7 @@ type UserPreferences struct {
DisableMarkdown bool `yaml:"disable_markdown"` DisableMarkdown bool `yaml:"disable_markdown"`
DisableHTML bool `yaml:"disable_html"` DisableHTML bool `yaml:"disable_html"`
DisableDownloads bool `yaml:"disable_downloads"` DisableDownloads bool `yaml:"disable_downloads"`
DisableNotifications bool `yaml:"disable_notifications"`
} }
// Config contains the main config of gomuks. // Config contains the main config of gomuks.

View File

@ -559,51 +559,76 @@ func cmdSetState(cmd *Command) {
} }
} }
type ToggleMessage interface {
Format(state bool) string
}
type HideMessage string
func (hm HideMessage) Format(state bool) string {
if state {
return string(hm) + " is now hidden"
} else {
return string(hm) + " is now visible"
}
}
type SimpleToggleMessage string
func (stm SimpleToggleMessage) Format(state bool) string {
if state {
return "Disabled " + string(stm)
} else {
return "Enabled " + string(stm)
}
}
var toggleMsg = map[string]ToggleMessage{
"rooms": HideMessage("room list sidebar"),
"users": HideMessage("user list sidebar"),
"baremessages": SimpleToggleMessage("bare message view"),
"images": SimpleToggleMessage("image rendering"),
"typingnotif": SimpleToggleMessage("typing notifications"),
"emojis": SimpleToggleMessage("emoji shortcode conversion"),
"html": SimpleToggleMessage("HTML input"),
"markdown": SimpleToggleMessage("markdown input"),
"downloads": SimpleToggleMessage("automatic downloads"),
"notifications": SimpleToggleMessage("desktop notifications"),
}
func cmdToggle(cmd *Command) { func cmdToggle(cmd *Command) {
if len(cmd.Args) == 0 { if len(cmd.Args) == 0 {
cmd.Reply("Usage: /toggle <rooms/users/baremessages/images/typingnotif/emojis>") cmd.Reply("Usage: /toggle <rooms/users/baremessages/images/typingnotif/emojis>")
return return
} }
var val *bool
switch cmd.Args[0] { switch cmd.Args[0] {
case "rooms": case "rooms":
cmd.Config.Preferences.HideRoomList = !cmd.Config.Preferences.HideRoomList val = &cmd.Config.Preferences.HideRoomList
case "users": case "users":
cmd.Config.Preferences.HideUserList = !cmd.Config.Preferences.HideUserList val = &cmd.Config.Preferences.HideUserList
case "baremessages": case "baremessages":
cmd.Config.Preferences.BareMessageView = !cmd.Config.Preferences.BareMessageView val = &cmd.Config.Preferences.BareMessageView
case "images": case "images":
cmd.Config.Preferences.DisableImages = !cmd.Config.Preferences.DisableImages val = &cmd.Config.Preferences.DisableImages
case "typingnotif": case "typingnotif":
cmd.Config.Preferences.DisableTypingNotifs = !cmd.Config.Preferences.DisableTypingNotifs val = &cmd.Config.Preferences.DisableTypingNotifs
case "emojis": case "emojis":
cmd.Config.Preferences.DisableEmojis = !cmd.Config.Preferences.DisableEmojis val = &cmd.Config.Preferences.DisableEmojis
case "html": case "html":
cmd.Config.Preferences.DisableHTML = !cmd.Config.Preferences.DisableHTML val = &cmd.Config.Preferences.DisableHTML
if cmd.Config.Preferences.DisableHTML {
cmd.Reply("Disabled HTML input")
} else {
cmd.Reply("Enabled HTML input")
}
case "markdown": case "markdown":
cmd.Config.Preferences.DisableMarkdown = !cmd.Config.Preferences.DisableMarkdown val = &cmd.Config.Preferences.DisableMarkdown
if cmd.Config.Preferences.DisableMarkdown {
cmd.Reply("Disabled Markdown input")
} else {
cmd.Reply("Enabled Markdown input")
}
case "downloads": case "downloads":
cmd.Config.Preferences.DisableDownloads = !cmd.Config.Preferences.DisableDownloads val = &cmd.Config.Preferences.DisableDownloads
if cmd.Config.Preferences.DisableDownloads { case "notifications":
cmd.Reply("Disabled Downloads input") val = &cmd.Config.Preferences.DisableNotifications
} else {
cmd.Reply("Enabled Downloads input")
}
default: default:
cmd.Reply("Usage: /toggle <rooms/users/baremessages/images/typingnotif/emojis>") cmd.Reply("Usage: /toggle <rooms/users/baremessages/images/typingnotif/emojis>")
return return
} }
// is there a reason this is called twice? *val = !(*val)
// cmd.UI.Render() cmd.Reply(toggleMsg[cmd.Args[0]].Format(*val))
cmd.UI.Render() cmd.UI.Render()
go cmd.Matrix.SendPreferencesToMatrix() go cmd.Matrix.SendPreferencesToMatrix()
} }

View File

@ -426,7 +426,7 @@ func (view *MainView) NotifyMessage(room *rooms.Room, message ifc.Message, shoul
view.matrix.MarkRead(room.ID, message.ID()) view.matrix.MarkRead(room.ID, message.ID())
} }
if shouldNotify && !recentlyFocused { if shouldNotify && !recentlyFocused && !view.config.Preferences.DisableNotifications {
// Push rules say notify and the terminal is not focused, send desktop notification. // Push rules say notify and the terminal is not focused, send desktop notification.
shouldPlaySound := should.PlaySound && shouldPlaySound := should.PlaySound &&
should.SoundName == "default" && should.SoundName == "default" &&