2017-08-17 05:17:35 +05:30
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-08-18 20:18:29 +05:30
|
|
|
"strconv"
|
2017-08-17 05:17:35 +05:30
|
|
|
|
|
|
|
"github.com/mono83/slf/wd"
|
|
|
|
"github.com/streadway/amqp"
|
|
|
|
|
2017-08-27 20:40:03 +05:30
|
|
|
"elyby/minecraft-skinsystem/db"
|
2017-08-18 03:20:23 +05:30
|
|
|
"elyby/minecraft-skinsystem/interfaces"
|
2017-08-18 20:18:29 +05:30
|
|
|
"elyby/minecraft-skinsystem/model"
|
2017-08-17 05:17:35 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
type Services struct {
|
2017-08-18 20:18:29 +05:30
|
|
|
Channel *amqp.Channel
|
|
|
|
SkinsRepo interfaces.SkinsRepository
|
|
|
|
AccountsAPI interfaces.AccountsAPI
|
|
|
|
Logger wd.Watchdog
|
2017-08-17 05:17:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
const exchangeName string = "events"
|
|
|
|
const queueName string = "skinsystem-accounts-events"
|
|
|
|
|
|
|
|
func (service *Services) Run() error {
|
|
|
|
deliveryChannel, err := setupConsume(service.Channel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
forever := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
for d := range deliveryChannel {
|
2017-08-21 18:07:15 +05:30
|
|
|
service.HandleDelivery(&d)
|
2017-08-17 05:17:35 +05:30
|
|
|
}
|
|
|
|
}()
|
|
|
|
<-forever
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-21 18:07:15 +05:30
|
|
|
func (service *Services) HandleDelivery(delivery *amqp.Delivery) {
|
|
|
|
service.Logger.Debug("Incoming message with routing key " + delivery.RoutingKey)
|
|
|
|
var result bool = true
|
|
|
|
switch delivery.RoutingKey {
|
|
|
|
case "accounts.username-changed":
|
|
|
|
var event *model.UsernameChanged
|
|
|
|
json.Unmarshal(delivery.Body, &event)
|
|
|
|
result = service.HandleChangeUsername(event)
|
|
|
|
case "accounts.skin-changed":
|
|
|
|
var event *model.SkinChanged
|
|
|
|
json.Unmarshal(delivery.Body, &event)
|
|
|
|
result = service.HandleSkinChanged(event)
|
|
|
|
default:
|
|
|
|
service.Logger.Info("Unknown delivery with routing key " + delivery.RoutingKey)
|
|
|
|
delivery.Ack(false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if result {
|
|
|
|
delivery.Ack(false)
|
|
|
|
} else {
|
|
|
|
delivery.Reject(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 05:17:35 +05:30
|
|
|
func (service *Services) HandleChangeUsername(event *model.UsernameChanged) bool {
|
2017-08-21 18:07:15 +05:30
|
|
|
service.Logger.IncCounter("worker.change_username", 1)
|
2017-08-17 05:17:35 +05:30
|
|
|
if event.OldUsername == "" {
|
2017-08-21 18:07:15 +05:30
|
|
|
service.Logger.IncCounter("worker.change_username_empty_old_username", 1)
|
2017-08-17 05:17:35 +05:30
|
|
|
record := &model.Skin{
|
|
|
|
UserId: event.AccountId,
|
|
|
|
Username: event.NewUsername,
|
|
|
|
}
|
|
|
|
|
|
|
|
service.SkinsRepo.Save(record)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
record, err := service.SkinsRepo.FindByUserId(event.AccountId)
|
|
|
|
if err != nil {
|
2017-08-27 20:40:03 +05:30
|
|
|
service.Logger.Info("Cannot find user id :accountId. Trying to search.", wd.IntParam("accountId", event.AccountId))
|
|
|
|
if _, isSkinNotFound := err.(*db.SkinNotFoundError); !isSkinNotFound {
|
|
|
|
service.Logger.Error("Unknown error when requesting a skin from the repository: :err", wd.ErrParam(err))
|
|
|
|
}
|
|
|
|
|
2017-08-21 18:07:15 +05:30
|
|
|
service.Logger.IncCounter("worker.change_username_id_not_found", 1)
|
2017-08-18 20:18:29 +05:30
|
|
|
record = &model.Skin{
|
2017-08-21 18:07:15 +05:30
|
|
|
UserId: event.AccountId,
|
2017-08-17 05:17:35 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
record.Username = event.NewUsername
|
|
|
|
service.SkinsRepo.Save(record)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-08-21 18:07:15 +05:30
|
|
|
// TODO: возможно стоит добавить проверку на совпадение id аккаунтов
|
2017-08-17 05:17:35 +05:30
|
|
|
func (service *Services) HandleSkinChanged(event *model.SkinChanged) bool {
|
2017-08-21 18:07:15 +05:30
|
|
|
service.Logger.IncCounter("worker.skin_changed", 1)
|
|
|
|
var record *model.Skin
|
2017-08-17 05:17:35 +05:30
|
|
|
record, err := service.SkinsRepo.FindByUserId(event.AccountId)
|
|
|
|
if err != nil {
|
2017-08-27 20:40:03 +05:30
|
|
|
if _, isSkinNotFound := err.(*db.SkinNotFoundError); !isSkinNotFound {
|
|
|
|
service.Logger.Error("Unknown error when requesting a skin from the repository: :err", wd.ErrParam(err))
|
|
|
|
}
|
|
|
|
|
2017-08-21 18:07:15 +05:30
|
|
|
service.Logger.IncCounter("worker.skin_changed_id_not_found", 1)
|
2017-08-27 20:40:03 +05:30
|
|
|
service.Logger.Info("Cannot find user id :accountId. Trying to search.", wd.IntParam("accountId", event.AccountId))
|
2017-08-18 20:18:29 +05:30
|
|
|
response, err := service.AccountsAPI.AccountInfo("id", strconv.Itoa(event.AccountId))
|
2017-08-17 05:17:35 +05:30
|
|
|
if err != nil {
|
2017-08-21 18:07:15 +05:30
|
|
|
service.Logger.IncCounter("worker.skin_changed_id_not_restored", 1)
|
2017-08-27 20:40:03 +05:30
|
|
|
service.Logger.Error(
|
|
|
|
"Cannot restore user info for :accountId: :err",
|
|
|
|
wd.IntParam("accountId", event.AccountId),
|
|
|
|
wd.ErrParam(err),
|
|
|
|
)
|
|
|
|
|
2017-08-17 05:17:35 +05:30
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-08-21 18:07:15 +05:30
|
|
|
service.Logger.IncCounter("worker.skin_changed_id_restored", 1)
|
2017-08-18 20:18:29 +05:30
|
|
|
service.Logger.Info("User info successfully restored.")
|
|
|
|
|
2017-08-21 18:07:15 +05:30
|
|
|
record = &model.Skin{
|
|
|
|
UserId: response.Id,
|
|
|
|
Username: response.Username,
|
|
|
|
}
|
2017-08-17 05:17:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
record.Uuid = event.Uuid
|
|
|
|
record.SkinId = event.SkinId
|
|
|
|
record.Hash = event.Hash
|
|
|
|
record.Is1_8 = event.Is1_8
|
|
|
|
record.IsSlim = event.IsSlim
|
|
|
|
record.Url = event.Url
|
|
|
|
record.MojangTextures = event.MojangTextures
|
|
|
|
record.MojangSignature = event.MojangSignature
|
|
|
|
|
|
|
|
service.SkinsRepo.Save(record)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupConsume(channel *amqp.Channel) (<-chan amqp.Delivery, error) {
|
|
|
|
var err error
|
|
|
|
err = channel.ExchangeDeclare(
|
|
|
|
exchangeName, // name
|
|
|
|
"topic", // type
|
|
|
|
true, // durable
|
|
|
|
false, // auto-deleted
|
|
|
|
false, // internal
|
|
|
|
false, // no-wait
|
|
|
|
nil, // arguments
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = channel.QueueDeclare(
|
|
|
|
queueName, // name
|
|
|
|
true, // durable
|
|
|
|
false, // delete when usused
|
|
|
|
false, // exclusive
|
|
|
|
false, // no-wait
|
|
|
|
nil, // arguments
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = channel.QueueBind(queueName, "accounts.username-changed", exchangeName, false, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = channel.QueueBind(queueName, "accounts.skin-changed", exchangeName, false, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
deliveryChannel, err := channel.Consume(
|
|
|
|
queueName, // queue
|
|
|
|
"", // consumer
|
|
|
|
false, // auto-ack
|
|
|
|
false, // exclusive
|
|
|
|
false, // no-local
|
|
|
|
false, // no-wait
|
|
|
|
nil, // args
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return deliveryChannel, nil
|
|
|
|
}
|