update line user name

This commit is contained in:
Jay 2019-05-29 09:50:50 +08:00
parent 93149fd39b
commit d2dcbdff71
2 changed files with 20 additions and 0 deletions

View File

@ -28,3 +28,9 @@ func (p *LineUser) Add() (err error) {
_, err = x.NamedExec(`insert into "public"."line_user" ("id", "name") values (:id, :name)`, p) _, err = x.NamedExec(`insert into "public"."line_user" ("id", "name") values (:id, :name)`, p)
return return
} }
// UpdateName -
func (p *LineUser) UpdateName() (err error) {
_, err = x.NamedExec(`update "public"."line_user" set "name" = :name, "mtime" = now() where "id" = :id`, p)
return
}

View File

@ -6,6 +6,7 @@ import (
"net/url" "net/url"
"os" "os"
"path" "path"
"time"
"git.trj.tw/golang/mtfosbot/model" "git.trj.tw/golang/mtfosbot/model"
"git.trj.tw/golang/mtfosbot/module/apis/line" "git.trj.tw/golang/mtfosbot/module/apis/line"
@ -71,6 +72,7 @@ func getSourceUser(uid, gid string) (u *model.LineUser, err error) {
if err != nil { if err != nil {
return return
} }
if userData == nil { if userData == nil {
tmpu, err := line.GetUserInfo(uid, gid) tmpu, err := line.GetUserInfo(uid, gid)
if err != nil || tmpu == nil { if err != nil || tmpu == nil {
@ -83,6 +85,18 @@ func getSourceUser(uid, gid string) (u *model.LineUser, err error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
} else {
if userData.Mtime.Unix() < (time.Now().Unix() - 432000) {
tmpu, err := line.GetUserInfo(uid, gid)
if err != nil || tmpu == nil {
return nil, err
}
userData.Name = tmpu.DisplayName
err = userData.UpdateName()
if err != nil {
return nil, err
}
}
} }
return userData, nil return userData, nil