173 lines
4.0 KiB
Go
173 lines
4.0 KiB
Go
package discord
|
|
|
|
import (
|
|
"dorisbot/models"
|
|
pubmodel "dorisbot/models/public"
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
var nerr = errors.New
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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()
|
|
query := `select id, server, enable_cmd, enable_notify, ctime, mtime from "discord"."channel" where id = $1`
|
|
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()
|
|
query := `select id, server, enable_cmd, enable_notify, ctime, mtime from "discord"."channel" where server = $1`
|
|
err = db.Select(&list, query, server)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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 -
|
|
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)
|
|
}
|
|
|
|
// 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
|
|
}
|
|
if rt == nil {
|
|
return "", errors.New("rt not found")
|
|
}
|
|
tmpl = rt.Tmpl
|
|
rt = nil
|
|
return tmpl, nil
|
|
}
|
|
|
|
// DeleteFacebookPage -
|
|
func (p *ChannelModel) DeleteFacebookPage(fbid string) (err error) {
|
|
return DeleteFacebookRT(p.ID, fbid)
|
|
}
|
|
|
|
// 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)
|
|
}
|