fix convo creation lastReplyBy default value bug

DefaultConversationStore.GetUser now returns ErrNoRows when there aren't any results.
This commit is contained in:
Azareal 2020-02-10 20:28:33 +10:00
parent f83da97fb9
commit 459a7f0205
1 changed files with 27 additions and 23 deletions

View File

@ -3,10 +3,11 @@ package common
import ( import (
"errors" "errors"
"time" "time"
//"log" //"log"
"strconv"
"database/sql" "database/sql"
"strconv"
qgen "github.com/Azareal/Gosora/query_gen" qgen "github.com/Azareal/Gosora/query_gen"
) )
@ -45,7 +46,7 @@ func init() {
createPost: acc.Insert(cpo).Columns("cid,body,post,createdBy").Fields("?,?,?,?").Prepare(), createPost: acc.Insert(cpo).Columns("cid,body,post,createdBy").Fields("?,?,?,?").Prepare(),
deletePost: acc.Delete(cpo).Where("pid=?").Prepare(), deletePost: acc.Delete(cpo).Where("pid=?").Prepare(),
getUsers: acc.Select("conversations_participants").Columns("uid").Where("cid = ?").Prepare(), getUsers: acc.Select("conversations_participants").Columns("uid").Where("cid=?").Prepare(),
} }
return acc.FirstError() return acc.FirstError()
}) })
@ -160,16 +161,17 @@ type DefaultConversationStore struct {
} }
func NewDefaultConversationStore(acc *qgen.Accumulator) (*DefaultConversationStore, error) { func NewDefaultConversationStore(acc *qgen.Accumulator) (*DefaultConversationStore, error) {
co := "conversations"
return &DefaultConversationStore{ return &DefaultConversationStore{
get: acc.Select("conversations").Columns("createdBy, createdAt, lastReplyBy, lastReplyAt").Where("cid=?").Prepare(), get: acc.Select(co).Columns("createdBy, createdAt, lastReplyBy, lastReplyAt").Where("cid=?").Prepare(),
getUser: acc.SimpleInnerJoin("conversations_participants AS cp", "conversations AS c", "cp.cid, c.createdBy, c.createdAt, c.lastReplyBy, c.lastReplyAt", "cp.cid=c.cid", "cp.uid=?", "c.lastReplyAt DESC, c.createdAt DESC, c.cid DESC", "?,?"), getUser: acc.SimpleInnerJoin("conversations_participants AS cp", "conversations AS c", "cp.cid, c.createdBy, c.createdAt, c.lastReplyBy, c.lastReplyAt", "cp.cid=c.cid", "cp.uid=?", "c.lastReplyAt DESC, c.createdAt DESC, c.cid DESC", "?,?"),
getUserCount: acc.Count("conversations_participants").Where("uid=?").Prepare(), getUserCount: acc.Count("conversations_participants").Where("uid=?").Prepare(),
delete: acc.Delete("conversations").Where("cid=?").Prepare(), delete: acc.Delete(co).Where("cid=?").Prepare(),
deletePosts: acc.Delete("conversations_posts").Where("cid=?").Prepare(), deletePosts: acc.Delete("conversations_posts").Where("cid=?").Prepare(),
deleteParticipants: acc.Delete("conversations_participants").Where("cid=?").Prepare(), deleteParticipants: acc.Delete("conversations_participants").Where("cid=?").Prepare(),
create: acc.Insert("conversations").Columns("createdBy, createdAt, lastReplyAt").Fields("?,UTC_TIMESTAMP(),UTC_TIMESTAMP()").Prepare(), create: acc.Insert(co).Columns("createdBy, createdAt, lastReplyBy, lastReplyAt").Fields("?,UTC_TIMESTAMP(),?,UTC_TIMESTAMP()").Prepare(),
addParticipant: acc.Insert("conversations_participants").Columns("uid, cid").Fields("?,?").Prepare(), addParticipant: acc.Insert("conversations_participants").Columns("uid,cid").Fields("?,?").Prepare(),
count: acc.Count("conversations").Prepare(), count: acc.Count(co).Prepare(),
}, acc.FirstError() }, acc.FirstError()
} }
@ -194,8 +196,14 @@ func (s *DefaultConversationStore) GetUser(uid, offset int) (cos []*Conversation
} }
cos = append(cos, co) cos = append(cos, co)
} }
err = rows.Err()
return cos, rows.Err() if err != nil {
return nil, err
}
if len(cos) == 0 {
err = sql.ErrNoRows
}
return cos, err
} }
func (s *DefaultConversationStore) GetUserExtra(uid, offset int) (cos []*ConversationExtra, err error) { func (s *DefaultConversationStore) GetUserExtra(uid, offset int) (cos []*ConversationExtra, err error) {
@ -205,10 +213,6 @@ func (s *DefaultConversationStore) GetUserExtra(uid, offset int) (cos []*Convers
} }
//log.Printf("raw: %+v\n", raw) //log.Printf("raw: %+v\n", raw)
if len(raw) == 0 {
//log.Println("r0")
return nil, sql.ErrNoRows
}
if len(raw) == 1 { if len(raw) == 1 {
//log.Print("r0b2") //log.Print("r0b2")
uids, err := raw[0].Uids() uids, err := raw[0].Uids()
@ -221,24 +225,24 @@ func (s *DefaultConversationStore) GetUserExtra(uid, offset int) (cos []*Convers
return nil, err return nil, err
} }
//log.Println("r2b2") //log.Println("r2b2")
users := make([]*User,len(umap)) users := make([]*User, len(umap))
var i int var i int
for _, user := range umap { for _, user := range umap {
users[i] = user users[i] = user
i++ i++
} }
return []*ConversationExtra{&ConversationExtra{raw[0],users}}, nil return []*ConversationExtra{&ConversationExtra{raw[0], users}}, nil
} }
//log.Println("1") //log.Println("1")
cmap := make(map[int]*ConversationExtra,len(raw)) cmap := make(map[int]*ConversationExtra, len(raw))
for _, co := range raw { for _, co := range raw {
cmap[co.ID] = &ConversationExtra{co,nil} cmap[co.ID] = &ConversationExtra{co, nil}
} }
// TODO: Add a function for the q stuff // TODO: Add a function for the q stuff
var q string var q string
idList := make([]interface{},len(raw)) idList := make([]interface{}, len(raw))
for i, co := range raw { for i, co := range raw {
idList[i] = strconv.Itoa(co.ID) idList[i] = strconv.Itoa(co.ID)
q += "?," q += "?,"
@ -260,7 +264,7 @@ func (s *DefaultConversationStore) GetUserExtra(uid, offset int) (cos []*Convers
if err != nil { if err != nil {
return nil, err return nil, err
} }
idmap[cid] = append(idmap[cid],uid) idmap[cid] = append(idmap[cid], uid)
puidmap[uid] = struct{}{} puidmap[uid] = struct{}{}
} }
if err = rows.Err(); err != nil { if err = rows.Err(); err != nil {
@ -270,7 +274,7 @@ func (s *DefaultConversationStore) GetUserExtra(uid, offset int) (cos []*Convers
//log.Printf("idmap: %+v\n", idmap) //log.Printf("idmap: %+v\n", idmap)
//log.Printf("puidmap: %+v\n",puidmap) //log.Printf("puidmap: %+v\n",puidmap)
puids := make([]int,len(puidmap)) puids := make([]int, len(puidmap))
var i int var i int
for puid, _ := range puidmap { for puid, _ := range puidmap {
puids[i] = puid puids[i] = puid
@ -285,7 +289,7 @@ func (s *DefaultConversationStore) GetUserExtra(uid, offset int) (cos []*Convers
for cid, uids := range idmap { for cid, uids := range idmap {
co := cmap[cid] co := cmap[cid]
for _, uid := range uids { for _, uid := range uids {
co.Users = append(co.Users,umap[uid]) co.Users = append(co.Users, umap[uid])
} }
//log.Printf("co.Conversation: %+v\n", co.Conversation) //log.Printf("co.Conversation: %+v\n", co.Conversation)
//log.Printf("co.Users: %+v\n", co.Users) //log.Printf("co.Users: %+v\n", co.Users)
@ -293,7 +297,7 @@ func (s *DefaultConversationStore) GetUserExtra(uid, offset int) (cos []*Convers
} }
//log.Printf("cmap: %+v\n", cmap) //log.Printf("cmap: %+v\n", cmap)
for _, ra := range raw { for _, ra := range raw {
cos = append(cos,cmap[ra.ID]) cos = append(cos, cmap[ra.ID])
} }
//log.Printf("cos: %+v\n", cos) //log.Printf("cos: %+v\n", cos)
@ -326,7 +330,7 @@ func (s *DefaultConversationStore) Create(content string, createdBy int, partici
if len(participants) == 0 { if len(participants) == 0 {
return 0, errors.New("no participants set") return 0, errors.New("no participants set")
} }
res, err := s.create.Exec(createdBy) res, err := s.create.Exec(createdBy,createdBy)
if err != nil { if err != nil {
return 0, err return 0, err
} }