1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-22 12:54:53 -05:00

Notifications: mark as read/unread and pin (#629)

* Use relative URLs

* Notifications - Mark as read/unread

* Feature of pinning a notification

* On view issue, do not mark as read a pinned notification
This commit is contained in:
Andrey Nering 2017-01-12 02:27:09 -02:00 committed by Lunny Xiao
parent cbf2a967c5
commit 769e0a3ea6
8 changed files with 169 additions and 32 deletions

View file

@ -589,7 +589,10 @@ func runWeb(ctx *cli.Context) error {
}) })
// ***** END: Repository ***** // ***** END: Repository *****
m.Get("/notifications", reqSignIn, user.Notifications) m.Group("/notifications", func() {
m.Get("", user.Notifications)
m.Post("/status", user.NotificationStatusPost)
}, reqSignIn)
m.Group("/api", func() { m.Group("/api", func() {
apiv1.RegisterRoutes(m) apiv1.RegisterRoutes(m)

View file

@ -448,7 +448,7 @@ func (issue *Issue) ReadBy(userID int64) error {
return err return err
} }
if err := setNotificationStatusRead(x, userID, issue.ID); err != nil { if err := setNotificationStatusReadIfUnread(x, userID, issue.ID); err != nil {
return err return err
} }

View file

@ -5,6 +5,7 @@
package models package models
import ( import (
"fmt"
"time" "time"
) )
@ -20,6 +21,8 @@ const (
NotificationStatusUnread NotificationStatus = iota + 1 NotificationStatusUnread NotificationStatus = iota + 1
// NotificationStatusRead represents a read notification // NotificationStatusRead represents a read notification
NotificationStatusRead NotificationStatusRead
// NotificationStatusPinned represents a pinned notification
NotificationStatusPinned
) )
const ( const (
@ -182,13 +185,19 @@ func getIssueNotification(e Engine, userID, issueID int64) (*Notification, error
} }
// NotificationsForUser returns notifications for a given user and status // NotificationsForUser returns notifications for a given user and status
func NotificationsForUser(user *User, status NotificationStatus, page, perPage int) ([]*Notification, error) { func NotificationsForUser(user *User, statuses []NotificationStatus, page, perPage int) ([]*Notification, error) {
return notificationsForUser(x, user, status, page, perPage) return notificationsForUser(x, user, statuses, page, perPage)
} }
func notificationsForUser(e Engine, user *User, status NotificationStatus, page, perPage int) (notifications []*Notification, err error) { func notificationsForUser(e Engine, user *User, statuses []NotificationStatus, page, perPage int) (notifications []*Notification, err error) {
// FIXME: Xorm does not support aliases types (like NotificationStatus) on In() method
s := make([]uint8, len(statuses))
for i, status := range statuses {
s[i] = uint8(status)
}
sess := e. sess := e.
Where("user_id = ?", user.ID). Where("user_id = ?", user.ID).
And("status = ?", status). In("status", s).
OrderBy("updated_unix DESC") OrderBy("updated_unix DESC")
if page > 0 && perPage > 0 { if page > 0 && perPage > 0 {
@ -241,15 +250,53 @@ func getNotificationCount(e Engine, user *User, status NotificationStatus) (coun
return return
} }
func setNotificationStatusRead(e Engine, userID, issueID int64) error { func setNotificationStatusReadIfUnread(e Engine, userID, issueID int64) error {
notification, err := getIssueNotification(e, userID, issueID) notification, err := getIssueNotification(e, userID, issueID)
// ignore if not exists // ignore if not exists
if err != nil { if err != nil {
return nil return nil
} }
if notification.Status != NotificationStatusUnread {
return nil
}
notification.Status = NotificationStatusRead notification.Status = NotificationStatusRead
_, err = e.Id(notification.ID).Update(notification) _, err = e.Id(notification.ID).Update(notification)
return err return err
} }
// SetNotificationStatus change the notification status
func SetNotificationStatus(notificationID int64, user *User, status NotificationStatus) error {
notification, err := getNotificationByID(notificationID)
if err != nil {
return err
}
if notification.UserID != user.ID {
return fmt.Errorf("Can't change notification of another user: %d, %d", notification.UserID, user.ID)
}
notification.Status = status
_, err = x.Id(notificationID).Update(notification)
return err
}
func getNotificationByID(notificationID int64) (*Notification, error) {
notification := new(Notification)
ok, err := x.
Where("id = ?", notificationID).
Get(notification)
if err != nil {
return nil, err
}
if !ok {
return nil, fmt.Errorf("Notification %d does not exists", notificationID)
}
return notification, nil
}

View file

@ -2712,6 +2712,12 @@ footer .ui.language .menu {
float: left; float: left;
margin-left: 7px; margin-left: 7px;
} }
.user.notification .buttons-panel button {
padding: 3px;
}
.user.notification .buttons-panel form {
display: inline-block;
}
.user.notification .octicon-issue-opened, .user.notification .octicon-issue-opened,
.user.notification .octicon-git-pull-request { .user.notification .octicon-git-pull-request {
color: #21ba45; color: #21ba45;
@ -2722,6 +2728,9 @@ footer .ui.language .menu {
.user.notification .octicon-git-merge { .user.notification .octicon-git-merge {
color: #a333c8; color: #a333c8;
} }
.user.notification .octicon-pin {
color: #2185d0;
}
.dashboard { .dashboard {
padding-top: 15px; padding-top: 15px;
padding-bottom: 80px; padding-bottom: 80px;

View file

@ -85,6 +85,16 @@
margin-left: 7px; margin-left: 7px;
} }
.buttons-panel {
button {
padding: 3px;
}
form {
display: inline-block;
}
}
.octicon-issue-opened, .octicon-git-pull-request { .octicon-issue-opened, .octicon-git-pull-request {
color: #21ba45; color: #21ba45;
} }
@ -94,5 +104,8 @@
.octicon-git-merge { .octicon-git-merge {
color: #a333c8; color: #a333c8;
} }
.octicon-pin {
color: #2185d0;
}
} }
} }

View file

@ -1,7 +1,9 @@
package user package user
import ( import (
"errors"
"fmt" "fmt"
"strconv"
"strings" "strings"
"github.com/Unknwon/paginater" "github.com/Unknwon/paginater"
@ -9,6 +11,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting"
) )
const ( const (
@ -56,7 +59,8 @@ func Notifications(c *context.Context) {
status = models.NotificationStatusUnread status = models.NotificationStatusUnread
} }
notifications, err := models.NotificationsForUser(c.User, status, page, perPage) statuses := []models.NotificationStatus{status, models.NotificationStatusPinned}
notifications, err := models.NotificationsForUser(c.User, statuses, page, perPage)
if err != nil { if err != nil {
c.Handle(500, "ErrNotificationsForUser", err) c.Handle(500, "ErrNotificationsForUser", err)
return return
@ -79,3 +83,32 @@ func Notifications(c *context.Context) {
c.Data["Page"] = paginater.New(int(total), perPage, page, 5) c.Data["Page"] = paginater.New(int(total), perPage, page, 5)
c.HTML(200, tplNotification) c.HTML(200, tplNotification)
} }
// NotificationStatusPost is a route for changing the status of a notification
func NotificationStatusPost(c *context.Context) {
var (
notificationID, _ = strconv.ParseInt(c.Req.PostFormValue("notification_id"), 10, 64)
statusStr = c.Req.PostFormValue("status")
status models.NotificationStatus
)
switch statusStr {
case "read":
status = models.NotificationStatusRead
case "unread":
status = models.NotificationStatusUnread
case "pinned":
status = models.NotificationStatusPinned
default:
c.Handle(500, "InvalidNotificationStatus", errors.New("Invalid notification status"))
return
}
if err := models.SetNotificationStatus(notificationID, c.User, status); err != nil {
c.Handle(500, "SetNotificationStatus", err)
return
}
url := fmt.Sprintf("%s/notifications", setting.AppSubURL)
c.Redirect(url, 303)
}

View file

@ -82,7 +82,7 @@
{{if .IsSigned}} {{if .IsSigned}}
<div class="right menu"> <div class="right menu">
<a href="/notifications" class="ui head link jump item poping up" data-content='{{.i18n.Tr "notifications"}}' data-variation="tiny inverted"> <a href="{{$.AppSubUrl}}/notifications" class="ui head link jump item poping up" data-content='{{.i18n.Tr "notifications"}}' data-variation="tiny inverted">
<span class="text"> <span class="text">
<i class="octicon octicon-inbox"><span class="sr-only">{{.i18n.Tr "notifications"}}</span></i> <i class="octicon octicon-inbox"><span class="sr-only">{{.i18n.Tr "notifications"}}</span></i>

View file

@ -5,7 +5,7 @@
<h1 class="ui header">{{.i18n.Tr "notification.notifications"}}</h1> <h1 class="ui header">{{.i18n.Tr "notification.notifications"}}</h1>
<div class="ui top attached tabular menu"> <div class="ui top attached tabular menu">
<a href="/notifications?q=unread"> <a href="{{$.AppSubUrl}}/notifications?q=unread">
<div class="{{if eq .Status 1}}active{{end}} item"> <div class="{{if eq .Status 1}}active{{end}} item">
{{.i18n.Tr "notification.unread"}} {{.i18n.Tr "notification.unread"}}
{{if eq .Status 1}} {{if eq .Status 1}}
@ -13,7 +13,7 @@
{{end}} {{end}}
</div> </div>
</a> </a>
<a href="/notifications?q=read"> <a href="{{$.AppSubUrl}}/notifications?q=read">
<div class="{{if eq .Status 2}}active{{end}} item"> <div class="{{if eq .Status 2}}active{{end}} item">
{{.i18n.Tr "notification.read"}} {{.i18n.Tr "notification.read"}}
{{if eq .Status 2}} {{if eq .Status 2}}
@ -30,15 +30,48 @@
{{.i18n.Tr "notification.no_read"}} {{.i18n.Tr "notification.no_read"}}
{{end}} {{end}}
{{else}} {{else}}
<div class="ui relaxed divided list"> <div class="ui relaxed divided selection list">
{{range $notification := .Notifications}} {{range $notification := .Notifications}}
{{$issue := $notification.GetIssue}} {{$issue := $notification.GetIssue}}
{{$repo := $notification.GetRepo}} {{$repo := $notification.GetRepo}}
{{$repoOwner := $repo.MustOwner}} {{$repoOwner := $repo.MustOwner}}
<div class="item"> <a class="item" href="{{$.AppSubUrl}}/{{$repoOwner.Name}}/{{$repo.Name}}/issues/{{$issue.Index}}">
<a href="{{$.AppSubUrl}}/{{$repoOwner.Name}}/{{$repo.Name}}/issues/{{$issue.Index}}"> <div class="buttons-panel right floated content">
{{if and $issue.IsPull}} {{if ne $notification.Status 3}}
<form action="{{$.AppSubUrl}}/notifications/status" method="POST">
{{$.CsrfTokenHtml}}
<input type="hidden" name="notification_id" value="{{$notification.ID}}" />
<input type="hidden" name="status" value="pinned" />
<button class="ui button" title="Pin notification">
<i class="octicon octicon-pin"></i>
</button>
</form>
{{end}}
{{if or (eq $notification.Status 1) (eq $notification.Status 3)}}
<form action="{{$.AppSubUrl}}/notifications/status" method="POST">
{{$.CsrfTokenHtml}}
<input type="hidden" name="notification_id" value="{{$notification.ID}}" />
<input type="hidden" name="status" value="read" />
<button class="ui button" title="Mark as read">
<i class="octicon octicon-check"></i>
</button>
</form>
{{else if eq $notification.Status 2}}
<form action="{{$.AppSubUrl}}/notifications/status" method="POST">
{{$.CsrfTokenHtml}}
<input type="hidden" name="notification_id" value="{{$notification.ID}}" />
<input type="hidden" name="status" value="unread" />
<button class="ui button" title="Mark as unread">
<i class="octicon octicon-bell"></i>
</button>
</form>
{{end}}
</div>
{{if eq $notification.Status 3}}
<i class="blue octicon octicon-pin"></i>
{{else if $issue.IsPull}}
{{if $issue.IsClosed}} {{if $issue.IsClosed}}
<i class="octicon octicon-git-merge"></i> <i class="octicon octicon-git-merge"></i>
{{else}} {{else}}
@ -57,7 +90,6 @@
<div class="description">#{{$issue.Index}} - {{$issue.Title}}</div> <div class="description">#{{$issue.Index}} - {{$issue.Title}}</div>
</div> </div>
</a> </a>
</div>
{{end}} {{end}}
</div> </div>
{{end}} {{end}}