MCPcopy
hub / github.com/tinode/chat / replyGetData

Method replyGetData

server/topic.go:2768–2827  ·  view source on GitHub ↗

replyGetData is a response to a get.data request - load a list of stored messages, send them to session as {data} response goes to a single session rather than all sessions in a topic

(sess *Session, asUid types.Uid, asChan bool, req *MsgGetOpts, msg *ClientComMessage)

Source from the content-addressed store, hash-verified

2766// replyGetData is a response to a get.data request - load a list of stored messages, send them to session as {data}
2767// response goes to a single session rather than all sessions in a topic
2768func (t *Topic) replyGetData(sess *Session, asUid types.Uid, asChan bool, req *MsgGetOpts, msg *ClientComMessage) error {
2769 now := types.TimeNow()
2770 toriginal := t.original(asUid)
2771
2772 if req != nil && (req.IfModifiedSince != nil || req.User != "" || req.Topic != "") {
2773 sess.queueOut(ErrMalformedReply(msg, now))
2774 return errors.New("invalid MsgGetOpts query")
2775 }
2776
2777 // Check if the user has permission to read the topic data
2778 count := 0
2779 if userData := t.perUser[asUid]; (userData.modeGiven & userData.modeWant).IsReader() {
2780 // Read messages from DB
2781 messages, err := store.Messages.GetAll(t.name, asUid, msgOpts2storeOpts(req))
2782 if err != nil {
2783 sess.queueOut(ErrUnknownReply(msg, now))
2784 return err
2785 }
2786
2787 // Push the list of messages to the client as {data}.
2788 if messages != nil {
2789 count = len(messages)
2790 if count > 0 {
2791 outgoingMessages := make([]*ServerComMessage, count)
2792 for i := range messages {
2793 mm := &messages[i]
2794 from := ""
2795 if !asChan {
2796 // Don't show sender for channel readers
2797 from = types.ParseUid(mm.From).UserId()
2798 }
2799 outgoingMessages[i] = &ServerComMessage{
2800 Data: &MsgServerData{
2801 Topic: toriginal,
2802 Head: mm.Head,
2803 SeqId: mm.SeqId,
2804 From: from,
2805 Timestamp: mm.CreatedAt,
2806 Content: mm.Content,
2807 },
2808 }
2809 }
2810 sess.queueOutBatch(outgoingMessages)
2811 }
2812 }
2813 } else {
2814 sess.queueOut(ErrPermissionDeniedReply(msg, now))
2815 return errors.New("attempt to get messages by non-reader")
2816 }
2817
2818 // Inform the requester that all the data has been served.
2819 if count == 0 {
2820 sess.queueOut(NoContentParamsReply(msg, now, map[string]any{"what": "data"}))
2821 } else {
2822 sess.queueOut(NoErrDeliveredParams(msg.Id, msg.Original, now,
2823 map[string]any{"what": "data", "count": count}))
2824 }
2825

Callers 2

handleMetaGetMethod · 0.95
handleSubscriptionMethod · 0.95

Calls 14

originalMethod · 0.95
TimeNowFunction · 0.92
ParseUidFunction · 0.92
ErrMalformedReplyFunction · 0.85
msgOpts2storeOptsFunction · 0.85
ErrUnknownReplyFunction · 0.85
ErrPermissionDeniedReplyFunction · 0.85
NoContentParamsReplyFunction · 0.85
NoErrDeliveredParamsFunction · 0.85
queueOutMethod · 0.80
IsReaderMethod · 0.80
UserIdMethod · 0.80

Tested by

no test coverage detected