Skip to content

Commit

Permalink
Add blocking interface to request a user (#368)
Browse files Browse the repository at this point in the history
* test TCCCore callback and TCCUser constructor (+5 squashed commit)

Squashed commit:

[28b327f] refactor TCCCore

[153d794] Show message author in group chat notification

[54d9c3a] add user api call with callback for new messages

[fdb11cb] fix sender user id key in message constructor

[54f21dd] Message includes chat object (+1 squashed commits)

Squashed commits:

[f5056f3] show title of chat in notification

* use TCCChat>>newFromNewChatEvent constructor in TCCChatsHandler

* Notification shows sender, chat and more of the message

* TCCNullMessage does not rely on the chat inst var

* fixed most linter tests

* fix some remarks of PR #339

* test API assumptions

* integrate pr feedback

* fix last remarks of PR #339

* fixed most linter tests

* fixed typo in method names

* Swalint considered harmful

* test Notification

* You can't see me

* beautiful acceptance tests

* im done with this linter

* go return yourself

* test message chatID method

* exclude TCTAcceptanceTests from long method test in linter; remove too many inst vars test

* rename testing* -> mock* in TCTAcceptanceTests; use ifFalse in TCUNotification>>chatTitle

* Added class Users and updated Core for promise handling.

* Added test for getUserFor in TCCUsers.

* integrate users getUserFor: into TCCCore

* use TCTNoTDLibCore in acceptance tests

* fix regressions from merge

* remove dead code and move mocks to TCTMocks

* use TCTNoTDLibCore only in specific test in TCTAcceptanceTests

* remove dead code, use NoTDLibCore in Users test (we mock all events)

Co-authored-by: permler <[email protected]>
Co-authored-by: Raphael Kunert <[email protected]>
  • Loading branch information
3 people authored May 30, 2021
1 parent 4de4626 commit 89884b0
Show file tree
Hide file tree
Showing 41 changed files with 215 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
events
addNewMessage: aMessageEvent from: aUser
addNewMessage: aMessageJSONObject from: aUser

| chat message |
chat := self app chats getChat: (aMessageEvent at: 'chat_id').
message := TCCMessage newFromMessageEvent: aMessageEvent in: chat from: aUser.
chat := self app chats getChat: (aMessageJSONObject at: 'chat_id').
message := TCCMessage newFromMessageEvent: aMessageJSONObject in: chat from: aUser.

chat addNewestMessage: message.
self app triggerEvent: #newMessageReceived with: message.
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
event handling
handleNewMessage: anEvent from: aUserEvent
handleNewMessage: aMessageJSONObject from: aUser

| message user |
message := anEvent at: 'message'.
user := TCCUser newFrom: aUserEvent.
self addNewMessage: message from: user.
self addNewMessage: aMessageJSONObject from: aUser
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
},
"instance" : {
"addChat:" : "per 5/15/2021 12:28",
"addNewMessage:from:" : "pk 5/17/2021 19:10",
"addNewMessage:from:" : "pk 5/21/2021 09:23",
"app" : "5/11/2021 10:09:15",
"app:" : "pk 5/17/2021 19:11",
"chatHistoryReceived:" : "pk 5/12/2021 12:35",
"client" : "js 6/1/2020 14:44",
"defaultMessageLimit" : "js 8/1/2020 18:24",
"getChatHistoryFrom:with:limit:" : "js 8/2/2020 13:17",
"getChats" : "js 7/31/2020 22:50",
"handleNewMessage:from:" : "pk 5/17/2021 18:28",
"handleNewMessage:from:" : "pk 5/21/2021 09:23",
"openNewChat:" : "pk 5/13/2021 12:45",
"remainingMessages" : "5/11/2021 10:09:15",
"remainingMessages:" : "js 8/1/2020 16:50",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
event handling
events
handleEvent: anEvent

| update |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
events
handleMessageEvent: anEvent

|callbackID message|
message := anEvent at: 'message'.
callbackID := (message at: 'id') asString, (message at: 'chat_id').
self pendingRequests at: callbackID put: [:aUserEvent |
self chatsHandler handleNewMessage: anEvent from: aUserEvent].
self getUser: ((message at: 'sender') at: 'user_id') with: callbackID
| message userID |
message := anEvent at: 'message'.
userID := (message at: 'sender') at: 'user_id'.
[self chatsHandler handleNewMessage: message from: (self users getUserFor: userID)] fork
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ events
handleUserEvent: anEvent

| extra |
extra := anEvent at: '@extra'.
extra := (anEvent at: '@extra') asString.
(self pendingRequests at: extra) value: anEvent.
self pendingRequests removeKey: extra ifAbsent: []
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ initialize
initializeHandlers;
chats: TCCChats newWithDefaultSorting;
loggedInUserID: 0;
pendingRequests: Dictionary new.
pendingRequests: Dictionary new;
users: (TCCUsers new core: self).

[self receiveLoop] fork.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
utility
registerCallback: aClosure with: aCallbackId

self pendingRequests at: (aCallbackId asString) put: aClosure
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
events
getUser: aUserId with: aCallbackID
requestUser: aUserId with: aCallbackID

self client send: (TCCRequest
newWithType: 'getUser'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
users: aDictionary

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

^ users
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@
"client:" : "js 6/13/2020 19:10",
"getChatHistoryFrom:" : "js 8/1/2020 18:19",
"getOwnProfile" : "pk 5/13/2021 09:44",
"getUser:with:" : "pk 5/17/2021 19:31",
"handleEvent:" : "pk 5/13/2021 09:53",
"handleMessageEvent:" : "pk 5/19/2021 11:29",
"handleUserEvent:" : "pk 5/17/2021 18:29",
"initialize" : "pk 5/13/2021 09:33",
"handleEvent:" : "RK 5/19/2021 09:57",
"handleMessageEvent:" : "pk 5/30/2021 13:16",
"handleUserEvent:" : "pk 5/21/2021 10:14",
"initialize" : "pk 5/21/2021 09:13",
"initializeHandlers" : "js 8/1/2020 18:09",
"isClientAlive" : "js 5/28/2020 17:18",
"loggedInUserID" : "rs 7/5/2020 14:48",
"loggedInUserID:" : "rs 7/5/2020 14:48",
"pendingRequests" : "pk 5/13/2021 09:46",
"pendingRequests:" : "pk 5/13/2021 09:46",
"receiveLoop" : "R.S 6/1/2020 15:24",
"registerCallback:with:" : "RK 5/19/2021 10:43",
"requestUser:with:" : "pk 5/21/2021 09:33",
"searchChat:" : "f.w. 7/15/2020 22:53",
"sendMessage:to:" : "rs 6/13/2020 15:26",
"sendPhoneNumber:" : "js 6/13/2020 15:35",
"setUserID:" : "per 5/13/2021 08:22",
"tryHandleError:" : "pk 5/7/2021 12:23",
"update" : "RS 4/28/2021 18:03" } }
"update" : "RS 4/28/2021 18:03",
"users" : "RK 5/19/2021 09:56",
"users:" : "RK 5/19/2021 09:56" } }
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"client",
"authHandler",
"chatsHandler",
"users",
"chats",
"loggedInUserID",
"pendingRequests" ],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cache of Users allowing for synchronous access.
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
getUserFor: aUserId

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

