From f9d8b369931fc0cd139bba7f9af2b08c248eba4c Mon Sep 17 00:00:00 2001 From: Matthieu Lalonde Date: Thu, 28 Jan 2021 18:33:21 +0000 Subject: [PATCH] Adding user privacy mode, closes #21 --- pkg/bot/bot.go | 88 ++++++++++++++++++++++++++++++++++++++-------- pkg/bot/msgs.go | 13 ++----- pkg/utils/utils.go | 10 ++++++ 3 files changed, 85 insertions(+), 26 deletions(-) diff --git a/pkg/bot/bot.go b/pkg/bot/bot.go index 271aa5d..0cd83cb 100644 --- a/pkg/bot/bot.go +++ b/pkg/bot/bot.go @@ -28,6 +28,55 @@ type UserEmoji struct { Name string } +type userPrivacy uint8 + +const ( + userPrivacyNone userPrivacy = 0 + userPrivacyPart userPrivacy = 1 << iota + userPrivacyJoin userPrivacy = 1 << iota + userPrivacyStatus userPrivacy = 1 << iota + userPrivacyAll userPrivacy = 0xFF +) + +func upSet(b, flag userPrivacy) userPrivacy { return b | flag } +func upClear(b, flag userPrivacy) userPrivacy { return b &^ flag } +func upToggle(b, flag userPrivacy) userPrivacy { return b ^ flag } +func upHas(b, flag userPrivacy) bool { return b&flag != 0 } + +func (up userPrivacy) String() string { + switch up { + case userPrivacyPart: + return "part" + case userPrivacyJoin: + return "join" + case userPrivacyStatus: + return "status" + case userPrivacyAll: + return "all" + case userPrivacyNone: + return "none" + } + return "none" +} + +func userPrivacyFromString(up string) userPrivacy { + switch up { + case userPrivacyPart.String(): + return userPrivacyPart + case userPrivacyJoin.String(): + return userPrivacyJoin + case userPrivacyStatus.String(): + return userPrivacyStatus + case userPrivacyAll.String(): + return userPrivacyAll + case userPrivacyNone.String(): + return userPrivacyNone + } + return userPrivacyNone +} + +const userPrivacyDefault = userPrivacyNone + // User is the structure for a single bot user type User struct { AltVRUserID string @@ -36,6 +85,7 @@ type User struct { DiscordEmoji UserEmoji Role Roles MsgMode msgMode + Privacy userPrivacy } // GetDiscordEmoji If it exists, builds a discord emoji for the user @@ -190,15 +240,23 @@ func (b *Type) periodicTicker() { } func (b *Type) userConnected(u altvr.User) { + uu, err := b.getUserByAltVRUserID(u.UserID) + if err != nil { + return + } s := fmt.Sprintf("%s**%s is now online in %s!**", b.getUserEmojiByAltVRUser(u), u.GetDisplayName(), u.CurrentSpace.Name) - if !b.isQuiet { + if !b.isQuiet && !upHas(uu.Privacy, userPrivacyJoin) { b.dg.Session.ChannelMessageSend(b.DGcID, s) } } func (b *Type) userDisconnected(u altvr.User) { + uu, err := b.getUserByAltVRUserID(u.UserID) + if err != nil { + return + } s := fmt.Sprintf("%s_%s is now offline!_", b.getUserEmojiByAltVRUser(u), u.GetDisplayName()) - if !b.isQuiet { + if !b.isQuiet && !upHas(uu.Privacy, userPrivacyPart) { b.dg.Session.ChannelMessageSend(b.DGcID, s) } } @@ -294,34 +352,34 @@ func (b *Type) getUserEmojiByAltVRUser(au altvr.User) string { return "" } -func (b *Type) getUserByAltVRUserID(avrID string) (User, error) { - for _, u := range b.users { +func (b *Type) getUserByAltVRUserID(avrID string) (*User, error) { + for k, u := range b.users { if u.AltVRUserID == avrID { - return u, nil + return &b.users[k], nil } } - return User{}, errors.New("User not found") + return &User{}, errors.New("User not found") } -func (b *Type) getUserByDiscordID(discordID string) (User, error) { - for _, u := range b.users { +func (b *Type) getUserByDiscordID(discordID string) (*User, error) { + for k, u := range b.users { if u.DiscordID == discordID { - return u, nil + return &b.users[k], nil } } - return User{}, errors.New("User not found") + return &User{}, errors.New("User not found") } -func (b *Type) getUserByDiscordName(discordName string) (User, error) { - for _, u := range b.users { +func (b *Type) getUserByDiscordName(discordName string) (*User, error) { + for k, u := range b.users { if u.DiscordName == discordName { - return u, nil + return &b.users[k], nil } } - return User{}, errors.New("User not found") + return &User{}, errors.New("User not found") } -func (b *Type) buildMention(uu User) string { +func (b *Type) buildMention(uu *User) string { var msg string if uu.DiscordEmoji.Name != "" { msg = fmt.Sprintf("<:%s:%s> ", uu.DiscordEmoji.Name, uu.DiscordEmoji.ID) diff --git a/pkg/bot/msgs.go b/pkg/bot/msgs.go index eba7a78..db0a864 100644 --- a/pkg/bot/msgs.go +++ b/pkg/bot/msgs.go @@ -25,15 +25,6 @@ func (mm msgMode) String() string { const msgModeLast = msgModeFlirty -var ( - commandFormats = map[string]string{ - "auser": " [Discord Emoji]", - "aemoji": " ", - "msg": " ...", - "accept": "(As reply) [Discord Emoji]", - "deny": "(As reply)", - "status": "[Discord Mention...]", - } msgStrings = map[msgMode]msgMap{ msgModeNormal: { @@ -102,8 +93,8 @@ func (b *Type) getMessageString(msgID string, params ...interface{}) string { mode := b.msgMode p := params if len(p) >= 1 { - if reflect.TypeOf(p[0]) == reflect.TypeOf(User{}) { - u := p[0].(User) + if reflect.TypeOf(p[0]) == reflect.TypeOf(&User{}) { + u := p[0].(*User) if u.MsgMode != msgModeNone { mode = u.MsgMode } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index dd005e1..77407cd 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -20,3 +20,13 @@ func SliceContainsChannelType(s []discordgo.ChannelType, e discordgo.ChannelType } return false } + +// SliceContainsString looks for int value in a slice +func SliceContainsString(s []string, e string) bool { + for _, a := range s { + if a == e { + return true + } + } + return false +}