Skip to content

Commit

Permalink
Add synchronous interface to get a form from a local image file id (#403
Browse files Browse the repository at this point in the history
)

* add synchronous interface to get a form from an image file id

* unify pendig request handling in core

* enforce same promise in TCCUsers>>requestUserFor:

* fix linter tests, allow for ignoring single methods with a comment

* remove duplicate core setter of stores

Co-authored-by: Tom Richter <tomrichter99@gmail.com>
  • Loading branch information
phkeese and tom-richter authored Jun 22, 2021
1 parent bd986b1 commit c015d8f
Show file tree
Hide file tree
Showing 44 changed files with 1,383 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ handleEvent: anEvent
| update |
update := nil.
anEvent isError ifTrue: [self tryHandleError: anEvent].
anEvent hasExtra ifTrue: [self handlePendingEvent: anEvent. ^ self].
anEvent type caseOf: {
['updateAuthorizationState'] -> [update := self authHandler handleEvent: anEvent].
['updateNewChat'] -> [self chatsHandler addChat: anEvent].
['updateChatPosition'] -> [self chatsHandler updateChatPosition: anEvent].
['updateChatLastMessage'] -> [self chatsHandler updateLastMessage: anEvent].
['messages'] -> [self chatsHandler chatHistoryReceived: anEvent].
['updateNewMessage'] -> [self handleMessageEvent: anEvent].
['user'] -> [self handlePendingEvent: anEvent].
['supergroupFullInfo'] -> [self handlePendingEvent: anEvent].
['chatMembers'] -> [self handlePendingEvent: anEvent].
['basicGroupFullInfo'] -> [self handlePendingEvent: anEvent].
['chat'] -> [self chatsHandler openNewChat: anEvent]}
['chat'] -> [self chatsHandler openNewChat: anEvent].
}
otherwise: [].
update isSymbol ifTrue: [self triggerEvent: update].
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
imageStore: anImageStore

imageStore := anImageStore.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
imageStore

^ imageStore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ initialize
chats: TCCChats newWithDefaultSorting;
loggedInUserID: 0;
pendingRequests: Dictionary new;
users: (TCCUsers new core: self);
users: (TCCUsers newWith: self);
imageStore: (TCCImageStore newWith: self);
callbackCounter: 0.

[self receiveLoop] fork.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
events
requestFile: aFileId with: aCallbackID

"documented here:"
"https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1download_file.html"

self client send: (TCCRequest
newWithType: 'downloadFile'
from: {
'file_id'->aFileId.
'priority'->32.
'offset'->0.
'limit'->0.
'synchronous' -> true.
'@extra'->aCallbackID
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
accessing
users: aDictionary

users := aDictionary.
self users core: self.
users := aDictionary.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
"generateCallbackID" : "pk 6/18/2021 10:27",
"getChatHistoryFrom:" : "per 6/4/2021 15:30",
"getOwnProfile" : "pk 5/13/2021 09:44",
"handleEvent:" : "per 6/4/2021 11:54",
"handleEvent:" : "pk 6/19/2021 17:45",
"handleMessageEvent:" : "pk 5/28/2021 09:41",
"handlePendingEvent:" : "5/30/2021 12:11:04",
"initialize" : "pk 6/18/2021 09:38",
"imageStore" : "pk 6/19/2021 17:15",
"imageStore:" : "TR 6/22/2021 09:33",
"initialize" : "pk 6/19/2021 17:21",
"initializeHandlers" : "js 8/1/2020 18:09",
"isClientAlive" : "js 5/28/2020 17:18",
"loggedInUserID" : "rs 7/5/2020 14:48",
Expand All @@ -31,6 +33,7 @@
"pendingRequests:" : "pk 5/13/2021 09:46",
"receiveLoop" : "R.S 6/1/2020 15:24",
"registerCallback:" : "pk 6/18/2021 09:41",
"requestFile:with:" : "pk 6/19/2021 17:27",
"requestUser:with:" : "pk 5/21/2021 09:33",
"searchChat:" : "f.w. 7/15/2020 22:53",
"sendMessage:to:" : "rs 6/13/2020 15:26",
Expand All @@ -39,4 +42,4 @@
"tryHandleError:" : "JB 6/4/2021 17:28",
"update" : "pk 6/8/2021 09:22",
"users" : "RK 5/19/2021 09:56",
"users:" : "pk 6/13/2021 10:09" } }
"users:" : "TR 6/22/2021 09:33" } }
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"authHandler",
"chatsHandler",
"users",
"imageStore",
"chats",
"loggedInUserID",
"pendingRequests",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ newFromTDLibEvent: anEvent

^ (self newFrom: anEvent)
type: (anEvent at: '@type');
extra: (anEvent at: '@extra' ifAbsent: [nil]);
removeKey: '@type';
yourself
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
extra: aString

extra := aString
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
extra

^ extra
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
hasExtra

