gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

stream.go (3297B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package stream
     19 
     20 import "sync"
     21 
     22 const (
     23 	// EventTypeNotification -- a user should be shown a notification
     24 	EventTypeNotification string = "notification"
     25 	// EventTypeUpdate -- a user should be shown an update in their timeline
     26 	EventTypeUpdate string = "update"
     27 	// EventTypeDelete -- something should be deleted from a user
     28 	EventTypeDelete string = "delete"
     29 )
     30 
     31 const (
     32 	// TimelineLocal -- public statuses from the LOCAL timeline.
     33 	TimelineLocal string = "public:local"
     34 	// TimelinePublic -- public statuses, including federated ones.
     35 	TimelinePublic string = "public"
     36 	// TimelineHome -- statuses for a user's Home timeline.
     37 	TimelineHome string = "user"
     38 	// TimelineNotifications -- notification events.
     39 	TimelineNotifications string = "user:notification"
     40 	// TimelineDirect -- statuses sent to a user directly.
     41 	TimelineDirect string = "direct"
     42 	// TimelineList -- statuses for a user's list timeline.
     43 	TimelineList string = "list"
     44 )
     45 
     46 // AllStatusTimelines contains all Timelines that a status could conceivably be delivered to -- useful for doing deletes.
     47 var AllStatusTimelines = []string{
     48 	TimelineLocal,
     49 	TimelinePublic,
     50 	TimelineHome,
     51 	TimelineDirect,
     52 	TimelineList,
     53 }
     54 
     55 // StreamsForAccount is a wrapper for the multiple streams that one account can have running at the same time.
     56 // TODO: put a limit on this
     57 type StreamsForAccount struct {
     58 	// The currently held streams for this account
     59 	Streams []*Stream
     60 	// Mutex to lock/unlock when modifying the slice of streams.
     61 	sync.Mutex
     62 }
     63 
     64 // Stream represents one open stream for a client.
     65 type Stream struct {
     66 	// ID of this stream, generated during creation.
     67 	ID string
     68 	// A set of types subscribed to by this stream: user/public/etc.
     69 	// It's a map to ensure no duplicates; the value is ignored.
     70 	StreamTypes map[string]any
     71 	// Channel of messages for the client to read from
     72 	Messages chan *Message
     73 	// Channel to close when the client drops away
     74 	Hangup chan interface{}
     75 	// Only put messages in the stream when Connected
     76 	Connected bool
     77 	// Mutex to lock/unlock when inserting messages, hanging up, changing the connected state etc.
     78 	sync.Mutex
     79 }
     80 
     81 // Message represents one streamed message.
     82 type Message struct {
     83 	// All the stream types this message should be delivered to.
     84 	Stream []string `json:"stream"`
     85 	// The event type of the message (update/delete/notification etc)
     86 	Event string `json:"event"`
     87 	// The actual payload of the message. In case of an update or notification, this will be a JSON string.
     88 	Payload string `json:"payload"`
     89 }