Skip to content

Commit

Permalink
use EmailLoopAdminsOnUserLeft in user purge as well
Browse files Browse the repository at this point in the history
  • Loading branch information
lil5 committed Feb 29, 2024
1 parent 3dd3df7 commit cff5ce6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
7 changes: 3 additions & 4 deletions server/internal/controllers/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/the-clothing-loop/website/server/internal/services"
"github.com/the-clothing-loop/website/server/internal/views"
"github.com/the-clothing-loop/website/server/pkg/tsp"
"gopkg.in/guregu/null.v3/zero"

"github.com/gin-gonic/gin"
uuid "github.com/satori/go.uuid"
Expand Down Expand Up @@ -549,12 +548,12 @@ func ChainRemoveUser(c *gin.Context) {
chain.ClearAllLastNotifiedIsUnapprovedAt(db)

// if the user is removed by an admin, do not send an email to this one
var excludedEmail zero.String
var excludedEmail string
if _, isChainAdmin := authUser.IsPartOfChain(chain.UID); isChainAdmin {
excludedEmail = authUser.Email
excludedEmail = authUser.Email.String
}
// send email to chain admins
services.EmailLoopAdminsOnUserLeft(db, user, chain.ID, excludedEmail)
services.EmailLoopAdminsOnUserLeft(db, user.Name, user.Email.String, excludedEmail, chain.ID)
}

func ChainApproveUser(c *gin.Context) {
Expand Down
16 changes: 15 additions & 1 deletion server/internal/controllers/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/the-clothing-loop/website/server/internal/app/auth"
"github.com/the-clothing-loop/website/server/internal/app/goscope"
"github.com/the-clothing-loop/website/server/internal/models"
"github.com/the-clothing-loop/website/server/internal/services"
"github.com/the-clothing-loop/website/server/internal/views"
"github.com/the-clothing-loop/website/server/pkg/noderoute"
"gopkg.in/guregu/null.v3"
Expand Down Expand Up @@ -344,6 +345,19 @@ func UserPurge(c *gin.Context) {
}
}

err := user.AddUserChainsToObject(db)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}

// notify connected hosts, send email to chain admins
chainIDs := []uint{}
for _, uc := range user.Chains {
chainIDs = append(chainIDs, uc.ChainID)
}
services.EmailLoopAdminsOnUserLeft(db, user.Name, user.Email.String, "", chainIDs...)

// find chains where user is the last chain admin
chainIDsToDelete := []uint{}
db.Raw(`
Expand All @@ -360,7 +374,7 @@ HAVING COUNT(uc.id) = 1

tx := db.Begin()

err := user.DeleteUserChainDependenciesAllChains(tx)
err = user.DeleteUserChainDependenciesAllChains(tx)
if err != nil {
tx.Rollback()
goscope.Log.Errorf("UserPurge: %v", err)
Expand Down
10 changes: 5 additions & 5 deletions server/internal/services/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/the-clothing-loop/website/server/internal/app/goscope"
"github.com/the-clothing-loop/website/server/internal/models"
"github.com/the-clothing-loop/website/server/internal/views"
"gopkg.in/guregu/null.v3/zero"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -46,9 +45,10 @@ func EmailLoopAdminsOnUserJoin(db *gorm.DB, user *models.User, chainIDs ...uint)
return nil
}

func EmailLoopAdminsOnUserLeft(db *gorm.DB, user *models.User, chainIDs uint, excludedEmail zero.String) error {
// excludedEmail can be an empty string
func EmailLoopAdminsOnUserLeft(db *gorm.DB, removedUserName, removedUserEmail, excludedEmail string, chainIDs ...uint) error {
// find admin users related to the chain to email
admins, err := models.UserGetAdminsByChain(db, chainIDs)
admins, err := models.UserGetAdminsByChain(db, chainIDs...)
if err != nil {
return err
}
Expand All @@ -60,14 +60,14 @@ func EmailLoopAdminsOnUserLeft(db *gorm.DB, user *models.User, chainIDs uint, ex

for _, admin := range admins {
email := admin.Email
if !email.Valid || email.String == excludedEmail.String || email.String == user.Email.String {
if !email.Valid || excludedEmail == email.String || removedUserEmail == email.String {
continue
}
views.EmailSomeoneLeftLoop(db, admin.I18n,
admin.Name,
admin.Email.String,
admin.ChainName,
user.Name,
removedUserName,
)
}

Expand Down

0 comments on commit cff5ce6

Please sign in to comment.