Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Fix API docs [#110](https://github.com/rokwire/notifications-building-block/issues/110)

## [1.6.0] - 2022-12-06
### Added
Expand Down
13 changes: 10 additions & 3 deletions core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ type Services interface {
GetMessagesStats(orgID string, appID string, userID string) (*model.MessagesStats, error)
GetMessage(orgID string, appID string, ID string) (*model.Message, error)
GetUserMessage(orgID string, appID string, ID string, accountID string) (*model.Message, error)
CreateMessage(inputMessage model.InputMessage, sender model.Sender, async bool) (*model.Message, error)
CreateMessage(orgID string, appID string,
sender model.Sender, priority int, subject string, body string, data map[string]string,
inputRecipients []model.MessageRecipient, recipientsCriteriaList []model.RecipientCriteria,
recipientAccountCriteria map[string]interface{}, topic *string, async bool) (*model.Message, error)
UpdateMessage(userID *string, message *model.Message) (*model.Message, error)
DeleteUserMessage(orgID string, appID string, userID string, messageID string) error
DeleteMessage(orgID string, appID string, ID string) error
Expand Down Expand Up @@ -98,8 +101,12 @@ func (s *servicesImpl) GetUserMessage(orgID string, appID string, ID string, acc
return s.app.getUserMessage(orgID, appID, ID, accountID)
}

func (s *servicesImpl) CreateMessage(inputMessage model.InputMessage, sender model.Sender, async bool) (*model.Message, error) {
return s.app.createMessage(inputMessage, sender, async)
func (s *servicesImpl) CreateMessage(orgID string, appID string,
sender model.Sender, priority int, subject string, body string, data map[string]string,
inputRecipients []model.MessageRecipient, recipientsCriteriaList []model.RecipientCriteria,
recipientAccountCriteria map[string]interface{}, topic *string, async bool) (*model.Message, error) {
return s.app.createMessage(orgID, appID, sender, priority, subject, body, data,
inputRecipients, recipientsCriteriaList, recipientAccountCriteria, topic, async)
}

func (s *servicesImpl) UpdateMessage(userID *string, message *model.Message) (*model.Message, error) {
Expand Down
29 changes: 0 additions & 29 deletions core/model/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,32 +82,3 @@ type MessagesStats struct {
}

///

//InputMessage is passed by the adapters for creating a message in the core module
type InputMessage struct {
OrgID string `json:"org_id"`
AppID string `json:"app_id"`

Priority int `json:"priority"`
Subject string `json:"subject"`
Body string `json:"body"`
Data map[string]string `json:"data"`

//recipients related
Recipients []InputMessageRecipient `json:"recipients"`
RecipientsCriteriaList []InputRecipientCriteria `json:"recipients_criteria_list"`
RecipientAccountCriteria map[string]interface{} `json:"recipient_account_criteria"`
Topic *string `json:"topic"`
}

// InputMessageRecipient is passed by the adapters for creating a message in the core module
type InputMessageRecipient struct {
UserID string `json:"user_id"`
Mute bool `json:"mute"`
}

// InputRecipientCriteria is passed by the adapters for creating a message in the core module
type InputRecipientCriteria struct {
AppVersion *string `json:"app_version"`
AppPlatform *string `json:"app_platform"`
}
82 changes: 37 additions & 45 deletions core/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ func (app *Application) updateTopic(topic *model.Topic) (*model.Topic, error) {
return app.storage.UpdateTopic(topic)
}

func (app *Application) createMessage(inputMessage model.InputMessage, sender model.Sender, async bool) (*model.Message, error) {
func (app *Application) createMessage(orgID string, appID string,
sender model.Sender, priority int, subject string, body string, data map[string]string,
inputRecipients []model.MessageRecipient, recipientsCriteriaList []model.RecipientCriteria,
recipientAccountCriteria map[string]interface{}, topic *string, async bool) (*model.Message, error) {

var err error
var persistedMessage *model.Message
var recipients []model.MessageRecipient
Expand All @@ -85,29 +89,16 @@ func (app *Application) createMessage(inputMessage model.InputMessage, sender mo
messageID := uuid.NewString()

//calculate the recipients
recipients, err = app.calculateRecipients(context, inputMessage, messageID)
recipients, err = app.calculateRecipients(context, orgID, appID,
subject, body, inputRecipients, recipientsCriteriaList,
recipientAccountCriteria, topic, messageID)
if err != nil {
fmt.Printf("error on calculating recipients for a message: %s", err)
return err
}

//create message object
orgID := inputMessage.OrgID
appID := inputMessage.AppID

priority := inputMessage.Priority
subject := inputMessage.Subject
body := inputMessage.Body
data := inputMessage.Data
calculatedRecipients := len(recipients)
var recipientsCriteriaList []model.RecipientCriteria
if len(inputMessage.RecipientsCriteriaList) > 0 {
recipientsCriteriaList = make([]model.RecipientCriteria, len(inputMessage.RecipientsCriteriaList))
for i, item := range inputMessage.RecipientsCriteriaList {
recipientsCriteriaList[i] = model.RecipientCriteria{AppVersion: item.AppVersion, AppPlatform: item.AppPlatform}
}
}
topic := inputMessage.Topic
dateCreated := time.Now()
message := model.Message{OrgID: orgID, AppID: appID, ID: messageID, Priority: priority,
Subject: subject, Sender: sender, Body: body, Data: data, RecipientsCriteriaList: recipientsCriteriaList,
Expand Down Expand Up @@ -185,38 +176,44 @@ func (app *Application) sendMessage(allRecipients []model.MessageRecipient, mess
}

func (app *Application) calculateRecipients(context storage.TransactionContext,
inputMessage model.InputMessage, messageID string) ([]model.MessageRecipient, error) {
orgID string, appID string,
subject string, body string,
recipients []model.MessageRecipient, recipientsCriteriaList []model.RecipientCriteria,
recipientAccountCriteria map[string]interface{}, topic *string, messageID string) ([]model.MessageRecipient, error) {

messageRecipients := []model.MessageRecipient{}
checkCriteria := true

// recipients from message
if len(inputMessage.Recipients) > 0 {
list := make([]model.MessageRecipient, len(inputMessage.Recipients))
for i, item := range inputMessage.Recipients {
cItem := model.MessageRecipient{OrgID: inputMessage.OrgID, AppID: inputMessage.AppID,
ID: uuid.NewString(), UserID: item.UserID,
MessageID: messageID, Mute: item.Mute, Read: false}
list[i] = cItem
if len(recipients) > 0 {
list := make([]model.MessageRecipient, len(recipients))
for i, item := range recipients {
item.OrgID = orgID
item.AppID = appID
item.ID = uuid.NewString()
item.MessageID = messageID
item.Read = false

list[i] = item
}

messageRecipients = append(messageRecipients, list...)
}

// recipients from topic
if inputMessage.Topic != nil {
topicUsers, err := app.storage.GetUsersByTopicWithContext(context, inputMessage.OrgID,
inputMessage.AppID, *inputMessage.Topic)
if topic != nil {
topicUsers, err := app.storage.GetUsersByTopicWithContext(context, orgID,
appID, *topic)
if err != nil {
fmt.Printf("error retrieving recipients by topic (%s): %s", *inputMessage.Topic, err)
fmt.Printf("error retrieving recipients by topic (%s): %s", *topic, err)
return nil, err
}
log.Printf("retrieve recipients (%+v) for topic (%s)", topicUsers, *inputMessage.Topic)
log.Printf("retrieve recipients (%+v) for topic (%s)", topicUsers, *topic)

topicRecipients := make([]model.MessageRecipient, len(topicUsers))
for i, item := range topicUsers {
topicRecipients[i] = model.MessageRecipient{
OrgID: inputMessage.OrgID, AppID: inputMessage.AppID,
OrgID: orgID, AppID: appID,
ID: uuid.NewString(), UserID: item.UserID, MessageID: messageID,
}
}
Expand All @@ -233,18 +230,13 @@ func (app *Application) calculateRecipients(context storage.TransactionContext,
}

log.Printf("construct recipients (%+v) for message (%s:%s:%s)",
messageRecipients, messageID, inputMessage.Subject, inputMessage.Body)
messageRecipients, messageID, subject, body)
}

// recipients from criteria
if (inputMessage.RecipientsCriteriaList != nil) && checkCriteria {
criteriaList := make([]model.RecipientCriteria, len(inputMessage.RecipientsCriteriaList))
for i, item := range inputMessage.RecipientsCriteriaList {
criteriaList[i] = model.RecipientCriteria{AppVersion: item.AppVersion, AppPlatform: item.AppPlatform}
}

if (recipientsCriteriaList != nil && len(recipientAccountCriteria) > 0) && checkCriteria {
criteriaUsers, err := app.storage.GetUsersByRecipientCriteriasWithContext(context,
inputMessage.OrgID, inputMessage.AppID, criteriaList)
orgID, appID, recipientsCriteriaList)
if err != nil {
fmt.Printf("error retrieving recipients by criteria: %s", err)
return nil, err
Expand All @@ -253,7 +245,7 @@ func (app *Application) calculateRecipients(context storage.TransactionContext,
criteriaRecipients := make([]model.MessageRecipient, len(criteriaUsers))
for i, item := range criteriaUsers {
criteriaRecipients[i] = model.MessageRecipient{
OrgID: inputMessage.OrgID, AppID: inputMessage.AppID,
OrgID: orgID, AppID: appID,
ID: uuid.NewString(), UserID: item.UserID, MessageID: messageID,
}
}
Expand All @@ -268,20 +260,20 @@ func (app *Application) calculateRecipients(context storage.TransactionContext,
messageRecipients = nil
}
log.Printf("construct message criteria recipients (%+v) for message (%s:%s:%s)",
messageRecipients, messageID, inputMessage.Subject, inputMessage.Body)
messageRecipients, messageID, subject, body)
}

// recipients from account criteria
if len(inputMessage.RecipientAccountCriteria) > 0 {
accounts, err := app.core.RetrieveCoreUserAccountByCriteria(inputMessage.RecipientAccountCriteria,
&inputMessage.AppID, &inputMessage.OrgID)
if len(recipientAccountCriteria) > 0 {
accounts, err := app.core.RetrieveCoreUserAccountByCriteria(recipientAccountCriteria,
&appID, &orgID)
if err != nil {
fmt.Printf("error retrieving recipients by account criteria: %s", err)
}

for _, account := range accounts {
messageRecipient := model.MessageRecipient{
OrgID: inputMessage.OrgID, AppID: inputMessage.AppID,
OrgID: orgID, AppID: appID,
ID: uuid.NewString(), UserID: account.ID, MessageID: messageID,
}

Expand Down
26 changes: 13 additions & 13 deletions driver/web/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"net/http"
"notifications/core"
"notifications/core/model"
"notifications/driver/web/rest"
"os"
"time"

Expand All @@ -39,17 +38,18 @@ import (

// Adapter entity
type Adapter struct {
host string
port string
host string
port string
notificationsServiceURL string

auth *Auth

cachedYamlDoc []byte

apisHandler rest.ApisHandler
adminApisHandler rest.AdminApisHandler
internalApisHandler rest.InternalApisHandler
bbsApisHandler rest.BBsAPIsHandler
apisHandler ApisHandler
adminApisHandler AdminApisHandler
internalApisHandler InternalApisHandler
bbsApisHandler BBsAPIsHandler

app *core.Application

Expand Down Expand Up @@ -132,7 +132,7 @@ func (we Adapter) serveDoc(w http.ResponseWriter, r *http.Request) {
}

func (we Adapter) serveDocUI() http.Handler {
url := fmt.Sprintf("%s/doc", we.host)
url := fmt.Sprintf("%s/doc", we.notificationsServiceURL)
return httpSwagger.Handler(httpSwagger.URL(url))
}

Expand Down Expand Up @@ -206,13 +206,13 @@ func NewWebAdapter(host string, port string, app *core.Application, config *mode
logger.Fatalf("error creating auth - %s", err.Error())
}

apisHandler := rest.NewApisHandler(app)
adminApisHandler := rest.NewAdminApisHandler(app)
internalApisHandler := rest.NewInternalApisHandler(app)
bbsApisHandler := rest.NewBBsAPIsHandler(app)
apisHandler := NewApisHandler(app)
adminApisHandler := NewAdminApisHandler(app)
internalApisHandler := NewInternalApisHandler(app)
bbsApisHandler := NewBBsAPIsHandler(app)
return Adapter{host: host, port: port, cachedYamlDoc: yamlDoc, auth: auth, apisHandler: apisHandler,
adminApisHandler: adminApisHandler, internalApisHandler: internalApisHandler, bbsApisHandler: bbsApisHandler,
app: app, logger: logger}
app: app, logger: logger, notificationsServiceURL: config.NotificationsServiceURL}
}

// AppListener implements core.ApplicationListener interface
Expand Down
27 changes: 22 additions & 5 deletions driver/web/rest/adminapis.go → driver/web/apis_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package rest
package web

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"notifications/core"
"notifications/core/model"
Expand All @@ -25,6 +26,8 @@ import (
"github.com/rokwire/logging-library-go/v2/logs"
"github.com/rokwire/logging-library-go/v2/logutils"

Def "notifications/driver/web/docs/gen"

"github.com/gorilla/mux"
)

Expand Down Expand Up @@ -142,18 +145,32 @@ func (h AdminApisHandler) GetMessages(l *logs.Log, r *http.Request, claims *toke
// @Security AdminUserAuth
// @Router /admin/message [post]
func (h AdminApisHandler) CreateMessage(l *logs.Log, r *http.Request, claims *tokenauth.Claims) logs.HTTPResponse {
var inputMessage *model.InputMessage
var inputMessage Def.SharedReqCreateMessage
err := json.NewDecoder(r.Body).Decode(&inputMessage)
if err != nil {
return l.HTTPResponseErrorAction(logutils.ActionDecode, logutils.TypeRequestBody, nil, err, http.StatusBadRequest, true)
}

inputMessage.OrgID = claims.OrgID
inputMessage.AppID = claims.AppID
orgID := claims.OrgID
appID := claims.AppID

priority := inputMessage.Priority
subject := inputMessage.Subject
body := inputMessage.Body
inputData := make(map[string]string, len(inputMessage.Data))
for key, value := range inputMessage.Data {
inputData[key] = fmt.Sprintf("%v", value)
}
inputRecipients := messagesRecipientsListFromDef(inputMessage.Recipients)
recipientsCriteria := recipientsCriteriaListFromDef(inputMessage.RecipientsCriteriaList)
recipientsAccountCriteria := inputMessage.RecipientAccountCriteria
topic := inputMessage.Topic

sender := model.Sender{Type: "user", User: &model.CoreAccountRef{UserID: claims.Subject, Name: claims.Name}}

message, err := h.app.Services.CreateMessage(*inputMessage, sender, false)
message, err := h.app.Services.CreateMessage(orgID, appID,
sender, priority, subject, body, inputData, inputRecipients, recipientsCriteria,
recipientsAccountCriteria, topic, false)
if err != nil {
return l.HTTPResponseErrorAction(logutils.ActionCreate, "message", nil, err, http.StatusInternalServerError, true)
}
Expand Down
Loading