self core
registerCallback:
[:userEvent | (self at: aUserId) resolveWith: (TCCUser newFrom: userEvent)]
with: (aUserId asString).

self core requestUser: aUserId with: aUserId
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"class" : {
},
"instance" : {
"core" : "RK 5/19/2021 10:34",
"core:" : "RK 5/19/2021 10:34",
"getUserFor:" : "pk 5/30/2021 12:55",
"requestUserFor:" : "pk 5/30/2021 13:19" } }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"category" : "TelegramClient-Core",
"classinstvars" : [
],
"classvars" : [
],
"commentStamp" : "RK 5/19/2021 09:58",
"instvars" : [
"core" ],
"name" : "TCCUsers",
"pools" : [
],
"super" : "Dictionary",
"type" : "normal" }
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
accessing
core: aCore

aCore when: #newMessageReceived send: #newMessageReceived: to: self.
core := aCore.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ setUp
20 seconds wait.
self core authHandler isAuthorizationStateReady ifFalse: [
self core loginWithTestData.
20 seconds wait].
20 seconds wait].
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
testing
testMessageAuthors

| message user chat_id notification |
| message user notification overrideCore |
"Fake core because some other tests need a working one"
overrideCore := TCTNoTDLibCore new.
overrideCore chats add: (TCCChat new id: TCTMocks mockChatID).
overrideCore when: #newMessageReceived send: #newMessageReceived: to: self.

self receivedMessage: nil.
chat_id := ((self core chats) first) id.
message := TCTMocks mockMessageEventFrom: chat_id.
message := TCTMocks mockMessageEventFrom: TCTMocks mockChatID.

