summaryrefslogtreecommitdiffstats
path: root/models/issue_watch.go
diff options
context:
space:
mode:
Diffstat (limited to 'models/issue_watch.go')
-rw-r--r--models/issue_watch.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/models/issue_watch.go b/models/issue_watch.go
index 96e080136f..d082211c77 100644
--- a/models/issue_watch.go
+++ b/models/issue_watch.go
@@ -16,5 +16,45 @@ type IssueWatch struct {
// BeforeInsert is invoked from XORM before inserting an object of this type.
func (iw *IssueWatch) BeforeInsert() {
+ iw.Created = time.Now()
iw.CreatedUnix = time.Now().Unix()
}
+
+// CreateOrUpdateIssueWatch set watching for a user and issue
+func CreateOrUpdateIssueWatch(userID, issueID int64, isWatching bool) error {
+ iw, exists, err := getIssueWatch(x, userID, issueID)
+ if err != nil {
+ return err
+ }
+
+ if !exists {
+ iw = &IssueWatch{
+ UserID: userID,
+ IssueID: issueID,
+ IsWatching: isWatching,
+ }
+
+ if _, err := x.Insert(iw); err != nil {
+ return err
+ }
+ } else {
+ if _, err := x.Table(&IssueWatch{}).Id(iw.ID).Update(map[string]interface{}{"is_watching": isWatching}); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+// GetIssueWatch returns an issue watch by user and issue
+func GetIssueWatch(userID, issueID int64) (iw *IssueWatch, exists bool, err error) {
+ iw, exists, err = getIssueWatch(x, userID, issueID)
+ return
+}
+func getIssueWatch(e Engine, userID, issueID int64) (iw *IssueWatch, exists bool, err error) {
+ iw = new(IssueWatch)
+ exists, err = e.
+ Where("user_id = ?", userID).
+ And("issue_id = ?", issueID).
+ Get(iw)
+ return
+}