github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/store/details.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2016 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     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 General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package store
    21  
    22  import (
    23  	"github.com/snapcore/snapd/jsonutil/safejson"
    24  	"github.com/snapcore/snapd/snap"
    25  	"github.com/snapcore/snapd/strutil"
    26  )
    27  
    28  // snapDetails encapsulates the data sent to us from the store as JSON.
    29  type snapDetails struct {
    30  	AnonDownloadURL  string             `json:"anon_download_url,omitempty"`
    31  	Architectures    []string           `json:"architecture"`
    32  	Channel          string             `json:"channel,omitempty"`
    33  	DownloadSha3_384 string             `json:"download_sha3_384,omitempty"`
    34  	Summary          safejson.String    `json:"summary,omitempty"`
    35  	Description      safejson.Paragraph `json:"description,omitempty"`
    36  	DownloadSize     int64              `json:"binary_filesize,omitempty"`
    37  	DownloadURL      string             `json:"download_url,omitempty"`
    38  	LastUpdated      string             `json:"last_updated,omitempty"`
    39  	Name             string             `json:"package_name"`
    40  	Prices           map[string]float64 `json:"prices,omitempty"`
    41  	// Note that the publisher is really the "display name" of the
    42  	// publisher
    43  	Publisher      string           `json:"publisher,omitempty"`
    44  	RatingsAverage float64          `json:"ratings_average,omitempty"`
    45  	Revision       int              `json:"revision"` // store revisions are ints starting at 1
    46  	SnapID         string           `json:"snap_id"`
    47  	License        string           `json:"license,omitempty"`
    48  	Base           string           `json:"base,omitempty"`
    49  	Media          []storeSnapMedia `json:"media,omitempty"`
    50  
    51  	// FIXME: the store should send "contact" here, once it does we
    52  	//        can remove support_url
    53  	SupportURL string `json:"support_url"`
    54  	Contact    string `json:"contact"`
    55  
    56  	Title   safejson.String `json:"title"`
    57  	Type    snap.Type       `json:"content,omitempty"`
    58  	Version string          `json:"version"`
    59  
    60  	Developer           string `json:"origin"`
    61  	DeveloperID         string `json:"developer_id"`
    62  	DeveloperName       string `json:"developer_name"`
    63  	DeveloperValidation string `json:"developer_validation"`
    64  
    65  	Private     bool   `json:"private"`
    66  	Confinement string `json:"confinement"`
    67  
    68  	CommonIDs []string `json:"common_ids,omitempty"`
    69  }
    70  
    71  func infoFromRemote(d *snapDetails) *snap.Info {
    72  	info := &snap.Info{}
    73  	info.Architectures = d.Architectures
    74  	info.SnapType = d.Type
    75  	info.Version = d.Version
    76  	info.RealName = d.Name
    77  	info.SnapID = d.SnapID
    78  	info.Revision = snap.R(d.Revision)
    79  
    80  	// https://forum.snapcraft.io/t/title-length-in-snapcraft-yaml-snap-yaml/8625/10
    81  	info.EditedTitle = strutil.ElliptRight(d.Title.Clean(), 40)
    82  
    83  	info.EditedSummary = d.Summary.Clean()
    84  	info.EditedDescription = d.Description.Clean()
    85  	// Note that the store side is using confusing terminology here.
    86  	// What the store calls "developer" is actually the publisher
    87  	// username.
    88  	//
    89  	// It also sends "publisher" and "developer_name" which are
    90  	// the "publisher display name" which we cannot use currently
    91  	// because it is not validated (i.e. the publisher could put
    92  	// anything in there and mislead the users this way).
    93  	info.Publisher = snap.StoreAccount{
    94  		ID:          d.DeveloperID,
    95  		Username:    d.Developer,
    96  		DisplayName: d.DeveloperName,
    97  		Validation:  d.DeveloperValidation,
    98  	}
    99  	info.Channel = d.Channel
   100  	info.Sha3_384 = d.DownloadSha3_384
   101  	info.Size = d.DownloadSize
   102  	info.AnonDownloadURL = d.AnonDownloadURL
   103  	info.DownloadURL = d.DownloadURL
   104  	info.Prices = d.Prices
   105  	info.Private = d.Private
   106  	info.Paid = len(info.Prices) > 0
   107  	info.Confinement = snap.ConfinementType(d.Confinement)
   108  	info.EditedContact = d.Contact
   109  	info.License = d.License
   110  	info.Base = d.Base
   111  	info.CommonIDs = d.CommonIDs
   112  
   113  	addMedia(info, d.Media)
   114  
   115  	// FIXME: once the store sends "contact" for everything, remove
   116  	//        the "SupportURL" part of the if
   117  	if info.EditedContact == "" {
   118  		info.EditedContact = d.SupportURL
   119  	}
   120  
   121  	return info
   122  }