self core handleEvent: message.
user := TCTMocks mockUserWith: (TCTMocks mockMessageID asString, chat_id).
self core handleEvent: user.

overrideCore handleEvent: message.
2 seconds wait.
user := TCTMocks mockUserEvent.
overrideCore handleEvent: user.
1 second wait.
notification := TCUNotification newWithMessage: self receivedMessage.

self assert: (TCTMocks mockUserFullName = (notification sender fullName)).
self assert: (chat_id = (notification chat id)).
self assert: (TCTMocks mockMessageText = (notification message message)).
self assert: (TCTMocks mockUserFullName) equals: (notification sender fullName).
self assert: (TCTMocks mockChatID) equals: (notification chat id).
self assert: (TCTMocks mockMessageText) equals: (notification message message)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
},
"instance" : {
"core" : "pk 5/17/2021 15:28",
"core:" : "pk 5/17/2021 19:19",
"core:" : "pk 5/30/2021 20:40",
"defaultTimeout" : "RS 5/27/2021 21:13",
"newMessageReceived:" : "pk 5/17/2021 19:20",
"receivedMessage" : "pk 5/17/2021 19:15",
Expand All @@ -13,4 +13,4 @@
"testCanCloseChatInfoPage" : "RS 5/28/2021 10:55",
"testCanOpenChatInfoPage" : "RS 5/28/2021 10:50",
"testChatInfoPageIsHidden" : "RS 5/28/2021 10:51",
"testMessageAuthors" : "JB 5/27/2021 09:54" } }
"testMessageAuthors" : "pk 5/30/2021 21:08" } }
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
user
mockUserWith: anExtra
accessing
mockUserEvent

^ (TCCEvent newFromTDLibEvent: (Dictionary newFrom: {
'@type' -> 'user'.
'id' -> self mockUserID.
'first_name' -> self mockUserFirstName.
'last_name' -> self mockUserLastName.
'username' -> self mockUsername.
'@extra' -> anExtra
'@extra' -> (self mockUserID asString)
}))
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
"mockMessageID" : "pk 5/19/2021 11:36",
"mockMessageJsonFrom:" : "RS 5/23/2021 17:26",
"mockMessageText" : "pk 5/19/2021 11:38",
"mockUserEvent" : "pk 5/30/2021 13:01",
"mockUserFirstName" : "pk 5/19/2021 11:38",
"mockUserFullName" : "pk 5/19/2021 11:49",
"mockUserID" : "pk 5/19/2021 11:34",
"mockUserLastName" : "pk 5/19/2021 11:38",
"mockUserWith:" : "pk 5/19/2021 11:41",
"mockUsername" : "pk 5/19/2021 11:39" },
"instance" : {
} }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A TCTNoTDLibCore is a TCCCore without a client. It cannot make requests and has to be fed every event.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
initialization
initialize

"override initialize to work without client or tdlib"
self
client: nil;
initializeHandlers;
chats: TCCChats newWithDefaultSorting;
loggedInUserID: 0;
pendingRequests: Dictionary new;
users: (TCCUsers new core: self).
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
initialization
initializeHandlers

self
chatsHandler: (TCCChatsHandler new
app: self).
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
events
requestUser: aUserId with: aCallbackID

^ self
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"class" : {
},
"instance" : {
"initialize" : "pk 5/30/2021 20:52",
"initializeHandlers" : "pk 5/21/2021 09:46",
"requestUser:with:" : "pk 5/21/2021 10:18" } }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"category" : "TelegramClient-Tests",
"classinstvars" : [
],
"classvars" : [
],
"commentStamp" : "pk 5/21/2021 09:45",
"instvars" : [
],
"name" : "TCTNoTDLibCore",
"pools" : [
],
"super" : "TCCCore",
"type" : "normal" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test TCCUsers
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
core: aTCCCore

core := aTCCCore
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,5 @@
running
setUp

self core: TCTNoTDLibCore new.
self users: self core users.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
testing
testGetUserFor

| user |
user := nil.
[user := self users getUserFor: TCTMocks mockUserID.] fork.
1 seconds wait.

self core handleEvent: TCTMocks mockUserEvent.
1 seconds wait.

self assert: TCTMocks mockUserID equals: user id.
self assert: TCTMocks mockUserFirstName equals: user firstName.
self assert: TCTMocks mockUserLastName equals: user lastName.
self assert: TCTMocks mockUsername equals: user username.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
users: aUsers

users := aUsers
Loading

0 comments on commit 89884b0

Please sign in to comment.