summaryrefslogtreecommitdiff
path: root/internal/db
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2023-03-19 13:11:46 +0100
committerLibravatar GitHub <noreply@github.com>2023-03-19 13:11:46 +0100
commit7db81cde444f6bc95e79527af0997de1788d48c7 (patch)
treef6c077ec298a4f018d0870798bc46bd64ba70069 /internal/db
parent[docs] Update docs on how to login (#1626) (diff)
downloadgotosocial-7db81cde444f6bc95e79527af0997de1788d48c7.tar.xz
[feature] Email notifications for new / closed moderation reports (#1628)
* start fiddling about with email sending to allow multiple recipients * do some fiddling * notifs working * notify on closed report * finishing up * envparsing * use strings.ContainsAny
Diffstat (limited to 'internal/db')
-rw-r--r--internal/db/bundb/instance.go31
-rw-r--r--internal/db/bundb/instance_test.go38
-rw-r--r--internal/db/instance.go4
3 files changed, 73 insertions, 0 deletions
diff --git a/internal/db/bundb/instance.go b/internal/db/bundb/instance.go
index b4bdeb1d9..4fa898639 100644
--- a/internal/db/bundb/instance.go
+++ b/internal/db/bundb/instance.go
@@ -156,3 +156,34 @@ func (i *instanceDB) GetInstanceAccounts(ctx context.Context, domain string, max
return accounts, nil
}
+
+func (i *instanceDB) GetInstanceModeratorAddresses(ctx context.Context) ([]string, db.Error) {
+ addresses := []string{}
+
+ // Select email addresses of approved, confirmed,
+ // and enabled moderators or admins.
+
+ q := i.conn.
+ NewSelect().
+ TableExpr("? AS ?", bun.Ident("users"), bun.Ident("user")).
+ Column("user.email").
+ Where("? = ?", bun.Ident("user.approved"), true).
+ Where("? IS NOT NULL", bun.Ident("user.confirmed_at")).
+ Where("? = ?", bun.Ident("user.disabled"), false).
+ WhereGroup(" AND ", func(q *bun.SelectQuery) *bun.SelectQuery {
+ return q.
+ Where("? = ?", bun.Ident("user.moderator"), true).
+ WhereOr("? = ?", bun.Ident("user.admin"), true)
+ }).
+ OrderExpr("? ASC", bun.Ident("user.email"))
+
+ if err := q.Scan(ctx, &addresses); err != nil {
+ return nil, i.conn.ProcessError(err)
+ }
+
+ if len(addresses) == 0 {
+ return nil, db.ErrNoEntries
+ }
+
+ return addresses, nil
+}
diff --git a/internal/db/bundb/instance_test.go b/internal/db/bundb/instance_test.go
index 4269df5ca..580a7699b 100644
--- a/internal/db/bundb/instance_test.go
+++ b/internal/db/bundb/instance_test.go
@@ -24,6 +24,8 @@ import (
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
+ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
+ "github.com/superseriousbusiness/gotosocial/testrig"
)
type InstanceTestSuite struct {
@@ -90,6 +92,42 @@ func (suite *InstanceTestSuite) TestGetInstanceAccounts() {
suite.Len(accounts, 1)
}
+func (suite *InstanceTestSuite) TestGetInstanceModeratorAddressesOK() {
+ // We have one admin user by default.
+ addresses, err := suite.db.GetInstanceModeratorAddresses(context.Background())
+ suite.NoError(err)
+ suite.EqualValues([]string{"admin@example.org"}, addresses)
+}
+
+func (suite *InstanceTestSuite) TestGetInstanceModeratorAddressesZorkAsModerator() {
+ // Promote zork to moderator role.
+ testUser := &gtsmodel.User{}
+ *testUser = *suite.testUsers["local_account_1"]
+ testUser.Moderator = testrig.TrueBool()
+ if err := suite.db.UpdateUser(context.Background(), testUser, "moderator"); err != nil {
+ suite.FailNow(err.Error())
+ }
+
+ addresses, err := suite.db.GetInstanceModeratorAddresses(context.Background())
+ suite.NoError(err)
+ suite.EqualValues([]string{"admin@example.org", "zork@example.org"}, addresses)
+}
+
+func (suite *InstanceTestSuite) TestGetInstanceModeratorAddressesNoAdmin() {
+ // Demote admin from admin + moderator roles.
+ testUser := &gtsmodel.User{}
+ *testUser = *suite.testUsers["admin_account"]
+ testUser.Admin = testrig.FalseBool()
+ testUser.Moderator = testrig.FalseBool()
+ if err := suite.db.UpdateUser(context.Background(), testUser, "admin", "moderator"); err != nil {
+ suite.FailNow(err.Error())
+ }
+
+ addresses, err := suite.db.GetInstanceModeratorAddresses(context.Background())
+ suite.ErrorIs(err, db.ErrNoEntries)
+ suite.Empty(addresses)
+}
+
func TestInstanceTestSuite(t *testing.T) {
suite.Run(t, new(InstanceTestSuite))
}
diff --git a/internal/db/instance.go b/internal/db/instance.go
index dff471193..3166a0a18 100644
--- a/internal/db/instance.go
+++ b/internal/db/instance.go
@@ -42,4 +42,8 @@ type Instance interface {
// GetInstancePeers returns a slice of instances that the host instance knows about.
GetInstancePeers(ctx context.Context, includeSuspended bool) ([]*gtsmodel.Instance, Error)
+
+ // GetInstanceModeratorAddresses returns a slice of email addresses belonging to active
+ // (as in, not suspended) moderators + admins on this instance.
+ GetInstanceModeratorAddresses(ctx context.Context) ([]string, Error)
}