From 01b7a18ea950b2efc454a11b28dfb168d6081431 Mon Sep 17 00:00:00 2001 From: Azareal Date: Wed, 12 Feb 2020 19:11:27 +1000 Subject: [PATCH] Show avatars for 1-1 conversations in the convo list. Don't show your own name in conversation titles. Don't record the same participant in a conversation twice. --- common/conversations.go | 12 +++++++++-- common/pages.go | 8 ++++++- common/template_init.go | 10 ++++++--- routes/convos.go | 38 ++++++++++++++++++++++++++++++---- templates/convos.html | 5 +++-- themes/cosora/public/convo.css | 20 +++++++++++++++++- themes/nox/public/convo.css | 17 +++++++++++++++ 7 files changed, 97 insertions(+), 13 deletions(-) diff --git a/common/conversations.go b/common/conversations.go index 9aa13350..908a0f7a 100644 --- a/common/conversations.go +++ b/common/conversations.go @@ -54,6 +54,7 @@ func init() { type Conversation struct { ID int + Link string CreatedBy int CreatedAt time.Time LastReplyBy int @@ -133,6 +134,10 @@ func (co *Conversation) Create() (int, error) { return int(lastID), err } +func BuildConvoURL(coid int) string { + return "/user/convo/" + strconv.Itoa(coid) +} + type ConversationExtra struct { *Conversation Users []*User @@ -141,7 +146,7 @@ type ConversationExtra struct { type ConversationStore interface { Get(id int) (*Conversation, error) GetUser(uid, offset int) (cos []*Conversation, err error) - GetUserExtra(uid int, offset int) (cos []*ConversationExtra, err error) + GetUserExtra(uid, offset int) (cos []*ConversationExtra, err error) GetUserCount(uid int) (count int) Delete(id int) error Count() (count int) @@ -330,7 +335,7 @@ func (s *DefaultConversationStore) Create(content string, createdBy int, partici if len(participants) == 0 { return 0, errors.New("no participants set") } - res, err := s.create.Exec(createdBy,createdBy) + res, err := s.create.Exec(createdBy, createdBy) if err != nil { return 0, err } @@ -346,6 +351,9 @@ func (s *DefaultConversationStore) Create(content string, createdBy int, partici } for _, p := range participants { + if p == createdBy { + continue + } _, err := s.addParticipant.Exec(p, lastID) if err != nil { return 0, err diff --git a/common/pages.go b/common/pages.go index d9fde7a3..c60c9929 100644 --- a/common/pages.go +++ b/common/pages.go @@ -283,9 +283,15 @@ type ResetPage struct { MFA bool } +type ConvoListRow struct { + *ConversationExtra + ShortUsers []*User + OneOnOne bool +} + type ConvoListPage struct { *Header - Convos []*ConversationExtra + Convos []ConvoListRow Paginator } diff --git a/common/template_init.go b/common/template_init.go index 46418ffb..ef6ca731 100644 --- a/common/template_init.go +++ b/common/template_init.go @@ -350,13 +350,17 @@ func compileTemplates(wg *sync.WaitGroup, c *tmpl.CTemplateSet, themeName string t.AddStd("account", "c.Account", accountPage) parti := []*User{&user} - convo := &Conversation{1, user.ID, time.Now(), 0, time.Now()} + convo := &Conversation{1, BuildConvoURL(1), user.ID, time.Now(), 0, time.Now()} convoItems := []ConvoViewRow{ConvoViewRow{&ConversationPost{1, 1, "hey", "", user.ID}, &user, "", 4, true}} convoPage := ConvoViewPage{header, convo, convoItems, parti, true, Paginator{[]int{1}, 1, 1}} t.AddStd("convo", "c.ConvoViewPage", convoPage) convos := []*ConversationExtra{&ConversationExtra{&Conversation{}, []*User{&user}}} - convoListPage := ConvoListPage{header, convos, Paginator{[]int{1}, 1, 1}} + var cRows []ConvoListRow + for _, convo := range convos { + cRows = append(cRows, ConvoListRow{convo, convo.Users, false}) + } + convoListPage := ConvoListPage{header, cRows, Paginator{[]int{1}, 1, 1}} t.AddStd("convos", "c.ConvoListPage", convoListPage) basePage := &BasePanelPage{header, PanelStats{}, "dashboard", ReportForumID} @@ -557,7 +561,7 @@ func compileJSTemplates(wg *sync.WaitGroup, c *tmpl.CTemplateSet, themeName stri t.AddStd("topic_c_poll_input", "c.TopicCPollInput", TopicCPollInput{Index: 0}) parti := []*User{&user} - convo := &Conversation{1, user.ID, time.Now(), 0, time.Now()} + convo := &Conversation{1, BuildConvoURL(1), user.ID, time.Now(), 0, time.Now()} convoItems := []ConvoViewRow{ConvoViewRow{&ConversationPost{1, 1, "hey", "", user.ID}, &user, "", 4, true}} convoPage := ConvoViewPage{header, convo, convoItems, parti, true, Paginator{[]int{1}, 1, 1}} t.AddStd("convo", "c.ConvoViewPage", convoPage) diff --git a/routes/convos.go b/routes/convos.go index 11722770..c03dd4d4 100644 --- a/routes/convos.go +++ b/routes/convos.go @@ -30,7 +30,24 @@ func Convos(w http.ResponseWriter, r *http.Request, user c.User, h *c.Header) c. return c.InternalError(err, w, r) } - pi := c.Account{h, "dashboard", "convos", c.ConvoListPage{h, convos, c.Paginator{pageList, page, lastPage}}} + var cRows []c.ConvoListRow + for _, convo := range convos { + var parti []*c.User + notMe := false + for _, u := range convo.Users { + if u.ID == user.ID { + continue + } + parti = append(parti, u) + notMe = true + } + if !notMe { + parti = convo.Users + } + cRows = append(cRows, c.ConvoListRow{convo, parti, len(parti) == 1}) + } + + pi := c.Account{h, "dashboard", "convos", c.ConvoListPage{h, cRows, c.Paginator{pageList, page, lastPage}}} return renderTemplate("account", w, r, h, pi) } @@ -138,16 +155,29 @@ func ConvosCreateSubmit(w http.ResponseWriter, r *http.Request, user c.User) c.R return c.NoPermissions(w, r, user) } - recps := c.SanitiseSingleLine(r.PostFormValue("recp")) + sRecps := c.SanitiseSingleLine(r.PostFormValue("recp")) body := c.PreparseMessage(r.PostFormValue("body")) rlist := []int{} + + // De-dupe recipients + var recps []string + unames := make(map[string]struct{}) + for _, recp := range strings.Split(sRecps, ",") { + recp = strings.TrimSpace(recp) + _, exists := unames[recp] + if !exists { + recps = append(recps, recp) + unames[recp] = struct{}{} + } + } + max := 10 // max number of recipients that can be added at once - for i, recp := range strings.Split(recps, ",") { + for i, recp := range recps { if i >= max { break } - u, err := c.Users.GetByName(strings.TrimSpace(recp)) + u, err := c.Users.GetByName(recp) if err == sql.ErrNoRows { return c.LocalError("One of the recipients doesn't exist", w, r, user) } else if err != nil { diff --git a/templates/convos.html b/templates/convos.html index aefe6297..749cde72 100644 --- a/templates/convos.html +++ b/templates/convos.html @@ -9,7 +9,7 @@
@@ -27,7 +27,8 @@ {{range .Convos}}
- {{range .Users}}{{.Name}} {{end}} + {{if .OneOnOne}}{{range .ShortUsers}}{{end}}{{end}} + {{range .ShortUsers}}{{.Name}} {{end}} {{reltime .LastReplyAt}}
diff --git a/themes/cosora/public/convo.css b/themes/cosora/public/convo.css index a5822f6d..f794fd89 100644 --- a/themes/cosora/public/convo.css +++ b/themes/cosora/public/convo.css @@ -1,6 +1,24 @@ -.rowhead .rowitem { +.rowhead .rowitem, .convos_list .rowitem { display: flex; } +.convos_list .to_left { + display: flex; +} +.convos_list .rowitem img { + width: 26px; + height: 26px; + margin-right: 8px; + border-radius: 24px; +} +.convos_list .rowitem a { + /*margin-top: auto; + margin-bottom: auto;*/ + margin-top: 4px; +} +.convos_list .to_right { + margin-top: auto; + margin-bottom: auto; +} .convos_item_user:not(:last-child):after { content: ","; } diff --git a/themes/nox/public/convo.css b/themes/nox/public/convo.css index a46d4197..b14df02b 100644 --- a/themes/nox/public/convo.css +++ b/themes/nox/public/convo.css @@ -8,6 +8,23 @@ .close_form { margin-left: 8px; } +.convos_list .to_left { + display: flex; +} +.convos_list .rowitem img { + width: 24px; + height: 24px; + margin-right: 8px; + border-radius: 24px; +} +.convos_list .rowitem a { + /*margin-top: auto; + margin-bottom: auto;*/ +} +.convos_list .to_right { + margin-top: auto; + margin-bottom: auto; +} .convos_item_user:not(:last-child):after { content: ","; }