gtsocial-umbx

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

handshake.go (2506B)


      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 dereferencing
     19 
     20 import (
     21 	"net/url"
     22 )
     23 
     24 func (d *deref) Handshaking(username string, remoteAccountID *url.URL) bool {
     25 	d.handshakeSync.Lock()
     26 	defer d.handshakeSync.Unlock()
     27 
     28 	if d.handshakes == nil {
     29 		// handshakes isn't even initialized yet so we can't be handshaking with anyone
     30 		return false
     31 	}
     32 
     33 	remoteIDs, ok := d.handshakes[username]
     34 	if !ok {
     35 		// user isn't handshaking with anyone, bail
     36 		return false
     37 	}
     38 
     39 	for _, id := range remoteIDs {
     40 		if id.String() == remoteAccountID.String() {
     41 			// we are currently handshaking with the remote account, yep
     42 			return true
     43 		}
     44 	}
     45 
     46 	// didn't find it which means we're not handshaking
     47 	return false
     48 }
     49 
     50 func (d *deref) startHandshake(username string, remoteAccountID *url.URL) {
     51 	d.handshakeSync.Lock()
     52 	defer d.handshakeSync.Unlock()
     53 
     54 	remoteIDs, ok := d.handshakes[username]
     55 	if !ok {
     56 		// there was nothing in there yet, so just add this entry and return
     57 		d.handshakes[username] = []*url.URL{remoteAccountID}
     58 		return
     59 	}
     60 
     61 	// add the remote ID to the slice
     62 	remoteIDs = append(remoteIDs, remoteAccountID)
     63 	d.handshakes[username] = remoteIDs
     64 }
     65 
     66 func (d *deref) stopHandshake(username string, remoteAccountID *url.URL) {
     67 	d.handshakeSync.Lock()
     68 	defer d.handshakeSync.Unlock()
     69 
     70 	remoteIDs, ok := d.handshakes[username]
     71 	if !ok {
     72 		return
     73 	}
     74 
     75 	newRemoteIDs := []*url.URL{}
     76 	for _, id := range remoteIDs {
     77 		if id.String() != remoteAccountID.String() {
     78 			newRemoteIDs = append(newRemoteIDs, id)
     79 		}
     80 	}
     81 
     82 	if len(newRemoteIDs) == 0 {
     83 		// there are no handshakes so just remove this user entry from the map and save a few bytes
     84 		delete(d.handshakes, username)
     85 	} else {
     86 		// there are still other handshakes ongoing
     87 		d.handshakes[username] = newRemoteIDs
     88 	}
     89 }