@@ -6,13 +6,10 @@ package conversations
66import  (
77	"context" 
88	"fmt" 
9- 	"strconv" 
109
1110	"code.gitea.io/gitea/models/db" 
1211	"code.gitea.io/gitea/models/organization" 
13- 	repo_model "code.gitea.io/gitea/models/repo" 
1412	user_model "code.gitea.io/gitea/models/user" 
15- 	"code.gitea.io/gitea/modules/container" 
1613	"code.gitea.io/gitea/modules/optional" 
1714
1815	"xorm.io/builder" 
@@ -21,29 +18,14 @@ import (
2118
2219// ConversationsOptions represents options of an conversation. 
2320type  ConversationsOptions  struct  { //nolint 
24- 	Paginator           * db.ListOptions 
25- 	RepoIDs             []int64  // overwrites RepoCond if the length is not 0 
26- 	AllPublic           bool     // include also all public repositories 
27- 	RepoCond            builder.Cond 
28- 	AssigneeID          int64 
29- 	PosterID            int64 
30- 	MentionedID         int64 
31- 	ReviewRequestedID   int64 
32- 	ReviewedID          int64 
33- 	SubscriberID        int64 
34- 	MilestoneIDs        []int64 
35- 	ProjectID           int64 
36- 	ProjectColumnID     int64 
37- 	IsClosed            optional.Option [bool ]
38- 	IsPull              optional.Option [bool ]
39- 	LabelIDs            []int64 
40- 	IncludedLabelNames  []string 
41- 	ExcludedLabelNames  []string 
42- 	IncludeMilestones   []string 
43- 	SortType            string 
44- 	ConversationIDs     []int64 
45- 	UpdatedAfterUnix    int64 
46- 	UpdatedBeforeUnix   int64 
21+ 	Paginator          * db.ListOptions 
22+ 	RepoIDs            []int64  // overwrites RepoCond if the length is not 0 
23+ 	AllPublic          bool     // include also all public repositories 
24+ 	RepoCond           builder.Cond 
25+ 	SortType           string 
26+ 	ConversationIDs    []int64 
27+ 	UpdatedAfterUnix   int64 
28+ 	UpdatedBeforeUnix  int64 
4729	// prioritize conversations from this repo 
4830	PriorityRepoID  int64 
4931	IsArchived      optional.Option [bool ]
@@ -124,75 +106,6 @@ func applyLimit(sess *xorm.Session, opts *ConversationsOptions) {
124106	sess .Limit (opts .Paginator .PageSize , start )
125107}
126108
127- func  applyLabelsCondition (sess  * xorm.Session , opts  * ConversationsOptions ) {
128- 	if  len (opts .LabelIDs ) >  0  {
129- 		if  opts .LabelIDs [0 ] ==  0  {
130- 			sess .Where ("conversation.id NOT IN (SELECT conversation_id FROM conversation_label)" )
131- 		} else  {
132- 			// deduplicate the label IDs for inclusion and exclusion 
133- 			includedLabelIDs  :=  make (container.Set [int64 ])
134- 			excludedLabelIDs  :=  make (container.Set [int64 ])
135- 			for  _ , labelID  :=  range  opts .LabelIDs  {
136- 				if  labelID  >  0  {
137- 					includedLabelIDs .Add (labelID )
138- 				} else  if  labelID  <  0  { // 0 is not supported here, so just ignore it 
139- 					excludedLabelIDs .Add (- labelID )
140- 				}
141- 			}
142- 			// ... and use them in a subquery of the form : 
143- 			//  where (select count(*) from conversation_label where conversation_id=conversation.id and label_id in (2, 4, 6)) = 3 
144- 			// This equality is guaranteed thanks to unique index (conversation_id,label_id) on table conversation_label. 
145- 			if  len (includedLabelIDs ) >  0  {
146- 				subQuery  :=  builder .Select ("count(*)" ).From ("conversation_label" ).Where (builder .Expr ("conversation_id = conversation.id" )).
147- 					And (builder .In ("label_id" , includedLabelIDs .Values ()))
148- 				sess .Where (builder.Eq {strconv .Itoa (len (includedLabelIDs )): subQuery })
149- 			}
150- 			// or (select count(*)...) = 0 for excluded labels 
151- 			if  len (excludedLabelIDs ) >  0  {
152- 				subQuery  :=  builder .Select ("count(*)" ).From ("conversation_label" ).Where (builder .Expr ("conversation_id = conversation.id" )).
153- 					And (builder .In ("label_id" , excludedLabelIDs .Values ()))
154- 				sess .Where (builder.Eq {"0" : subQuery })
155- 			}
156- 		}
157- 	}
158- }
159- 
160- func  applyMilestoneCondition (sess  * xorm.Session , opts  * ConversationsOptions ) {
161- 	if  len (opts .MilestoneIDs ) ==  1  &&  opts .MilestoneIDs [0 ] ==  db .NoConditionID  {
162- 		sess .And ("conversation.milestone_id = 0" )
163- 	} else  if  len (opts .MilestoneIDs ) >  0  {
164- 		sess .In ("conversation.milestone_id" , opts .MilestoneIDs )
165- 	}
166- 
167- 	if  len (opts .IncludeMilestones ) >  0  {
168- 		sess .In ("conversation.milestone_id" ,
169- 			builder .Select ("id" ).
170- 				From ("milestone" ).
171- 				Where (builder .In ("name" , opts .IncludeMilestones )))
172- 	}
173- }
174- 
175- func  applyProjectCondition (sess  * xorm.Session , opts  * ConversationsOptions ) {
176- 	if  opts .ProjectID  >  0  { // specific project 
177- 		sess .Join ("INNER" , "project_conversation" , "conversation.id = project_conversation.conversation_id" ).
178- 			And ("project_conversation.project_id=?" , opts .ProjectID )
179- 	} else  if  opts .ProjectID  ==  db .NoConditionID  { // show those that are in no project 
180- 		sess .And (builder .NotIn ("conversation.id" , builder .Select ("conversation_id" ).From ("project_conversation" ).And (builder.Neq {"project_id" : 0 })))
181- 	}
182- 	// opts.ProjectID == 0 means all projects, 
183- 	// do not need to apply any condition 
184- }
185- 
186- func  applyProjectColumnCondition (sess  * xorm.Session , opts  * ConversationsOptions ) {
187- 	// opts.ProjectColumnID == 0 means all project columns, 
188- 	// do not need to apply any condition 
189- 	if  opts .ProjectColumnID  >  0  {
190- 		sess .In ("conversation.id" , builder .Select ("conversation_id" ).From ("project_conversation" ).Where (builder.Eq {"project_board_id" : opts .ProjectColumnID }))
191- 	} else  if  opts .ProjectColumnID  ==  db .NoConditionID  {
192- 		sess .In ("conversation.id" , builder .Select ("conversation_id" ).From ("project_conversation" ).Where (builder.Eq {"project_board_id" : 0 }))
193- 	}
194- }
195- 
196109func  applyRepoConditions (sess  * xorm.Session , opts  * ConversationsOptions ) {
197110	if  len (opts .RepoIDs ) ==  1  {
198111		opts .RepoCond  =  builder.Eq {"conversation.repo_id" : opts .RepoIDs [0 ]}
@@ -217,85 +130,16 @@ func applyConditions(sess *xorm.Session, opts *ConversationsOptions) {
217130
218131	applyRepoConditions (sess , opts )
219132
220- 	if  opts .IsClosed .Has () {
221- 		sess .And ("conversation.is_closed=?" , opts .IsClosed .Value ())
222- 	}
223- 
224- 	if  opts .PosterID  >  0  {
225- 		applyPosterCondition (sess , opts .PosterID )
226- 	}
227- 
228- 	if  opts .MentionedID  >  0  {
229- 		applyMentionedCondition (sess , opts .MentionedID )
230- 	}
231- 
232- 	if  opts .SubscriberID  >  0  {
233- 		applySubscribedCondition (sess , opts .SubscriberID )
234- 	}
235- 
236- 	applyMilestoneCondition (sess , opts )
237- 
238133	if  opts .UpdatedAfterUnix  !=  0  {
239134		sess .And (builder.Gte {"conversation.updated_unix" : opts .UpdatedAfterUnix })
240135	}
241136	if  opts .UpdatedBeforeUnix  !=  0  {
242137		sess .And (builder.Lte {"conversation.updated_unix" : opts .UpdatedBeforeUnix })
243138	}
244139
245- 	applyProjectCondition (sess , opts )
246- 
247- 	applyProjectColumnCondition (sess , opts )
248- 
249- 	if  opts .IsPull .Has () {
250- 		sess .And ("conversation.is_pull=?" , opts .IsPull .Value ())
251- 	}
252- 
253140	if  opts .IsArchived .Has () {
254141		sess .And (builder.Eq {"repository.is_archived" : opts .IsArchived .Value ()})
255142	}
256- 
257- 	applyLabelsCondition (sess , opts )
258- }
259- 
260- func  applyPosterCondition (sess  * xorm.Session , posterID  int64 ) {
261- 	sess .And ("conversation.poster_id=?" , posterID )
262- }
263- 
264- func  applyMentionedCondition (sess  * xorm.Session , mentionedID  int64 ) {
265- 	sess .Join ("INNER" , "conversation_user" , "conversation.id = conversation_user.conversation_id" ).
266- 		And ("conversation_user.is_mentioned = ?" , true ).
267- 		And ("conversation_user.uid = ?" , mentionedID )
268- }
269- 
270- func  applySubscribedCondition (sess  * xorm.Session , subscriberID  int64 ) {
271- 	sess .And (
272- 		builder .
273- 			NotIn ("conversation.id" ,
274- 				builder .Select ("conversation_id" ).
275- 					From ("conversation_watch" ).
276- 					Where (builder.Eq {"is_watching" : false , "user_id" : subscriberID }),
277- 			),
278- 	).And (
279- 		builder .Or (
280- 			builder .In ("conversation.id" , builder .
281- 				Select ("conversation_id" ).
282- 				From ("conversation_watch" ).
283- 				Where (builder.Eq {"is_watching" : true , "user_id" : subscriberID }),
284- 			),
285- 			builder .In ("conversation.id" , builder .
286- 				Select ("conversation_id" ).
287- 				From ("comment" ).
288- 				Where (builder.Eq {"poster_id" : subscriberID }),
289- 			),
290- 			builder.Eq {"conversation.poster_id" : subscriberID },
291- 			builder .In ("conversation.repo_id" , builder .
292- 				Select ("id" ).
293- 				From ("watch" ).
294- 				Where (builder .And (builder.Eq {"user_id" : subscriberID },
295- 					builder .In ("mode" , repo_model .WatchModeNormal , repo_model .WatchModeAuto ))),
296- 			),
297- 		),
298- 	)
299143}
300144
301145// Conversations returns a list of conversations by given conditions. 
0 commit comments