^ self extra notNil
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"class" : {
"newFromTDLibEvent:" : "pk 5/17/2021 20:25" },
"newFromTDLibEvent:" : "pk 6/19/2021 17:40" },
"instance" : {
"extra" : "pk 6/19/2021 18:05",
"extra:" : "pk 6/19/2021 17:39",
"hasExtra" : "pk 6/19/2021 17:39",
"isError" : "js 8/1/2020 12:32",
"type" : "js 7/31/2020 22:28",
"type:" : "js 7/31/2020 22:28" } }
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
],
"commentStamp" : "js 8/1/2020 13:35",
"instvars" : [
"type" ],
"type",
"extra" ],
"name" : "TCCEvent",
"pools" : [
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A TCCImageStore is a cache for local image files on disk with a synchronous interface.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
utility
extractPathFrom: anEvent

^ (anEvent at: 'local') at: 'path'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
utility
imageFormFrom: aPath

^ Form fromFileNamed: aPath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
instance creation
newWith: aCore

^ self new
core: aCore;
yourself
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
core: aCore

core := aCore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
core

^ core
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
accessing
getFormFor: anImageId

| promise |
promise := self at: anImageId ifAbsent:
[self at: anImageId put: Promise new.
self requestImageFor: anImageId with: (self at: anImageId).
self at: anImageId].
promise wait.
^ self class imageFormFrom: promise value
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
utility
requestImageFor: anImageId with: aPromise

| callbackID |
callbackID := self core
registerCallback:
[:imageEvent | aPromise resolveWith: (self class extractPathFrom: imageEvent)].

self core requestFile: anImageId with: callbackID
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"class" : {
"extractPathFrom:" : "pk 6/19/2021 16:23",
"imageFormFrom:" : "pk 6/19/2021 16:25",
"newWith:" : "pk 6/19/2021 18:09" },
"instance" : {
"core" : "pk 6/19/2021 16:13",
"core:" : "pk 6/19/2021 16:13",
"getFormFor:" : "pk 6/19/2021 17:25",
"requestImageFor:with:" : "pk 6/19/2021 16:26" } }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"category" : "TelegramClient-Core",
"classinstvars" : [
],
"classvars" : [
],
"commentStamp" : "pk 6/19/2021 16:13",
"instvars" : [
"core" ],
"name" : "TCCImageStore",
"pools" : [
],
"super" : "Dictionary",
"type" : "normal" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
instance creation
newWith: aCore

^ self new
core: aCore;
yourself
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ getUserFor: aUserId
| promise |
promise := self at: aUserId ifAbsent:
[self at: aUserId put: Promise new.
self requestUserFor: aUserId.
self requestUserFor: aUserId with: (self at: aUserId).
self at: aUserId].
promise wait.
^ (promise value)
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
utility
requestUserFor: aUserId
requestUserFor: aUserId with: aPromise

| callbackID |
callbackID := self core
registerCallback:
[:userEvent | (self at: aUserId) resolveWith: (TCCUser newFrom: userEvent)].
[:userEvent | aPromise resolveWith: (TCCUser newFrom: userEvent)].

self core requestUser: aUserId with: callbackID
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"class" : {
},
"newWith:" : "pk 6/19/2021 17:20" },
"instance" : {
"core" : "RK 5/19/2021 10:34",
"core:" : "TR 6/15/2021 16:52",
"getUserFor:" : "RK 5/19/2021 11:05",
"requestUserFor:" : "pk 6/18/2021 09:47" } }
"getUserFor:" : "pk 6/19/2021 17:48",
"requestUserFor:with:" : "pk 6/19/2021 17:48" } }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A TCTCImageStoreTests tests wether the TCCImageStore works as it should.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
running
setUp

super setUp.
TCTMMocks writeMockImage.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
testing
testCanGetImageForm

| form |
form := nil.

self mockTeleClient onRequestType: 'downloadFile' respond: TCTMMocks mockImageResponseJson.

[form := self core imageStore getFormFor: TCTMMocks mockImageId.] fork.
1 seconds wait.

self assert: form notNil.
self assert: form isForm.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
testing
testCanGetImagePath

self mockTeleClient onRequestType: 'downloadFile' respond: TCTMMocks mockImageResponseJson.

[self core imageStore getFormFor: TCTMMocks mockImageId.] fork.
1 seconds wait.

self assert: TCTMMocks mockImagePath equals: (self core imageStore at: TCTMMocks mockImageId) value.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
testing
testDidCreateFile

self assert: (FileDirectory default isAFileNamed: TCTMMocks mockImagePath).
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"class" : {
},
"instance" : {
"setUp" : "pk 6/19/2021 17:37",
"testCanGetImageForm" : "pk 6/19/2021 17:02",
"testCanGetImagePath" : "pk 6/19/2021 17:24",
"testDidCreateFile" : "pk 6/19/2021 17:35" } }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"category" : "TelegramClientTests-Core",
"classinstvars" : [
],
"classvars" : [
],
"commentStamp" : "pk 6/19/2021 18:08",
"instvars" : [
],
"name" : "TCTCImageStoreTests",
"pools" : [
],
"super" : "TCTCNoTdlibTestCase",
"type" : "normal" }
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ private
methodsLinesDo: aBlock

self methodTestObjects do: [:aSLMethodTestObject |
aBlock value: aSLMethodTestObject sourceCode string lines]
| lines |
lines := aSLMethodTestObject sourceCode string lines.
((lines size >= 2) and: [lines second includesSubstring: '"@linter-ignore"'])
ifFalse: [
aBlock value: lines
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"initialize" : "ct 10/7/2020 19:31",
"isLineWithOnlyTabsAndSpaces:" : "j 7/16/2020 12:30",
"methodTestObjects" : "js 6/13/2020 14:06",
"methodsLinesDo:" : "pk 5/18/2021 10:19",
"methodsLinesDo:" : "pk 6/19/2021 18:20",
"methodsLinesDo:excludeClasses:" : "JB 6/10/2021 22:45",
"testAssertOrder" : "JB 6/10/2021 22:44",
"testCorrectClassPrefix" : "JB 6/7/2021 09:25",
Expand Down
Loading

0 comments on commit c015d8f

Please sign in to comment.