2019-07-13 09:44:31 +00:00
|
|
|
package discord
|
|
|
|
|
|
|
|
import (
|
|
|
|
"dorisbot/models"
|
2019-07-14 07:04:14 +00:00
|
|
|
pubmodel "dorisbot/models/public"
|
2019-07-13 09:44:31 +00:00
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-08-26 09:45:17 +00:00
|
|
|
var nerr = errors.New
|
|
|
|
|
2019-07-13 09:44:31 +00:00
|
|
|
// ChannelModel -
|
|
|
|
type ChannelModel struct {
|
|
|
|
ID string `db:"id"`
|
|
|
|
Server string `db:"server"`
|
|
|
|
EnableCMD bool `db:"enable_cmd"`
|
|
|
|
EnableNotify bool `db:"enable_notify"`
|
|
|
|
Ctime time.Time `db:"ctime"`
|
|
|
|
Mtime time.Time `db:"mtime"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewChannel -
|
|
|
|
func NewChannel(id, server string, enableCMD, enableNotify bool) (ch *ChannelModel, err error) {
|
|
|
|
if len(id) == 0 || len(server) == 0 {
|
|
|
|
return nil, errors.New("id or server is empty")
|
|
|
|
}
|
|
|
|
ch = &ChannelModel{
|
|
|
|
ID: id,
|
|
|
|
Server: server,
|
|
|
|
EnableCMD: enableCMD,
|
|
|
|
EnableNotify: enableNotify,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-14 07:04:14 +00:00
|
|
|
// GetChannelByID -
|
|
|
|
func GetChannelByID(id string) (ch *ChannelModel, err error) {
|
|
|
|
if len(id) == 0 {
|
|
|
|
return nil, errors.New("id is empty")
|
|
|
|
}
|
|
|
|
ch = &ChannelModel{}
|
|
|
|
db := models.GetConn()
|
2019-08-25 11:27:00 +00:00
|
|
|
query := `select id, server, enable_cmd, enable_notify, ctime, mtime from "discord"."channel" where id = $1`
|
2019-07-14 07:04:14 +00:00
|
|
|
err = db.Get(ch, query, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetChannelListByServer -
|
|
|
|
func GetChannelListByServer(server string) (list []*ChannelModel, err error) {
|
|
|
|
if len(server) == 0 {
|
|
|
|
return nil, errors.New("server is empty")
|
|
|
|
}
|
|
|
|
db := models.GetConn()
|
2019-08-25 11:27:00 +00:00
|
|
|
query := `select id, server, enable_cmd, enable_notify, ctime, mtime from "discord"."channel" where server = $1`
|
2019-07-14 07:04:14 +00:00
|
|
|
err = db.Select(&list, query, server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-13 09:44:31 +00:00
|
|
|
// Write -
|
|
|
|
func (p *ChannelModel) Write() (err error) {
|
|
|
|
db := models.GetConn()
|
|
|
|
query := `insert into "discord"."channel" (id, server, enable_cmd, enable_notify) values
|
|
|
|
(:id, :server, :enable_cmd, :enable_notify) returning *`
|
|
|
|
rows, err := db.NamedQuery(query, p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
if rows.Next() {
|
|
|
|
err = rows.StructScan(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCMDState -
|
|
|
|
func (p *ChannelModel) SetCMDState(state bool) (err error) {
|
|
|
|
if state == p.EnableCMD {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
db := models.GetConn()
|
|
|
|
query := `update "discord"."channel" set enable_cmd = $1, mtime = now() where id = $2`
|
|
|
|
_, err = db.Exec(query, state, p.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.EnableCMD = state
|
|
|
|
return
|
|
|
|
}
|
2019-07-14 07:04:14 +00:00
|
|
|
|
|
|
|
// SetNotifyState -
|
|
|
|
func (p *ChannelModel) SetNotifyState(state bool) (err error) {
|
|
|
|
if state == p.EnableNotify {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
db := models.GetConn()
|
|
|
|
query := `update "discord"."channel" set enable_notify = $1, mtime = now() where id = $2`
|
|
|
|
_, err = db.Exec(query, state, p.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.EnableNotify = state
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFacebookPageList -
|
2019-07-21 01:41:06 +00:00
|
|
|
func (p *ChannelModel) GetFacebookPageList() (pages []*pubmodel.FacebookPageModel, err error) {
|
|
|
|
return pubmodel.GetFacebookPageListByChannel(p.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFacebookPageByID -
|
|
|
|
func (p *ChannelModel) GetFacebookPageByID(id string) (page *pubmodel.FacebookPageModel, err error) {
|
|
|
|
return pubmodel.GetFacebookPageByChannelAndID(id, p.ID)
|
2019-07-14 07:04:14 +00:00
|
|
|
}
|
2019-07-22 12:54:25 +00:00
|
|
|
|
|
|
|
// GetFacebookPageTmpl -
|
|
|
|
func (p *ChannelModel) GetFacebookPageTmpl(fbid string) (tmpl string, err error) {
|
|
|
|
if len(fbid) == 0 {
|
|
|
|
return "", errors.New("id is empty")
|
|
|
|
}
|
|
|
|
rt, err := GetFacebookRT(p.ID, fbid)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-08-26 09:45:17 +00:00
|
|
|
if rt == nil {
|
|
|
|
return "", errors.New("rt not found")
|
|
|
|
}
|
2019-07-22 12:54:25 +00:00
|
|
|
tmpl = rt.Tmpl
|
|
|
|
rt = nil
|
|
|
|
return tmpl, nil
|
|
|
|
}
|
2019-08-20 09:29:06 +00:00
|
|
|
|
|
|
|
// DeleteFacebookPage -
|
|
|
|
func (p *ChannelModel) DeleteFacebookPage(fbid string) (err error) {
|
|
|
|
return DeleteFacebookRT(p.ID, fbid)
|
|
|
|
}
|
2019-08-26 09:45:17 +00:00
|
|
|
|
|
|
|
// GetInstagramByID -
|
|
|
|
func (p *ChannelModel) GetInstagramByID(id string) (ig *pubmodel.InstagramModel, err error) {
|
|
|
|
return pubmodel.GetInstagramByChannelAndID(id, p.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetInstagramTmpl -
|
|
|
|
func (p *ChannelModel) GetInstagramTmpl(id string) (tmpl string, err error) {
|
|
|
|
if len(id) == 0 {
|
|
|
|
return "", nerr("id is empty")
|
|
|
|
}
|
|
|
|
rt, err := GetInstagramRT(p.ID, id)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if rt == nil {
|
|
|
|
return "", nerr("rt not found")
|
|
|
|
}
|
|
|
|
tmpl = rt.Tmpl
|
|
|
|
rt = nil
|
|
|
|
return tmpl, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteInstagram -
|
|
|
|
func (p *ChannelModel) DeleteInstagram(id string) (err error) {
|
|
|
|
return DeleteInstagramRT(p.ID, id)
|
|
|
|
}
|