Skip to content

Commit

Permalink
Merge e14983f into 530fd05
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiloutre committed Nov 2, 2017
2 parents 530fd05 + e14983f commit dbb440b
Show file tree
Hide file tree
Showing 50 changed files with 678 additions and 322 deletions.
2 changes: 1 addition & 1 deletion controllers/moderator/announcements.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func postAnnouncement(c *gin.Context) {

// deleteAnnouncement : Controller for deleting an announcement
func deleteAnnouncement(c *gin.Context) {
id, _ := strconv.ParseInt(c.Query("id"), 10, 32)
id, _ := strconv.ParseInt(c.PostForm("id"), 10, 32)
announcement, err := notifications.FindByID(uint(id))
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
Expand Down
2 changes: 1 addition & 1 deletion controllers/moderator/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func torrentManyAction(c *gin.Context) {
query.Append("torrent_id", torrentID)
reports, _, _ := reports.FindOrderBy(query, "", 0, 0)
for _, report := range reports {
report.Delete(false)
report.Delete()
}
messages.AddInfoTf("infos", "torrent_reports_deleted", torrent.Name)
}
Expand Down
18 changes: 10 additions & 8 deletions controllers/moderator/reports.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package moderatorController

import (
"fmt"
"html"
"net/http"
"strconv"
Expand Down Expand Up @@ -39,13 +38,16 @@ func TorrentReportListPanel(c *gin.Context) {
func TorrentReportDeleteModPanel(c *gin.Context) {
id := c.PostForm("id")

fmt.Println(id)
idNum, _ := strconv.ParseUint(id, 10, 64)
_, _, _ = reports.Delete(uint(idNum))
/* If we need to log report delete activity
if err == nil {
activity.Log(&models.User{}, torrent.Identifier(), "delete", "torrent_report_deleted_by", strconv.Itoa(int(report.ID)), router.GetUser(c).Username)
if c.Request.URL.Query()["all"] != nil {
reports.DeleteAll()
} else {
idNum, _ := strconv.ParseUint(id, 10, 64)
_, _, _ = reports.Delete(uint(idNum))
/* If we need to log report delete activity
if err == nil {
activity.Log(&models.User{}, torrent.Identifier(), "delete", "torrent_report_deleted_by", strconv.Itoa(int(report.ID)), router.GetUser(c).Username)
}
*/
}
*/
c.Redirect(http.StatusSeeOther, "/mod/reports?deleted")
}
27 changes: 10 additions & 17 deletions controllers/moderator/torrents.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,26 +129,19 @@ func TorrentDeleteModPanel(c *gin.Context) {
var err error
if definitely != nil {
_, _, err = torrent.DefinitelyDelete()

//delete reports of torrent
query := &search.Query{}
query.Append("torrent_id", id)
reports, _, _ := reports.FindOrderBy(query, "", 0, 0)
for _, report := range reports {
report.Delete(true)
}
returnRoute = "/mod/torrents/deleted"
} else {
_, _, err = torrent.Delete(false)

//delete reports of torrent
query := &search.Query{}
query.Append("torrent_id", id)
reports, _, _ := reports.FindOrderBy(query, "", 0, 0)
for _, report := range reports {
report.Delete(false)
}
}

//delete reports of torrent
query := &search.Query{}
query.Append("torrent_id", id)
reports, _, _ := reports.FindOrderBy(query, "", 0, 0)
for _, report := range reports {
report.Delete()
}

if err == nil {
if torrent.Uploader == nil {
torrent.Uploader = &models.User{}
Expand Down Expand Up @@ -176,7 +169,7 @@ func DeleteTagsModPanel(c *gin.Context) {

// TorrentBlockModPanel : Controller to lock torrents, redirecting to previous page
func TorrentBlockModPanel(c *gin.Context) {
id, _ := strconv.ParseInt(c.Query("id"), 10, 32)
id, _ := strconv.ParseInt(c.PostForm("id"), 10, 32)
torrent, _, err := torrents.ToggleBlock(uint(id))
var returnRoute, action string
if torrent.IsDeleted() {
Expand Down
8 changes: 6 additions & 2 deletions controllers/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func ReportTorrentHandler(c *gin.Context) {
messages := msg.GetMessages(c)
captchaError := "?reported"
currentUser := router.GetUser(c)
if currentUser.NeedsCaptcha() {
if currentUser.ID == 0 {
userCaptcha := captcha.Extract(c)
if !captcha.Authenticate(userCaptcha) {
captchaError = "?badcaptcha"
Expand All @@ -33,7 +33,11 @@ func ReportTorrentHandler(c *gin.Context) {
messages.Error(err)
}
if !messages.HasErrors() {
_, err := reports.Create(c.PostForm("report_type"), torrent, currentUser)
reportMessage := c.PostForm("report_message")
if len(reportMessage) > 60 {
reportMessage = reportMessage[0:60]
}
_, err := reports.Create(c.PostForm("report_type"), reportMessage, torrent, currentUser)
messages.AddInfoTf("infos", "report_msg", id)
if err != nil {
messages.ImportFromError("errors", err)
Expand Down
4 changes: 3 additions & 1 deletion controllers/themeToggle/themeToggle.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ func toggleThemeHandler(c *gin.Context) {
http.SetCookie(c.Writer, &http.Cookie{Name: "theme2", Value: theme, Domain: getDomainName(), Path: "/", Expires: timeHelper.FewDaysLater(365)})

//Redirect user to page he was in beforehand
c.Redirect(http.StatusSeeOther, c.Param("redirect") + "#footer")
if c.Request.URL.Query()["no_redirect"] == nil {
c.Redirect(http.StatusSeeOther, c.Param("redirect") + "#footer")
}
return
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/torrent/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TorrentDeleteUserPanel(c *gin.Context) {
query.Append("torrent_id", id)
torrentReports, _, _ := reports.FindOrderBy(query, "", 0, 0)
for _, report := range torrentReports {
report.Delete(false)
report.Delete()
}
}
c.Redirect(http.StatusSeeOther, "/?deleted")
Expand Down
6 changes: 3 additions & 3 deletions controllers/torrent/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func ViewHandler(c *gin.Context) {
// Retrieve the torrent
torrent, err := torrents.FindByID(uint(id))

// If come from notification, toggle the notification as read
if c.Request.URL.Query()["notif"] != nil && user.ID > 0 {
notifications.ToggleReadNotification(torrent.Identifier(), user.ID)
// Toggle the notifications related to this torrent as read
if user.ID > 0 {
notifications.ToggleReadNotification(torrent.Identifier(), user)
}

// If torrent not found, display 404
Expand Down
5 changes: 4 additions & 1 deletion controllers/user/follow.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func UserFollowHandler(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
currentUser := router.GetUser(c)
user, _, errorUser := users.FindForAdmin(uint(id))
if errorUser == nil && user.ID > 0 {
if errorUser == nil && user.ID > 0 && currentUser.ID > 0 && user.ID != currentUser.ID {
if !currentUser.IsFollower(uint(id)) {
followAction = "followed"
currentUser.SetFollow(user)
Expand All @@ -28,5 +28,8 @@ func UserFollowHandler(c *gin.Context) {
if c.Query("id") != "" {
url = "/view/" + c.Query("id") + "?" + followAction
}
if currentUser.ID == 0 {
url = "/login"
}
c.Redirect(http.StatusSeeOther, url)
}
22 changes: 14 additions & 8 deletions controllers/user/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/http"

"github.com/NyaaPantsu/nyaa/controllers/router"
"github.com/NyaaPantsu/nyaa/models"
"github.com/NyaaPantsu/nyaa/models/notifications"
"github.com/NyaaPantsu/nyaa/models/users"
"github.com/NyaaPantsu/nyaa/templates"
Expand Down Expand Up @@ -190,9 +189,13 @@ func UserProfileFormHandler(c *gin.Context) {
validator.ValidateForm(&userForm, messages)
if !messages.HasErrors() {
if userForm.Email != userProfile.Email {
email.SendVerificationToUser(currentUser, userForm.Email)
messages.AddInfoTf("infos", "email_changed", userForm.Email)
userForm.Email = userProfile.Email // reset, it will be set when user clicks verification
if currentUser.HasAdmin() {
userProfile.Email = userForm.Email
} else {
email.SendVerificationToUser(currentUser, userForm.Email)
messages.AddInfoTf("infos", "email_changed", userForm.Email)
userForm.Email = userProfile.Email // reset, it will be set when user clicks verification
}
}
user, _, err := users.UpdateFromRequest(c, &userForm, &userSettingsForm, currentUser, uint(id))
if err != nil {
Expand All @@ -204,6 +207,7 @@ func UserProfileFormHandler(c *gin.Context) {
}
if !messages.HasErrors() {
messages.AddInfoT("infos", "profile_updated")
userProfile = user
}
}
}
Expand All @@ -215,11 +219,13 @@ func UserProfileFormHandler(c *gin.Context) {
func UserNotificationsHandler(c *gin.Context) {
currentUser := router.GetUser(c)
if currentUser.ID > 0 {
messages := msg.GetMessages(c)
if c.Request.URL.Query()["clear"] != nil {
notifications.DeleteAllNotifications(currentUser.ID)
messages.AddInfoT("infos", "notifications_cleared")
currentUser.Notifications = []models.Notification{}
notifications.DeleteNotifications(currentUser, false)

} else if c.Request.URL.Query()["clear_all"] != nil {
notifications.DeleteNotifications(currentUser, true)
} else if c.Request.URL.Query()["read_all"] != nil {
notifications.MarkAllNotificationsAsRead(currentUser)
}
templates.UserProfileNotifications(c, currentUser)
} else {
Expand Down
3 changes: 3 additions & 0 deletions models/comments/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
// When a new comment is added this is called
func NewCommentEvent(comment *models.Comment, torrent *models.Torrent) {
comment.Torrent = torrent
if comment.UserID == torrent.UploaderID {
return
}
url := "/view/" + strconv.FormatUint(uint64(torrent.ID), 10)
if torrent.UploaderID > 0 {
torrent.Uploader.ParseSettings()
Expand Down
3 changes: 2 additions & 1 deletion models/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ type Notification struct {
Identifier string
URL string
Expire time.Time
Date time.Time
UserID uint
// User *User `gorm:"AssociationForeignKey:UserID;ForeignKey:user_id"` // Don't think that we need it here
}

// NewNotification : Create a new notification
func NewNotification(identifier string, c string, url string) Notification {
return Notification{Identifier: identifier, Content: c, URL: url}
return Notification{Identifier: identifier, Content: c, URL: url, Date: time.Now()}
}

// TableName : Return the name of notification table
Expand Down
37 changes: 32 additions & 5 deletions models/notifications/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,38 @@ func NotifyUser(user *models.User, name string, msg string, url string, email bo
}

// ToggleReadNotification : Make a notification as read according to its identifier
func ToggleReadNotification(identifier string, id uint) { //
models.ORM.Model(&models.Notification{}).Where("identifier = ? AND user_id = ?", identifier, id).Updates(models.Notification{Read: true})
func ToggleReadNotification(identifier string, user *models.User) { //
models.ORM.Model(&models.Notification{}).Where("identifier = ? AND user_id = ?", identifier, user.ID).Updates(models.Notification{Read: true})
for i, notif := range user.Notifications {
if notif.Identifier == identifier {
user.Notifications[i].Read = true
}
}
//Need to update both DB and variable, otherwise when the function is called the user still needs to do an additional refresh to see the notification gone/read
}

// DeleteAllNotifications : Erase notifications from a user
func DeleteAllNotifications(id uint) { //
models.ORM.Where("user_id = ?", id).Delete(&models.Notification{})
// MarkAllNotificationsAsRead : Force every notification as read
func MarkAllNotificationsAsRead(user *models.User) { //
models.ORM.Model(&models.Notification{}).Where("user_id = ?", user.ID).Updates(models.Notification{Read: true})
for i := range user.Notifications {
user.Notifications[i].Read = true
}
//Need to update both DB and variable, otherwise when the function is called the user still needs to do an additional refresh to see the notification gone/read
}

// DeleteNotifications : Erase notifications from a user
func DeleteNotifications(user *models.User, all bool) { //
if all {
models.ORM.Where("user_id = ?", user.ID).Delete(&models.Notification{})
user.Notifications = []models.Notification{}
} else {
models.ORM.Where("user_id = ? AND read = ?", user.ID, true).Delete(&models.Notification{})
NewNotifications := []models.Notification{}
for _, notif := range user.Notifications {
if !notif.Read {
NewNotifications = append(NewNotifications, notif)
}
}
user.Notifications = NewNotifications
}
}
13 changes: 6 additions & 7 deletions models/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
// User can be null (anonymous reports)
// FIXME can't preload field Torrents for models.TorrentReport
type TorrentReport struct {
ID uint `gorm:"column:torrent_report_id;primary_key"`
ID uint `gorm:"column:torrent_report_id;primary_key"`
Description string `gorm:"column:type"`
Message string `gorm:"column:message"`
TorrentID uint `gorm:"column:torrent_id"`
UserID uint `gorm:"column:user_id"`

Expand All @@ -31,6 +32,7 @@ func (report TorrentReport) TableName() string {
type TorrentReportJSON struct {
ID uint `json:"id"`
Description string `json:"description"`
Message string `json:"message"`
Torrent TorrentJSON `json:"torrent"`
User UserJSON `json:"user"`
}
Expand All @@ -45,7 +47,7 @@ func (report *TorrentReport) ToJSON() TorrentReportJSON {
if report.User != nil {
u = report.User.ToJSON()
}
json := TorrentReportJSON{report.ID, report.Description, t, u}
json := TorrentReportJSON{report.ID, report.Description, report.Message, t, u}
return json
}

Expand All @@ -59,9 +61,6 @@ func TorrentReportsToJSON(reports []TorrentReport) []TorrentReportJSON {
}

// Delete : Delete torrent report
func (report *TorrentReport) Delete(definitely bool) (int, error) {
if definitely {
return 0, ORM.Unscoped().Delete(report).Error
}
return http.StatusOK, nil
func (report *TorrentReport) Delete() (int, error) {
return http.StatusOK, ORM.Unscoped().Delete(report).Error
}
3 changes: 2 additions & 1 deletion models/reports/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"errors"
)

func Create(desc string, torrent *models.Torrent, user *models.User) (*models.TorrentReport, error) {
func Create(desc string, message string, torrent *models.Torrent, user *models.User) (*models.TorrentReport, error) {
report := &models.TorrentReport{
Description: desc,
Message: message,
TorrentID: torrent.ID,
UserID: user.ID,
CreatedAt: time.Now(),
Expand Down
21 changes: 6 additions & 15 deletions models/reports/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,22 @@ type Query interface {
Prepend(string, ...interface{})
}

// Delete : Delete a torrent report by id
func Delete(id uint) (*models.TorrentReport, int, error) {
return delete(id, false)
}

// DeleteDefinitely : Delete definitely a torrent report by id
func DeleteDefinitely(id uint) (*models.TorrentReport, int, error) {
return delete(id, true)
}

func delete(id uint, definitely bool) (*models.TorrentReport, int, error) {
var torrentReport models.TorrentReport
db := models.ORM
if definitely {
db = models.ORM.Unscoped()
}
db := models.ORM.Unscoped()
if db.First(&torrentReport, id).RecordNotFound() {
return &torrentReport, http.StatusNotFound, errors.New("try_to_delete_report_inexistant")
}
if _, err := torrentReport.Delete(false); err != nil {
if _, err := torrentReport.Delete(); err != nil {
return &torrentReport, http.StatusInternalServerError, err
}
return &torrentReport, http.StatusOK, nil
}

func DeleteAll() {
models.ORM.Delete(&models.TorrentReport{})
}

func findOrderBy(parameters Query, orderBy string, limit int, offset int, countAll bool) (
torrentReports []models.TorrentReport, count int, err error,
) {
Expand Down
Loading

0 comments on commit dbb440b

Please sign in to comment.