Fix "only mail on mention" bug (#12775)
* fix mail mention bug fix #12774 Signed-off-by: a1012112796 <1012112796@qq.com> * fix test Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
		
							parent
							
								
									0cd49aaebd
								
							
						
					
					
						commit
						ffa12bdb71
					
				
					 4 changed files with 30 additions and 3 deletions
				
			
		|  | @ -1419,11 +1419,21 @@ func getUserEmailsByNames(e Engine, names []string) []string { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // GetMaileableUsersByIDs gets users from ids, but only if they can receive mails
 | // GetMaileableUsersByIDs gets users from ids, but only if they can receive mails
 | ||||||
| func GetMaileableUsersByIDs(ids []int64) ([]*User, error) { | func GetMaileableUsersByIDs(ids []int64, isMention bool) ([]*User, error) { | ||||||
| 	if len(ids) == 0 { | 	if len(ids) == 0 { | ||||||
| 		return nil, nil | 		return nil, nil | ||||||
| 	} | 	} | ||||||
| 	ous := make([]*User, 0, len(ids)) | 	ous := make([]*User, 0, len(ids)) | ||||||
|  | 
 | ||||||
|  | 	if isMention { | ||||||
|  | 		return ous, x.In("id", ids). | ||||||
|  | 			Where("`type` = ?", UserTypeIndividual). | ||||||
|  | 			And("`prohibit_login` = ?", false). | ||||||
|  | 			And("`is_active` = ?", true). | ||||||
|  | 			And("`email_notifications_preference` IN ( ?, ?)", EmailNotificationsEnabled, EmailNotificationsOnMention). | ||||||
|  | 			Find(&ous) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	return ous, x.In("id", ids). | 	return ous, x.In("id", ids). | ||||||
| 		Where("`type` = ?", UserTypeIndividual). | 		Where("`type` = ?", UserTypeIndividual). | ||||||
| 		And("`prohibit_login` = ?", false). | 		And("`prohibit_login` = ?", false). | ||||||
|  |  | ||||||
|  | @ -389,3 +389,20 @@ func TestGetUserIDsByNames(t *testing.T) { | ||||||
| 	assert.Error(t, err) | 	assert.Error(t, err) | ||||||
| 	assert.Equal(t, []int64(nil), IDs) | 	assert.Equal(t, []int64(nil), IDs) | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func TestGetMaileableUsersByIDs(t *testing.T) { | ||||||
|  | 	results, err := GetMaileableUsersByIDs([]int64{1, 4}, false) | ||||||
|  | 	assert.NoError(t, err) | ||||||
|  | 	assert.Equal(t, 1, len(results)) | ||||||
|  | 	if len(results) > 1 { | ||||||
|  | 		assert.Equal(t, results[0].ID, 1) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	results, err = GetMaileableUsersByIDs([]int64{1, 4}, true) | ||||||
|  | 	assert.NoError(t, err) | ||||||
|  | 	assert.Equal(t, 2, len(results)) | ||||||
|  | 	if len(results) > 2 { | ||||||
|  | 		assert.Equal(t, results[0].ID, 1) | ||||||
|  | 		assert.Equal(t, results[1].ID, 4) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -118,7 +118,7 @@ func mailIssueCommentBatch(ctx *mailCommentContext, ids []int64, visited map[int | ||||||
| 				visited[id] = true | 				visited[id] = true | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 		recipients, err := models.GetMaileableUsersByIDs(unique) | 		recipients, err := models.GetMaileableUsersByIDs(unique, fromMention) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return err | 			return err | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | @ -27,7 +27,7 @@ func MailNewRelease(rel *models.Release) { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	recipients, err := models.GetMaileableUsersByIDs(watcherIDList) | 	recipients, err := models.GetMaileableUsersByIDs(watcherIDList, false) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Error("models.GetMaileableUsersByIDs: %v", err) | 		log.Error("models.GetMaileableUsersByIDs: %v", err) | ||||||
| 		return | 		return | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue