xcvr tui

this uses the new resolution flow more or less

+37 -10
+37 -10
main.go
··· 1112 1112 func (m model) updateResolvingChannel(msg tea.Msg) (tea.Model, tea.Cmd) { 1113 1113 switch msg := msg.(type) { 1114 1114 case resolutionMsg: 1115 - c := m.clm.curchannel() 1116 - var host string 1117 - if c != nil { 1118 - host = c.Host 1119 - } 1120 - wsurl := fmt.Sprintf("%s%s", host, msg.resolution.URL) 1115 + wsurl := fmt.Sprintf("%s%s", strings.TrimPrefix(msg.channelhost, "https://"), msg.resolution.URL) 1121 1116 m.gsd.state = ConnectingToChannel 1122 1117 ctx, cancel := context.WithCancel(context.Background()) 1123 1118 return m, m.connectToChannel(ctx, cancel, wsurl) ··· 1240 1235 func ResolveChannel(host string, did string, rkey string) tea.Cmd { 1241 1236 return func() tea.Msg { 1242 1237 c := &http.Client{Timeout: 10 * time.Second} 1243 - res, err := c.Get(fmt.Sprintf("http://%s/xrpc/org.xcvr.actor.resolveChannel?did=%s&rkey=%s", host, did, rkey)) 1244 - 1238 + var res *http.Response 1239 + var err error 1240 + if strings.HasPrefix(host, "did:web:") { 1241 + res, err = c.Get(fmt.Sprintf("https://%s/.well-known/did.json", strings.TrimPrefix(host, "did:web:"))) 1242 + } else if !strings.HasPrefix(host, "did:plc") { 1243 + res, err = c.Get(fmt.Sprintf("https://plc.directory/%s", host)) 1244 + } else { 1245 + err = errors.New("unsupported method") 1246 + } 1245 1247 if err != nil { 1246 1248 return errMsg{err} 1247 1249 } 1250 + dec := json.NewDecoder(res.Body) 1251 + var diddoc struct { 1252 + Services []struct { 1253 + Id string `json:"id"` 1254 + ServiceEndpoint string `json:"serviceEndpoint"` 1255 + } `json:"service"` 1256 + } 1257 + dec.Decode(&diddoc) 1258 + if diddoc.Services == nil { 1259 + return errMsg{errors.New("no services")} 1260 + 1261 + } 1262 + var url string 1263 + for _, s := range diddoc.Services { 1264 + if s.Id != "#xcvr_ch" { 1265 + continue 1266 + } 1267 + url = s.ServiceEndpoint 1268 + } 1269 + if url == "" { 1270 + return errMsg{errors.New("no serviceEndpoint")} 1271 + } 1272 + res, err = c.Get(fmt.Sprintf("%s/xrpc/org.xcvr.actor.resolveChannel?did=%s&rkey=%s", url, did, rkey)) 1273 + 1248 1274 if res.StatusCode != 200 { 1249 1275 return errMsg{errors.New(fmt.Sprintf("error resolving channel: %d", res.StatusCode))} 1250 1276 } ··· 1254 1280 if err != nil { 1255 1281 return errMsg{err} 1256 1282 } 1257 - return resolutionMsg{resolution} 1283 + return resolutionMsg{resolution, url} 1258 1284 } 1259 1285 } 1260 1286 1261 1287 type resolutionMsg struct { 1262 - resolution Resolution 1288 + resolution Resolution 1289 + channelhost string 1263 1290 } 1264 1291 1265 1292 type Resolution struct {