A More Secure Internet Connection for Your Home https://fen.gg
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

182 lines
5.8 KiB

  1. package handler
  2. //
  3. // Fengg Security Gateway Server Application
  4. // Copyright (C) 2020 Lukas Matt <support@fen.gg>
  5. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. //
  18. import (
  19. "fmt"
  20. "net/http"
  21. "strconv"
  22. "strings"
  23. "tea.fen.gg/fengg/server/helper"
  24. "tea.fen.gg/fengg/server/model"
  25. "github.com/gin-gonic/gin"
  26. )
  27. /**
  28. * @api {get} /notifications/ Fetch notifications
  29. * @apiVersion 1.0.0
  30. * @apiName notificationHandler
  31. * @apiGroup notification
  32. * @apiPermission user
  33. *
  34. * @apiParam {Boolean} [reviewed] Filter for reviewed notifications. If not defined it will return reviewed and un-reviewed content.
  35. *
  36. * @apiExample Usage (curl):
  37. * curl -s http://localhost:8080/notifications/
  38. *
  39. * @apiSuccess {Number} id The notification identifier.
  40. * @apiSuccess {Date} createdAt Notification creation timestamp.
  41. * @apiSuccess {Date} updatedAt The last time the notification was updated.
  42. * @apiSuccess {Number} triggerCount How often the notification was triggered.
  43. * @apiSuccess {Number} type The notification type identifier.
  44. * @apiSuccess {Boolean} reviewed If the notification was reviewed.
  45. * @apiSuccess {String} pattern The actual rule or pattern which is responsible for the notification.
  46. *
  47. * @apiSuccessExample Response
  48. * HTTP/1.1. 200 OK
  49. * [{
  50. * "id": 316,
  51. * "createdAt": "2020-06-27T23:06:21.338453Z",
  52. * "updatedAt": "2020-07-17T10:06:46.223833Z",
  53. * "triggerCount": 202,
  54. * "type": 49,
  55. * "reviewed": true,
  56. * "pattern": "googleads.g.doubleclick.net"
  57. * }]
  58. *
  59. * @apiError {String} type Will be set to <code>error</code>.
  60. * @apiError {String} msg Contains more details about the error itself.
  61. *
  62. * @apiErrorExample Authentication error
  63. * HTTP/1.1 403 Forbidden
  64. * {
  65. * "type": "error",
  66. * "msg": "no user found for session"
  67. * }
  68. *
  69. * @apiErrorExample Server error
  70. * HTTP/1.1 500 Internal Server Error
  71. * {
  72. * "type": "error",
  73. * "msg": "too many connections"
  74. * }
  75. */
  76. func NotificationHandler(ctx *gin.Context) {
  77. var query strings.Builder
  78. reviewed, ok := ctx.GetQuery("reviewed")
  79. if ok && strings.EqualFold(reviewed, "true") {
  80. query.WriteString("WHERE reviewed = true ")
  81. } else if ok && strings.EqualFold(reviewed, "false") {
  82. query.WriteString("WHERE reviewed = false ")
  83. }
  84. query.WriteString("ORDER BY a.updated_at DESC")
  85. db, err := model.Database()
  86. if err != nil {
  87. helper.HandlerJsonError("cannot connect to database", err, ctx)
  88. return
  89. }
  90. defer db.Close()
  91. var notifications model.Notifications
  92. err = db.Select(&notifications, fmt.Sprintf(model.NotificationQueryTmpl, query.String()))
  93. if err != nil {
  94. helper.HandlerJsonError("cannot select database entries", err, ctx)
  95. return
  96. }
  97. ctx.JSON(http.StatusOK, notifications)
  98. }
  99. /**
  100. * @api {post} /notifications/:id/reviewed/:action Set notification attribute reviewed
  101. * @apiVersion 1.0.0
  102. * @apiName notificationUpdateHandler
  103. * @apiGroup notification
  104. * @apiPermission user
  105. *
  106. * @apiParam {Number} id Notification identifier
  107. * @apiParam {Boolean} action Specify either <code>true</code> or <code>false</code> as action.
  108. *
  109. * @apiExample Usage (curl):
  110. * curl -s -X POST http://localhost:8080/notifications/1/reviewed/true
  111. *
  112. * @apiSuccess {Number} id The notification identifier.
  113. * @apiSuccess {Date} createdAt Notification creation timestamp.
  114. * @apiSuccess {Date} updatedAt The last time the notification was updated.
  115. * @apiSuccess {Number} triggerCount How often the notification was triggered.
  116. * @apiSuccess {Number} type The notification type identifier.
  117. * @apiSuccess {Boolean} reviewed If the notification was reviewed.
  118. * @apiSuccess {String} pattern The actual rule or pattern which is responsible for the notification.
  119. *
  120. * @apiSuccessExample Response
  121. * HTTP/1.1. 200 OK
  122. * {
  123. * "id": 316,
  124. * "createdAt": "2020-06-27T23:06:21.338453Z",
  125. * "updatedAt": "2020-07-17T10:06:46.223833Z",
  126. * "triggerCount": 202,
  127. * "type": 49,
  128. * "reviewed": true,
  129. * "pattern": "googleads.g.doubleclick.net"
  130. * }
  131. *
  132. * @apiError {String} type Will be set to <code>error</code>.
  133. * @apiError {String} msg Contains more details about the error itself.
  134. *
  135. * @apiErrorExample Authentication error
  136. * HTTP/1.1 403 Forbidden
  137. * {
  138. * "type": "error",
  139. * "msg": "no user found for session"
  140. * }
  141. *
  142. * @apiErrorExample Server error
  143. * HTTP/1.1 500 Internal Server Error
  144. * {
  145. * "type": "error",
  146. * "msg": "too many connections"
  147. * }
  148. */
  149. func NotificationUpdateHandler(ctx *gin.Context) {
  150. id, err := strconv.Atoi(ctx.Param("id"))
  151. if err != nil {
  152. helper.HandlerJsonError("cannot convert id to integer", err, ctx)
  153. return
  154. }
  155. reviewed := ctx.Param("action") == "true"
  156. notification := model.NewNotification()
  157. notification.ID = uint(id)
  158. err = notification.FindByID()
  159. if err != nil {
  160. helper.HandlerJsonError(fmt.Sprintf(
  161. "cannot find notification by the spcified ID %d", id), err, ctx)
  162. return
  163. }
  164. notification.Reviewed = reviewed
  165. err = notification.Update()
  166. if err != nil {
  167. helper.HandlerJsonError("cannot update notification", err, ctx)
  168. return
  169. }
  170. ctx.JSON(http.StatusOK, *notification)
  171. }