diff --git a/go.sum b/go.sum index f6ca6b1..1a3ae6f 100644 --- a/go.sum +++ b/go.sum @@ -110,6 +110,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190329044733-9eb1bfa1ce65 h1:hOY+O8MxdkPV10pNf7/XEHaySCiPKxixMKUshfHsGn0= golang.org/x/sys v0.0.0-20190329044733-9eb1bfa1ce65/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190402142545-baf5eb976a8c h1:3xiKTkef8QqBJ8q+4fVUDMRoxnI0H/MVNFswa+aExbo= golang.org/x/sys v0.0.0-20190402142545-baf5eb976a8c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= google.golang.org/appengine v1.5.0 h1:KxkO13IPW4Lslp2bz+KHP2E3gtFlrIGNThxkZQ3g+4c= diff --git a/model/instagram.go b/model/instagram.go new file mode 100644 index 0000000..8af8164 --- /dev/null +++ b/model/instagram.go @@ -0,0 +1,74 @@ +package model + +import ( + "database/sql" + "time" +) + +type IGGroup struct { + *LineGroup + Tmpl string `db:"tmpl"` +} + +//Instagram - +type Instagram struct { + ID string `db:"id" cc:"id"` + LastPost string `db:"lastpost" cc:"lastpost"` + Ctime time.Time `db:"ctime" cc:"ctime"` + Mtime time.Time `db:"mtime" cc:"ctime"` + Groups []*IGGroup `db:"-"` +} + +// GetAllInstagram - +func GetAllInstagram() (igs []*Instagram, err error) { + err = x.Select(&igs, `select * from "public"."instagram"`) + if err != nil { + return nil, err + } + return +} + +// GetInstagram - +func GetInstagram(id string) (ig *Instagram, err error) { + ig = &Instagram{} + err = x.Get(ig, `select * from "public"."instagram" where "id" = $1`, id) + if err == sql.ErrNoRows { + return nil, nil + } + return +} + +// AddIG - +func (p *Instagram) AddIG() (err error) { + stmt, err := x.PrepareNamed(`insert into "public"."instagram" ("id", "lastpost") values (:id, :lastpost) returning *`) + if err != nil { + return err + } + err = stmt.Get(p, p) + return +} + +// UpdatePost - +func (p *Instagram) UpdatePost(postID string) (err error) { + query := `update "public"."instagram" set "lastpost" = $1, "mtime" = now() where "id" = $2` + _, err = x.Exec(query, postID, p.ID) + if err != nil { + return + } + p.LastPost = postID + return +} + +// GetGroups - +func (p *Instagram) GetGroups() (err error) { + query := `select g.*, rt.tmpl as tmpl from "public"."facebook_page" p + left join "public"."line_fb_rt" rt + on rt."facebook" = p.id + left join "public"."line_group" g + on g.id = rt."line" + where + p.id = $1 + and rt.facebook is not null` + err = x.Select(&p.Groups, query, p.ID) + return +} diff --git a/model/rt_table.go b/model/rt_table.go index 14d1948..77f87ea 100644 --- a/model/rt_table.go +++ b/model/rt_table.go @@ -19,6 +19,12 @@ type LineYoutubeRT struct { Tmpl string `db:"tmpl" cc:"tmpl"` } +type LineIGRT struct { + Line string `db:"line" cc:"line"` + IG string `db:"ig" cc:"ig"` + Tmpl string `db:"tmpl" cc:"tmpl"` +} + // AddRT - add facebook line rt func (p *LineFacebookRT) AddRT() (err error) { _, err = x.NamedExec(`insert into "public"."line_fb_rt" ("line", "facebook", "tmpl") values (:line, :facebook, :tmpl)`, p) @@ -64,3 +70,15 @@ func (p *LineYoutubeRT) DelRT() (err error) { _, err = x.NamedExec(`delete from "public"."line_youtube_rt" where "line" = :line and "youtube" = :youtube`, p) return } + +// AddRT - +func (p *LineIGRT) AddRT() (err error) { + _, err = x.NamedExec(`insert into "public"."line_ig_rt" ("line", "ig", "tmpl") values (:line, :ig, :tmpl)`, p) + return +} + +// DelRT - +func (p *LineIGRT) DelRT() (err error) { + _, err = x.NamedExec(`delete from "public"."line_ig_rt" where "line" = :line and "ig" = :ig`, p) + return +} diff --git a/router/private/private.go b/router/private/private.go index 3ddd349..ec8ff3b 100644 --- a/router/private/private.go +++ b/router/private/private.go @@ -46,6 +46,24 @@ func GetFacebookPageIDs(c *context.Context) { }) } +//GetInstagramIDs - +func GetInstagramIDs(c *context.Context) { + igs, err := model.GetAllInstagram() + if err != nil { + c.ServerError(nil) + return + } + + ids := make([]string, 0, len(igs)) + for _, v := range igs { + ids = append(ids, v.ID) + } + + c.Success(map[string]interface{}{ + "list": ids, + }) +} + // UpdateFacebookPagePost - func UpdateFacebookPagePost(c *context.Context) { var err error @@ -106,3 +124,63 @@ func UpdateFacebookPagePost(c *context.Context) { c.Success(nil) } + +// UpdateInstagramPost - +func UpdateInstagramPost(c *context.Context) { + var err error + type pageStruct struct { + ID string `json:"id"` + PostID string `json:"post_id"` + Link string `json:"link"` + Text string `json:"text"` + } + bodyArg := struct { + IGs []pageStruct `json:"igs"` + }{} + + err = c.BindData(&bodyArg) + if err != nil { + c.DataFormat(nil) + return + } + + for _, v := range bodyArg.IGs { + if len(v.ID) == 0 || len(v.PostID) == 0 || len(v.Link) == 0 { + continue + } + + ig, err := model.GetInstagram(v.ID) + if err != nil { + continue + } + if ig.LastPost == v.PostID { + continue + } + err = ig.UpdatePost(v.PostID) + if err != nil { + continue + } + + err = ig.GetGroups() + if err != nil { + continue + } + + for _, g := range ig.Groups { + if g.Notify { + tmpl := g.Tmpl + if len(tmpl) > 0 { + tmpl = strings.Replace(tmpl, "{link}", v.Link, -1) + tmpl = strings.Replace(tmpl, "{txt}", v.Text, -1) + } else { + tmpl = fmt.Sprintf("%s\n%s", v.Text, v.Link) + } + + msg := line.TextMessage{Text: tmpl} + line.PushMessage(g.ID, msg) + } + } + } + + c.Success(nil) +} diff --git a/router/routes/routes.go b/router/routes/routes.go index b49362c..fb1bfa0 100644 --- a/router/routes/routes.go +++ b/router/routes/routes.go @@ -91,6 +91,8 @@ func SetRoutes(r *gin.Engine) { { privateAPIGroup.GET("/pages", context.PatchCtx(private.GetFacebookPageIDs)) privateAPIGroup.POST("/pageposts", context.PatchCtx(private.UpdateFacebookPagePost)) + privateAPIGroup.GET("/ig", context.PatchCtx(private.GetInstagramIDs)) + privateAPIGroup.POST("/igposts", context.PatchCtx(private.UpdateInstagramPost)) } apiTwitchGroup := apiGroup.Group("/twitch", context.PatchCtx(api.CheckSession))