This commit is contained in:
Jay 2019-07-22 20:54:25 +08:00
parent 4f0b650ac2
commit c7032bde6f
4 changed files with 65 additions and 3 deletions

View File

@ -119,3 +119,17 @@ func (p *ChannelModel) GetFacebookPageList() (pages []*pubmodel.FacebookPageMode
func (p *ChannelModel) GetFacebookPageByID(id string) (page *pubmodel.FacebookPageModel, err error) { func (p *ChannelModel) GetFacebookPageByID(id string) (page *pubmodel.FacebookPageModel, err error) {
return pubmodel.GetFacebookPageByChannelAndID(id, p.ID) 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
}
tmpl = rt.Tmpl
rt = nil
return tmpl, nil
}

View File

@ -69,3 +69,21 @@ func GetFacebookRT(channel, facebook string) (rt *ChannelFacebookRT, err error)
} }
return return
} }
// Write -
func (p *ChannelFacebookRT) Write() (err error) {
db := models.GetConn()
query := `insert into discord.channel_facebook_rt ("channel", "facebook", "tmpl") values (:channel, :facebook, :tmpl) returning *`
row, err := db.NamedQuery(query, p)
if err != nil {
return err
}
defer row.Close()
if row.Next() {
err = row.StructScan(p)
if err != nil {
return err
}
}
return
}

View File

@ -35,10 +35,10 @@ func GetFacebookPageByID(id string) (page *FacebookPageModel, err error) {
db := models.GetConn() db := models.GetConn()
query := `select id, lastpost, ctime, mtime from public.facebook_page where id = $1` query := `select id, lastpost, ctime, mtime from public.facebook_page where id = $1`
err = db.Get(page, query, id) err = db.Get(page, query, id)
if err != nil { if err != nil && err != sql.ErrNoRows {
return return
} }
return return page, nil
} }
// GetFacebookPageListByChannel - // GetFacebookPageListByChannel -
@ -64,7 +64,7 @@ func GetFacebookPageByChannelAndID(id, channel string) (page *FacebookPageModel,
page = &FacebookPageModel{} page = &FacebookPageModel{}
query := `select id, lastpost, ctime, mtime from public.facebook_page where id = $1 and channel = $2` query := `select id, lastpost, ctime, mtime from public.facebook_page where id = $1 and channel = $2`
err = db.Get(page, query, channel) err = db.Get(page, query, channel)
if err != nil { if err != nil && err != sql.ErrNoRows {
return nil, err return nil, err
} }
return return

View File

@ -2,6 +2,7 @@ package discord
import ( import (
dsmodel "dorisbot/models/discord" dsmodel "dorisbot/models/discord"
pubmodel "dorisbot/models/public"
"log" "log"
) )
@ -39,6 +40,35 @@ func addServer(info EvtInfo, name string) string {
} }
func addFacebookPage(info EvtInfo, fbid, tmpl string) string { func addFacebookPage(info EvtInfo, fbid, tmpl string) string {
ch, err := dsmodel.GetChannelByID(info.Channel)
if err != nil {
return ""
}
var page *pubmodel.FacebookPageModel
page, err = pubmodel.GetFacebookPageByID(fbid)
if err != nil {
return "check facebook fail"
}
if page == nil {
// TODO: write facebook page
page, err = pubmodel.NewFacebookPage(fbid)
if err != nil {
return "write data fail"
}
err = page.Write()
if err != nil {
return "write data fail"
}
} else {
fbrt, err := ch.GetFacebookPageByID(fbid)
if err != nil {
return "check data fail"
}
if fbrt != nil {
return "facebook page already linked"
}
}
return "" return ""
} }