Diffdown is a real-time collaborative Markdown editor/previewer built on the AT Protocol diffdown.com

Fetch shared documents for dashboard

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

+59 -16
+59 -16
internal/handler/handler.go
··· 65 65 OwnerDID string 66 66 } 67 67 68 + type DashboardContent struct { 69 + OwnDocs []*model.Document 70 + SharedDocs []*SharedDocument 71 + } 72 + 73 + type SharedDocument struct { 74 + RKey string 75 + OwnerDID string 76 + Title string 77 + UpdatedAt string 78 + CreatedAt string 79 + } 80 + 68 81 func (h *Handler) currentUser(r *http.Request) (*model.User, string) { 69 82 uid := auth.UserIDFromContext(r.Context()) 70 83 if uid == "" { ··· 136 149 return 137 150 } 138 151 152 + empty := DashboardContent{OwnDocs: []*model.Document{}, SharedDocs: []*SharedDocument{}} 153 + 139 154 client, err := h.xrpcClient(user.ID) 140 155 if err != nil { 141 156 log.Printf("Dashboard: xrpc client: %v", err) 142 - h.render(w, "documents.html", PageData{ 143 - Title: "Documents", 144 - User: user, 145 - UserHandle: userHandle, 146 - Content: []*model.Document{}, 147 - }) 157 + h.render(w, "documents.html", PageData{Title: "Documents", User: user, UserHandle: userHandle, Content: empty}) 148 158 return 149 159 } 150 160 161 + // Own documents 151 162 records, _, err := client.ListRecords(client.DID(), collectionDocument, 100, "") 152 163 if err != nil { 153 164 log.Printf("Dashboard: list records: %v", err) 154 - h.render(w, "documents.html", PageData{ 155 - Title: "Documents", 156 - User: user, 157 - UserHandle: userHandle, 158 - Content: []*model.Document{}, 159 - }) 165 + h.render(w, "documents.html", PageData{Title: "Documents", User: user, UserHandle: userHandle, Content: empty}) 160 166 return 161 167 } 162 - 163 - var docs []*model.Document 168 + var ownDocs []*model.Document 164 169 for _, rec := range records { 165 170 doc := &model.Document{} 166 171 if err := json.Unmarshal(rec.Value, doc); err != nil { ··· 169 174 doc.URI = rec.URI 170 175 doc.CID = rec.CID 171 176 doc.RKey = model.RKeyFromURI(rec.URI) 172 - docs = append(docs, doc) 177 + ownDocs = append(ownDocs, doc) 178 + } 179 + 180 + // Shared documents 181 + session, _ := h.DB.GetATProtoSession(user.ID) 182 + var sharedDocs []*SharedDocument 183 + if session != nil { 184 + collabs, err := h.DB.GetCollaborations(session.DID) 185 + if err != nil { 186 + log.Printf("Dashboard: get collaborations: %v", err) 187 + } 188 + for _, c := range collabs { 189 + ownerUser, err := h.DB.GetUserByDID(c.OwnerDID) 190 + if err != nil { 191 + log.Printf("Dashboard: owner not found %s: %v", c.OwnerDID, err) 192 + continue 193 + } 194 + ownerClient, err := h.xrpcClient(ownerUser.ID) 195 + if err != nil { 196 + log.Printf("Dashboard: owner xrpc client %s: %v", c.OwnerDID, err) 197 + continue 198 + } 199 + value, _, err := ownerClient.GetRecord(c.OwnerDID, collectionDocument, c.DocumentRKey) 200 + if err != nil { 201 + log.Printf("Dashboard: get shared record %s/%s: %v", c.OwnerDID, c.DocumentRKey, err) 202 + continue 203 + } 204 + var doc model.Document 205 + if err := json.Unmarshal(value, &doc); err != nil { 206 + continue 207 + } 208 + sharedDocs = append(sharedDocs, &SharedDocument{ 209 + RKey: c.DocumentRKey, 210 + OwnerDID: c.OwnerDID, 211 + Title: doc.Title, 212 + UpdatedAt: doc.UpdatedAt, 213 + CreatedAt: doc.CreatedAt, 214 + }) 215 + } 173 216 } 174 217 175 218 h.render(w, "documents.html", PageData{ 176 219 Title: "Documents", 177 220 User: user, 178 221 UserHandle: userHandle, 179 - Content: docs, 222 + Content: DashboardContent{OwnDocs: ownDocs, SharedDocs: sharedDocs}, 180 223 }) 181 224 } 182 225