mirror of
https://github.com/pushbits/server.git
synced 2025-05-30 01:06:34 +02:00
Initialize repository
This commit is contained in:
commit
1d758fcfd0
28 changed files with 1107 additions and 0 deletions
63
api/application.go
Normal file
63
api/application.go
Normal file
|
@ -0,0 +1,63 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/eikendev/pushbits/authentication"
|
||||
"github.com/eikendev/pushbits/model"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// The ApplicationDatabase interface for encapsulating database access.
|
||||
type ApplicationDatabase interface {
|
||||
CreateApplication(application *model.Application) error
|
||||
GetApplicationByToken(token string) (*model.Application, error)
|
||||
}
|
||||
|
||||
// The ApplicationDispatcher interface for relaying notifications.
|
||||
type ApplicationDispatcher interface {
|
||||
RegisterApplication(name, user string) (string, error)
|
||||
}
|
||||
|
||||
// ApplicationHandler holds information for processing requests about applications.
|
||||
type ApplicationHandler struct {
|
||||
DB ApplicationDatabase
|
||||
Dispatcher ApplicationDispatcher
|
||||
}
|
||||
|
||||
func (h *ApplicationHandler) applicationExists(token string) bool {
|
||||
application, _ := h.DB.GetApplicationByToken(token)
|
||||
return application != nil
|
||||
}
|
||||
|
||||
// CreateApplication is used to create a new user.
|
||||
func (h *ApplicationHandler) CreateApplication(ctx *gin.Context) {
|
||||
application := model.Application{}
|
||||
|
||||
if success := successOrAbort(ctx, http.StatusBadRequest, ctx.Bind(&application)); !success {
|
||||
return
|
||||
}
|
||||
|
||||
user := authentication.GetUser(ctx)
|
||||
|
||||
application.Token = authentication.GenerateNotExistingToken(authentication.GenerateApplicationToken, h.applicationExists)
|
||||
application.UserID = user.ID
|
||||
|
||||
log.Printf("User %s will receive notifications for application %s.\n", user.Name, application.Name)
|
||||
|
||||
matrixid, err := h.Dispatcher.RegisterApplication(application.Name, user.MatrixID)
|
||||
|
||||
if success := successOrAbort(ctx, http.StatusInternalServerError, err); !success {
|
||||
return
|
||||
}
|
||||
|
||||
application.MatrixID = matrixid
|
||||
|
||||
if success := successOrAbort(ctx, http.StatusInternalServerError, h.DB.CreateApplication(&application)); !success {
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, &application)
|
||||
}
|
53
api/notification.go
Normal file
53
api/notification.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/eikendev/pushbits/authentication"
|
||||
"github.com/eikendev/pushbits/model"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// The NotificationDatabase interface for encapsulating database access.
|
||||
type NotificationDatabase interface {
|
||||
}
|
||||
|
||||
// The NotificationDispatcher interface for relaying notifications.
|
||||
type NotificationDispatcher interface {
|
||||
SendNotification(a *model.Application, n *model.Notification) error
|
||||
}
|
||||
|
||||
// NotificationHandler holds information for processing requests about notifications.
|
||||
type NotificationHandler struct {
|
||||
DB NotificationDatabase
|
||||
Dispatcher NotificationDispatcher
|
||||
}
|
||||
|
||||
// CreateNotification is used to create a new notification for a user.
|
||||
func (h *NotificationHandler) CreateNotification(ctx *gin.Context) {
|
||||
notification := model.Notification{}
|
||||
|
||||
if success := successOrAbort(ctx, http.StatusBadRequest, ctx.Bind(¬ification)); !success {
|
||||
return
|
||||
}
|
||||
|
||||
application := authentication.GetApplication(ctx)
|
||||
log.Printf("Sending notification for application %s.\n", application.Name)
|
||||
|
||||
notification.ID = 0
|
||||
notification.ApplicationID = application.ID
|
||||
if strings.TrimSpace(notification.Title) == "" {
|
||||
notification.Title = application.Name
|
||||
}
|
||||
notification.Date = time.Now()
|
||||
|
||||
if success := successOrAbort(ctx, http.StatusInternalServerError, h.Dispatcher.SendNotification(application, ¬ification)); !success {
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, ¬ification)
|
||||
}
|
48
api/user.go
Normal file
48
api/user.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/eikendev/pushbits/model"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// The UserDatabase interface for encapsulating database access.
|
||||
type UserDatabase interface {
|
||||
CreateUser(user *model.User) error
|
||||
GetUserByName(name string) (*model.User, error)
|
||||
}
|
||||
|
||||
// UserHandler holds information for processing requests about users.
|
||||
type UserHandler struct {
|
||||
DB UserDatabase
|
||||
}
|
||||
|
||||
func (h *UserHandler) userExists(name string) bool {
|
||||
user, _ := h.DB.GetUserByName(name)
|
||||
return user != nil
|
||||
}
|
||||
|
||||
// CreateUser creates a new user.
|
||||
func (h *UserHandler) CreateUser(ctx *gin.Context) {
|
||||
externalUser := model.ExternalUserWithCredentials{}
|
||||
|
||||
if success := successOrAbort(ctx, http.StatusBadRequest, ctx.Bind(&externalUser)); !success {
|
||||
return
|
||||
}
|
||||
|
||||
user := externalUser.IntoInternalUser()
|
||||
|
||||
if h.userExists(user.Name) {
|
||||
ctx.AbortWithError(http.StatusBadRequest, errors.New("username already exists"))
|
||||
return
|
||||
}
|
||||
|
||||
if success := successOrAbort(ctx, http.StatusInternalServerError, h.DB.CreateUser(user)); !success {
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, user.IntoExternalUser())
|
||||
}
|
13
api/util.go
Normal file
13
api/util.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func successOrAbort(ctx *gin.Context, code int, err error) bool {
|
||||
if err != nil {
|
||||
ctx.AbortWithError(code, err)
|
||||
}
|
||||
|
||||
return err == nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue