2018-05-17 18:35:53 +03:00
|
|
|
package main
|
|
|
|
|
2018-06-08 13:57:51 +03:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
2018-05-31 16:18:52 +03:00
|
|
|
|
2018-05-24 18:08:48 +03:00
|
|
|
func getConnection(uid string) *Connection {
|
2018-05-17 18:35:53 +03:00
|
|
|
var connection Connection
|
|
|
|
orm.DB.First(&connection, "client_id = ?", uid)
|
|
|
|
|
2018-05-24 18:08:48 +03:00
|
|
|
return &connection
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
2018-05-24 18:08:48 +03:00
|
|
|
func getConnectionByURL(urlCrm string) *Connection {
|
2018-05-17 18:35:53 +03:00
|
|
|
var connection Connection
|
|
|
|
orm.DB.First(&connection, "api_url = ?", urlCrm)
|
|
|
|
|
2018-05-24 18:08:48 +03:00
|
|
|
return &connection
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) setConnectionActivity() error {
|
|
|
|
return orm.DB.Model(c).Where("client_id = ?", c.ClientID).Update("Active", c.Active).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) createConnection() error {
|
|
|
|
return orm.DB.Create(c).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) saveConnection() error {
|
2018-08-31 16:09:44 +03:00
|
|
|
return orm.DB.Save(c).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) saveConnectionByClientID() error {
|
2018-05-17 18:35:53 +03:00
|
|
|
return orm.DB.Model(c).Where("client_id = ?", c.ClientID).Update(c).Error
|
|
|
|
}
|
|
|
|
|
2018-05-28 18:16:13 +03:00
|
|
|
func (c *Connection) createBot(b Bot) error {
|
|
|
|
return orm.DB.Model(c).Association("Bots").Append(&b).Error
|
|
|
|
}
|
|
|
|
|
2018-05-31 19:44:23 +03:00
|
|
|
func getBotByToken(token string) (*Bot, error) {
|
|
|
|
var bot Bot
|
|
|
|
err := orm.DB.First(&bot, "token = ?", token).Error
|
2018-05-31 16:18:52 +03:00
|
|
|
if gorm.IsRecordNotFoundError(err) {
|
2018-05-31 19:44:23 +03:00
|
|
|
return &bot, nil
|
2018-05-31 16:18:52 +03:00
|
|
|
} else {
|
2018-05-31 19:44:23 +03:00
|
|
|
return &bot, err
|
2018-05-31 16:18:52 +03:00
|
|
|
}
|
|
|
|
|
2018-05-31 19:44:23 +03:00
|
|
|
return &bot, nil
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
2018-06-28 12:17:19 +03:00
|
|
|
func (b *Bot) deleteBot() error {
|
|
|
|
return orm.DB.Delete(b, "token = ?", b.Token).Error
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func getBotChannelByToken(token string) uint64 {
|
|
|
|
var b Bot
|
|
|
|
orm.DB.First(&b, "token = ?", token)
|
|
|
|
|
|
|
|
return b.Channel
|
|
|
|
}
|
|
|
|
|
2018-05-31 16:18:52 +03:00
|
|
|
func (c Connection) getBotsByClientID() Bots {
|
|
|
|
var b Bots
|
|
|
|
err := orm.DB.Model(c).Association("Bots").Find(&b).Error
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return b
|
2018-05-28 18:16:13 +03:00
|
|
|
}
|
|
|
|
|
2018-06-20 16:14:51 +03:00
|
|
|
func getBot(cid int, ch uint64) *Bot {
|
2018-06-09 17:54:42 +03:00
|
|
|
var bot Bot
|
2018-06-20 16:14:51 +03:00
|
|
|
orm.DB.First(&bot, "connection_id = ? AND channel = ?", cid, ch)
|
2018-06-09 17:54:42 +03:00
|
|
|
|
|
|
|
return &bot
|
|
|
|
}
|
|
|
|
|
2018-05-28 18:16:13 +03:00
|
|
|
func getConnectionById(id int) *Connection {
|
|
|
|
var connection Connection
|
|
|
|
orm.DB.First(&connection, "id = ?", id)
|
|
|
|
|
|
|
|
return &connection
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|
2018-06-04 16:48:04 +03:00
|
|
|
|
2018-06-09 17:54:42 +03:00
|
|
|
func (u *User) save() error {
|
2018-06-04 16:48:04 +03:00
|
|
|
return orm.DB.Save(u).Error
|
|
|
|
}
|
|
|
|
|
2018-06-09 17:54:42 +03:00
|
|
|
func getUserByExternalID(eid int) *User {
|
|
|
|
var user User
|
2018-06-08 17:40:50 +03:00
|
|
|
orm.DB.First(&user, "external_id = ?", eid)
|
2018-06-04 16:48:04 +03:00
|
|
|
|
|
|
|
return &user
|
|
|
|
}
|
2018-06-08 13:57:51 +03:00
|
|
|
|
|
|
|
//Expired method
|
2018-06-09 17:54:42 +03:00
|
|
|
func (u *User) Expired(updateInterval int) bool {
|
2018-06-08 13:57:51 +03:00
|
|
|
return time.Now().After(u.UpdatedAt.Add(time.Hour * time.Duration(updateInterval)))
|
|
|
|
}
|