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"
) )
@ -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()
@ -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
} }