Files
zeekay 8feb24ea1f One name for this fork, and it is ours
The server module named github.com/hanzoai/team/server/v8 — a path that
resolves to the retired workspace's repository, not this one — while
server/public still published github.com/mattermost/mattermost/server/public.
Both are github.com/hanzoteam/server/... now, which is where this code
actually lives.

That second half is what made the binary unbootable. Upstream's public path
stayed claimed by upstream, mattermost-plugin-ai imported it, and the build
linked public/model twice; public/plugin's gob.Register then ran twice under
one name and panicked at init, before main. Reverting the rename traded the
brand for a working binary. Rebranding the plugin alongside it needs no such
trade: the agents fork is github.com/hanzoteam/agents/v2 against our public
module, so no module named mattermost/mattermost is in the graph at all.

Measured: `go list -deps ./cmd/mattermost` names exactly one public/model, and
it is ours.
2026-08-28 23:35:36 -07:00

253 lines
8.1 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package resend_invitation_email
import (
"encoding/json"
"net/http"
"os"
"strconv"
"github.com/hanzoteam/server/server/public/model"
"github.com/hanzoteam/server/server/public/shared/configservice"
"github.com/hanzoteam/server/server/public/shared/mlog"
"github.com/hanzoteam/server/server/public/shared/request"
"github.com/hanzoteam/server/server/v8/channels/jobs"
"github.com/hanzoteam/server/server/v8/channels/store"
)
const FourtyEightHoursInMillis int64 = 172800000
type AppIface interface {
configservice.ConfigService
GetUserByEmail(email string) (*model.User, *model.AppError)
GetTeamMembersByIds(teamID string, userIDs []string, restrictions *model.ViewUsersRestrictions) ([]*model.TeamMember, *model.AppError)
InviteNewUsersToTeamGracefully(rctx request.CTX, memberInvite *model.MemberInvite, teamID, senderId string, reminderInterval string) ([]*model.EmailInviteWithError, *model.AppError)
}
type ResendInvitationEmailWorker struct {
name string
stop chan bool
stopped chan bool
jobs chan model.Job
jobServer *jobs.JobServer
logger mlog.LoggerIFace
app AppIface
store store.Store
}
func MakeWorker(jobServer *jobs.JobServer, app AppIface, store store.Store) *ResendInvitationEmailWorker {
const workerName = "ResendInvitationEmail"
worker := ResendInvitationEmailWorker{
name: workerName,
stop: make(chan bool, 1),
stopped: make(chan bool, 1),
jobs: make(chan model.Job),
jobServer: jobServer,
logger: jobServer.Logger().With(mlog.String("worker_name", workerName)),
app: app,
store: store,
}
return &worker
}
func (rseworker *ResendInvitationEmailWorker) Run() {
rseworker.logger.Debug("Worker started")
defer func() {
rseworker.logger.Debug("Worker finished")
rseworker.stopped <- true
}()
for {
select {
case <-rseworker.stop:
rseworker.logger.Debug("Worker received stop signal")
return
case job := <-rseworker.jobs:
rseworker.DoJob(&job)
}
}
}
func (rseworker *ResendInvitationEmailWorker) IsEnabled(cfg *model.Config) bool {
return *cfg.ServiceSettings.EnableEmailInvitations
}
func (rseworker *ResendInvitationEmailWorker) Stop() {
rseworker.logger.Debug("Worker stopping")
rseworker.stop <- true
<-rseworker.stopped
}
func (rseworker *ResendInvitationEmailWorker) JobChannel() chan<- model.Job {
return rseworker.jobs
}
func (rseworker *ResendInvitationEmailWorker) DoJob(job *model.Job) {
logger := rseworker.logger.With(jobs.JobLoggerFields(job)...)
logger.Debug("Worker: Received a new candidate job.")
defer rseworker.jobServer.HandleJobPanic(logger, job)
elapsedTimeSinceSchedule, DurationInMillis := rseworker.GetDurations(job)
if elapsedTimeSinceSchedule > DurationInMillis {
if appErr := rseworker.ResendEmails(logger, job, "48"); appErr != nil {
rseworker.setJobError(logger, job, appErr)
return
}
rseworker.TearDown(logger, job)
}
}
func (rseworker *ResendInvitationEmailWorker) setJobSuccess(logger mlog.LoggerIFace, job *model.Job) {
if err := rseworker.jobServer.SetJobSuccess(job); err != nil {
logger.Error("Worker: Failed to set success for job", mlog.Err(err))
rseworker.setJobError(logger, job, err)
}
}
func (rseworker *ResendInvitationEmailWorker) setJobError(logger mlog.LoggerIFace, job *model.Job, appError *model.AppError) {
if err := rseworker.jobServer.SetJobError(job, appError); err != nil {
logger.Error("Worker: Failed to set job error", mlog.Err(err))
}
}
func (rseworker *ResendInvitationEmailWorker) cleanEmailData(emailStringData string) ([]string, error) {
// emailStringData looks like this ["[email protected]","[email protected]"]
emails := []string{}
err := json.Unmarshal([]byte(emailStringData), &emails)
if err != nil {
return nil, err
}
return emails, nil
}
func (rseworker *ResendInvitationEmailWorker) cleanChannelsData(channelStringData string) ([]string, error) {
// channelStringData looks like this ["uuuiiiiidddd","uuuiiiiidddd"]
channels := []string{}
err := json.Unmarshal([]byte(channelStringData), &channels)
if err != nil {
return nil, err
}
return channels, nil
}
func (rseworker *ResendInvitationEmailWorker) removeAlreadyJoined(teamID string, emailList []string) []string {
var notJoinedYet []string
for _, email := range emailList {
// check if the user with this email is on the system already
user, appErr := rseworker.app.GetUserByEmail(email)
if appErr != nil {
notJoinedYet = append(notJoinedYet, email)
continue
}
// now we check if they are part of the team already
userID := []string{user.Id}
members, appErr := rseworker.app.GetTeamMembersByIds(teamID, userID, nil)
if len(members) == 0 || appErr != nil {
notJoinedYet = append(notJoinedYet, email)
}
}
return notJoinedYet
}
func (rseworker *ResendInvitationEmailWorker) GetDurations(job *model.Job) (int64, int64) {
scheduledAt, _ := strconv.ParseInt(job.Data["scheduledAt"], 10, 64)
now := model.GetMillis()
elapsedTimeSinceSchedule := now - scheduledAt
duration := os.Getenv("MM_RESEND_INVITATION_EMAIL_JOB_DURATION")
DurationInMillis, parseError := strconv.ParseInt(duration, 10, 64)
if parseError != nil {
// default to 48 hours
DurationInMillis = FourtyEightHoursInMillis
}
return elapsedTimeSinceSchedule, DurationInMillis
}
func (rseworker *ResendInvitationEmailWorker) TearDown(logger mlog.LoggerIFace, job *model.Job) {
if _, err := rseworker.store.System().PermanentDeleteByName(job.Id); err != nil {
logger.Error("Worker: Failed to tear down data", mlog.Err(err))
}
rseworker.setJobSuccess(logger, job)
}
func (rseworker *ResendInvitationEmailWorker) ResendEmails(logger mlog.LoggerIFace, job *model.Job, interval string) *model.AppError {
rctx := request.EmptyContext(logger)
teamID := job.Data["teamID"]
emailListData := job.Data["emailList"]
emailList, err := rseworker.cleanEmailData(emailListData)
if err != nil {
appErr := model.NewAppError("worker: "+rseworker.name, "job_id: "+job.Id, nil, "", http.StatusInternalServerError).Wrap(err)
logger.Error("Worker: Failed to clean emails string data", mlog.Err(appErr))
return appErr
}
var channelList []string
if channelListData := job.Data["channelList"]; channelListData != "" {
channelList, err = rseworker.cleanChannelsData(channelListData)
if err != nil {
appErr := model.NewAppError("worker: "+rseworker.name, "job_id: "+job.Id, nil, "", http.StatusInternalServerError).Wrap(err)
logger.Error("Worker: Failed to clean channel string data", mlog.Err(appErr))
return appErr
}
}
emailList = rseworker.removeAlreadyJoined(teamID, emailList)
if len(emailList) == 0 {
return nil
}
memberInvite := model.MemberInvite{
Emails: emailList,
}
if len(channelList) > 0 {
memberInvite.ChannelIds = channelList
}
if profileListData := job.Data["profilesList"]; profileListData != "" {
var profiles []*model.MemberInviteProfile
if err := json.Unmarshal([]byte(profileListData), &profiles); err != nil {
appErr := model.NewAppError("worker: "+rseworker.name, "job_id: "+job.Id, nil, "", http.StatusInternalServerError).Wrap(err)
logger.Error("Worker: Failed to clean profiles string data", mlog.Err(appErr))
return appErr
}
remainingEmails := make(map[string]struct{}, len(emailList))
for _, email := range emailList {
remainingEmails[model.NormalizeEmail(email)] = struct{}{}
}
for _, profile := range profiles {
if profile == nil {
memberInvite.Profiles = append(memberInvite.Profiles, profile)
continue
}
if _, ok := remainingEmails[model.NormalizeEmail(profile.Email)]; ok {
memberInvite.Profiles = append(memberInvite.Profiles, profile)
}
}
}
invitesWithErrors, appErr := rseworker.app.InviteNewUsersToTeamGracefully(rctx, &memberInvite, teamID, job.Data["senderID"], interval)
if appErr != nil {
logger.Error("Worker: Failed to send emails", mlog.Err(appErr))
return appErr
}
for _, invite := range invitesWithErrors {
if invite != nil && invite.Error != nil {
logger.Error("Worker: Failed to resend invitation", mlog.String("email", invite.Email), mlog.Err(invite.Error))
return invite.Error
}
}
return nil
}