MCPcopy Index your code
hub / github.com/Preloading/TwitterAPIBridge / UpdateProfilePicture

Function UpdateProfilePicture

twitterv1/account.go:147–209  ·  view source on GitHub ↗
(c *fiber.Ctx)

Source from the content-addressed store, hash-verified

145}
146
147func UpdateProfilePicture(c *fiber.Ctx) error {
148 // auth
149 my_did, pds, _, oauthToken, err := GetAuthFromReq(c)
150 if err != nil {
151 return MissingAuth(c, err)
152 }
153
154 // Lock the mutex for this user
155 userMutex := getUserMutex(*my_did)
156 userMutex.Lock()
157 defer userMutex.Unlock()
158
159 // get the old profile
160 oldProfile, err := blueskyapi.GetRecord(*pds, "app.bsky.actor.profile", *my_did, "self")
161 if err != nil {
162 fmt.Println("Error:", err)
163 return HandleBlueskyError(c, err.Error(), "com.atproto.repo.getRecord", UpdateProfilePicture)
164 }
165
166 // get our new image
167 image, err := c.FormFile("image")
168 if err != nil {
169 fmt.Println("Error:", err)
170 return ReturnError(c, "Please upload an image", 195, 403) // idk about this error code, since it's for url params instead of post data.
171 }
172
173 // read the image file content
174 file, err := image.Open()
175 if err != nil {
176 fmt.Println("Error:", err)
177 return ReturnError(c, "Uploaded image is invalid.", 195, 403)
178 }
179 defer file.Close()
180
181 imageData, err := io.ReadAll(file)
182 if err != nil {
183 fmt.Println("Error:", err)
184 return ReturnError(c, "Uploaded image is invalid.", 195, 403)
185 }
186
187 // upload our new profile picture
188 profilePictureBlob, err := blueskyapi.UploadBlob(*pds, *oauthToken, imageData, c.Get("Content-Type"))
189 if err != nil {
190 fmt.Println("Error:", err)
191 return HandleBlueskyError(c, err.Error(), "com.atproto.repo.uploadBlob", UpdateProfilePicture)
192 }
193
194 // change our thing
195 oldProfile.Value.Avatar = *profilePictureBlob
196
197 if err := blueskyapi.UpdateRecord(*pds, *oauthToken, "app.bsky.actor.profile", *my_did, "self", oldProfile.CID, oldProfile.Value); err != nil {
198 fmt.Println("Error:", err)
199 return HandleBlueskyError(c, err.Error(), "com.atproto.repo.putRecord", UpdateProfilePicture)
200 }
201
202 user, err := blueskyapi.GetUserInfo(*pds, *oauthToken, *my_did, true)
203 if err != nil {
204 fmt.Println("Error:", err)

Callers

nothing calls this directly

Calls 7

GetAuthFromReqFunction · 0.85
MissingAuthFunction · 0.85
getUserMutexFunction · 0.85
HandleBlueskyErrorFunction · 0.85
ReturnErrorFunction · 0.85
EncodeAndSendFunction · 0.85
GetMethod · 0.45

Tested by

no test coverage detected