|
| 1 | +// Copyright 2017 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package models |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "fmt" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/go-xorm/builder" |
| 13 | + "github.com/go-xorm/xorm" |
| 14 | + |
| 15 | + "code.gitea.io/gitea/modules/setting" |
| 16 | +) |
| 17 | + |
| 18 | +// Reaction represents a reactions on issues and comments. |
| 19 | +type Reaction struct { |
| 20 | + ID int64 `xorm:"pk autoincr"` |
| 21 | + Type string `xorm:"INDEX UNIQUE(s) NOT NULL"` |
| 22 | + IssueID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"` |
| 23 | + CommentID int64 `xorm:"INDEX UNIQUE(s)"` |
| 24 | + UserID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"` |
| 25 | + User *User `xorm:"-"` |
| 26 | + Created time.Time `xorm:"-"` |
| 27 | + CreatedUnix int64 `xorm:"INDEX created"` |
| 28 | +} |
| 29 | + |
| 30 | +// AfterLoad is invoked from XORM after setting the values of all fields of this object. |
| 31 | +func (s *Reaction) AfterLoad() { |
| 32 | + s.Created = time.Unix(s.CreatedUnix, 0).Local() |
| 33 | +} |
| 34 | + |
| 35 | +// FindReactionsOptions describes the conditions to Find reactions |
| 36 | +type FindReactionsOptions struct { |
| 37 | + IssueID int64 |
| 38 | + CommentID int64 |
| 39 | +} |
| 40 | + |
| 41 | +func (opts *FindReactionsOptions) toConds() builder.Cond { |
| 42 | + var cond = builder.NewCond() |
| 43 | + if opts.IssueID > 0 { |
| 44 | + cond = cond.And(builder.Eq{"reaction.issue_id": opts.IssueID}) |
| 45 | + } |
| 46 | + if opts.CommentID > 0 { |
| 47 | + cond = cond.And(builder.Eq{"reaction.comment_id": opts.CommentID}) |
| 48 | + } |
| 49 | + return cond |
| 50 | +} |
| 51 | + |
| 52 | +func findReactions(e Engine, opts FindReactionsOptions) ([]*Reaction, error) { |
| 53 | + reactions := make([]*Reaction, 0, 10) |
| 54 | + sess := e.Where(opts.toConds()) |
| 55 | + return reactions, sess. |
| 56 | + Asc("reaction.issue_id", "reaction.comment_id", "reaction.created_unix", "reaction.id"). |
| 57 | + Find(&reactions) |
| 58 | +} |
| 59 | + |
| 60 | +func createReaction(e *xorm.Session, opts *ReactionOptions) (*Reaction, error) { |
| 61 | + reaction := &Reaction{ |
| 62 | + Type: opts.Type, |
| 63 | + UserID: opts.Doer.ID, |
| 64 | + IssueID: opts.Issue.ID, |
| 65 | + } |
| 66 | + if opts.Comment != nil { |
| 67 | + reaction.CommentID = opts.Comment.ID |
| 68 | + } |
| 69 | + if _, err := e.Insert(reaction); err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + |
| 73 | + return reaction, nil |
| 74 | +} |
| 75 | + |
| 76 | +// ReactionOptions defines options for creating or deleting reactions |
| 77 | +type ReactionOptions struct { |
| 78 | + Type string |
| 79 | + Doer *User |
| 80 | + Issue *Issue |
| 81 | + Comment *Comment |
| 82 | +} |
| 83 | + |
| 84 | +// CreateReaction creates reaction for issue or comment. |
| 85 | +func CreateReaction(opts *ReactionOptions) (reaction *Reaction, err error) { |
| 86 | + sess := x.NewSession() |
| 87 | + defer sess.Close() |
| 88 | + if err = sess.Begin(); err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + |
| 92 | + reaction, err = createReaction(sess, opts) |
| 93 | + if err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + |
| 97 | + if err = sess.Commit(); err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + return reaction, nil |
| 101 | +} |
| 102 | + |
| 103 | +// CreateIssueReaction creates a reaction on issue. |
| 104 | +func CreateIssueReaction(doer *User, issue *Issue, content string) (*Reaction, error) { |
| 105 | + return CreateReaction(&ReactionOptions{ |
| 106 | + Type: content, |
| 107 | + Doer: doer, |
| 108 | + Issue: issue, |
| 109 | + }) |
| 110 | +} |
| 111 | + |
| 112 | +// CreateCommentReaction creates a reaction on comment. |
| 113 | +func CreateCommentReaction(doer *User, issue *Issue, comment *Comment, content string) (*Reaction, error) { |
| 114 | + return CreateReaction(&ReactionOptions{ |
| 115 | + Type: content, |
| 116 | + Doer: doer, |
| 117 | + Issue: issue, |
| 118 | + Comment: comment, |
| 119 | + }) |
| 120 | +} |
| 121 | + |
| 122 | +func deleteReaction(e *xorm.Session, opts *ReactionOptions) error { |
| 123 | + reaction := &Reaction{ |
| 124 | + Type: opts.Type, |
| 125 | + UserID: opts.Doer.ID, |
| 126 | + IssueID: opts.Issue.ID, |
| 127 | + } |
| 128 | + if opts.Comment != nil { |
| 129 | + reaction.CommentID = opts.Comment.ID |
| 130 | + } |
| 131 | + _, err := e.Delete(reaction) |
| 132 | + return err |
| 133 | +} |
| 134 | + |
| 135 | +// DeleteReaction deletes reaction for issue or comment. |
| 136 | +func DeleteReaction(opts *ReactionOptions) error { |
| 137 | + sess := x.NewSession() |
| 138 | + defer sess.Close() |
| 139 | + if err := sess.Begin(); err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + if err := deleteReaction(sess, opts); err != nil { |
| 144 | + return err |
| 145 | + } |
| 146 | + |
| 147 | + return sess.Commit() |
| 148 | +} |
| 149 | + |
| 150 | +// DeleteIssueReaction deletes a reaction on issue. |
| 151 | +func DeleteIssueReaction(doer *User, issue *Issue, content string) error { |
| 152 | + return DeleteReaction(&ReactionOptions{ |
| 153 | + Type: content, |
| 154 | + Doer: doer, |
| 155 | + Issue: issue, |
| 156 | + }) |
| 157 | +} |
| 158 | + |
| 159 | +// DeleteCommentReaction deletes a reaction on comment. |
| 160 | +func DeleteCommentReaction(doer *User, issue *Issue, comment *Comment, content string) error { |
| 161 | + return DeleteReaction(&ReactionOptions{ |
| 162 | + Type: content, |
| 163 | + Doer: doer, |
| 164 | + Issue: issue, |
| 165 | + Comment: comment, |
| 166 | + }) |
| 167 | +} |
| 168 | + |
| 169 | +// ReactionList represents list of reactions |
| 170 | +type ReactionList []*Reaction |
| 171 | + |
| 172 | +// HasUser check if user has reacted |
| 173 | +func (list ReactionList) HasUser(userID int64) bool { |
| 174 | + if userID == 0 { |
| 175 | + return false |
| 176 | + } |
| 177 | + for _, reaction := range list { |
| 178 | + if reaction.UserID == userID { |
| 179 | + return true |
| 180 | + } |
| 181 | + } |
| 182 | + return false |
| 183 | +} |
| 184 | + |
| 185 | +// GroupByType returns reactions grouped by type |
| 186 | +func (list ReactionList) GroupByType() map[string]ReactionList { |
| 187 | + var reactions = make(map[string]ReactionList) |
| 188 | + for _, reaction := range list { |
| 189 | + reactions[reaction.Type] = append(reactions[reaction.Type], reaction) |
| 190 | + } |
| 191 | + return reactions |
| 192 | +} |
| 193 | + |
| 194 | +func (list ReactionList) getUserIDs() []int64 { |
| 195 | + userIDs := make(map[int64]struct{}, len(list)) |
| 196 | + for _, reaction := range list { |
| 197 | + if _, ok := userIDs[reaction.UserID]; !ok { |
| 198 | + userIDs[reaction.UserID] = struct{}{} |
| 199 | + } |
| 200 | + } |
| 201 | + return keysInt64(userIDs) |
| 202 | +} |
| 203 | + |
| 204 | +func (list ReactionList) loadUsers(e Engine) ([]*User, error) { |
| 205 | + if len(list) == 0 { |
| 206 | + return nil, nil |
| 207 | + } |
| 208 | + |
| 209 | + userIDs := list.getUserIDs() |
| 210 | + userMaps := make(map[int64]*User, len(userIDs)) |
| 211 | + err := e. |
| 212 | + In("id", userIDs). |
| 213 | + Find(&userMaps) |
| 214 | + if err != nil { |
| 215 | + return nil, fmt.Errorf("find user: %v", err) |
| 216 | + } |
| 217 | + |
| 218 | + for _, reaction := range list { |
| 219 | + if user, ok := userMaps[reaction.UserID]; ok { |
| 220 | + reaction.User = user |
| 221 | + } else { |
| 222 | + reaction.User = NewGhostUser() |
| 223 | + } |
| 224 | + } |
| 225 | + return valuesUser(userMaps), nil |
| 226 | +} |
| 227 | + |
| 228 | +// LoadUsers loads reactions' all users |
| 229 | +func (list ReactionList) LoadUsers() ([]*User, error) { |
| 230 | + return list.loadUsers(x) |
| 231 | +} |
| 232 | + |
| 233 | +// GetFirstUsers returns first reacted user display names separated by comma |
| 234 | +func (list ReactionList) GetFirstUsers() string { |
| 235 | + var buffer bytes.Buffer |
| 236 | + var rem = setting.UI.ReactionMaxUserNum |
| 237 | + for _, reaction := range list { |
| 238 | + if buffer.Len() > 0 { |
| 239 | + buffer.WriteString(", ") |
| 240 | + } |
| 241 | + buffer.WriteString(reaction.User.DisplayName()) |
| 242 | + if rem--; rem == 0 { |
| 243 | + break |
| 244 | + } |
| 245 | + } |
| 246 | + return buffer.String() |
| 247 | +} |
| 248 | + |
| 249 | +// GetMoreUserCount returns count of not shown users in reaction tooltip |
| 250 | +func (list ReactionList) GetMoreUserCount() int { |
| 251 | + if len(list) <= setting.UI.ReactionMaxUserNum { |
| 252 | + return 0 |
| 253 | + } |
| 254 | + return len(list) - setting.UI.ReactionMaxUserNum |
| 255 | +} |
0 commit comments