dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit 641dff16e76e2e548b0382f0364a36427adb0eae
parent e3079abcd43c156d9ab13b9799810871fdd0e3c2
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Tue, 15 Nov 2022 17:05:23 -0500

do not send inboxes to people who ignore/blacklist you

Diffstat:
Mpkg/database/tableIgnoredUsers.go | 6++++++
Mpkg/database/tablePmBlacklistedUsers.go | 6++++++
Apkg/migrations/113.sql | 5+++++
Mpkg/web/handlers/api/v1/msgInterceptor.go | 24++++++++++++++++++++++++
4 files changed, 41 insertions(+), 0 deletions(-)

diff --git a/pkg/database/tableIgnoredUsers.go b/pkg/database/tableIgnoredUsers.go @@ -19,6 +19,12 @@ func GetIgnoredUsers(userID int64) (out []IgnoredUser, err error) { return } +// GetIgnoredByUsers get a list of people who ignore userID +func GetIgnoredByUsers(userID int64) (out []IgnoredUser, err error) { + err = DB.Where("ignored_user_id = ?", userID).Find(&out).Error + return +} + func IgnoreUser(userID, ignoredUserID int64) { ignore := IgnoredUser{UserID: userID, IgnoredUserID: ignoredUserID} if err := DB.Create(&ignore).Error; err != nil { diff --git a/pkg/database/tablePmBlacklistedUsers.go b/pkg/database/tablePmBlacklistedUsers.go @@ -23,6 +23,12 @@ func GetPmBlacklistedUsers(userID int64) (out []PmBlacklistedUsers, err error) { return } +// GetPmBlacklistedByUsers returns a list of users that are blacklisting userID +func GetPmBlacklistedByUsers(userID int64) (out []PmBlacklistedUsers, err error) { + err = DB.Where("blacklisted_user_id = ?", userID).Find(&out).Error + return +} + // ToggleBlacklistedUser returns true if the user was added to the blacklist func ToggleBlacklistedUser(userID, blacklistedUserID int64) bool { if IsUserPmBlacklisted(blacklistedUserID, userID) { diff --git a/pkg/migrations/113.sql b/pkg/migrations/113.sql @@ -0,0 +1,5 @@ +-- +migrate Up +CREATE INDEX ignored_users_ignored_user_id_idx ON ignored_users (ignored_user_id); +CREATE INDEX pm_blacklisted_users_blacklisted_user_id_idx ON pm_blacklisted_users (blacklisted_user_id); + +-- +migrate Down diff --git a/pkg/web/handlers/api/v1/msgInterceptor.go b/pkg/web/handlers/api/v1/msgInterceptor.go @@ -93,9 +93,33 @@ func sendInboxes(room database.ChatRoom, authUser, toUser *database.User, msgID if authUser.IsHellbanned { return } + // Early return if we don't need to send inboxes + if toUser == nil && len(taggedUsersIDsMap) == 0 { + return + } + + blacklistedBy, _ := database.GetPmBlacklistedByUsers(authUser.ID) + blacklistedByMap := make(map[int64]struct{}) + for _, b := range blacklistedBy { + blacklistedByMap[b.UserID] = struct{}{} + } + + ignoredBy, _ := database.GetIgnoredByUsers(authUser.ID) + ignoredByMap := make(map[int64]struct{}) + for _, b := range ignoredBy { + ignoredByMap[b.UserID] = struct{}{} + } sendInbox := func(user database.User, isPM, modCh bool) { if !managers.ActiveUsers.IsUserActiveInRoom(user.ID, room) || user.AFK { + // Do not send notification if receiver is blacklisting you + if _, ok := blacklistedByMap[user.ID]; ok { + return + } + // Do not send notification if receiver is ignoring you + if _, ok := ignoredByMap[user.ID]; ok { + return + } database.CreateInboxMessage(html, room.ID, authUser.ID, user.ID, isPM, modCh, &msgID) } }