feat: add im relationship flows

This commit is contained in:
richjohnrambo
2026-08-12 11:31:16 +08:00
parent 5bd35f97a6
commit d02041659e
11 changed files with 1100 additions and 12 deletions
+29
View File
@@ -1013,6 +1013,16 @@ func (t *Topic) sendSubNotifications(asUid types.Uid, sid, userAgent string) {
// in response to a client request (msg, asUid) and broadcasts it to the attached sessions.
func (t *Topic) saveAndBroadcastMessage(msg *ClientComMessage, asUid types.Uid, noEcho bool, attachments []string, head map[string]any, content any) error {
pud, userFound := t.perUser[asUid]
if !userFound && msg.sess != nil {
asUid = msg.sess.uid
pud, userFound = t.perUser[asUid]
}
if !userFound && msg.sess != nil {
if psd, ok := t.sessions[msg.sess]; ok {
asUid = psd.uid
pud, userFound = t.perUser[asUid]
}
}
if t.cat == types.TopicCatSys {
if msg.sess != nil && msg.sess.authLvl != auth.LevelRoot {
msg.sess.queueOut(ErrPermissionDenied(msg.Id, t.original(asUid), msg.Timestamp))
@@ -1042,6 +1052,22 @@ func (t *Topic) saveAndBroadcastMessage(msg *ClientComMessage, asUid types.Uid,
return types.ErrPermissionDenied
}
}
if t.cat == types.TopicCatP2P && msg.sess != nil && msg.sess.authLvl != auth.LevelRoot &&
store.Relations != nil && store.Store != nil && store.Store.IsOpen() {
peer := t.p2pOtherUser(asUid)
if !peer.IsZero() {
canSend, _, err := store.Relations.CanSendP2P(t.tenantID, asUid, peer)
if err != nil {
logs.Warn.Printf("topic[%s]: failed to check p2p relationship: %v", t.name, err)
msg.sess.queueOut(ErrUnknown(msg.Id, t.original(asUid), msg.Timestamp))
return err
}
if !canSend {
msg.sess.queueOut(ErrPermissionDenied(msg.Id, t.original(asUid), msg.Timestamp))
return types.ErrPermissionDenied
}
}
}
saveResult, err := store.Messages.Save(t.tenantID,
&types.Message{
@@ -1124,6 +1150,9 @@ func (t *Topic) saveAndBroadcastMessage(msg *ClientComMessage, asUid types.Uid,
// This is a NON-proxy broadcast.
func (t *Topic) handlePubBroadcast(msg *ClientComMessage) {
asUid := types.ParseUserId(msg.AsUser)
if asUid.IsZero() && msg.sess != nil {
asUid = msg.sess.uid
}
if t.isInactive() {
// Ignore broadcast - topic is paused or being deleted.
msg.sess.queueOut(ErrLocked(msg.Id, t.original(asUid), msg.Timestamp))